If and else help

53 views
Skip to first unread message

Andrés

unread,
Jun 12, 2023, 12:59:46 PM6/12/23
to AMPL Modeling Language
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;

Lothar Löwer

unread,
Jun 13, 2023, 2:22:28 AM6/13/23
to AMPL Modeling Language
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  ;) ):

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

Regards,
Lothar

Andrés

unread,
Jun 13, 2023, 6:48:55 AM6/13/23
to AMPL Modeling Language
Hi Lothar, thank you very much for answer me.

It doesnt work :(

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

Lothar Löwer

unread,
Jun 13, 2023, 7:14:53 AM6/13/23
to AMPL Modeling Language
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,
Lothar

AMPL Google Group

unread,
Jun 13, 2023, 10:27:00 AM6/13/23
to AMPL Modeling Language
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.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2271214372-117788#}
On Tue, Jun 13, 2023 at 11:15 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
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,
Lothar

On Tue, Jun 13, 2023 at 10:49 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Lothar, thank you very much for answer me.

It doesnt work :(

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

On Tue, Jun 13, 2023 at 6:22 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
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 ;) ):

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

Regards,
Lothar

Andrés schrieb am Montag, 12. Juni 2023 um 18:59:46 UTC+2:

Andrés

unread,
Jun 13, 2023, 10:48:32 AM6/13/23
to AMPL Modeling Language
First, thank you (both) very much for your valuable time and help.

Lothar:
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"

Robert:

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.

Andrés

Gleb Belov

unread,
Jun 13, 2023, 12:33:39 PM6/13/23
to AMPL Modeling Language
For this case:

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

In general:

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

Andrés

unread,
Jun 13, 2023, 12:42:21 PM6/13/23
to AMPL Modeling Language
As this?

let

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

Thank you

Andrés

unread,
Jun 14, 2023, 8:04:01 AM6/14/23
to AMPL Modeling Language
Hi, It's me again... jajaja

Sorry for insist...

AMPL Google Group

unread,
Jun 14, 2023, 2:03:04 PM6/14/23
to AMPL Modeling Language
If you are using our latest version of the gurobi, highs, copt, cbc, or xpress 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 through a constraint, by using the constraint operator ==> which means "implies":

set Lset;
set Sset;

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

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

minimize testobj:
   sum {l in Lset, s in Sset} DL[l,s];

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;

This is only currently supported by gurobi, however.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2271214372-117788#}
On Wed, Jun 14, 2023 at 12:05 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi, It's me again... jajaja

Sorry for insist...

On Tue, Jun 13, 2023 at 4:42 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
As this?

let

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

Thank you

On Tue, Jun 13, 2023 at 4:34 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
For this case:

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

In general:

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

See https://amplmp.readthedocs.io/en/latest/rst/model-guide.html.

On Tue, Jun 13, 2023 at 2:48 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
First, thank you (both) very much for your valuable time and help.

Lothar:
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"

Robert:

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.

Andrés

On Tue, Jun 13, 2023 at 2:26 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
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.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
On Tue, Jun 13, 2023 at 11:15 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
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,
Lothar

On Tue, Jun 13, 2023 at 10:49 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Lothar, thank you very much for answer me.

It doesnt work :(

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

On Tue, Jun 13, 2023 at 6:22 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
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 ;) ):

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

Regards,
Lothar

Andrés schrieb am Montag, 12. Juni 2023 um 18:59:46 UTC+2:

Reply all
Reply to author
Forward
0 new messages