Reuse model for multiple iterations and updating constants

209 views
Skip to first unread message

Sagarika Khandelwal

unread,
Mar 26, 2019, 3:27:24 AM3/26/19
to Gurobi Optimization
In my model , I have a constraint as below


#Constraint 5: the salary for all employees should be within the budget   
m.addConstr(  sum(Wi[(e)] * expert_costs.get(e) for e in Experts)    <= 270 )

I want to change the value from 270 to various other constant values and keeping all other constraints/variables of the model the same.

How re-use the model and save the loading time?

Silke Horn

unread,
Mar 26, 2019, 3:46:17 AM3/26/19
to gur...@googlegroups.com
Hi,

First you need to get a hold of that constraint. The easiest way would be to save it to a variable upon creation, e.g.:

c = m.addConstr( sum(Wi[(e)] * expert_costs.get(e) for e in Experts) <= 270 )

(You could also use the model’s getConstrByName method if you know the constraint’s name, but if possible I’d recommend to store it directly.)

Then you can simply set the RHS attribute to a new value, e.g.:

c.RHS = 300

- Silke
> --
>
> ---
> 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.

sagarika1493

unread,
Mar 26, 2019, 12:57:57 PM3/26/19
to gur...@googlegroups.com
Hi ,

Thanks for the prompt reply. It does work but in python, it again loads all the variables and the model in the memory, how do I make sure only this constraint is updated and not all others?

Thanks!
Sagarika

Silke Horn

unread,
Mar 27, 2019, 4:07:49 AM3/27/19
to noreply-spamdigest via Gurobi Optimization
Hi Sagarika,

I am not sure I understand what you are saying. What do you mean with “it again loads all the variables and the model in the memory”? And what exactly are you trying to achieve?

The approach I suggested would work as follows:

* build the model
* optimize it (and extract the solution) if you like
* change the model
* optimize again

Then Gurobi simply performs the changes without having to rebuild the entire model (and it might even warm-start and hence speed up the second optimization).

- Silke
Reply all
Reply to author
Forward
0 new messages