Variable bound assignment in a loop

Archive of Gamsworld Google Group
Post Reply
Archiver
User
User
Posts: 7876
Joined: 2 years ago

Variable bound assignment in a loop

Post by Archiver » 2 years ago


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

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 2 years ago

Re: Variable bound assignment in a loop

Post by Archiver » 2 years ago


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


On Monday, April 18, 2016 at 11:09:48 PM UTC-4, jaster.merel@gmail.com wrote:

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

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Post Reply