different constraints subject to multiple conditions

266 views
Skip to first unread message

宋知寒

unread,
Apr 1, 2022, 10:16:00 AM4/1/22
to AMPL Modeling Language
Hi Robert,
    I"m working on the constraints with multiple conditions. I not familiar with Ampl. And I"m wondering how to deal with it in the ample. Here is the constraints. Could you give me any suggestions?

constraints with multiple conditions.pdf

AMPL Google Group

unread,
Apr 1, 2022, 3:37:20 PM4/1/22
to AMPL Modeling Language
There are many symbols in this constraint: S, O, D, L, K, and x.
  • Which are parameters, whose values are given in the data for the problem?
  • Which are decision variables, whose optimal values will be set by the solver?
Also, does x(n) have to be either 0 or 1? This information can make a big difference to the difficulty of representing this constraint in AMPL, in a way that the solver can understand.


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}

宋知寒

unread,
Apr 1, 2022, 11:37:17 PM4/1/22
to AMPL Modeling Language
Hi Robert,
     Thank you for replying. X is the decision variable and it is also the binary variable. S,O,L,K are variables which are affected by x. D is the parameter given from the data.

AMPL Google Group

unread,
Apr 2, 2022, 11:01:59 PM4/2/22
to AMPL Modeling Language
Are you trying to say that either

x(n) = 1 and Si(n) = Si(n-1) + Di(n)

or else

x(n) = 0 and Si(n) = max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0)

Also, are all of the terms in your objective and constraints (except for this max) going to be linear?


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Sat, Apr 2, 2022 at 3:37 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

Thank you for replying. X is the decision variable and it is also the binary variable. S,O,L,K are variables which are affected by x. D is the parameter given from the data.
On Fri, Apr 1, 2022 at 7:36 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
There are many symbols in this constraint: S, O, D, L, K, and x.
  • Which are parameters, whose values are given in the data for the problem?
  • Which are decision variables, whose optimal values will be set by the solver?
Also, does x(n) have to be either 0 or 1? This information can make a big difference to the difficulty of representing this constraint in AMPL, in a way that the solver can understand.


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 3, 2022, 9:16:13 AM4/3/22
to AMPL Modeling Language
Dear Robert,
     Thank you for replying. It's exactly what I mean. And they are all linear.

AMPL Google Group

unread,
Apr 5, 2022, 6:25:50 PM4/5/22
to AMPL Modeling Language
Do you have access to CPLEX, Gurobi, or Xpress for AMPL? Then you can use the ==> operator, which means "implies", to write the following "indicator constraints" that those solvers will recognize:

x(n) = 1 ==> Si(n) = Si(n-1) + Di(n)
x(n) = 0 ==> Si(n) >= 0
x(n) = 0 ==> Si(n) >= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0

Actually this only implies that if x(n) = 0, then Si(n) >= max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0). That might be enough to make your model correct.

But if it is necessary for your model to explicitly require that also Si(n) = max ( . . . ), then you will need to also enforce Si(n) <= max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0). To do that, you can define an extra set of zero-one variables z(i), and constraints like this:

x(n) = 0 ==> Si(n) <= M * (1-y(n))
x(n) = 0 ==> Si(n) <= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0 + M * y(n)

where M is some upper limit on possible value of Si(n).

Of course, you will need to transform these statements to proper AMPL syntax, as described in the AMPL book. (For example, Si(n) could be Si[n] in AMPL.)


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Sun, Apr 3, 2022 at 1:16 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,
Thank you for replying. It's exactly what I mean. And they are all linear.

On Sun, Apr 3, 2022 at 3:01 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Are you trying to say that either

x(n) = 1 and Si(n) = Si(n-1) + Di(n)

or else

x(n) = 0 and Si(n) = max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0)

Also, are all of the terms in your objective and constraints (except for this max) going to be linear?


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 6, 2022, 7:07:06 PM4/6/22
to AMPL Modeling Language
Dear Robert,
Thank you for replying again. I was just code as you taught, and I was not sure about the meanning of y(n). Also, the new problem caught me up. When I run the code, it showed that 'Sorry, minos cannot handle logical constraints'.
Here is my code. I don't find where I forgot to put a operator in my constraints.
code.pdf

AMPL Google Group

unread,
Apr 7, 2022, 11:28:14 AM4/7/22
to AMPL Modeling Language
You cannot use the MINOS solver for this problem. MINOS does not accept the ==> operator; i it is possible to linearize the constraints that use ==>, but that requires defining some extra binary (zero-one) variables, which MINOS also does not handle. To solve your problem, try selecting the CPLEX, Gurobi, or Xpress solver, by giving one of the following commands before "solve":

option solver cplex;
option solver gurobi;
option solver xpress;

If you want, you can make three different test runs, each specifying a different solver, and then keep using the one that gives the fastest results in your test.


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Wed, Apr 6, 2022 at 11:07 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,

Thank you for replying again. I was just code as you taught, and I was not sure about the meanning of y(n). Also, the new problem caught me up. When I run the code, it showed that

'Sorry, minos cannot handle logical constraints'.

Here is my code. I don't find where I forgot to put a operator in my constraints.

On Tue, Apr 5, 2022 at 10:25 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Do you have access to CPLEX, Gurobi, or Xpress for AMPL? Then you can use the ==> operator, which means "implies", to write the following "indicator constraints" that those solvers will recognize:

x(n) = 1 ==> Si(n) = Si(n-1) + Di(n)
x(n) = 0 ==> Si(n) >= 0
x(n) = 0 ==> Si(n) >= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0

Actually this only implies that if x(n) = 0, then Si(n) >= max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0). That might be enough to make your model correct.

But if it is necessary for your model to explicitly require that also Si(n) = max ( . . . ), then you will need to also enforce Si(n) <= max (0, Si(n-1) + Di(n) + Oi(n) - Li(n) - K0). To do that, you can define an extra set of zero-one variables z(i), and constraints like this:

x(n) = 0 ==> Si(n) <= M * (1-y(n))
x(n) = 0 ==> Si(n) <= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0 + M * y(n)

where M is some upper limit on possible value of Si(n).

Of course, you will need to transform these statements to proper AMPL syntax, as described in the AMPL book. (For example, Si(n) could be Si[n] in AMPL.)


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 8, 2022, 9:06:08 AM4/8/22
to AMPL Modeling Language
Dear Robert,
I'm so sorry to bother you again. I tried to use the cplex, and the result showed that ' Constraint _scon[1] is not convex quadratic since it is an equality constraint.'
 
I think the problem is that  I am multiplying a variable by another variable in an equality constraint. Am I right? Could you please give me some help?

Here is my code.

#PARAMETERS
param first > 0 integer;
param last > 0 integer;
set N:= first..last; #number of ponding time table
set Stop:=1..6; #number of all bus stop
set M:= 2..3; #id of ponding stop
set W:= Stop diff M; #other bus stops
param Ecost >0; #electricty bus cost
param Ocost>0; #oil bus cost
param Scost>0; #stay cost
param Rcost>0; #in-vehicle cost
param D{Stop,N} >=0;
param G{Stop,N} >=0;
param KO integer; #capicity of Oil bus


#  VARIABLES
var x{N} binary; #0 Oil bus, 1 electricty bus
var Stay{Stop,first-1..last} >=0 integer; #The number of people stranded in ponding
var Re{Stop,N} >=0 integer; # people in the bus in ponding
var O{N,Stop} >=0 integer;
var C{Stop,first-1..last} >=0;
var L{Stop,N} >=0;
var tt{Stop,N} >=0;
var RA{N} binary;
var SA{N} binary;
var SB{N} binary;
var S{N} binary;
var ss;
     
#CONSTRAINTS
#subject to O{i in Stop, n in N}:= Re[i-1,n];
subject to ss1 {i in M, n in N}:
     O[i,n]= Re[i-1,n];
subject to ss20{i in M}:
     C[i,first-1]=2;
subject to ss2 {i in M, n in N}:
     C[i,n] = C[i,n-1]*x[n] + G[i,n]*x[n];
subject to ss3 {i in M, n in N}:
     L[i,n] = C[i,n-1] + G[i,n]-C[i,n-1]*x[n] + G[i,n]*x[n];
subject to ss4 {i in M, n in N}:
     tt[i,n] = O[i,n]+D[i,n]+Stay[i-1,n]-L[i,n];
subject to ss5 {i in M, n in N}:
    tt[i,n]<=KO ==> Re[i,n] = (1-x[n])*tt[i,n] else Re[i,n] =(1-x[n])*KO;
subject to ss60{i in M}:
     Stay[i,first-1]=3;
subject to ss6 {i in M, n in N}:
     x[n] = 1 ==> Stay[i,n] = Stay[i,n-1] + D[i,n];
subject to ss7 {i in M, n in N}:
     x[n] = 0 ==> Stay[i,n] >= 0;
subject to ss8 {i in M, n in N}:
     x[n] = 0 ==> Stay[i,n] >= Stay[i,n-1] + D[i,n] + O[i,n] - L[i,n] - KO;

#OBJETCTIVE
subject to ssn :
 ss = sum{i in M, n in N} (x[n] * Ecost + x[n] * Ocost + Scost * Stay[i,n] + Rcost * Re[i,n]);
minimize cost {i in M, n in N}:ss;




AMPL Google Group

unread,
Apr 9, 2022, 10:53:10 AM4/9/22
to AMPL Modeling Language
That is right, to solve with CPLEX, you will need to linearize all quadratic terms like "C[i,n-1]*x[n]" that appear in equality constraints.


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Fri, Apr 8, 2022 at 1:06 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,
On Thu, Apr 7, 2022 at 3:27 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You cannot use the MINOS solver for this problem. MINOS does not accept the ==> operator; i it is possible to linearize the constraints that use ==>, but that requires defining some extra binary (zero-one) variables, which MINOS also does not handle. To solve your problem, try selecting the CPLEX, Gurobi, or Xpress solver, by giving one of the following commands before "solve":

option solver cplex;
option solver gurobi;
option solver xpress;

If you want, you can make three different test runs, each specifying a different solver, and then keep using the one that gives the fastest results in your test.


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 19, 2022, 9:15:27 AM4/19/22
to AMPL Modeling Language
Dear Robert,
       The constraints like 'x(n) = 0 ==> Si(n) >= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0' doesn't work. The specific constraints in my code are ss3e, ss4d, ss9c, ss10a... I examined these constraints and found when I remove the term 'Stay[i,n-1]', the constraints will work. It seems like ampl doesn't find the value of 'Stay[i,n-1]'. What should I do?
0410_1.mod.txt
0410.dat.txt

AMPL Google Group

unread,
Apr 19, 2022, 3:45:58 PM4/19/22
to AMPL Modeling Language
When I send your model and data to the CPLEX solver, it reports the following result:

CPLEX 20.1.0.0: integer infeasible.

This says that your problem is infeasible: there is no way to assign values to the variables that will satisfy all of your constraints, and also all the bounds and integrality restrictions on the variables. Since the problem is infeasible, any solution that CPLEX returns cannot satisfy all of the requirements of your problem, and thus cannot be a useful solution for your model.

Both CPLEX and AMPL have worked correctly here. Most likely, the infeasible result is due to an error in your model. Or it might be that there is something wrong with your data, that makes a feasible solution impossible. There is no simple and general way to deal with an error like this; you may need to study the model and the data for a while before you can understand the cause of the infeasibility. It is possible to suggest some good ways to get started, however.

As an initial troubleshooting step, it is often helpful to use AMPL's expand command to see see whether AMPL generated the constraints that you expected. By itself, expand; shows all of the constraints. If there are many of them, you can use, for example, expand ss3e; to expand all of the constraints that have a particular name. Also you can write, for instance, expand >listing.txt; or expand ss3e >listing.txt; to send all of the output to the file listing.txt.

You should also consider using AMPL's drop and restore commands to narrow your search for the cause of the infeasibility. If you drop some constraints and the resulting problem is still infeasible, then you don't need to consider the dropped constraints in looking for the cause of infeasibility. By trying a series of drops, you may be able to determine that infeasibility is being caused by only a small subset of the constraints. (Removing a variable is not helpful, though.)

If you are using one of the popular MIP solvers then to get more help with diagnosing the infeasibility, you can ask the solver to compute an irreducible infeasible subset. For more on this topic, search "IIS" at groups.google.com/group/ampl, and also see the discussion of "infeasibility diagnosis" in chapter 14 of the AMPL book (https://ampl.com/BOOK/CHAPTERS/17-solvers.pdf#page=25). I have found that the Gurobi solver is the most reliable for IIS finding.


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Tue, Apr 19, 2022 at 1:15 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,

The constraints like 'x(n) = 0 ==> Si(n) >= Si(n-1) + Di(n) + Oi(n) - Li(n) - K0' doesn't work. The specific constraints in my code are ss3e, ss4d, ss9c, ss10a... I examined these constraints and found when I remove the term 'Stay[i,n-1]', the constraints will work. It seems like ampl doesn't find the value of 'Stay[i,n-1]'. What should I do?

On Sat, Apr 9, 2022 at 2:52 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
That is right, to solve with CPLEX, you will need to linearize all quadratic terms like "C[i,n-1]*x[n]" that appear in equality constraints.


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 21, 2022, 7:36:08 AM4/21/22
to AMPL Modeling Language
Dear Robert, 
        I tried to find the 'iis' command and 'drop' command to determine which constraints caused the infeasible. They all point to the constraints like:
      subject to ss3d {i in NV,n in N}:
       x[n] = 0 ==>Stay[i,n]>= O[i,n] + D[i,n] + Stay[i,n-1] - G[i,n] - KO;

      The error is that 'CPLEX 12.5.0.0: logical constraint _slogcon[12] is not an indicator constraint.' . 

       However, the constraint like 'x[n] = 0 ==>Stay[i,n]>=0;' works.
       Then , I tried to modify the constraints to 'Stay[i,n]>= O[i,n] + D[i,n] + Stay[i,n-1] - G[i,n] - KO;', then it does not report the error.  I really don't know the reason. And how can i fix it?
0410.dat.txt
0410_2.mod.txt

AMPL Google Group

unread,
Apr 22, 2022, 11:30:16 AM4/22/22
to AMPL Modeling Language
I do see that error:

ampl: solve;
CPLEX 12.5.0.1: logical constraint _slogcon[7] is not an indicator constraint.


The following AMPL command confirms that the error message is referring to constraint ss3d[4,2]:

ampl: print _slogconname[7];
ss3d[4,2]


But in this case, the AMPL statement that defines ss3d is correct, and the error is due to a bug in old versions of CPLEX for AMPL. Versions 12.6 and later handle the constraint correctly; for example:

ampl: solve;
CPLEX 20.1.0.0: optimal integer solution; objective 76.4
56 MIP simplex iterations
0 branch-and-bound nodes


So you will need to update your version of CPLEX to use this feature.


--
Robert Fourer
am...@googlegroups.com
{#HS:1833854505-109374#}
On Thu, Apr 21, 2022 at 11:36 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Dear Robert,

I tried to find the 'iis' command and 'drop' command to determine which constraints caused the infeasible. They all point to the constraints like:

subject to ss3d {i in NV,n in N}:
x[n] = 0 ==>Stay[i,n]>= O[i,n] + D[i,n] + Stay[i,n-1] - G[i,n] - KO;

The error is that 'CPLEX 12.5.0.0: logical constraint _slogcon[12] is not an indicator constraint.' .

However, the constraint like 'x[n] = 0 ==>Stay[i,n]>=0;' works.

Then , I tried to modify the constraints to 'Stay[i,n]>= O[i,n] + D[i,n] + Stay[i,n-1] - G[i,n] - KO;', then it does not report the error. I really don't know the reason. And how can i fix it?

On Tue, Apr 19, 2022 at 7:45 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
When I send your model and data to the CPLEX solver, it reports the following result:

CPLEX 20.1.0.0: integer infeasible.

This says that your problem is infeasible: there is no way to assign values to the variables that will satisfy all of your constraints, and also all the bounds and integrality restrictions on the variables. Since the problem is infeasible, any solution that CPLEX returns cannot satisfy all of the requirements of your problem, and thus cannot be a useful solution for your model.

Both CPLEX and AMPL have worked correctly here. Most likely, the infeasible result is due to an error in your model. Or it might be that there is something wrong with your data, that makes a feasible solution impossible. There is no simple and general way to deal with an error like this; you may need to study the model and the data for a while before you can understand the cause of the infeasibility. It is possible to suggest some good ways to get started, however.

As an initial troubleshooting step, it is often helpful to use AMPL's expand command to see see whether AMPL generated the constraints that you expected. By itself, expand; shows all of the constraints. If there are many of them, you can use, for example, expand ss3e; to expand all of the constraints that have a particular name. Also you can write, for instance, expand >listing.txt; or expand ss3e >listing.txt; to send all of the output to the file listing.txt.

You should also consider using AMPL's drop and restore commands to narrow your search for the cause of the infeasibility. If you drop some constraints and the resulting problem is still infeasible, then you don't need to consider the dropped constraints in looking for the cause of infeasibility. By trying a series of drops, you may be able to determine that infeasibility is being caused by only a small subset of the constraints. (Removing a variable is not helpful, though.)

If you are using one of the popular MIP solvers then to get more help with diagnosing the infeasibility, you can ask the solver to compute an irreducible infeasible subset. For more on this topic, search "IIS" at groups.google.com/group/ampl, and also see the discussion of "infeasibility diagnosis" in chapter 14 of the AMPL book (https://ampl.com/BOOK/CHAPTERS/17-solvers.pdf#page=25). I have found that the Gurobi solver is the most reliable for IIS finding.


--
Robert Fourer
am...@googlegroups.com

宋知寒

unread,
Apr 22, 2022, 9:52:35 PM4/22/22
to AMPL Modeling Language
Dear Robert,
        Thanks a lot. It do works.

Reply all
Reply to author
Forward
0 new messages