2D variable with different vector sizes AMPL

127 views
Skip to first unread message

Samuele Viaro

unread,
Nov 26, 2021, 12:06:21 PM11/26/21
to AMPL Modeling Language
Hello!!
I have a variable with a fixed number of rows but a number of columns that depend on the row:

set ROW := {1..N}
param js{ROW,1..2}  #here are specified the number of columns for each row

var t{ROW, js[r,2] }   #this would not work it is just to give an idea...

the variable t should look like
ROW1:  A, B, C
ROW2:  D, E, F, G, H, I
ROW3: L, M

Could someone please explain how to do this (if possible)?
I hope I was clear enough... :)

Thanks

AMPL Google Group

unread,
Nov 26, 2021, 9:04:06 PM11/26/21
to AMPL Modeling Language
This structure is often represented in AMPL by indexed collection of sets. Here is an example:

param N integer > 0;
set allCOLS = setof {i in 65 .. 90} char(i);

set ROWS = 1..N;
set COLS {ROWS} within allCOLS; # COLS[i] = set of columns in row i

var t {i in ROWS, COLS[i]};


Here's how your data would be written, in AMPL's text data format:

param N := 3;
set COLS[1] := A B C ;
set COLS[2] := D E F G H I ;
set COLS[3] := L M ;


An indexing expression like {i in ROWS, j in COLS[i]} would run over all columns in all rows.

The same data could also be modeled as a set of pairs (i,j) where i is a row and j is a column in that row. The possibilities are described in Chapter 6: Compound Sets and Indexing in the AMPL book; sets of pairs and indexed collections of sets are introduced in section 6.1 and section 6.5, respectively.


--
Robert Fourer
am...@googlegroups.com
{#HS:1708701466-107366#}

Bence Savanya

unread,
Nov 27, 2022, 7:24:01 PM11/27/22
to AMPL Modeling Language
Hey there,

how would you fill the cols with numbers 1..n in this instance? I've been stuck on this for a while, because in the .dat file you cant use this syntax and in the .mod you cant index it as far as i found.

Thanks in advance,
Bence

AMPL Google Group

unread,
Nov 30, 2022, 12:22:41 PM11/30/22
to AMPL Modeling Language
Can you give a small example of what you mean by "fill the cols with numbers 1..n"? It seems to be something different from the "2D variable with different vector sizes" mentioned in the subject.


--
Robert Fourer
am...@googlegroups.com
{#HS:1708701466-107366#}
On Mon, Nov 28, 2022 at 12:24 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hey there,

how would you fill the cols with numbers 1..n in this instance? I've been stuck on this for a while, because in the .dat file you cant use this syntax and in the .mod you cant index it as far as i found.

Thanks in advance,
Bence

On Sat, Nov 27, 2021 at 2:03 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
This structure is often represented in AMPL by indexed collection of sets. Here is an example:

param N integer > 0;
set allCOLS = setof {i in 65 .. 90} char(i);

set ROWS = 1..N;
set COLS {ROWS} within allCOLS; # COLS[i] = set of columns in row i

var t {i in ROWS, COLS[i]};


Here's how your data would be written, in AMPL's text data format:

param N := 3;
set COLS[1] := A B C ;
set COLS[2] := D E F G H I ;
set COLS[3] := L M ;


An indexing expression like {i in ROWS, j in COLS[i]} would run over all columns in all rows.

The same data could also be modeled as a set of pairs (i,j) where i is a row and j is a column in that row. The possibilities are described in Chapter 6: Compound Sets and Indexing in the AMPL book; sets of pairs and indexed collections of sets are introduced in section 6.1 and section 6.5, respectively.


--
Robert Fourer
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages