User Tools

Site Tools


gams:a_scalar_drives_the_length_of_a_set

A scalar drives the length of a set

I need my set to be able to changing depending on the value of a scalar. e.g:

 scalar dim /10/; set myset /1*dim/;

In general we do it the other way around:

set myset /1*10/;
scalar dim;
dim = card(myset);

In the GAMS philosophy sets drive the model. Creating sets based on data (eg scalars) requires the use of dynamic sets, which are a little bit more difficult to use than static sets. Here is a fragment that illustrates how you can use dynamic sets:

set univ the universe /1*1000/;
scalar dim /6/;
set myset(univ);

myset(univ)$(ord(univ) <= dim) = yes;

Display myset; 

* declare equations, variables and parameters over the universe set
* but use them over myset
parameter p(univ);
p(myset) = 1;

You can also pass compile time information through the command line: gams x.gms –dim=5 with x.gms

$if not set dim $set dim 5

set myset / 1*%dim%/ 
gams/a_scalar_drives_the_length_of_a_set.txt · Last modified: 2008/06/30 14:26 by support