Pass set data Topic is solved

Problems with syntax of GAMS
Post Reply
Janisch
User
User
Posts: 38
Joined: 3 years ago

Pass set data

Post by Janisch »

Hello all,
so far I have the following code, which specifies the number of grid points:

Code: Select all

$if not set gridpoints $set gridpoints 23

Set
   g         'grid points' / g0*g%gridpoints% /
Now I would like to specify the number of grid points via Excel. The parameter is then read from the Excel file. I have already tried:

Code: Select all

Parameter num_gridpoints
Set
   g         'grid points' / g0*g%num_gridpoints% /
But this approach didn't work. I hope somebody can help me :)
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Pass set data

Post by abhosekar »

You cannot use parameter to define a set. I am not sure if this is the best way, but you can define a subset of a big set and assign it using the parameter that you read form excel as follows:

Code: Select all

parameter set_size /5/;
set
univ /i1*i10000/
i(univ)
;
i(univ)$(ord(univ) <= set_size) = yes;

display i;
- Atharv
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Pass set data

Post by Janisch »

Thank you for your approach! It works now :)
User avatar
bussieck
Moderator
Moderator
Posts: 1038
Joined: 7 years ago

Re: Pass set data

Post by bussieck »

If you get the parameter (or better the scalar) at compile time then there is a way. Since you get your values from Excel, I guess you do at some stage:

Code: Select all

Scalar num_gridpoints;
$gdxIn fromExcel.gdx
$load num_gridpoints
...
$gdxIn 
Now you can turn the value of scalar num_gridpoints at compile time into a compile time variable and use that in your grid definition:

Code: Select all

$eval NUM_GRIDPOINTS num_gridpoints
Set
   g         'grid points' / g0*g%NUM_GRIDPOINTS% /
...
$gdxIn 
Important this does not work with scalar calculated (at execution time). You can do some limited calculation at compile time with the $eval command, you can also use some functions, e.g. card, so if you have a 2-dim grid and need 1-dim set representing all points this also works:

Code: Select all

$if not set XMAX $set XMAX 9
$if not set YMAX $set YMAX 9
Set
   x       'x-axis' / x0*x%XMAX% /
   y       'y-axis' / y0*y%YMAX% /
   g(x,y)  'grid points' / #x.#y /;

$eval PMAX card(x)*card(y)-1
Set p / p0*p%PMAX% /
     xypmap(x,y,p) / #x.#y:#p /;
...
-Michael
Janisch
User
User
Posts: 38
Joined: 3 years ago

Re: Pass set data

Post by Janisch »

Ah great! Thanks for your solution :) This works for my problem a little better than Atharv's solution.
Thank you both!
Post Reply