If in constraints (AMPL-CPLEX)

676 views
Skip to first unread message

Wafa Lakdhar

unread,
Aug 14, 2016, 6:09:30 PM8/14/16
to AMPL Modeling Language
Dear All,

Please how can i  translate this condition statement into AMPL:

if X[i,j]=0 then  A[i] = a value  and  A[j] =0;   (where X and A are variables)


If i wrote  "if X[i,j]=0 then  A[i] = a value ;" it works but when i add the second on A[j] = 0; it shows a syntax error.

I appreciate you help!!

Thanks,

Wafa

Robert Fourer

unread,
Aug 15, 2016, 12:48:50 PM8/15/16
to am...@googlegroups.com
You need to write "if ... then ... else" rather than "if ... then ... and". However in AMPL an expression of the if-then-else kind gives you only a value and not a constraint, so it will not accomplish what you want.

If the variable X is defined as "binary" (it can only take the values 0 or 1) and you are solving with CPLEX then you can use AMPL's "implies" operator ==> to express your constraint:

X[i,j] = 0 ==> A[i] = <value> else A[j] = 0

(Of course you have to replace <value> with an actual number or expression.) Otherwise you will need to linearize the constraint; in that case, to get more help you will need to post your AMPL var definitions of X and A.

Bob Fourer
am...@googlegroups.com

=======
Message has been deleted
Message has been deleted

Wafa Lakdhar

unread,
Aug 16, 2016, 6:56:22 AM8/16/16
to AMPL Modeling Language, 4...@ampl.com
thank you for your reply Bob.

Please find the attached model file.
Gam.mod

Wafa Lakdhar

unread,
Aug 16, 2016, 6:56:54 AM8/16/16
to AMPL Modeling Language, 4...@ampl.com
param n;


param C_old{i in 1..n} >= 0;
param d{i in 1..n};


var X{i in 1..n , j in 1..n} binary ;
var Cnew{i in 1..n}>= 0; 

 
maximize res: sum {i in 1..n, j in 1..n } X[i,j];


subject to c1 { i in 1..n, j in 1..n: i<>j } : if (d[i] - d[j]) = 0 then X[i,j]=1;
subject to c2 { i in 1..n, j in 1..n : i<>j} : if (d[i] - d[j]) <> 0 then X[i,j]=0;

subject to c3 { i in 1..n, j in 1..n : i<>j} : if X[i,j]=0 then Cnew[i]= C_old[i] ;
subject to c4 { i in 1..n, j in 1..n : i<>j} : if X[i,j]=1 then Cnew[i] = C_old[i] + C_old[j] and Cnew[j] = 0; 
 
 



Victor Zverovich

unread,
Aug 16, 2016, 1:31:18 PM8/16/16
to am...@googlegroups.com
The constraint

  subject to c1 { i in 1..n, j in 1..n: i<>j } : if (d[i] - d[j]) = 0 then X[i,j]=1;

is equivalent to

  subject to c1 { i in 1..n, j in 1..n: i<>j } : (if (d[i] - d[j]) = 0 then X[i,j]) = 1;

which is probably not what you meant. If you want X[i, j] to be equal to 1 if (d[i] - d[j]) = 0 and 0 otherwise, you can rewrite c1/c2 as

  subject to c1 { i in 1..n, j in 1..n: i<>j } : X[i,j] = if (d[i] - d[j]) = 0 then 1;

but note that X[i,j] will only depend on parameters.

HTH,
Victor

On Tue, Aug 16, 2016 at 3:56 AM Wafa Lakdhar <wafa.la...@gmail.com> wrote:
thank you for your reply Bob.

Please find the attached model file.

--
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 https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Wafa Lakdhar

unread,
Aug 16, 2016, 2:11:07 PM8/16/16
to AMPL Modeling Language
Thanks sir , i will change it.


But my problem is how to translate this :  if X[i,j]=1 then Cnew[i] = C_old[i] + C_old[j] and Cnew[j] = 0 ; 

into ampl ?


Wafa

Robert Fourer

unread,
Aug 16, 2016, 2:23:04 PM8/16/16
to am...@googlegroups.com

Since X is a binary variable, when using CPLEX you can write constraints c3 and c4 as

 

 subject to c3 { i in 1..n, j in 1..n : i<>j} : X[i,j]=0 ==> Cnew[i] = C_old[i];

  subject to c4a { i in 1..n, j in 1..n : i<>j} : X[i,j]=1 ==> Cnew[i] = C_old[i] + C_old[j];

  subject to c4b { i in 1..n, j in 1..n : i<>j} : X[i,j]=1 ==> Cnew[j] = 0;

 

Something must be wrong with the c1 and c2 constraints, however, since they fix the values of all of the X variables according to the given values of param d, and thus fix the objective value and all the Cnew values -- leaving nothing to optimize.

 

In general, the if-then construct is used to specify expressions in AMPL, but not constraints.

 

Bob Fourer

am...@googlegroups.com

--

Wafa Lakdhar

unread,
Aug 16, 2016, 3:01:59 PM8/16/16
to AMPL Modeling Language, 4...@ampl.com

Thanks for your reply sir.

When i add this I got a synthax error:

Robert Fourer

unread,
Aug 16, 2016, 4:08:15 PM8/16/16
to am...@googlegroups.com
I see that when you run AMPL it gives you the message "AMPL Version 20021031 (Win32)". This indicates that you are using a version from 31 October 2002 -- almost 14 years ago! The ==> operator was introduced in a later version. (I tested your constraint c3 on my computer and the current version of AMPL did not give a syntax error.)

It is impossible for us to support very old versions like the one you have, so you should consider upgrading. There are a variety of alternatives that you can consider; see our "Try AMPL" page at http://ampl.com/try-ampl/ for an overview and links to detailed descriptions.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Wafa Lakdhar
Sent: Tuesday, August 16, 2016 2:02 PM
To: AMPL Modeling Language
Cc: 4...@ampl.com
<<screenshot>>


Wafa Lakdhar

unread,
Aug 16, 2016, 4:23:16 PM8/16/16
to AMPL Modeling Language, 4...@ampl.com
I m so grateful for your help sir

Wafa Lakdhar

unread,
Aug 18, 2016, 4:11:51 AM8/18/16
to AMPL Modeling Language, 4...@ampl.com

I consider upgrading and the "==>" operator works very well but my problem that i  didn't get the result that i want. Cnew  has the same value of C_old.






Wafa Lakdhar

unread,
Aug 18, 2016, 4:16:26 AM8/18/16
to AMPL Modeling Language, 4...@ampl.com
While Cnew must be:

1   70
2   0
3   20
4   80
5   0

Victor Zverovich

unread,
Aug 18, 2016, 2:47:46 PM8/18/16
to am...@googlegroups.com
The presolve messages indicate that your problem is infeasible. You should check your constraint c1.

HTH,
Victor

On Thu, Aug 18, 2016 at 1:11 AM Wafa Lakdhar <wafa.la...@gmail.com> wrote:

I consider upgrading and the "==>" operator works very well but my problem that i  didn't get the result that i want. Cnew  has the same value of C_old.






Message has been deleted

Wafa Lakdhar

unread,
Aug 18, 2016, 5:08:08 PM8/18/16
to AMPL Modeling Language

i set the constraint C1 but i have the same problem

Victor Zverovich

unread,
Aug 19, 2016, 1:22:31 PM8/19/16
to am...@googlegroups.com
What do you mean by setting the constraint c1? Could you post the code?

- Victor

On Thu, Aug 18, 2016 at 10:07 PM Wafa Lakdhar <wafa.la...@gmail.com> wrote:
i set the constraint c1 but i have the same problem

Wafa Lakdhar

unread,
Aug 20, 2016, 6:25:08 AM8/20/16
to AMPL Modeling Language
Gam.mod

Victor Zverovich

unread,
Aug 22, 2016, 1:43:08 PM8/22/16
to am...@googlegroups.com
Since d is a parameter, you can replace c1 and c2 with

  var X{i in 1..n , j in 1..n} binary = if (d[i] - d[j]) = 0 then 1;

Note that values of X are determined by values of parameters so X should be a parameter as well.

HTH,
Victor

Wafa Lakdhar

unread,
Aug 29, 2016, 7:08:11 AM8/29/16
to AMPL Modeling Language
Sir i want to compute Cnew but it has the same value of C_old.

How do i process ?

Robert Fourer

unread,
Aug 30, 2016, 4:02:54 PM8/30/16
to am...@googlegroups.com

Please post your model and data again so that we can be sure we are looking at the current version.

Bob Fourer
am...@googlegroups.com


From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Wafa Lakdhar


Sent: Monday, August 29, 2016 6:08 AM
To: AMPL Modeling Language

Wafa Lakdhar

unread,
Aug 30, 2016, 4:48:48 PM8/30/16
to AMPL Modeling Language, 4...@ampl.com
Please fin the attached mod file and data file.
data.dat
Gam.mod

Robert Fourer

unread,
Aug 31, 2016, 4:40:55 PM8/31/16
to am...@googlegroups.com
With this version of your model you are still getting a lot of constraints like

presolve, constraint c4a[1,2]:
all variables eliminated, but lower bound = 20 > 0

which are telling you that AMPL's presolve phase has determined that no feasible solution is possible for your constraints. Indeed since d[1] = d[2], constraints c1[1,2] and c1[2,1] imply that X[1,2] = 1 and X[2,1] = 1. Substituting these values of X into constraints c4a[1,2] and c4b[1,2], you have

Cnew[1] = C_old[1] + C_old[2] = 70
Cnew[2] = 0

But also, substituting these values of X into constraints c4a[2,1] and c4b[2,1], you have

Cnew[2] = C_old[2] + C_old[1] = 70
Cnew[1] = 0

Obviously these cannot all be true, so AMPL's presolve is correct in reporting that no feasible solution is possible. There must be something wrong with the constraints, and you will have to think some more about what the correct formulation of the constraints should be.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Wafa Lakdhar
Sent: Tuesday, August 30, 2016 3:49 PM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 12567] If in constraints (AMPL-CPLEX)
s
Please fin the attached mod file and data file.

=======

Wafa Lakdhar

unread,
Aug 31, 2016, 4:59:10 PM8/31/16
to AMPL Modeling Language, 4...@ampl.com
You are right sir !!!.

 My goal is to obtain this:

if X[i,j]=0        then      Cnew[i]= C_old[i];
else if X[i,j]=1 then      Cnew[i]= C_old[i] +  C_old[j]    and   Cnew[j]=0

Robert Fourer

unread,
Sep 2, 2016, 9:49:45 AM9/2/16
to am...@googlegroups.com

Indeed, but in your example X[1,2] = 1 and also X[2,1] = 1, and in that case I cannot see any way to obtain a feasible solution to your if-then conditions.

 

Bob Fourer

am...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages