how to manage unused binary values during solve?
AnsweredI use Python API and GUROBI. I have a model consisting of some binary decision variables (in MVar manner) and constraints implemented using matrix multiplication operator.
I had earlier implemented my model with YALMIP using gurobi but I saw the variables that are not being used in model had 'NaN' values in the solution. In MATLAB, values for a binvar (binary variable type used for model in YALMIP) is 0, 1, NaN.
How could I set a default initial value of 0 or NaN for all binary variables?
-
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?. -
Hi I used this code
pG = m.addMVar(shape = p_Ng * p_Ns, vtype = GRB.BINARY)
for i in range(0, p_Ng * p_Ns):
pG[i].Start = GRB.UNDEFINED
# or pG[i].Start = 0 did not workbut it says 'No start values specified in MIP start' . After solve, some pG variables which did not affected by the constarints is still set to 1.
----------------------
I would like to say my problem in another way
A = np.array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]])
m = gp.Model('mat_mul_model_test')
some_var = m.addVar(vtype = GRB.INTEGER)
x = m.addMVar(5, vtype = GRB.BINARY)
m.addConstr(A @ x == 0)
m.setObjective(some_var, GRB.MINIMIZE)
m.optimize()
x_values = x.X
print(x_values)it prints
[-0. -0. -0. -0. -0.]
and when I set to 1 using
m.addConstr(A @ x == 1)
it prints
[1. 1. 1. 1. 1.]
But, I expect to have this output
[1 1 0 0 0]
or something that i can see it is not used with NaN values like this one:
[1 1 NaN NaN NaN]
How do I define this behavior for unused variables using Python API attributes?
0 -
Hi Amir,
Unused variables are set by Gurobi to any feasible value, in this case either 0 or 1. Returning NaN is not correct because NaN is not a feasible value for a binary variable. I am no expert with YALMIP but it sounds strange to set the values of unused variables to NaN in the optimal solution because the optimal solution is then actually not a feasible one.
If you want to fix the value of unused variables, you can add equality constraints setting these to 0 (or any other feasible value). Alternatively, you can add the unused variables to your objective function with the correct sign to force the unused variable to take the 0 value.
Best regards,
Jaromił0
Post is closed for comments.
Comments
4 comments