Variable bound assignment in a loop

27 views
Skip to first unread message

jaster...@gmail.com

unread,
Apr 18, 2016, 11:09:48 PM4/18/16
to gamsworld
Hi all

I have a quick question regarding dynamically assigning variable bounds in a loop. As an example I started with:

##############
set i /a0*a3/ ;

variable test_a(i);
variable test_b(i);

set lim / FX,LO/;

parameter tofix(i,lim)/
a0.FX 5
a1.FX 10
a2.LO 3
a3.FX 70
/;

test_a.FX(i)=tofix(i,"FX");
test_a.LO(i)=tofix(i,"LO");
##############

I then tried to put the variable bound assignments into a loop by trying a variety of things. In essence what I want is something like:

loop(lim,
$batinclude test1.gms "." lim tofix(i,lim)
);

where test1.gms would be:

test_b%1%2(i)=tofix(i,%2) ;

But that won't work unless I change it to:

loop(lim,
$batinclude test1.gms "." FX lim tofix(i,lim)
);

and update test1.gms to:

test_b%1%2(i)=tofix(i,%3) ;

But that doesn't give me what I want either. That "FX" (aka %2 above) needs to be dynamic in the loop and I can't figure out how to get control variables to be dynamic. Is it possible? Any thoughts?

Many thanks

James

Michael Bussieck

unread,
Apr 18, 2016, 11:25:22 PM4/18/16
to gamsworld
James,

The listing (lst) file echos the expanded program at the top of the file. This usually helps debugging compilation errors. When I do what you describe first it gives me:

  23  loop(lim,
BATINCLUDE C:\Users\Michael\Documents\eBug\test1.gms
  25  test_b.lim(i)=tofix(i,lim) ;
****           $315

The batinclude does a textual/string replace of arguments on the call with %1, %2, ... and hence what you get is not valid GAMS code. Moreover, you can't use the content/value of a set (lim) that changes at run time to rewrite code at compile time. The separation between compile and run time in GAMS is not easy to grasp first but essential and very powerful. I am not sure why you want what you describe in your post, but you would need to do it this way (determine this at runtime based on the content/value of lim) in test1.gms:

test_b%1lo(i)$sameas('lo',%2) = tofix(i,%2);
test_b%1up(i)$sameas('up',%2) = tofix(i,%2);
test_b%1fx(i)$sameas('fx',%2) = tofix(i,%2);

Hope this helps,
Michael
Reply all
Reply to author
Forward
0 new messages