problem with nonquadratic nonlinear constraints with Gurobi

69 views
Skip to first unread message

Vivi

unread,
Apr 20, 2020, 4:20:42 PM4/20/20
to AMPL Modeling Language
Hi Mister Robert,

I have those variables in my problem
  
I have had this statement from AMPL:Gurobi 9.0.0: Gurobi can't handle nonquadratic nonlinear constraints. I would like to ask you if this is because of the expression
 
because I deleted all nonlinear constraints in myproblem. I would also like to ask you if it is because of variable above, how can I write it so that gurobi can accept it.
Many thanks in advance for your reply and your help.
Best regards.

AMPL Google Group

unread,
Apr 21, 2020, 12:28:53 PM4/21/20
to AMPL Modeling Language
You might have some logical expression in your constraints, which Gurobi would reject as "nonlinear" since it is not a linear expression. For example, an "if-then" or "if-then-else" construct will be considered nonlinear if there are variables in the expression after "if". However, for a simple logical condition like the one that defines your variable e_ih^m, there is often a reformulation that Gurobi does accept.

To get more help, try attaching the file that has your model in it, and also your data file if that is practical.


--
Robert Fourer
am...@googlegroups.com
{#HS:1142498786-74413#}

Vivi

unread,
Apr 21, 2020, 4:37:57 PM4/21/20
to AMPL Modeling Language
Hi Mr Robert,

The statements I wrote in the variables description are the followings:

var TA_F1 {d in ND, t in F1[d], i in (ND union NS) } >= 0 ;
var TA_MRS_SAT {d in ND, t in F1[d], s in NS, c in NC} >= 0 ;
var TD_MRS_SAT {s in NS, c in NC} >= 0 ;
var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} = if TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] then 1 else 0;
var TAF1_TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TA_F1[d,t,s] <= TD_MRS_SAT[s,c] then 1 else 0;
var TAMSTAF1_TAF1TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1 then 1 else 0;

They contain the "if-then-else" expression as you have mentioned before, perhaps it because of this I have the notice of " nonquadratic nonlinear constraints" from Gurobi.
I would like to aks you how I can write those variables so that they can be accepted by Gurobi.
Thanks a lot for your support.
Best Regards.

AMPL Google Group

unread,
Apr 22, 2020, 3:13:29 PM4/22/20
to AMPL Modeling Language
To satisfy Gurobi, write these variable definitions as "indicator constraints" instead. For example, you have


var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} = 
   if TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] then 1 else 0;

For Gurobi, define a binary variable and a separate constraint:

var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} binary;
subject to TAMS_TAF1_define {d in ND, t in F1[d], s in NS, c in NC}:
   TAMS_TAF1[d,t,s,c] = 1 ==>
      TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] else
      TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s];

The ==> operator means "implies". For a constraint using ==> to be an indicator constraint, the expression to the left must be "binary-variable = 0" or "binary-variable=1". The above formulation allows TAMS_TAF1[d,t,s,c] to be either 1 or 0 when TA_MRS_SAT[d,t,s,c] = TA_F1[d,t,s]; if you don't want to permit that, you could try something like TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s] + 0.0001 in the "else" part. (It's not possible to enforce > on continuous variables in optimization problems.)

TAF1_TDMS would be handled similarly. In TAMSTAF1_TAF1TDMS, AMPL doesn't recognize the double-constraint TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1, but since TAMS_TAF1[d,t,s,c] and TAF1_TDMS[d,t,s,c] and both zero-one (binary) variables, you can write that equivalently as TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2, and then apply the same approach as for the previous constraints.


--
Robert Fourer
am...@googlegroups.com
{#HS:1142498786-74413#}
On Tue, Apr 21, 2020 at 8:38 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Mr Robert,

The statements I wrote in the variables description are the followings:

var TA_F1 {d in ND, t in F1[d], i in (ND union NS) } >= 0 ;
var TA_MRS_SAT {d in ND, t in F1[d], s in NS, c in NC} >= 0 ;
var TD_MRS_SAT {s in NS, c in NC} >= 0 ;
var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} = if TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] then 1 else 0;
var TAF1_TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TA_F1[d,t,s] <= TD_MRS_SAT[s,c] then 1 else 0;
var TAMSTAF1_TAF1TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1 then 1 else 0;

They contain the "if-then-else" expression as you have mentioned before, perhaps it because of this I have the notice of " nonquadratic nonlinear constraints" from Gurobi.
I would like to aks you how I can write those variables so that they can be accepted by Gurobi.
Thanks a lot for your support.
Best Regards.

On Tue, Apr 21, 2020 at 4:28 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You might have some logical expression in your constraints, which Gurobi would reject as "nonlinear" since it is not a linear expression. For example, an "if-then" or "if-then-else" construct will be considered nonlinear if there are variables in the expression after "if". However, for a simple logical condition like the one that defines your variable e_ih^m, there is often a reformulation that Gurobi does accept.

To get more help, try attaching the file that has your model in it, and also your data file if that is practical.


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

Vivi

unread,
Apr 22, 2020, 11:59:57 PM4/22/20
to AMPL Modeling Language
Hi Robert,

Thanks a lot. Your help is very precious for me.

Best Regards.

Vivi

unread,
Apr 24, 2020, 5:54:40 AM4/24/20
to AMPL Modeling Language
Hi Robert,

I write to you again because I have an another preoccupation.

I wrote this equation and I got syntax error statement:

subject to Contrainte_3 {d in ND, i in NS,  j in NS: i <> j, c in NC, m in NC: m<>c}:

   sum {t in F1[d]}( X_F1[d,t,i,j]+TRUCK_CUST[d,t,i,c]+TRUCK_CUST[d,t,j,m]) <= 2 ;


I would like to ask you how to write variables wtih are different twice time in the bracket in front of contrainte _3{  }.

Best regards.


AMPL Google Group

unread,
Apr 24, 2020, 1:07:48 PM4/24/20
to AMPL Modeling Language
Place all of the conditions at the end of the indexing expression, like this:

subject to Contrainte_3
{d in ND, i in NS, j in NS, c in NC, m in NC: i<>j and m<>c}: . . .


--
Robert Fourer
am...@googlegroups.com
{#HS:1142498786-74413#}
On Fri, Apr 24, 2020 at 9:54 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

I write to you again because I have an another preoccupation.

I wrote this equation and I got syntax error statement:

subject to Contrainte_3 {d in ND, i in NS, j in NS: i <> j, c in NC, m in NC: m<>c}:

sum {t in F1[d]}( X_F1[d,t,i,j]+TRUCK_CUST[d,t,i,c]+TRUCK_CUST[d,t,j,m]) <= 2 ;

I would like to ask you how to write variables wtih are different twice time in the bracket in front of contrainte _3{ }.

Best regards.

On Thu, Apr 23, 2020 at 4:00 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

Thanks a lot. Your help is very precious for me.

Best Regards.

On Thu, Apr 23, 2020 at 3:55 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

Many thanks for your help. It is very precious for me.

On Wed, Apr 22, 2020 at 7:13 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
To satisfy Gurobi, write these variable definitions as "indicator constraints" instead. For example, you have

var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} = 
   if TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] then 1 else 0;

For Gurobi, define a binary variable and a separate constraint:

var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} binary;
subject to TAMS_TAF1_define {d in ND, t in F1[d], s in NS, c in NC}:
   TAMS_TAF1[d,t,s,c] = 1 ==>
      TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] else
      TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s];

The ==> operator means "implies". For a constraint using ==> to be an indicator constraint, the expression to the left must be "binary-variable = 0" or "binary-variable=1". The above formulation allows TAMS_TAF1[d,t,s,c] to be either 1 or 0 when TA_MRS_SAT[d,t,s,c] = TA_F1[d,t,s]; if you don't want to permit that, you could try something like TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s] + 0.0001 in the "else" part. (It's not possible to enforce > on continuous variables in optimization problems.)

TAF1_TDMS would be handled similarly. In TAMSTAF1_TAF1TDMS, AMPL doesn't recognize the double-constraint TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1, but since TAMS_TAF1[d,t,s,c] and TAF1_TDMS[d,t,s,c] and both zero-one (binary) variables, you can write that equivalently as TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2, and then apply the same approach as for the previous constraints.


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

Vivi

unread,
Apr 24, 2020, 10:52:38 PM4/24/20
to AMPL Modeling Language
Thank you very much!

Vivi

unread,
Apr 27, 2020, 4:53:37 AM4/27/20
to AMPL Modeling Language
Hi Mr Robert,

I rewrote the variable that I wrote incorrectly (var TAMSTAF1_TAF1TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1 then 1 else 0 ) this way:

subject to TAMSTAF1_TAF1TDMS_define {d in ND, t in F1[d], s in NS, c in NC}:
TAMSTAF1_TAF1TDMS[d,t,s,c] = 1 ==>
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2 else
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2-0.0001;

I would like to ask you if this formulation is correct.

Best regards.

AMPL Google Group

unread,
Apr 27, 2020, 1:40:13 PM4/27/20
to AMPL Modeling Language
In the "else" part of your constraint, you have

TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] = 2 - 0.0001

But as I understand your problem, TAMS_TAF1[d,t,s,c] and TAF1_TDMS[d,t,s,c] are supposed to be variables that only take the value 0 or 1. Thus TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] can only be 0, 1, or 2 -- and it cannot be 2 - 0.0001.

So the "else" part will need to be fixed. You want to specify an inequality that lets TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] equal 0 or 1, but not 2.


--
Robert Fourer
am...@googlegroups.com
{#HS:1142498786-74413#}
On Mon, Apr 27, 2020 at 8:53 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Mr Robert,

I rewrote the variable that I wrote incorrectly (var TAMSTAF1_TAF1TDMS {d in ND, t in F1[d], s in NS, c in NC} = if TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1 then 1 else 0 ) this way:

subject to TAMSTAF1_TAF1TDMS_define {d in ND, t in F1[d], s in NS, c in NC}:
TAMSTAF1_TAF1TDMS[d,t,s,c] = 1 ==>
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2 else
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2-0.0001;

I would like to ask you if this formulation is correct.

Best regards.

On Sat, Apr 25, 2020 at 2:52 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thank you very much!

On Fri, Apr 24, 2020 at 5:07 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Place all of the conditions at the end of the indexing expression, like this:

subject to Contrainte_3
{d in ND, i in NS, j in NS, c in NC, m in NC: i<>j and m<>c}: . . .


--
Robert Fourer
am...@googlegroups.com
On Fri, Apr 24, 2020 at 9:54 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

I write to you again because I have an another preoccupation.

I wrote this equation and I got syntax error statement:

subject to Contrainte_3 {d in ND, i in NS, j in NS: i <> j, c in NC, m in NC: m<>c}:

sum {t in F1[d]}( X_F1[d,t,i,j]+TRUCK_CUST[d,t,i,c]+TRUCK_CUST[d,t,j,m]) <= 2 ;

I would like to ask you how to write variables wtih are different twice time in the bracket in front of contrainte _3{ }.

Best regards.

On Thu, Apr 23, 2020 at 4:00 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

Thanks a lot. Your help is very precious for me.

Best Regards.

On Thu, Apr 23, 2020 at 3:55 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Robert,

Many thanks for your help. It is very precious for me.

On Wed, Apr 22, 2020 at 7:13 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
To satisfy Gurobi, write these variable definitions as "indicator constraints" instead. For example, you have

var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} = 
   if TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] then 1 else 0;

For Gurobi, define a binary variable and a separate constraint:

var TAMS_TAF1 {d in ND, t in F1[d], s in NS, c in NC} binary;
subject to TAMS_TAF1_define {d in ND, t in F1[d], s in NS, c in NC}:
   TAMS_TAF1[d,t,s,c] = 1 ==>
      TA_MRS_SAT[d,t,s,c] <= TA_F1[d,t,s] else
      TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s];

The ==> operator means "implies". For a constraint using ==> to be an indicator constraint, the expression to the left must be "binary-variable = 0" or "binary-variable=1". The above formulation allows TAMS_TAF1[d,t,s,c] to be either 1 or 0 when TA_MRS_SAT[d,t,s,c] = TA_F1[d,t,s]; if you don't want to permit that, you could try something like TA_MRS_SAT[d,t,s,c] >= TA_F1[d,t,s] + 0.0001 in the "else" part. (It's not possible to enforce > on continuous variables in optimization problems.)

TAF1_TDMS would be handled similarly. In TAMSTAF1_TAF1TDMS, AMPL doesn't recognize the double-constraint TAMS_TAF1[d,t,s,c]=TAF1_TDMS[d,t,s,c]=1, but since TAMS_TAF1[d,t,s,c] and TAF1_TDMS[d,t,s,c] and both zero-one (binary) variables, you can write that equivalently as TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2, and then apply the same approach as for the previous constraints.


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

Vivi

unread,
Apr 27, 2020, 5:15:16 PM4/27/20
to AMPL Modeling Language
Hi Sir,

Thanks very much for reply.
But how can I write TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] to tell that it must equal 0 or 1?
Like this?

subject to TAMSTAF1_TAF1TDMS_define {d in ND, t in F1[d], s in NS, c in NC}:
TAMSTAF1_TAF1TDMS[d,t,s,c] = 1 ==>
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2 else
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=0 or 1;

AMPL Google Group

unread,
Apr 28, 2020, 11:15:36 AM4/28/20
to AMPL Modeling Language
Consider using a <= condition for the "else" part.


--
Robert Fourer
am...@googlegroups.com
{#HS:1142498786-74413#}
On Mon, Apr 27, 2020 at 9:15 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi Sir,

Thanks very much for reply.
But how can I write TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] to tell that it must equal 0 or 1?
Like this?

subject to TAMSTAF1_TAF1TDMS_define {d in ND, t in F1[d], s in NS, c in NC}:
TAMSTAF1_TAF1TDMS[d,t,s,c] = 1 ==>
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=2 else
TAMS_TAF1[d,t,s,c]+TAF1_TDMS[d,t,s,c]=0 or 1;

On Mon, Apr 27, 2020 at 5:39 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
In the "else" part of your constraint, you have

TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] = 2 - 0.0001

But as I understand your problem, TAMS_TAF1[d,t,s,c] and TAF1_TDMS[d,t,s,c] are supposed to be variables that only take the value 0 or 1. Thus TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] can only be 0, 1, or 2 -- and it cannot be 2 - 0.0001.

So the "else" part will need to be fixed. You want to specify an inequality that lets TAMS_TAF1[d,t,s,c] + TAF1_TDMS[d,t,s,c] equal 0 or 1, but not 2.


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

Vivi

unread,
Apr 28, 2020, 6:37:53 PM4/28/20
to AMPL Modeling Language
Thanks a lot for your help.
Reply all
Reply to author
Forward
0 new messages