[AMPL 24683] If and else help

Hi, sorry for question,

Is possible write this? Ampl cant undestarnd

if L[l,s]>=K[l,s] then DL[l,s]=L[l,s]

else DL[l,s]= 0;

Hi Andrés,

you have to use the “let” statement allocating data to a parameter or indipendend variable. Also “:=” instead of “=” Thus, the correct syntax is (maybe I am using to many brackets :wink: ):

if (L[l,s]>=K[l,s]) then {let DL[l,s]:=L[l,s];} else {let DL[l,s]:=0;}


Hi Lothar, thank you very much for answer me.

It doesnt work :frowning:

syntax error
context: if L[l,s]>=K[l,s] then >>> DL[l,s]:= <<< L[l,s] else let DL[l,s]=0;

Hi Andrés,

I think you still forgot to use the “let”-statement. You habe to use “let” to allocate data during runtime.

Best regards,

It is not clear what you are trying to do with this statement. Can you provide some additional information?

  • First, show your model’s definitions of L, K, and DL, so that it is clear which ones are parameters (data) and which are decision variables.
  • Then, explain whether “if L[l,s]>=K[l,s] then DL[l,s]=L[l,s] else DL[l,s]= 0;” is supposed to be a constraint in your model, or whether it is just an assignment of values to parameter DL.

First, thank you (both) very much for your valuable time and help.

The AMPL output is as follows:

conditional.mod, line 142 (offset 5855):
let is not defined
context: if (L[l,s]>=K[l,s]) then {let >>> DL[ <<< l,s]:=L[l,s];} else {let DL[l ,s]:=0;}

Something is wrong with “let”


  1. L, K and DL are variables. In fact, DL is a construction from L, as long as it is not a value equal to K. I mean, DL[l,s] is constructed from the values of L[l,s] that are greater than the values of K[l,s]. In the rest of the positions where it is not fulfilled, it must be filled with 0.

  2. It is not properly a restriction. DL for me, is an auxiliary variable that is built from L and K.


For this case:

DL[l, s] = if … then L[i, s] else 0; # the else part can be omitted if 0

In general:

==> <constraint 1> [else <constraint 2>];

See Modeling Guide for MP-based AMPL Solvers — ampl::mp 20211021 documentation.

As this?


DL[l, s] = L[l,s] if L[l,s]>K[l,s] else 0;

Thank you

Hi, It’s me again… jajaja

Sorry for insist…

If you are using our latest version of the gurobi, highs, copt, cbc, xpress, or mosek solver, then you can define DL straightforwardly using an AMPL if-then-else expression. Here’s an example, where I call the index sets Lset and Sset:

set Lset;
set Sset;

var L {Lset,Sset};
var K {Lset,Sset};

var DL {l in Lset, s in Sset} = 
   if L[l,s] > K[l,s] then L[l,s] else 0;

This condition can also be specified by defining variable DL without an = value, and then specifying a constraint on the variable:

var DL {l in Lset, s in Sset};

subject to setDL {l in Lset, s in Sset}:
   DL[l,s] = if L[l,s] > K[l,s] then L[l,s] else 0;

There is also this different way to specify the constraint, but using the AMPL operator ==> which means “implies”:

var DL {l in Lset, s in Sset};

subject to setDL {l in Lset, s in Sset}:
   L[l,s] > K[l,s] ==> DL[l,s] = L[l,s] else DL[l,s] = 0;

The ==> operator has the advantage that it is more general: it can express constraints that can’t be written in the other forms. However, solvers other than gurobi and copt may ask you to supply bounds on the variables, or a “big M value”, before they will accept some a model that uses the ==> operator.