Simple way to find maximum index of a set? then create matrix based on that value.

89 views
Skip to first unread message

Endowment Investor

unread,
Feb 20, 2021, 1:53:30 PM2/20/21
to AMPL Modeling Language
Q1: Suppose I have a sparsely populated param with values at index 1,2, and 5. Is there a quick way to return 5? I've looked through Chapter 5 but get confused.

set NODE;
param thing {NODE};

param: NODE: thing :=
1 10
2 11
5 12;

Q2: My goal is to create a second param with zero values in the missing indices. Somthing like:

param: NODE: newthing :=
1 10
2 11
3 0
4 0
5 12;

I could brute force this by initializing to zero then replacing newthing[i] :=thing[i], but is there an elegant way to do this? 

--newbie who appreciates any insights




Mikhail

unread,
Feb 20, 2021, 10:16:33 PM2/20/21
to AMPL Modeling Language
You can return the value of the parameter with the index value "5" as follows:
set NODE;
param thing {NODE};
data;
param: NODE: thing: =
1 10
2 11
5 12;
display thing [5],
sum {i in NODE: i in {5}} thing [i],
sum {i in NODE: i = 5} thing [i];
if NODE is a literal set then its element must be enclosed in '' or "".
set NODE;
param thing {NODE};
data;
param: NODE: thing: =
1 10
2 11
A5 12;
display thing ['A5'],
sum {i in NODE: i in {'A5'}} thing [i],
sum {i in NODE: i = 'A5'} thing [i];
Q2: First of all, you need to declare a new set:
set NODE2: = {1..5};
then declare a new parameter, newthing, based on the values ​​of thing:
param newthing {i in NODE2}: = if i in NODE then thing [i] else 0;
You can also declare the thing parameter with a default value of 0:
set NODE: = {1..5};
param thing {NODE};
data;
param thing default 0: =
1 10
2 11
5 12;
display thing [3];

суббота, 20 февраля 2021 г. в 21:53:30 UTC+3, endowmen...@gmail.com:

Mikhail

unread,
Feb 20, 2021, 10:32:03 PM2/20/21
to AMPL Modeling Language
Simple way to find maximum index of a set:
you need to declare an ordered set NODE:
set NODE ordered;
then the ordered set functions can be used: https://ampl.com/BOOK/CHAPTERS/08-sets1.pdf#page=12.
display thing [last (NODE)];

воскресенье, 21 февраля 2021 г. в 06:16:33 UTC+3, Mikhail:

AMPL Google Group

unread,
Feb 22, 2021, 11:35:26 AM2/22/21
to AMPL Modeling Language
The following short example addresses both of your questions:

param maxNODE = max {i in NODE} i;
param newthing {i in 1..maxNODE} = if i in NODE then thing[i] else 0;


If you are going to use only newthing in your model formulation, however, then you may instead want to make 0 the default value for thing,

param N integer > 0;
param thing {1..N} default 0;


and give the data like this:

param N := 5 ;
param thing :=

1 10
2 11
5 12 ;


(Caution: "display thing;" won't show the default values until a solve or other action causes those values to be used.)


--
Robert Fourer
am...@googlegroups.com
{#HS:1431799979-101190#}
On Sun, Feb 21, 2021 at 3:32 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Simple way to find maximum index of a set:
you need to declare an ordered set NODE:
*set NODE ordered;*

then the ordered set functions can be used:
https://ampl.com/BOOK/CHAPTERS/08-sets1.pdf#page=12.
*display thing [last (NODE)];*


воскресенье, 21 февраля 2021 г. в 06:16:33 UTC+3, Mikhail:
On Sun, Feb 21, 2021 at 3:16 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
You can return the value of the parameter with the index value "5" as
follows:
*set NODE;*
*param thing {NODE};*
*data;*
*param: NODE: thing: =*
*1 10*
*2 11*
*5 12;*
*display thing [5],*
*sum {i in NODE: i in {5}} thing ,*
*sum {i in NODE: i = 5} thing ;*

if NODE is a literal set then its element must be enclosed in '' or "".
*set NODE;*
*param thing {NODE};*
*data;*
*param: NODE: thing: =*
*1 10*
*2 11*
*A5 12;*
*display thing ['A5'],*
*sum {i in NODE: i in {'A5'}} thing ,*
*sum {i in NODE: i = 'A5'} thing ;*

Q2: First of all, you need to declare a new set:
*set NODE2: = {1..5};*
then declare a new parameter, *newthing*, based on the values of thing:
*param newthing {i in NODE2}: = if i in NODE then thing else 0;*
You can also declare the *thing* parameter with a default value of 0:
*set NODE: = {1..5};*
*param thing {NODE};*
*data;*
*param thing default 0: =*
*1 10*
*2 11*
*5 12;*
*display thing [3];*


суббота, 20 февраля 2021 г. в 21:53:30 UTC+3, endowmen...@gmail.com:

William Jennings

unread,
Feb 22, 2021, 1:31:19 PM2/22/21
to AMPL Modeling Language
Outstanding. Thanks.
Reply all
Reply to author
Forward
0 new messages