If then statement in Macro Topic is solved

questions about GAMS' tools
Post Reply
User avatar
linkho
User
User
Posts: 15
Joined: 5 years ago

If then statement in Macro

Post by linkho »

Hi all


I have binary parameter b(j) , j/1*100/ . I need to change the element of b (change 1 to 0 and 0 to 1 ) to get new parameter c(j). I want to use macro option, is it possible?
Thanks!
Last edited by linkho 4 years ago, edited 1 time in total.
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: If then statement in Macro

Post by bussieck »

Instead of putting the 0/1 swap logic in a complicated loop/if structure, you can just use a one-liner parallel assignment statement. But your can also put the parallel assignment or the complicated loop/if logic into a macro:

Code: Select all

set j/1*10/;
parameter c(j);
c(j)=uniformInt(0,1);
display c;

loop(j,
  if(c(j)=1,
    c(j)=0;
  else
    c(j)=1;
  )
);
display c;

* Simple parallel assignment
c(j) = not c(j);
display c;

* Macro with simple parallel assignment
$macro swap1(sym,dom) sym(dom) = not sym(dom)
swap1(c,j);
display c;

* Macro with loop/if statements
$macro swap2(sym,dom) loop(dom, if(sym(dom)=1, sym(dom)=0; else sym(dom)=1;))
swap2(c,j);
display c;
-Michael
User avatar
linkho
User
User
Posts: 15
Joined: 5 years ago

Re: If then statement in Macro

Post by linkho »

Realy thanks!
Post Reply