Formulating a constraint

1,737 views
Skip to first unread message

mattias

unread,
Jan 21, 2008, 5:12:33 PM1/21/08
to AMPL Modeling Language
Hi,

I would be ever so grateful for help in formulating a constraint.


I am trying to formulate a constraint to give the highest value for
Var1 where:

SET1 has 3 indices: i,j,p Var1 is a decison variable in SET1

SET2 has 1 index: k

SET3 has 3 indices: i,j,k Param1 is a parameter in SET3

SET4 has 2 indices: i,k Param2 is a parameter in SET4

Note: 'i' is the same in SET1, SET3 and SET4
'j' is the same in SET2 and SET3
'k' is the same in SET2, SET3 and SET4

Essentially, the constrained value of VAR1 is the max of Param2 *
Param1, where Param1 = p

Another way of expressing this is:
subject to {( i,j,p) in SET1}:
Var1( i,j,p) = max{ k in SET2} Param2[ i,k] * Param1[i,j,k] (where
Param1[i,j,k] = p;


Maybe it may be better to formulate 2 constraints if it is not
possible with 1 constraint.

Many thanks in advance.
Mattias


Paul

unread,
Jan 22, 2008, 9:46:25 AM1/22/08
to AMPL Modeling Language


On Jan 21, 5:12 pm, mattias <mattiasl...@hotmail.co.uk> wrote:

> Essentially, the constrained value of VAR1 is the max of Param2 *
> Param1, where Param1 = p
>
> Another way of expressing this is:
> subject to {( i,j,p) in SET1}:
> Var1( i,j,p) = max{ k in SET2} Param2[ i,k] * Param1[i,j,k] (where
> Param1[i,j,k] = p;
>

When you say "where Param1[i,j,k] = p", do you mean that Param1[i,j,k]
= p for some values of k and not for all values of k? If so, you
could rewrite the maximum as

max {k in SET2 : Param1[i,j,k] = p} p*Param2[i,j].

If Param1 and Param2 are truly parameters (fixed values) and Var1 is
the maximum product (as indexed above), then Var1 is actually a
parameter and not a variable (its value is fixed before you solve the
model). So you can define it as you did above, but as a parameter
definition, not as a constraint.

The only way I could see Var1 actually varying above would be if SET2
were itself variable, so that the suitable choices of k (such that
Param1[i,j,k] = p) changed for different candidate solutions of the
model.

/Paul

Robert Fourer

unread,
Jan 22, 2008, 12:31:43 PM1/22/08
to am...@googlegroups.com, mattias

It's not entirely clear to me what you're trying to do here, but I believe it's
possible that you intend something like the following:

set I;
set J;
set P;
set SET1 within {I,J,P};
set K;
set SET3 within {I,J,K};
set SET4 within {I,K};

param Param1 {SET3};
param Param2 {SET4};
var Var1 {SET1};

subject to Var1Constraint {(i,j,p) in SET1}:
Var1[i,j,p] =
max {k in K: Param1[i,j,k] = p}
Param2[i,k] * Param1[i,j,k];

This constraint is fixing all of the values of the variables, however, so I
doubt that it's really what you want. To say anything more definite, one would
have to know where this problem comes from and what the sets, parameters,
variable, and constraint are supposed to mean.

Bob Fourer
4...@ampl.com

mattias

unread,
Jan 25, 2008, 6:58:59 AM1/25/08
to AMPL Modeling Language
Paul,

I tried your formulation but AMPL retuned errors re - infinity. I
then realised that the explanation I provided was not entirely
correct. The correct version is as follows:

The constrained value of Var1 is the max of Param2, where Param1 =
p.

The 2nd statement should have been:
subject to {( i,j,p) in SET1}:
Var1( i,j,p) = max{ k in SET2} Param2[ i,k] * (Param1[i,j,k] = p);

I need to explain the last part of the constraint above. What I would
like is for AMPL to return 1 if Param1[i,j,k] = p, othewise return 0.
Param2 is then multipled by either 1 or 0, when evaluating the max.

I am sorry for the error in my original request.
Kind regards
Mattias

mattias

unread,
Jan 25, 2008, 7:02:21 AM1/25/08
to AMPL Modeling Language
Bob,

can you please read the response I sent to Paul. I tried the
suggested constraint but AMPL returned errors re -infinity. I then
realised I had made an error in the explanation I provided.

Kind regards
Mattias
> > Mattias- Hide quoted text -
>
> - Show quoted text -

Robert Fourer

unread,
Jan 25, 2008, 10:59:42 AM1/25/08
to am...@googlegroups.com, mattias
Mattias,

Since only parameters are involved in your expression to the right of "=", I
believe you would be satisfied with

... = max {k in SET2}
Param2[i,k] * (if Param1[i,j,k] = p then 1 else 0);

Or you could express the same thing more concisely as

... = max {k in SET2} (if Param1[i,j,k] = p then Param2[i,k] else 0);

or even

... = max (0, max {k in SET2: Param1[i,j,k] = p} Param2[i,k]);

Bob Fourer
4...@ampl.com


> -----Original Message-----
> From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of
> mattias
> Sent: Friday, January 25, 2008 5:59 AM
> To: AMPL Modeling Language

Paul

unread,
Jan 26, 2008, 2:10:38 PM1/26/08
to AMPL Modeling Language
Mattias,

On Jan 25, 6:58 am, mattias <mattiasl...@hotmail.co.uk> wrote:
>
> The constrained value of Var1 is the max of Param2, where Param1 =
> p.
>
> The 2nd statement should have been:
> subject to {( i,j,p) in SET1}:
> Var1( i,j,p) = max{ k in SET2} Param2[ i,k] * (Param1[i,j,k] = p);
>
> I need to explain the last part of the constraint above. What I would
> like is for AMPL to return 1 if Param1[i,j,k] = p, othewise return 0.
> Param2 is then multipled by either 1 or 0, when evaluating the max.
>

As Bob indicated, you can use an if-then construct to accomplish the
equivalent of the 0-1 logical expression, or you can include
Param1[i,j,k] = p as a qualifier inside the indexing expression of the
max operator.

However, there is a more fundamental issue here. What you wrote above
does not exactly make sense (constraining a variable to be a
constant), and there are three possible ways I could reinterpret it:

1. Var1 is declared param, not var. In this case, the two options
Bob provided would work (and should be equally valid), but you would
not put either in a "subject to" statement. You use "subject to" to
introduce constraints on variables, not definitions or conditions on
parameters. Put another way (possibly not too precisely), "subject
to" is meant to be enforced during the solution process, with the
presumption that something in the constraint changes when different
solutions are tried. If Param1, Param2 and Var1 are all parameters,
nothing in the "constraint" above varies with different candidate
solutions, so it's not a constraint. You can use Bob's answer to
assign values to Var1 outside the constraints of the model.

2. Var1 is declared var (it's a variable) and you are trying to
assign initial values to Var1 (which may then change during the
solution process). Again, in this case the assignment should not be
done in constraints (which would restrain Var1 from changing value
during the solution process); it should be done outside the
constraints.

3. Var1 is declared var and you want to express an if-then type
constraint in which the antecedent (and possibly the consequent)
change with different candidate solutions. In this case, using a
constraint ("subject to") is appropriate, but either Param1, Param2 or
SET2 would have to vary during the solution. If either Param1 or
Param2 varies, it's a variable and not a parameter (must be declared
var). If SET2 varies, this becomes much more difficult to model.

/Paul

mattias

unread,
Jan 28, 2008, 6:11:26 PM1/28/08
to AMPL Modeling Language
Paul,

many thanks for the detailed reply. I agree with your 1st
interpretation where Var1 should in fact be a parameter. I have used
Bob's = max {k in SET2}
Param2[i,k] * (if Param1[i,j,k] = p then 1 else 0);
to define the paramater, and it is works fine.

Thank you and Bob for your help. It is most appreciated.

I have however run into another difficulty...
The parameter, now called Param1 instead of Var1, is used in a
constraint as follows:

subject to {(i,j,p) in SET1 : Param1[i,j,p] > 0 }:
Var1[i,j,p] < Var2[i, Param1[i,j,p]] ;

SET1 has 3 indices i,j,p Param1 is the parameter per the recent query
Var1 is a decision variable in SET1

SET2 has 2 indices i,k Var2 is a binary decision variable in SET2

Note: 'i' is the same in SET1 and SET2

The following error messages are returned by AMPL/CPLEX
(1) Caution: Treating strict inequality constraint as a logical
constraint.
(2) logical constraint _slcon[1] is not an indicator constraint

Thank you again for all your help
Mattias

mattias

unread,
Jan 28, 2008, 6:14:25 PM1/28/08
to AMPL Modeling Language
Bob,
many thanks for your fer helpful reply. I have used
= max {k in SET2}
Param2[i,k] * (if Param1[i,j,k] = p then 1 else 0);
to define a paramater (instead of a variable), and it is works fine.

I have however encountered another difficulty when using the
parameter, which I have set out in my reply to Paul.

Thanks again
Mattias
> > > /Paul- Hide quoted text -

Paul

unread,
Jan 28, 2008, 11:03:52 PM1/28/08
to AMPL Modeling Language


On Jan 28, 6:11 pm, mattias <mattiasl...@hotmail.co.uk> wrote:
>
> I have however run into another difficulty...
> The parameter, now called Param1 instead of Var1, is used in a
> constraint as follows:
>
> subject to {(i,j,p) in SET1 : Param1[i,j,p] > 0 }:
> Var1[i,j,p] < Var2[i, Param1[i,j,p]] ;
>
> SET1 has 3 indices i,j,p Param1 is the parameter per the recent query
> Var1 is a decision variable in SET1
>
> SET2 has 2 indices i,k Var2 is a binary decision variable in SET2

I suspect that you mean Var1 is indexed by SET1 and Var2 is indexed by
SET2, rather than Var1 and Var2 being in (taking values from) the
respective sets.
>
> Note: 'i' is the same in SET1 and SET2
>
> The following error messages are returned by AMPL/CPLEX
> (1) Caution: Treating strict inequality constraint as a logical
> constraint.
> (2) logical constraint _slcon[1] is not an indicator constraint
>

Mathematical programming models need to be defined with weak, not
strong, inequality constraints. So you can either say Var1[i,j,p] <=
whatever or define a strictly positive parameter (call it epsilon) and
say Var1[i,j,p] <= whatever - epsilon. If Var1 and Var2 are
restricted to integer variables, you can use 1 for epsilon (i.e., Var1
< x equates to Var1 <= x - 1 if Var1 and x are both integer). Did you
mean to write Var1 <= Var2?

I'm not sure if the second warning means that AMPL thinks you are
indexing a variable with a variable (i.e., that Param1 is actually a
variable), or if it reflects a problem with the definition of one of
the model entities, or if it is fallout from the strict inequality.
Without seeing all the declarations, I can't guess why it shows up.

/Paul

Robert Fourer

unread,
Jan 29, 2008, 2:44:53 PM1/29/08
to am...@googlegroups.com, mattias, Paul

> > The following error messages are returned by AMPL/CPLEX
> > (1) Caution: Treating strict inequality constraint as a logical
> > constraint.
> > (2) logical constraint _slcon[1] is not an indicator constraint

The second message is indeed a fallout from the first. The first message says
that AMPL is treating the strict inequality constraint as a "logical
constraint" in the output that it sends to CPLEX. But CPLEX cannot handle
logical constraints unless they are of a special kind known as "indicator"
constraints. The second message says that this particular constraint is NOT an
indicator constraint, implying that CPLEX can't handle it. (An indicator
constraint has the form "BinaryVar = 0 implies LinearConstraint" or "BinaryVar
= 1 implies LinearConstraint".)

The fix, as Paul indicates, is to use >= or <= and not > or < in constraint
expressions.

Bob Fourer
4...@ampl.com


> -----Original Message-----
> From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Paul
> Sent: Monday, January 28, 2008 10:04 PM
> To: AMPL Modeling Language

mattias

unread,
Jan 30, 2008, 8:49:34 AM1/30/08
to AMPL Modeling Language
Paul and Bob,

many thanks for all your help. I have now used <= and the errors do
not now appear.

Kind regards
Mattias

leonardo.p...@gmail.com

unread,
Jun 16, 2013, 6:37:50 PM6/16/13
to am...@googlegroups.com, matti...@hotmail.co.uk
And if I need the strict inequality < and "<=" is not good for me?

Robert Fourer

unread,
Jun 17, 2013, 6:01:55 PM6/17/13
to am...@googlegroups.com

I am having some trouble understanding your description of the constraint, but it seems to me you may be looking for the following:

 

subject to SetVar1 {(i,j,p) in SET1}:

   Var1[i,j,p] = max {k in SET2: Param1[i,j,k] = p} Param2[i,k] * p;

 

The indexing expression following "max" means: "all k in SET2 such that Param1[i,j,k] equals p".

 

Bob Fourer

am...@googlegroups.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of leonardo.p...@gmail.com
Sent: Sunday, June 16, 2013 5:38 PM
To: am...@googlegroups.com
Cc: matti...@hotmail.co.uk
Subject: [AMPL 7173] Re: Formulating a constraint

 

I am trying to formulate a constraint to give the highest value for

Var1 where: . . .

Marcelo Freitas da Silva

unread,
Jul 4, 2018, 10:57:24 AM7/4/18
to AMPL Modeling Language
Hi Guys, I have  one problem.

I am using a binary variable for power lines. (0: switch off,1: switch on).
After this, I am summing all variables e comparing to a max lines that can be ON, for example, 177.

I have got the same problem.
subject to Max_Lines_Closed {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN}(Zk[k,i,j])=170; 

Error: amplin, line 67 (offset 2307):
processing commands.
Error: Caution: Treating strict inequality constraint as a logical constraint.
Error: context:  sum{(k,i,j) in  >>> DLIN}(Zk[k,i,j])<170; <<< 

how can i resolve this?

AMPL Google Group

unread,
Jul 5, 2018, 12:44:08 PM7/5/18
to Ampl Modeling Language
You have written the constraint as


subject to Max_Lines_Closed {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN}(Zk[k,i,j])=170;


but the error message indicates you have the strict inequality constraints. The optimization is not well-defined for the strict inequality, so change it to the <=.

Thanks,

--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:614352797-13044#}
--
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.



Marcelo Freitas da Silva

unread,
Jul 5, 2018, 1:52:59 PM7/5/18
to am...@googlegroups.com
qEven with this modification, nothing happened.

I had set up as follows:
subject to Max_Lines_Closed3 {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN} Zc[k,i,j] <=171;
the problem still persists... total cost and sum of binary variables in "1" do not change if I try to change the value of constraint.


Setting $presolve_inteps < 2.18e-07 or >= 5.19e-06
could change presolve results.
CPLEX 12.7.0.0: timelimit=300
threads=4
CPLEX 12.7.0.0: QP Hessian is not positive semi-definite.
0 MIP simplex iterations
0 branch-and-bound nodes
No basis.



To unsubscribe from this group and stop receiving emails from it, send an email to ampl+unsubscribe@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.



--
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+unsubscribe@googlegroups.com.

AMPL Google Group

unread,
Jul 5, 2018, 2:08:29 PM7/5/18
to Ampl Modeling Language
I ran your model at my end and I got the infeasible error. I have suggested the solution at https://groups.google.com/forum/#!topic/ampl/StCsTE79H_o.


Thanks,

--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:614352797-13044#}
On Thu, Jul 5, 2018 at 5:53 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
qEven with this modification, nothing happened.

I had set up as follows:


subject to Max_Lines_Closed3 {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN} Zc[k,i,j] <=171;


the problem still persists... total cost and sum of binary variables in "1" do not change if I try to change the value of constraint.








Setting $presolve_inteps < 2.18e-07 or >= 5.19e-06
could change presolve results.
CPLEX 12.7.0.0: timelimit=300
threads=4
CPLEX 12.7.0.0: QP Hessian is not positive semi-definite.
0 MIP simplex iterations
0 branch-and-bound nodes
No basis.



On Thu, Jul 5, 2018 at 4:43 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You have written the constraint as

subject to Max_Lines_Closed {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN}(Zk[k,i,j])=170;


but the error message indicates you have the strict inequality constraints. The optimization is not well-defined for the strict inequality, so change it to the <=.

Thanks,

--
Dr. Paras Tiwari
am...@googlegroups.com


On Wed, Jul 4, 2018 at 2:57 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Hi Guys, I have one problem.

I am using a binary variable for power lines. (0: switch off,1: switch on).
After this, I am summing all variables e comparing to a max lines that can be ON, for example, 177.

I have got the same problem.
subject to Max_Lines_Closed {(k,i,j) in DLIN}:
sum{(k,i,j) in DLIN}(Zk[k,i,j])=170;

Error: amplin, line 67 (offset 2307):
processing commands.
Error: Caution: Treating strict inequality constraint as a logical constraint.
Error: context: sum{(k,i,j) in >>> DLIN}(Zk[k,i,j])<170; <<<

how can i resolve this?
--
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.

Marcelo Freitas da Silva

unread,
Jul 5, 2018, 2:30:27 PM7/5/18
to am...@googlegroups.com
Actually this model is for working in NEOS Server CPLEX (attaching file by file).
Please, try again in https://neos-server.org/neos/solvers/lp:CPLEX/AMPL.html

Attaching again with correct version.

Thank you so much!

To unsubscribe from this group and stop receiving emails from it, send an email to ampl+unsubscribe@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.



--
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+unsubscribe@googlegroups.com.
neos-cplex.dat
neos-cplex.mod
neos-cplex.run
Reply all
Reply to author
Forward
0 new messages