メインコンテンツへスキップ

Retrieving multiple solutions using Pyomo

回答済み

コメント

2件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Lennart Lahrs
    • Gurobi Staff

    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,
    Lennart

    0

投稿コメントは受け付けていません。