Skip to main content

Get all solutions

Answered

Comments

5 comments

  • Official comment
    Matthias Miltenberger
    • Gurobi Staff

    Hi Maxim,

    Did you already check out this guide on SolutionPools?
    This post about how to save solutions directly to disk as they are found using the SolFiles parameter might also be interesting.

    Cheers,
    Matthias

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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 why not try our AI Gurobot?.
  • Maxim Santalov
    • Gurobi-versary
    • First Question
    • First Comment

    Thank you for your reply.

     

    I am new to Gurobi and some solutions presented at guide are not obvious for me.

    By this moment, my code looks like this:

    ....(Model initialization, setting up Vars and Cons)...

    m.setParam(GRB.Param.PoolSearchMode, 2)

    m.optimize()

    nSolutions = m.SolCount
    print('Number of solutions found: ' + str(nSolutions))
    for sol in range(nSolutions):
        m.setParam(GRB.Param.SolutionNumber, sol)
        print(m.ObjVal)
        for station_idx in range(0, len(station_in_route)):

            m.cbGetSolution(m.getVars())

           ### Here I need to request the current solution from the model

           # If variable in solution is  > 0:
                   # print(' Station %d' % station_idx, end='\n')
    print('')

    If you could help with editing part of the code marked as(###) I will be very glad.

    WIth kind regards,

    Maxim

     

    0
  • Matthias Miltenberger
    • Gurobi Staff

    Hi Maxim,

    To query the n'th solution value of a variable you need to query the Xn attribute of that variable after setting the SolutionNumber to the respective number n. The method cbGetSolution is only used to work with a solution while running inside a callback. You don't seem to be using callbacks in your code.

    Cheers,
    Matthias

    1
  • Maxim Santalov
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Matthias,

    Thank you for the reply. It helped to restore order in my code.

    I will leave it here( possibly, it would help someone):.

    ...(previous part remains the same)
    for
    sol in range(nSolutions):
    m.setParam(GRB.Param.SolutionNumber, sol)
    values = m.Xn
    ...I receive a list of values of the solution with specified SolNumber
    0

Post is closed for comments.