Solve Gurobi MIP model multiple times

1,873 views
Skip to first unread message

Vusal Babashov

unread,
Oct 9, 2017, 11:59:41 AM10/9/17
to Gurobi Optimization
Hello,

How can you solve MIP model repeatedly (e.g., 30 times) and change some of the input parameters at each iteration?  

Appreciate your help in advance.

Vusal

Tobias Achterberg

unread,
Oct 10, 2017, 6:40:48 AM10/10/17
to gur...@googlegroups.com
You construct and solve the model as usual, then you modify the model (bounds,
rhs values, and objective function coefficients are set via attributes) and
solve the model again.

But note that for a MIP there is not that much warm start information that one
can use. Gurobi will keep the incumbent solution and check whether this is still
feasible in the modified model, but apart from this, most of the solving process
has to start from scratch.

This is unlike LP solving, where the solving process can continue from the
previously optimal basis.

Best regards,

Tobias

Vusal Babashov

unread,
Oct 10, 2017, 8:27:10 AM10/10/17
to Gurobi Optimization
To be more specific, what should the for loop include? Redefinition of variables and constraints at each iteration?

Michael Winkler

unread,
Oct 10, 2017, 9:38:14 AM10/10/17
to gur...@googlegroups.com, Vusal Babashov

Hi Vusul,


you do not need to redefine variables or constraints, you can just use the model and change what you need on your model like constraints, variables, bounds, or coefficients... So after the optimization is done you can call reset() to throw away any solution information, see http://www.gurobi.com/documentation/7.5/refman/py_model_reset.html.

To change the mdoel take a look at http://www.gurobi.com/documentation/7.5/refman/py_model.html and http://www.gurobi.com/documentation/7.5/refman/attributes.html.


Best,
Michael
--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Espinoza

unread,
Oct 10, 2017, 10:38:13 AM10/10/17
to Gurobi Optimization
Message has been deleted
Message has been deleted

Vusal Babashov

unread,
Oct 12, 2017, 9:05:44 AM10/12/17
to Gurobi Optimization

The change I want to make is the parameter which is on the RHS of my constraints defined by addConstrs(). The name of the bulk constraints is name= "up1".

I am trying to get the constraint by name and then change the D[arrDay] parameter on the RHS through setAttr, but it doesn't work. 

c = model.getConstrByName("up1")

c.setAttr(GRB.Attr.rhs, D[arrday])

Daniel Espinoza

unread,
Oct 13, 2017, 8:21:56 AM10/13/17
to Gurobi Optimization
Hi,

Can you show what kind of error are you getting?

GurobiUser

unread,
Jun 10, 2018, 7:34:15 AM6/10/18
to Gurobi Optimization
Hi Daniel,

I am running a similar case repeatedly (i.e., 24) and now I want to update one of my constraint during each iteration. 

The procedure I follow is this:

1. Create the model and solve it with m.optimize()

2. Get the constraint I want to update as above: c = model.getConstrByName("name")

3. Set the rhs attribute which I want to change as: c.setAttr(GRB.Attr.rhs, new expression...)

4. Then I solve it again with m.optimize() but I get the error: "Encountered an attribute error" . This error comes after the 3 step: c.setAttr(GRB.Attr.rhs, new expression...)

Is this the right procedure to follow for updating a constraint each iteration? And why I get the error?

Thanks for any reply and help,

Tobias Achterberg

unread,
Jun 11, 2018, 2:24:16 AM6/11/18
to gur...@googlegroups.com
What do you mean by "new expression"? The right hand side of a constraint is just a single
floating point number.

In Python, and the other object oriented APIs, you can define constraints in a very
flexible way like

lhs-expression <= rhs-expression.

But internally, this is then converted into standard form

lhs-expression <= rhs

with rhs being just a floating number. All variables are on the left hand side, all
constants are aggregated into the right hand side.

The RHS attribute only deals with this single floating point value.

If you want to modify the coefficients of the variables in a constraint, you can use the
chgCoeff() method. Alternatively, you could just delete the constraint and add back the
new version of the constraint.


Best regards,

Tobias


Am 2018-06-10 um 13:04 schrieb GurobiUser:
> Hi Daniel,
>
> I am running a similar case repeatedly (i.e., 24) and now I want to update one of my
> constraint during each iteration.
>
> The procedure I follow is this:
>
> 1. Create the model and solve it with m.optimize()
>
> 2. Get the constraint I want to update as above: *c = model.getConstrByName("name")*
>
> 3. Set the rhs attribute which I want to change as: *c.setAttr(GRB.Attr.rhs, new
> expression...)*
>
> 4. Then I solve it again with m.optimize() but I get the error: "Encountered an attribute
> error" . This error comes after the 3 step: *c.setAttr(GRB.Attr.rhs, new expression...)*
>
> Is this the right procedure to follow for updating a constraint each iteration? And why I
> get the error?
>
> Thanks for any reply and help,
>
> On Friday, 13 October 2017 13:21:56 UTC+1, Daniel Espinoza wrote:
>
> Hi,
>
> Can you show what kind of error are you getting?
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Gurobi
> Optimization" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> gurobi+un...@googlegroups.com <mailto:gurobi+un...@googlegroups.com>.

GurobiUser

unread,
Jun 13, 2018, 12:16:39 PM6/13/18
to Gurobi Optimization
Hi Tobias,

Thanks a lot for your answer. Actually, in my problem I was referring to the RHS as a variable and was trying to update it but as you say this is not the right way to do it.

Thank you very much for your help!
Reply all
Reply to author
Forward
0 new messages