Page 1 of 1

$Setglobal using if statement

Posted: Tue Dec 18, 2018 3:22 pm
by Youngghee
Hello, GAMS experts

I would like to branch to two different module base on scalar 's'. The following is my GAMS code, however it is not working correctly.
Please help me what is problem for this model. If know, let me know what is best solution.

Code: Select all

scalars
  s / 100 /
;
  if (s > 0,
$Setglobal optcase "Case1"
  else
$Setglobal optcase "Case2"
  );
  display "%optcase%";

$If %optcase% == "Case2" $goto Subroutine2
$label Subroutine1
...
...
$goto SolverRun

$label Subroutine2
...
...

$label SolverRun
  model myModel / all /;
  solve myModel using MINLP minimizing myTarget;

Re: $Setglobal using if statement

Posted: Wed Dec 19, 2018 7:35 am
by Fred
Hi,

You are mixing compile time statements (like $setglobal) with execution time commands (like if/else).
GAMS is a two pass system with a compile time phase and an execution time phase (see https://www.gams.com/latest/docs/UG_Gam ... ll_TwoPass).
$commands like setglobal are carried out at compile time where the execution time if/elsehas no meaning.
The solution is to replace your if/else with the compile time counterpart (https://www.gams.com/latest/docs/UG_Dol ... LLARifthen)

Code: Select all

scalars
  s / 100 /
;
$ifthene s>0
$  Setglobal optcase "Case1"
$else
$  Setglobal optcase "Case2"
$endif
display "%optcase%";
I hope this helps!

Fred

Re: $Setglobal using if statement

Posted: Wed Dec 19, 2018 10:07 am
by Youngghee
Thank so much Fred.
Your sample code is working well, It was clear.