how to output the decision variable values in gurobi?
AnsweredI have two decision variables in my model, but I do not know how to return the value of these decision variables seperately.
import pandas as pd
import gurobipy as gpy
string = '/Users/user/Desktop/ISE 402/distance_matrix.csv'
df = pd.read_csv(string,index_col=0) #input the distance matrix c_ij
#save the index and column of the distance matrix
index = df.index #Chicago school #行
column = df.columns #Chicago libraries #列
#create the model
model = gpy.Model(name='8.12(a)')
x = model.addVars(column,vtype=gpy.GRB.BINARY,name='x') #=1 if we build the library
y = model.addVars(index, vtype=gpy.GRB.BINARY,name='y') #=1 if the school is covered
model.update()
#set the objective function
model.setObjective(gpy.quicksum(y[i] for i in index),sense=gpy.GRB.MAXIMIZE)
#add the constraints
model.addConstr(gpy.quicksum(x[j] for j in column)<=12)
model.addConstrs(y[i]<=gpy.quicksum(df.at[i,j]*x[j] for j in column)for i in index)
#optimization
model.optimize()
print("Obj: ",model.objVal)
0
-
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?. -
You can simply look at the X attribute of your variables. Here is an example of how to do this in Python. Before accessing the X attribute, please make sure to check if a solution is available. If you are trying to query the solution values, but there is no solution this will result in an error.
Best,
Sonja
0
Post is closed for comments.
Comments
2 comments