unrelax a model
AnsweredHi All,
I am using Python-Gurobi.
I create an integer linear model, then I relax and solve it. Now, I would like to switch back the model to its integer form after solving its relaxation. Is that possible?
best,
Ghafour
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
When you relax a model in Python, a new model object is returned. So you should still have access to the original "unrelaxed" model. -G
0 -
Correct. But, I am adding new variables to the relaxed model in an iterative process, and from time to time I need to switch back the model to integer form and solve the model. Therefore, I need to switch back the relaxed model to the integer form, not the original one created at the begnining.
Any help is appreciated.
best,
Ghafour
0 -
The VType variable attribute gives the type of each variable. You can store the original types of all model variables in a new attribute, then switch all of the variables to continuous to build the relaxed model. Whenever you want to switch back to the integer form, revert all of the variable types to their original values using the new attribute.
For example:
# Store original variable types and relax model
orig_vars = model.getVars()
for v in orig_vars:
v._VType = v.VType
v.VType = GRB.CONTINUOUS
# Do something with relaxed model here
# Revert back to original variable types
for v in orig_vars:
v.VType = v._VType0
Post is closed for comments.
Comments
4 comments