translate if then sentence into AMPL

5,194 views
Skip to first unread message

lidia saez

unread,
Dec 5, 2011, 1:49:20 PM12/5/11
to AMPL Modeling Language
Hello,
Can anyone suggest me how can I translate below condition statement
into
AMPL:

if (sum {j in P} y[j] > 0 then A=1 else 0

A is a parameter, and y[j] is a variable

I've tried many times but always show a syntax error.

I appreciate you help

Robert Fourer

unread,
Dec 5, 2011, 11:56:09 PM12/5/11
to am...@googlegroups.com
If you are just trying to set the value of parameter A based on values
previously assigned to the variables y[j] -- assigned by a previous solve,
for example -- then you can write

let A := (if sum {j in P} y[j] > 0 then 1 else 0);

or

if sum {j in P} y[j] > 0
then let A := 1;
else let A := 0;

You cannot write

param A = (if sum {j in P} y[j] > 0 then 1 else 0);

as this defines a parameter in terms of a variable. On the other hand you
can legally write

var A = (if sum {j in P} y[j] > 0 then 1 else 0);

to make A's value dependent on y[j], but if this variable is used in an
objective or constraint then attempts to solve the resulting model will be
unsuccessful, as the current solvers can't deal with this kind of function.

Bob Fourer
4...@ampl.com

Melisa

unread,
Dec 12, 2011, 12:30:36 PM12/12/11
to AMPL Modeling Language
Hello, I have a query similar to the above question. I got some
troubles to write the following constraint.The model use two binary
variables Yij and Xj,  I want that
for all {(i,j) in links}         if Yij = 1 then Xi = 1 and Xj = 1  
 else do nothing
any suggest about how can I translate these condition?
I appreciate your help.
On Dec 6, 12:56 am, "Robert Fourer" <4...@ampl.com> wrote:
> Ifyou are just trying to set the value of parameter A based on values

> previously assigned to the variables y[j] -- assigned by a previous solve,
> for example -- then you can write
>
>    let A := (ifsum {j in P} y[j] > 0 then 1 else 0);
>
> or
>
>    ifsum {j in P} y[j] > 0

>       then let A := 1;
>       else let A := 0;
>
> You cannot write
>
>    param A = (ifsum {j in P} y[j] > 0 then 1 else 0);

>
> as this defines a parameter in terms of a variable.  On the other hand you
> can legally write
>
>    var A = (ifsum {j in P} y[j] > 0 then 1 else 0);
>
> to make A's value dependent on y[j], butifthis variable is used in an

> objective or constraint then attempts to solve the resulting model will be
> unsuccessful, as the current solvers can't deal with this kind of function.
>
> Bob Fourer
> 4...@ampl.com
>
>
>
>
>
>
>
> > -----Original Message-----
> > From: am...@googlegroups.com [mailto:am...@googlegroups.com]
> > On Behalf Of lidia saez
> > Sent: Monday, December 05, 2011 12:49 PM
> > To: AMPL Modeling Language
> > Subject: [AMPL 5266] translateifthen sentence into AMPL
>
> > Hello,
> > Can anyone suggest me how can I translate belowconditionstatement
> > into
> > AMPL:
>
> >if(sum {j in P} y[j] > 0 then A=1 else 0

Robert Fourer

unread,
Dec 13, 2011, 3:53:43 PM12/13/11
to am...@googlegroups.com
If you are using CPLEX then you can write an AMPL constraint

subject to C {(i,j) in links}:
Y[i,j] = 1 ==> X[i] + X[j] = 2;

where ==> is the AMPL operator for "implies". This works since for two
binary variables, their sum is 2 if and only if they are both equal to 1.
You can also disaggregate this into two separate constraints, Y[i,j] = 1 ==>
X[i] = 1 and Y[i,j] = 1 ==> X[j] = 1; such a formulation might give better
performance, though whether it does so for any particular problem is best
resolved by running some experiments.

For any MIP solver (including CPLEX) you can also reformulate these as
linear constraints:

subject to C {(i,j) in links}:
2 * Y[i,j] <= X[i] + X[j];

since if Y[i,j] is 1 this forces X[i] and X[j] to be 1, while if Y[i,j] is 0
it imposes no additional restrictions on X[i] or X[j]. Again you can
disaggregate this, to Y[i,j] <= X[i] and Y[i,j] <= X[j]. Whether this
linearization would work better or worse than using "==>" is again something
that is best resolved by testing on your particular problem.

Bob Fourer
4...@ampl.com


> -----Original Message-----
> From: am...@googlegroups.com [mailto:am...@googlegroups.com]
> On Behalf Of Melisa
> Sent: Monday, December 12, 2011 11:31 AM
> To: AMPL Modeling Language
> Subject: [AMPL 5304] Re: translate if then sentence into AMPL
>
> Hello, I have a query similar to the above question. I got some

> troubles to write the following constraint. The model use two binary

Mel

unread,
Dec 14, 2011, 11:54:02 AM12/14/11
to AMPL Modeling Language
Thank's for your response. I proved both of them in my model without
satisfactory results for my specific problem.  (Actually I was using
the seconds constraints suggested, in aggregate  and disaggregate
form).Let mi explain more about the model: have a network G(N,E), each
links is associated a cost,
then I want find the shortest path between a node origin and a
destination node,
and for the nodes that are in  the path fixed an instalation. The
objetive funcion minimize the shortest path ( cost_link[i,j] * Y[i,j])
for all (i,j) in Es.t  flow constraintssum{j in Nodes: (j,i) in E}
Y[j,i] - sum{h in Nodes: (i,h) in E} Y[i,h]= demand[i];

until here the problem works well, when add the "localization"
constraints the resulting X vector is 0.
The variable descriptions are: if Y[i,j]  = 1 the link is in the
shortest path, 0 is not.then I use the binary variable Xj to indicate
if a facilitie is located at node j or not. So, thats the reason
because I want that when Yij = 1 then Xi = 1 and Xj = 1, but I have
problem yet.
I really appreciate your advice very much.

wanba...@163.com

unread,
Dec 16, 2011, 7:42:06 PM12/16/11
to AMPL Modeling Language
Please post your code, so that we help you find where the problem.

Mel

unread,
Dec 17, 2011, 10:48:10 AM12/17/11
to AMPL Modeling Language
Hello, here is the code:

# Model
param numNodes;
set Nodes := 1..numNodes;
set E within {Nodes,Nodes};

param Cost{E};
param demand{i in Nodes};

var Y{E} binary; # if the arc (i,j) is in the shortest path
var X{Nodes} binary; # binary variable for location

#Objective Function
minimize trip: sum{(i,j) in E} Y[i,j] * Cost[i,j];

#Constraint
subject to conservation{i in Nodes}:


sum{j in Nodes: (j,i) in E} Y[j,i] - sum{h in Nodes: (i,h) in
E} Y[i,h]= demand[i];

subject to Indicator1 {i in Nodes, j in Nodes: (i,j) in E}:
Y[i,j] <= X[i];

subject to Indicator2 {i in Nodes, j in Nodes: (i,j) in E}:
Y[i,j] <= X[j];

-----------------------------------------------------------

Finally, I use other option, I replace the constraints Indicador1 and
Indicator1 by:

subject to indicator1{ i in Nodes: i != t}:
sum{j in Nodes: (i,j) in E}Y[i,j] = X[i];

subject to fixedest: X[t]=1;

where t is the destination node and fixed to 1. In this way, it work.


but, I don't no why the first don't work well. I'm interesting in
avoid fixed the destination node to 1 in the model (don't use use
constraint fixedest).

This is the result when using Indicator1 and Indicator2:

Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;

X [*] :=
1 1
2 1
3 1
4 1
;


Thanks for your advises!

wanba...@163.com

unread,
Dec 19, 2011, 4:05:56 AM12/19/11
to AMPL Modeling Language
# Model param numNodes; set Nodes := 1..numNodes; set E within
{Nodes,Nodes}; param Cost{E}; param demand{i in Nodes}; var Y{E}
binary; # if the arc (i,j) is in the shortest path var X{Nodes}
binary; # binary variable for location #Objective Function minimize
trip: sum{(i,j) in E} Y[i,j] * Cost[i,j]; #Constraint conservation{i

in Nodes}: sum{j in Nodes: (j,i) in E} Y[j,i] - sum{h in Nodes:
(i,h) in E} Y[i,h]= demand[i]; Indicator1 {i in Nodes, j in Nodes:
(i,j) in E}: Y[i,j] <= X[i]; Indicator2 {i in Nodes, j in
Nodes: (i,j) in E}: Y[i,j] <= X[j]; data;param numNodes:=4;param :
E : Cost := 1 2 4 1 3 8 1 4 9 2 3 6 2 4 3 3 4
10 ;param demand := 1 -1 2 0 3 0 4 1 ;
options solver gurobi;solve;display Y,X;

The results follows:


Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;

X [*] :=
1 1
2 1
3 1
4 1
;

Mel

unread,
Dec 19, 2011, 10:36:10 AM12/19/11
to AMPL Modeling Language
Hello!

Yes, I get the same result for the vector X using these data:
# Data
param numNodes = 4;


param demand :=
1 -1
2 0
3 0
4 1;

set E := (1,2) (1,3) (1,4) (2,3) (2,4) (3,4);

param: Cost:=
1 2 0.1513
1 3 0.4518
1 4 0.6279
2 3 0.4513
2 4 0.4518
3 4 0.1513;

And my interest is that only the nodes that are on the shortestpath
are 1. I replace the constraints Indicador1 and
Indicator1 by indicator1 and fixedest (described above) to obtain:

Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;
X [*] :=
1 1
2 1

3 0
4 1
;

but still I don't know why using Indicador1 and Indicator2 the model
don't work as i hope...

Robert Fourer

unread,
Dec 20, 2011, 3:31:16 PM12/20/11
to am...@googlegroups.com
In the first formulation, the Indicator1 and Indicator2 constraints may
always be satisfied by setting all of the X-variables to 1, without any
limitation on the choice of the Y-variables so as to satisfy the other
constraints and minimize the objective. So there's evidently something
wrong with this formulation.

Note that in the second formulation one cannot simply set all the
X-variables to 1, as setting any X[j] to 1 forces at least one Y[i,j] to 1
-- which does make a difference to the conservation constraints and the
objective.

Mel

unread,
Dec 21, 2011, 10:20:02 AM12/21/11
to AMPL Modeling Language
Many thank's for your help!

Regards!

sagar...@gmail.com

unread,
Apr 16, 2015, 11:09:28 AM4/16/15
to am...@googlegroups.com, lisa...@gmail.com
Hello,
I want to implement the following:
for all i,j in CITIES
    sum{k in CITIES} if route[i,j,k]>=handling_cost[2] then (handling_cost_city +handling[k,2])=handling_cost_city
            else handling_cost_city=handling_cost_city;

where
I know the values of handling[k,2] and handling_cost[2].

In plain english the problem is:
I want to find the numerical sum of handling[k,2] if route[i,j,k]>handling_cost[2] over k for all i,j
I would be using this cost as a variable in minimization cost problem.

Robert Fourer

unread,
Apr 17, 2015, 2:47:50 PM4/17/15
to am...@googlegroups.com

It is not entirely clear from your description what are the parameters and what are the variables in your expression.  That makes a big difference to how you should write your condition.  I imagine that you are looking for something like

 

   sum {k in CITIES: route[i,j,k]>=handling_cost[2]} handling[k,2]

 

but that works only if route and handling_cost are parameters.  For more help, I suggest posting the AMPL definitions of CITIES, route, handling_cost, and handling_cost_city.

 

Bob Fourer

am...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Sagar Agarwal

unread,
Apr 17, 2015, 8:20:17 PM4/17/15
to am...@googlegroups.com
It is still giving syntax error at line 51
You can view the updated files.

On Sat, Apr 18, 2015 at 2:04 AM, Sagar Agarwal <sagar...@gmail.com> wrote:
CITIES is a set
rest are parameters.

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/BlolphCk0Pw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.

To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



--
Regards,
Sagar Agarwal
--------------------------------------------------
General Secretary                                 
Aerospace Engineering Department
IIT Bombay                                             
+919833570048                                   
--------------------------------------------------



--
Regards,
Sagar Agarwal
--------------------------------------------------
General Secretary                                 
Aerospace Engineering Department
IIT Bombay                                             
+919833570048                                   
--------------------------------------------------
datav3.dat
stage1v3.mod

Robert Fourer

unread,
Apr 19, 2015, 8:07:16 PM4/19/15
to am...@googlegroups.com
If you index your constraint using "k in CITIES",

subject to airport_handling_constrain1 {i in CITIES, j in CITIES,k in CITIES,p in WEIGHTS:i!=j}:

then you cannot also use k as a summation index inside the constraint, as you are doing:

sum {k in CITIES: route[i,j,k]>=handling_cost[p]} handling[k,2] = handling_cost_city[p];

Using k as the summation index is giving you the syntax error. Probably "k in CITIES" should be removed from the indexing set for this constraint.

Bob Fourer
am...@googlegroups.com

=======

azr...@gmail.com

unread,
Apr 25, 2015, 9:05:48 PM4/25/15
to am...@googlegroups.com, 4...@ampl.com
Hi:

I have a similar question. I want to specify that if the binary variable x[j,1] =1 then that a specific constraint needs to be true. I would appreciate some help with this.

set J := {1..50};
set L:= {1,2};
set R:= {1,2};

x[j,l] and z[j,r] - variables.

I want to specify that:

if x[j,1] =1

Then the following constraint has to be true for j :

subject to A {j in J}:  z[j,1] <= (z[j,2]/5);

if x[j,1]=0 then the above constraint should not be valid.


How should it be correctly formulated?

Thank you!

Robert Fourer

unread,
Apr 28, 2015, 6:45:02 PM4/28/15
to am...@googlegroups.com
If you are using CPLEX then you can write this as an "indicator constraint":

subject to A {j in J}: x[j,1] = 1 ==> z[j,1] <= z[j,2]/5;

(Read the operator ==> as "implies".) A linear reformulation that will work with any MIP solver (and might even work better with CPLEX) depends on defining a parameter U[j] (indexed over j in J) whose value is an upper bound on z[j,1] - z[j,2]/5. Then the constraint is

subject to A {j in J}: z[j,1] <= z[j,2]/5 + U[j]*(1-x[j,1]);

Bob Fourer
am...@googlegroups.com

=======

Md Mashum Billal

unread,
Nov 8, 2020, 4:55:41 PM11/8/20
to AMPL Modeling Language
If I have two continuous variables, x[i] and y[j]. I want to set the equation in that way that I got say x[1]=y[1], x[2]=y[2], x[3]=y[3] ...........that means i=j. How can I write in AMPL? 

Mikhail

unread,
Nov 8, 2020, 9:41:21 PM11/8/20
to AMPL Modeling Language
Hello. If i = j then you can write:
var X{i in ...}:= Y[i];
or use constraint:
subject to
A_1
{i in ...}:X[i] = Y[i];
or
A_1{i in ...}:X[i] - Y[i] = 0;

Md.Mashum Billal

unread,
Nov 8, 2020, 10:30:01 PM11/8/20
to AMPL Modeling Language
Thank you for your reply. Let say, X[i] is the quantity in the facility and Y[j] is the quantity in the distribution center(DC). I want to get the same quantity in both facility and DC. For example, If the facility chooses X[1]=20 units then the DC must have to choose Y[1]=20 UNITS.  I am getting the result by writing n number of constraints shown below.
  subject to1: X[1]=Y[1];
  subject to2: X[2]=Y[2];
  subject to3: X[3]=Y[3];
  subject to4: X[4]=Y[4];
.
. .......... n.  
Here I need to write n number of constraints. But I want to write these in one or two constraints AMPL. I appreciate your kind help.

Mikhail

unread,
Nov 9, 2020, 2:39:44 AM11/9/20
to AMPL Modeling Language
if i = j then it is advisable to leave only i
set I:={'N1','N2','N3','N4'...}; # List of enterprises.
Each facility has a distribution center with the same name;
var X {i in I}> = 0; # Quantity at each ('N1', 'N2', 'N3', 'N4' ...) facility
var Y {i in I}> = 0; # Quantity in each ('N1', 'N2', 'N3', 'N4' ...) distribution center
subject to A_1 {i in I}: X [i] = Y [i]; # Conditions apply to all i in I ('N1', 'N2', 'N3', 'N4' ...)

AMPL Google Group

unread,
Nov 9, 2020, 11:04:33 AM11/9/20
to AMPL Modeling Language
It appears that you have defined the variables in a way that is similar to this:

param n integer > 0;
var X {1..n} >= 0;
var Y {1..n} >= 0;


Then to make each variable Y[i] take the same value as the corresponding X[i], you can add constraints like this:

subject to YequalsX {j in 1..n}: Y[j] = X[j];

However, I would recommend instead defining X and Y like this:

param n integer > 0;
var X {1..n} >= 0;
var Y {j in 1..n} = X[j];


This causes X[j] to be substituted for Y[j] wherever it appears in your optimization problem. No extra constraints are needed in this case, and AMPL does not need to send the Y variables to the solver.


--
Robert Fourer
am...@googlegroups.com
{#HS:1332551916-92903#}
On Mon, Nov 9, 2020 at 7:39 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
if i = j then it is advisable to leave only i
set I:={'N1','N2','N3','N4'...}; # List of enterprises.
Each facility has a distribution center with the same name;
var X {i in I}> = 0; # Quantity at each ('N1', 'N2', 'N3', 'N4' ...) facilityvar Y {i in I}> = 0; # Quantity in each ('N1', 'N2', 'N3', 'N4' ...) distribution center
subject to A_1 {i in I}: X = Y ; # Conditions apply to all i in I ('N1', 'N2', 'N3', 'N4' ...)

On Mon, Nov 9, 2020 at 3:30 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thank you for your reply. Let say, X is the quantity in the facility and

Y[j] is the quantity in the distribution center(DC). I want to get the same
quantity in both facility and DC. For example, If the facility chooses
X[1]=20 units then the DC must have to choose Y[1]=20 UNITS. I am getting
the result by writing n number of constraints shown below.
subject to1: X[1]=Y[1];
subject to2: X[2]=Y[2];
subject to3: X[3]=Y[3];
subject to4: X[4]=Y[4];
.
. .......... n.
Here I need to write n number of constraints*. But I want to write these in
one or two constraints AMPL*. I appreciate your kind help.

On Mon, Nov 9, 2020 at 2:41 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hello. If i = j then you can write:

var X{i in ...}:= Y;
or use constraint:
subject to
A_1{i in ...}:X = Y;orA_1{i in ...}:X - Y = 0;


--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.

Md Mashum Billal

unread,
Nov 9, 2020, 12:09:41 PM11/9/20
to AMPL Modeling Language
Thank you so much for your valuable information. Now my model is working.
Reply all
Reply to author
Forward
0 new messages