How do I set subset of variables to constant value or disable optimization
AnsweredI am trying to find solution for binary variable `x_{i,j}`, where `i =1,2,...,n` and `j=1,2,... m`.
x is defined as:
x = model.addVars(n,m, vtype=GRB.BINARY)
However, I want to disable the optimization on a subset of `x_{i,j}`, i.e., set them to constant 0. In this way, the search space will be much smaller.
I read the document and didn't find anything. Is there a good way to do this? Alternatively, I can redefine the variable. Say if n=10, m=10, the size for `x_{i,j}` will be 100. If I want to set 70 of them to constant 0 and disable the optimization of them, I can define `y_k` with the size of 30, and only do optimization on y. And I will need to define another map to record the mapping relations between x and y. In this way, the search space is only 30.
I wonder if there is a more elegant way to lock some of the variables?
Thank you!

Hi!
This can be efficiently done in two ways:
1. Add a constraint and set a variable to zero:
model.addLConstr(x[n,m] == 0)
By the way, you can sum all the variables you want to be fixed to zero on the lefthand side instead of having variablemany constraints.
2. Set the bounds on variables you wish to "lock":
x[n,m].lb = 0
x[n,m].ub = 0This needs to be done for each variable individually.
Hope this helps.
Best regards
Jonasz0 
Hi,
Thank you!
I wonder by the design of Gurobi, will these two methods help accelerate the optimization or reduce the search space?
Danning
0 
This question is better suited to Gurobi staff.
From my own experience, the variables which may only assume one value are removed in the presolve and hence reduce the search space. The more variables you "fix", the smaller the search space becomes and hence you can hope for a faster solution process.
0 
Jonasz is correct. Fixed variables are removed in the presolve step and thus reduce the search space, which in general often results in a faster optimization process.
Best regards,
Jaromił0 
Thank you Jonasz and Jaromił. This helps a lot.
Danning
0
Please sign in to leave a comment.
Comments
5 comments