I am trying to conditionally switch between compiling the model with one representation vs compiling it with another.
If the condtional is true I can get the model to compile fine. If the conditional is false I get error $2 on the closing $endIf. Examples below:
THE CODE BELOW COMPILES FINE
$setglobal refurbish_version refurbish_akl // <-- this will evaluate every $ifThenI to true
sets
map_g_type(g,type) "Map units to their type"
map_g_pool(g,p) "Map units to their pool"
$ifThenI %refurbish_version% == refurbish_akl
valid_refurbish(g,y) "Is it valid for a state that began existing on year y to exist in year y2? Once a state becomes active it cannot become inactive"
$else
valid_refurbish(g,u,y,y2) "Is it valid for a state that began existing on year y to exist in year y2? But once a state becomes active it cannot become inactive"
valid_refurbish_view(g,u,y,y2) "Is it valid for a state that began existing on year y to exist in year y2?"
valid_refurbish_transition(g,u,y,u2,y2) "Is it valid, in year y2, to transition from u->u2 for a state that began existing on year y?"
$endIf
;
<some other code with similar $ifThenI conditionals>
Positive Variables
v_generation, v_capacity, v_base_capacity, v_retrofit_transition, v_obj_cost, v_transmission, v_energy_pool,
v_energy_stored, v_energy_retrieved, v_refurbish_capacity, v_capacity_degradation ,v_refurbish_transition
$ifThenI %refurbish_version% == refurbish_akl
,v_built, v_refurbish, v_reinstate, v_retire_clean, v_retire_degraded, v_postpone
$else // <-- If I don't have this here even though there is no else it throws a compilation error
$endif
;
All of the above works fine.
If however I change the global to something other than refurbish_akl I get the compilation error:
197 $endIf
**** $2
2 Identifier expected
257 Solve statement not checked because of previous errors
299 Unexpected end of file
675 Closing $endif missing
**** 10 ERROR(S) 0 WARNING(S)
Can someone explain what I am doing wrong?
One thing I thought of was maybe the $else can only contain a single statement. So I rewrote the code to be something like below:
$ifThenI %refurbish_version% == refurbish_akl
valid_refurbish(g,y) "Is it valid for a state that began existing on year y to exist in year y2? Once a state becomes active it cannot become inactive"
$else
$endif
$ifThenI NOT %refurbish_version% == refurbish_akl
valid_refurbish(g,u,y,y2) "Is it valid for a state that began existing on year y to exist in year y2? But once a state becomes active it cannot become inactive"
valid_refurbish_view(g,u,y,y2) "Is it valid for a state that began existing on year y to exist in year y2?"
valid_refurbish_transition(g,u,y,u2,y2) "Is it valid, in year y2, to transition from u->u2 for a state that began existing on year y?"
$else
$endIf
This gives me a compile error of:
198 $endIf
**** $2
2 Identifier expected
257 Solve statement not checked because of previous errors
299 Unexpected end of file
675 Closing $endif missing
**** 4 ERROR(S) 0 WARNING(S)
I can get around these problems with gotos, but i'd rather just use if then else endif statements as they seem cleaner.
All help appreciated.
Andrew C
$ifthen with $else code block issue
Re: $ifthen with $else code block issue
Andrew,
All the pieces look okay to me but without the entire source it is hard to say where the problem is. I can recommend to use $ifThen/$else/$endIf with suffix names. This way the compiler reports things in a better way:
-Michael
All the pieces look okay to me but without the entire source it is hard to say where the problem is. I can recommend to use $ifThen/$else/$endIf with suffix names. This way the compiler reports things in a better way:
Code: Select all
$ifThen.label1 cond1
...
$ifThen.label2 cond2
...
$endIf.label2
$else.label1
...
$endIf.label1
Re: $ifthen with $else code block issue
Andrew C,
It would be helpful to attach your GAMS source: don't include it in the text. Also, attach the complete .lst and .log files. And if you feel the source is too big to attach, then create a copy and cut, cut, cut until you have a small reproducible example. Probably you will find the solution yourself while doing this. If not, you make it easier for somebody else to help you.
-Steve
It would be helpful to attach your GAMS source: don't include it in the text. Also, attach the complete .lst and .log files. And if you feel the source is too big to attach, then create a copy and cut, cut, cut until you have a small reproducible example. Probably you will find the solution yourself while doing this. If not, you make it easier for somebody else to help you.
-Steve
Re: $ifthen with $else code block issue
I have managed to track down the cause of the problem. We use the $onEnd option. If I don't use that option my syntax works. So now the question has shifted to being - what is the correct syntax to use for $ifThenI statements if the $onEnd option is enabled??
I have attached a dummy model to demonstrate. The model uses #ifThenI and $else statements to conditionally compile in different versions of code.
In the attached file are the lines:
{ syntax_version is the flag we use in the $if statements to compile in/out different bits of code.
Comment/uncomment to switch between the two syntax versions }
$setglobal syntax_version simple
//$setglobal syntax_version complex
This toggles the switch used by the $ifThenI statements that are throughout the model. If "simple" is enabled the code works even if the $onEnd syntax is enable. If the "complex" line of code is enabled the code throws compilation errors if $onEnd is enabled, but works fine if we comment out the $onEnd directive. I have attached the .lst file for the simple (works) and complex (fails) versions. NOTE I HAD TO CHANGE THEIR EXTENSION TO .TXT BECAUSE THE ATTACHMENTS FUNCTIONALITY SAYS .LST FILES ARE NOT A SUPPORTED TYPE AND WONT LET ME UPLOAD THEM.
Our models all use $onEnd syntax so I would like to know how to add in $ifThenI and $else directives to our models. We probably could surround the #ifThenI statements with $offEnd and $onEnd, but it would mean switching to the alternative syntax when coding between the $ifThenI and $end statements so I would like to avoid that if possible.
All help appreciated
I have attached a dummy model to demonstrate. The model uses #ifThenI and $else statements to conditionally compile in different versions of code.
In the attached file are the lines:
{ syntax_version is the flag we use in the $if statements to compile in/out different bits of code.
Comment/uncomment to switch between the two syntax versions }
$setglobal syntax_version simple
//$setglobal syntax_version complex
This toggles the switch used by the $ifThenI statements that are throughout the model. If "simple" is enabled the code works even if the $onEnd syntax is enable. If the "complex" line of code is enabled the code throws compilation errors if $onEnd is enabled, but works fine if we comment out the $onEnd directive. I have attached the .lst file for the simple (works) and complex (fails) versions. NOTE I HAD TO CHANGE THEIR EXTENSION TO .TXT BECAUSE THE ATTACHMENTS FUNCTIONALITY SAYS .LST FILES ARE NOT A SUPPORTED TYPE AND WONT LET ME UPLOAD THEM.
Our models all use $onEnd syntax so I would like to know how to add in $ifThenI and $else directives to our models. We probably could surround the #ifThenI statements with $offEnd and $onEnd, but it would mean switching to the alternative syntax when coding between the $ifThenI and $end statements so I would like to avoid that if possible.
All help appreciated
Re: $ifthen with $else code block issue
Hi,
Your system is >12 years old. The issue has been fixed 10 years ago (GAMS 23.6).
-Michael
Your system is >12 years old. The issue has been fixed 10 years ago (GAMS 23.6).
-Michael