Nested if-then-else statement or multiple if-statements

347 views
Skip to first unread message

louisperrault

unread,
Oct 24, 2017, 9:47:39 AM10/24/17
to AMPL Modeling Language
Hello,

I have been solving a problem with AMPL (Knitro solver) where I create other variables based on the choice variables with If-then-else statements, for example (a few other variables are omitted for brevity):

var c1{i in N}= if (I[i]>=CutOff[i]) then ((Per1NetInc[i]/PweightC)+(Need/2)) else (omega1*(TotNetInc[i]/PweightC)+(Need/2)) ;

Here I[i] and another variable in CutOff[i] are the true choice variables. This has been working so far. Now, I am facing a situation where I have two cutoff values, this means that I would need something like the "elif" in Python or the nested if...else in R (https://www.programiz.com/r-programming/if-else-statement). 

Is a nested if-else statement possible in AMPL?

For example, could I write

var c1{i in N}= if (I[i]>CutOff1[i]) then (X1) else ( if (I[i]<CutOff2[i]) then (X2) else (X3) )?

Thank you so much for your time.

Regards,

Louis Perrault

Robert Fourer

unread,
Oct 26, 2017, 5:45:43 PM10/26/17
to am...@googlegroups.com
An if-then-else is a kind of expression in AMPL. It can appear anywhere that an expression is valid, such as after the "else" of another if-then-else expression -- as in your example.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages