value the variable
回答済みHello All!
I have a MIP model in gurobi and I want to value the variables.
in other words I have a binary variable called x(r,i,j). it is 1 when vehicle r goes from i to j.
I want to value this variable like this in loop and compare the objectives:
for j in N:
x[(1,0,j)].ub=1
x[(1,0,j)].lb=1
it is important to me to know what is difference when vehicle 1 after node 0 go to node 1 or 2 or 3 or ...
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
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ł0 -
Actually I do not want to print variable value, I want to give them value by myself and then compare each solution.
0 -
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 constraint
x = model.addVar(vtype=GRB.BINARY)
model.addConstr(x==1)
# fix via bounds
x.lb = 1
x.ub = 1
# [...]
# optimize model with fixed values to get new solution and objective value
model.optimize()Best regards,
Jaromił0 -
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.
0 -
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 = 1You can find more information about model and variable attributes in the attributes documentation.
Best regards,
Jaromił0 -
Thank a lot. It works.
0 -
Sorry, I have another question.
how can I bring the objective value in a dictionary or table with their variable?
0 -
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)])0 -
Thanks again.
0
投稿コメントは受け付けていません。
コメント
10件のコメント