Multiple mixed conditions (string and value) using $Ifthen

Problems with syntax of GAMS
Post Reply
zhj0735
User
User
Posts: 16
Joined: 4 years ago

Multiple mixed conditions (string and value) using $Ifthen

Post by zhj0735 »

I posted another question for the multiple numerical conditions. viewtopic.php?f=2&t=11859. That is helpful!

Now due to the need of the naming system and the modeling scenarios, I have both string and value conditions to set using $ifthen structure. The following example is the trail I fail to activate the display statement with iclone=2 and random=R. Need help to correct the condition and let the display work when the iclone=2 and random=R.

Thanks,
Jia

Code: Select all

$ifThen.cl (%iclone%<>1)or(%random%==R)
display a
$endif.cl
GFA
User
User
Posts: 50
Joined: 5 years ago

Re: Multiple mixed conditions (string and value) using $Ifthen

Post by GFA »

Hi zhj0735,

Use $ifthenE for numerical expressions, also see:
https://www.gams.com/mccarlGuide/ifthen ... onditi.htm

This seems to work but I'm not sure if it is the most elegant solution:

Code: Select all

$ifThene.cl %iclone%<>1
$ifThen.cl2 %random%==R
display a;
$endif.cl2
$endif.cl
Regards,
GFA
zhj0735
User
User
Posts: 16
Joined: 4 years ago

Re: Multiple mixed conditions (string and value) using $Ifthen

Post by zhj0735 »

Hi GFA,

Thanks for the answer.

But the nested structure you propose an "AND" logic instead of "OR" logic that I need. Wondering is there any $ifthen or $if options to go with to deal with this problem.

Best
GFA
User
User
Posts: 50
Joined: 5 years ago

Re: Multiple mixed conditions (string and value) using $Ifthen

Post by GFA »

This should work:

Code: Select all

$ife %iclone%<>1 $set x yes
$if %random%==R $set x yes
$if %x% == yes display A
Or this:

Code: Select all

$ife %iclone%<>1 $goto x
$if %random%==R $goto x

$goto skipx
$label x
display a
* + other statements if u wish
$label skipx
GFA
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Multiple mixed conditions (string and value) using $Ifthen

Post by bussieck »

You can do string comparisons in a compile-time evaluation (e.g. ifThenE) using function sameas:

Code: Select all

$ifThenE.cl (%iclone%<>1)or(sameas('%random%','R'))
display a
$endif.cl
Beware that sameas does a case insensitive string compare.

-Michael
zhj0735
User
User
Posts: 16
Joined: 4 years ago

Re: Multiple mixed conditions (string and value) using $Ifthen

Post by zhj0735 »

Thank you Michael!
Post Reply