logical constraint

118 views
Skip to first unread message

Hossein Haghighat

unread,
Feb 20, 2022, 2:21:15 PM2/20/22
to AMPL Modeling Language
hello,
I am solving an MILP model in AMPL. I have the logical constraint below:

if    | X - Y |  > 0.01  ====>  binary variable Z = 1;

where X,Y, are continuous variables and Z is a binary variable. 

I was wondering if this constraint can be represented in my model while keeping it MILP. many thanks in advance. 

AMPL Google Group

unread,
Feb 21, 2022, 10:59:06 AM2/21/22
to AMPL Modeling Language
You can write this condition equivalently as

Z = 0 ==> | X - Y | <= 0.01

which is equivalent to

Z = 0 ==> -0.01 <= X - Y <= 0.01

Constraints of this form are recognized by CPLEX, Gurobi, and Xpress (search "indicator constraint" in this forum for more information). For other solvers, this constraint can be linearized as

-0.01 - M * Z <= X - Y <= 0.001 + M * Z

where M is some upper limit on | X - Y |.


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}

Hossein Haghighat

unread,
Feb 27, 2022, 12:13:03 PM2/27/22
to AMPL Modeling Language
many thanks for the answer. 
I was wondering if AMPL-Gurobi supports the suffix  ".unbdd" when a model is unbounded. If not, is there any workaround for this issue? 

AMPL Google Group

unread,
Feb 28, 2022, 11:48:12 AM2/28/22
to AMPL Modeling Language
For unbounded problems that have linear objectives and constraints, and continuous variables, Gurobi uses the .unbdd suffix to return a direction (ray) of unboundedness.

Gurobi does not support the .unbdd suffix for other kinds of problems. However, for unbounded problems that have linear objectives and constraints, but some integer (including binary) variables, it may be useful to know a direction of unboundedness for the continuous relaxation (which is also unbounded). You can get that by setting "option relax_integrality 1;" before solving.

Note that when there are constraints that are not linear, like indicator constraints, it is not possible to get .unbdd values from Gurobi.

(In testing this, I noticed that "variable.unbdd returned" sometimes appears in the Gurobi messages even when the .unbdd suffix is not created or returned. This issue has been reported.)


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}
On Sun, Feb 27, 2022 at 5:13 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
many thanks for the answer.
I was wondering if AMPL-Gurobi supports the suffix ".unbdd" when a model is unbounded. If not, is there any workaround for this issue?

On Mon, Feb 21, 2022 at 3:58 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You can write this condition equivalently as

Z = 0 ==> | X - Y | <= 0.01

which is equivalent to

Z = 0 ==> -0.01 <= X - Y <= 0.01

Constraints of this form are recognized by CPLEX, Gurobi, and Xpress (search "indicator constraint" in this forum for more information). For other solvers, this constraint can be linearized as

-0.01 - M * Z <= X - Y <= 0.001 + M * Z

where M is some upper limit on | X - Y |.


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

Hossein Haghighat

unread,
Feb 28, 2022, 11:07:11 PM2/28/22
to am...@googlegroups.com
many thanks for the detailed description. 
In my problem, there is only a bi-linear term in the objective, and no integer variables. what is the workaround to get the direction of unboundedness? What about using CPLEX?

Mailtrack Sender notified by
Mailtrack
03/01/22, 06:12:49 AM

--
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/NT4yMEz0gsw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-1793392955-5301161312-1646066892-1246598970%40helpscout.net.

Hossein Haghighat

unread,
Mar 1, 2022, 11:02:45 AM3/1/22
to am...@googlegroups.com
hello,
I have attached my model. I implement a Benders-like algorithm, where :
1- subproblems SP1 & SP2 have bilinear terms only in the objective and return the extreme points.
2- subproblem SP3 which computes the extreme ray (using "var.unbdd") is Linear

here is the problem : 
AMPL does not allow getting the "var.unbdd" from SP3 (line 172 of file *.run) although it is a linear model because SP1 and SP3 are not linear in the loop.  

Is this an AMPL issue or solvers? 
 


--
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/NT4yMEz0gsw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-1793392955-5301161312-1646066892-1246598970%40helpscout.net.
UC_ddu_ToU.rar

AMPL Google Group

unread,
Mar 1, 2022, 5:00:06 PM3/1/22
to AMPL Modeling Language
The problem here is that you are referencing the solver-defined suffix .unbdd inside a loop that includes your solve statements. In that case, to prevent the "Bad suffix .unbdd" error, you must explicitly define the .unbdd suffix in your script, by adding the statement

suffix unbdd OUT;

before the loop. (The cause of this problem is that AMPL does an initial analysis of all the statements in the loop, before it starts executing the loop. But during AMPL's initial analysis of the loop, .unbdd has not yet been created by the solver.)


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}
On Tue, Mar 1, 2022 at 4:02 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
hello,

I have attached my model. I implement a Benders-like algorithm, where :
1- subproblems SP1 & SP2 have bilinear terms only in the objective and return the extreme points.
2- subproblem SP3 which computes the extreme ray (using "var.unbdd") is Linear.

here is the problem :
AMPL does not allow getting the "var.unbdd" from SP3 (line 172 of file *.run) although it is a linear model because SP1 and SP3 are not linear in the loop.

Is this an AMPL issue or solvers?

On Tue, Mar 1, 2022 at 4:07 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
many thanks for the detailed description.
In my problem, there is only a bi-linear term in the objective, and no integer variables. what is the workaround to get the direction of unboundedness? What about using CPLEX?

Mailtrack
Sender notified by
Mailtrack
03/01/22, 06:12:49 AM


On Mon, Feb 28, 2022 at 4:47 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
For unbounded problems that have linear objectives and constraints, and continuous variables, Gurobi uses the .unbdd suffix to return a direction (ray) of unboundedness.

Gurobi does not support the .unbdd suffix for other kinds of problems. However, for unbounded problems that have linear objectives and constraints, but some integer (including binary) variables, it may be useful to know a direction of unboundedness for the continuous relaxation (which is also unbounded). You can get that by setting "option relax_integrality 1;" before solving.

Note that when there are constraints that are not linear, like indicator constraints, it is not possible to get .unbdd values from Gurobi.

(In testing this, I noticed that "variable.unbdd returned" sometimes appears in the Gurobi messages even when the .unbdd suffix is not created or returned. This issue has been reported.)


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

Hossein Haghighat

unread,
Mar 2, 2022, 9:27:15 AM3/2/22
to am...@googlegroups.com
many thanks for your help and answer.

Mailtrack Sender notified by
Mailtrack
03/02/22, 08:21:26 AM

--
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/NT4yMEz0gsw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.

Hossein Haghighat

unread,
Mar 13, 2022, 9:58:04 AM3/13/22
to AMPL Modeling Language
hello,
when solving a bi-linear mip model with Gurobi (by setting "nonconvex =2"), sometimes the solution time gets very long. Is there any control, to terminate the solver (in a loop with repeated solve statements) from proving optimality and continuing the solution process with the best bound found? 

AMPL Google Group

unread,
Mar 13, 2022, 9:33:55 PM3/13/22
to AMPL Modeling Language
When you have a long run with nonconvex=2, Gurobi is doing a branch-and-bound search similar to what it does for problems that have integer variables. Thus the following settings (listed under Gurobi Options for AMPL) will stop Gurobi and return the best solution found, as soon as a certain condition is satisfied:

mipgap r -- when the relative gap between lower and upper bounds is <= r
mipgapabs r -- when the absolute gap between lower and upper bounds is <= r
nodelim n -- when the number of search nodes generated equals n
solnlimit n -- when the number of solutions found equals n
timelim n -- when the elapsed time is n seconds


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}
On Sun, Mar 13, 2022 at 1:58 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
hello,

when solving a bi-linear mip model with Gurobi (by setting "nonconvex =2"), sometimes the solution time gets very long. Is there any control, to terminate the solver (in a loop with repeated solve statements) from proving optimality and continuing the solution process with the best bound found?
On Wed, Mar 2, 2022 at 2:27 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
many thanks for your help and answer.

Mailtrack
Sender notified by
Mailtrack
03/02/22, 08:21:26 AM


On Tue, Mar 1, 2022 at 9:59 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
The problem here is that you are referencing the solver-defined suffix .unbdd inside a loop that includes your solve statements. In that case, to prevent the "Bad suffix .unbdd" error, you must explicitly define the .unbdd suffix in your script, by adding the statement

suffix unbdd OUT;

before the loop. (The cause of this problem is that AMPL does an initial analysis of all the statements in the loop, before it starts executing the loop. But during AMPL's initial analysis of the loop, .unbdd has not yet been created by the solver.)


--
Robert Fourer
am...@googlegroups.com
Message has been deleted

AMPL Google Group

unread,
Mar 22, 2022, 12:01:47 PM3/22/22
to AMPL Modeling Language
I do not see this error when I run "UC_var_ToU_ddu.run" on my computer. The output, in the attached file, does not show any error messages. Here are some questions to follow up:
  • When the error appears for a particular run file, does it appear every time that you execute that run file? Or for the same file, does it sometimes appear, and sometimes not?
  • Which operating system (Windows, Mac, or LInux) are you using?
  • What is the output of the command "ampl -v" on your computer?


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}
On Sun, Mar 20, 2022 at 1:51 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
hello,
my model works well but sometimes it gives the following error, which seems to be a bug in the AMPL software:

UC_var_ToU_ddu.run, line 266 (offset 74443454):
missing "
context: >>> ********* "> UC_var_ToU.stt; <<<

UC_var_ToU_ddu.run, line 267 (offset 74443499):
Bug: Bad ncmd = 71 in havestmt()
context: printf "\n" > >>> UC_var_ToU.stt; <<<
sw:

I have attached my model. could you please advise me on how to resolve it?


Mailtrack
Sender notified by
Mailtrack
03/20/22, 09:02:39 AM


On Mon, Mar 14, 2022 at 1:33 AM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
When you have a long run with nonconvex=2, Gurobi is doing a branch-and-bound search similar to what it does for problems that have integer variables. Thus the following settings (listed under Gurobi Options for AMPL) will stop Gurobi and return the best solution found, as soon as a certain condition is satisfied:

mipgap r -- when the relative gap between lower and upper bounds is <= r
mipgapabs r -- when the absolute gap between lower and upper bounds is <= r
nodelim n -- when the number of search nodes generated equals n
solnlimit n -- when the number of solutions found equals n
timelim n -- when the elapsed time is n seconds


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

Hossein Haghighat

unread,
Mar 22, 2022, 11:37:49 PM3/22/22
to am...@googlegroups.com
thank you for following up. here are the answers questions:
- this eros appears only sometimes, not always. 
- I use Windows 7 os. 
- the output of command "ampl -vv" is :  "AMPL Version 20220219 (MS VC++ 10.0, 64-bit), .. ,  Maintenance expires with version 20220531."

Mailtrack Sender notified by
Mailtrack
03/23/22, 06:46:35 AM

--
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/NT4yMEz0gsw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.

AMPL Google Group

unread,
Mar 24, 2022, 1:22:00 PM3/24/22
to AMPL Modeling Language
When AMPL gives an error like this,


UC_var_ToU_ddu.run, line 266 (offset 74443454):
missing "
context: >>> ********* "> UC_var_ToU.stt; <<<


it normally means that a " character was accidentally left out when writing or updating the run file. Thus I would expect the error to happen every time that you use that run file, and I would expect the error to go away as soon as the file is fixed or replaced.

Thus I recommend the following test: The next time that you see this error, run exactly the same file again, and see whether the error appears again. Then try the same run several more times, all without changing the file. If you always get the error in this situation, then send the files and there is a good chance that we can reproduce the error on our systems and get it fixed. (Until we can reproduce an error, there is not much hope of fixing it.)

Also, instead of

printf "%s \n", " *********** Output ********** " > UC_var_ToU.stt;

you can use

printf " *********** Output ********** \n" > UC_var_ToU.stt;

and similarly in other places in the run file. This form might be less prone to errors.


--
Robert Fourer
am...@googlegroups.com
{#HS:1793392955-108731#}
On Wed, Mar 23, 2022 at 3:37 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
thank you for following up. here are the answers questions:
- this eros appears only sometimes, not always.
- I use Windows 7 os.
- the output of command "ampl -vv" is : "AMPL Version 20220219 (MS VC++ 10.0, 64-bit), .. , Maintenance expires with version 20220531."

Mailtrack
Sender notified by
Mailtrack
03/23/22, 06:46:35 AM


On Tue, Mar 22, 2022 at 4:01 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I do not see this error when I run "UC_var_ToU_ddu.run" on my computer. The output, in the attached file, does not show any error messages. Here are some questions to follow up:
  • When the error appears for a particular run file, does it appear every time that you execute that run file? Or for the same file, does it sometimes appear, and sometimes not?
  • Which operating system (Windows, Mac, or LInux) are you using?
  • What is the output of the command "ampl -v" on your computer?


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

Hossein Haghighat

unread,
Mar 25, 2022, 1:16:04 AM3/25/22
to am...@googlegroups.com
thank you very much Dr. Fourer. I will as you recommended. I appreciate it.  

Mailtrack Sender notified by
Mailtrack
03/25/22, 06:10:39 AM

--
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/NT4yMEz0gsw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages