Retrieving multiple solutions using Pyomo
回答済みHi!
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
-
正式なコメント
This post is more than two 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 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
投稿コメントは受け付けていません。
コメント
2件のコメント