Skip to main content

Retrieving multiple solutions using Pyomo

Answered

Comments

1 comment

  • 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

Please sign in to leave a comment.