Changing model type depending on control variable

Top  Previous  Next

Here I set up a model as either a linear or nonlinear form depending on the control variable nonlin ( nlplp.gms).

 

$Setglobal nonlin yes

*$Setglobal nonlin no

variables            z   objective

positive variables   x   decision variables;

equations            obj

                     xlim;

$If %nonlin% == yes $goto nonlin

        obj..   z=e=3*x;

$goto around

$label nonlin

        obj..   z=e=3*x-3*x**2;

$label around

        xlim..   x=l=4;

model cond /all/;

$If %nonlin% == yes solve cond using nlp maximizing z;

$If not %nonlin% == yes solve cond using lp maximizing z

 

When NONLIN is set to yes I get

 

 3  variables            z   objective

 4  positive variables   x   decision variables;

 5  equations            obj

 6                       xlim;

 8          obj..   z=e=3*x-3*x**2;

10          xlim..   x=l=4;

11  model cond /all/;

13  solve cond using nlp maximizing z;

 

otherwise

 

 3  variables            z   objective

 4  positive variables   x   decision variables;

 5  equations            obj

 6                       xlim;

 8          obj..   z=e=3*x;

10          xlim..   x=l=4;

11  model cond /all/;

13  solve cond using lp maximizing z;