$Setglobal using if statement Topic is solved

Problems with modeling
Post Reply
Youngghee
User
User
Posts: 9
Joined: 5 years ago

$Setglobal using if statement

Post 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;
Fred
Posts: 372
Joined: 7 years ago

Re: $Setglobal using if statement

Post 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
Youngghee
User
User
Posts: 9
Joined: 5 years ago

Re: $Setglobal using if statement

Post by Youngghee »

Thank so much Fred.
Your sample code is working well, It was clear.
Post Reply