Why we can't Modify value for variables?

340 views
Skip to first unread message

Cheng-Lung Chen

unread,
Apr 19, 2017, 12:53:24 AM4/19/17
to Gurobi Optimization
Hi Gurobi developers

I found it is very weird that we can't modify the solution of variables. 

I am using Python + Gurobi. Suppose I have a MIP problem and I would like to try some rounding heuristic

After I solve LP relaxation (also there is no convenient way to solve LP relaxation, I have to write a loop to collect binary variables and set them to continuous variables)

I would like to either round up or round down those binary variables with fractional values, then fix their values throw them back to the model and solve it again. 

Since this is not possible to use var.setAttr('X', 0 or 1), what should I do?

And why we can't modify values of variables? AMPL can do that. 

I just start to learn Python and Gurobi not long ago, I find this maybe a serious limitation?

Thank you. 

Sonja Mars

unread,
Apr 19, 2017, 3:36:32 AM4/19/17
to gur...@googlegroups.com
Hi,

> I would like to either round up or round down those binary variables with fractional values, then fix their values throw them back to the model and solve it again.
>
> Since this is not possible to use var.setAttr('X', 0 or 1), what should I do?


The X attribute of a variable is indeed not modifiable as this is always the result of one of our algorithms.

However, if you want to modify variables, for example if you want to fix them to specific values, you can use the UB and LB attributes for setting the lower and the upper bound of a variable. If you set both to the same value then the variables will be fixed to a specific value.

Please also see here:
http://www.gurobi.com/documentation/7.0/refman/lb.html#attr:LB
http://www.gurobi.com/documentation/7.0/refman/ub.html#attr:UB

> After I solve LP relaxation (also there is no convenient way to solve LP relaxation, I have to write a loop to collect binary variables and set them to continuous variables)
You might want to take a look at this routine: http://www.gurobi.com/documentation/7.0/refman/py_model_relax.html
I am not sure, but it might exactly do, what you are looking for.

Best regards,
Sonja


----
Dr. Sonja Mars
Gurobi Optimization - Technical Support





Cheng-Lung Chen

unread,
Apr 20, 2017, 2:09:15 AM4/20/17
to Gurobi Optimization
Hi Sonja

Thank you for the reply. 

Actually when I try to set UB and LB to 0 for a set of variables, it doesn't work. In my MIP model, first of all I collect variables with attribute VType == GRB.BINARY, then set then to GRB.CONTINUOUS with LB =0 and UB=1 in order to solve LP relaxation. 

However, some variables keep giving me very small negative value (-2.5231141231e-17, something like this), and then cause my the other optimization problem infeasible. But I am pretty sure the formulation will be feasible anyway. That's why I think why I can't just modify the value by using round up or round down to force it to be zero. 

I am aware that I can use model.relax() to get the LP relaxation objective value, however I can't retrieve solution value. For example, let's say I have a model called m, normally I will just do m.optimize() and then retrieve the value of a variable called "setup[(k,t)]" by using setup[(k,t)].X or setup[(k,t)].getAttr('X'). But when I use the relax method, suppose 

n = m.relax()
n.optimize()
then I know n.objval is able to be queried, but what about retrieving value of setup[(k,t)]? Gurobi will tell me something like variables have not been added to model n yet. 

Thank you so much for answering. 


Sonja Mars於 2017年4月19日星期三 UTC-4上午3時36分32秒寫道:

Sonja Mars

unread,
Apr 20, 2017, 2:27:56 AM4/20/17
to gur...@googlegroups.com
Hi,

However, some variables keep giving me very small negative value (-2.5231141231e-17, something like this), and then cause my the other optimization problem infeasible. But I am pretty sure the formulation will be feasible anyway. That's why I think why I can't just modify the value by using round up or round down to force it to be zero. 
Please note, MIP solvers like Gurobi Optimizer solve integer programs via a series of linear programming relaxations.  A solution is deemed integer if all integer variables are within a tolerance value (IntFeasTol) of an integer solution.  The default value of IntFeasTol is 1e-5, so a value like 1.000000465 would be considered an integer solution.  Gurobi Optimizer does not provide a rounding feature because in many models, rounding to an exact integer value can create problems, such as making the solution infeasible.


I am aware that I can use model.relax() to get the LP relaxation objective value, however I can't retrieve solution value. For example, let's say I have a model called m, normally I will just do m.optimize() and then retrieve the value of a variable called "setup[(k,t)]" by using setup[(k,t)].X or setup[(k,t)].getAttr('X'). But when I use the relax method, suppose 

n = m.relax()
n.optimize()
then I know n.objval is able to be queried, but what about retrieving value of setup[(k,t)]? Gurobi will tell me something like variables have not been added to model n yet. 

n is a different model and your variables object setup[(k,t)] belong to the old model m. You will have to ask n for its variables, so for example by calling 
n_vars=n.getVars()

Then you can do something with the variables stored in n_vars. 
Reply all
Reply to author
Forward
0 new messages