I am trying to output the objective value from my pyomo model. I did access to the variable values but I cannot get access to the objective function value. My codes are:
instance = model.create_instance(data)
opt = SolverFactory('cplex')
results = opt.solve(instance)
instance.solutions.store_to(results)
results.write()
# instance.display()
# output the solution
var_val = []
for v in instance.component_data_objects(Var):
var_val.append(int(v.value))
obj_val = value(instance.obj)
And the last line gives error info:
obj_val = value(instance.obj)
NameError: name 'value' is not defined
But I can clearly see the value from result.write()
:
Message: None
Objective:
obj:
Value: 104728.80233047833
Variable:
x[0,1]:
Value: 1569
x[1,0]:
Value: 1569
x[1,1]:
Value: 206
x[2,2]:
Value: 230
x[2,3]:
Value: 213
x[3,2]:
Value: 213
How are you importing Pyomo? If you're using from pyomo.environ import *
the value function will be included. If you're importing each thing you're using individually then you just need to make sure you import the value
function: from pyomo.environ import value