unrelax a model
AnsweredHi All,
I am using PythonGurobi.
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

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
Please sign in to leave a comment.
Comments
3 comments