• Gurobi Staff

Hi Niloofar,

You can access the value of an optimization variable after at least one feasible solution has been found via the X attribute. Example mip1.py shows how to print variable values after a successful optimization.

Best regards,
Jaromił

Actually I do not want to print variable value, I want to give them value by myself and then compare each solution.

• Gurobi Staff

If you want to fix a variable to a particular value you can either add an equality constraint fixing it to a constant value or setting a variable's lower and upper bound to the same value.

# fix via equality constraintx = model.addVar(vtype=GRB.BINARY)model.addConstr(x==1)# fix via boundsx.lb = 1x.ub = 1# [...]# optimize model with fixed values to get new solution and objective valuemodel.optimize()

Best regards,
Jaromił

Dear,

Actually I want to give value 1 to all of x variable but in order.

for example: at first x(1,0,2)=1 and get model objective, then x(1,0,3)=1 and get model objective by this criteria again.

this would be loop for value this variable.

please give me hand to get the solution.

• Gurobi Staff

You can use the code from my previous comment and put it in a $$\texttt{for}$$-loop to achieve what you need.

for j in J:  # fix variable to desired value  x[(1,0,j)].lb = 1  x[(1,0,j)].ub = 1  # optimize model  model.optimize()  # if at least one solution point has been found,   # set obj to objective value and infinity otherwise  if (model.SolCount > 0)    obj = model.ObjVal  else    obj = GRB.INFINITY  # do something with the objective value for this run  # [...]  # reset bound of the variable  x[(1,0,j)].lb = 0  x[(1,0,j)].ub = 1

Best regards,
Jaromił

Thank a lot. It works.

Sorry, I have another question.

how can I bring the objective value in a dictionary or table with their variable?

• Gurobi Staff

Note that this is no longer Gurobi related. You can use a standard Python dictionary for this

d = {}for j in J:  #[..]  d[(1,0,j)] = obj# access objective value for fixed variable (1,0,0) print(d[(1,0,0)])

Thanks again.