Retrieving multiple solutions using Pyomo
AnsweredHi!
I am using Pyomo and I want to retrieve the solutions from the SolutionPool that Gurobi found. I know how to do it with Gurobi's Python API, but I am struggling to do the same when using Pyomo.
Thanks,
Federico
0
-
Hi Frederico,
Please find below a suggestion of how to access objective values and the Xn attribute of Gurobi model variables for multiple solutions in Pyomo.
import pyomo.environ as pe
m = pe.ConcreteModel()
...
opt = pe.SolverFactory('gurobi_persistent')
opt.set_instance(m)
opt.set_gurobi_param("PoolSolutions", 2)
opt.set_gurobi_param("PoolSearchMode", 2)
opt.solve()
# Print number of solutions stored
nSolutions = opt.get_model_attr("SolCount")
print('Number of solutions found: ' + str(nSolutions))
# Print objective values of solutions
for e in range(nSolutions):
opt.set_gurobi_param("SolutionNumber", e)
print('%g ' % opt.get_model_attr("PoolObjVal"), end='')
print('')
# Print variable values of different solutions
opt.set_gurobi_param("SolutionNumber", 0)
opt._solver_model.printAttr('Xn')
opt.set_gurobi_param("SolutionNumber", 1)
opt._solver_model.printAttr('Xn')Did this do the trick for you?
Best regards,
Lennart0
Please sign in to leave a comment.
Comments
1 comment