Skip to main content

Get all solutions

Answered

Comments

4 comments

  • Official comment
    Matthias Miltenberger
    Gurobi Staff 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

  • 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 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

Please sign in to leave a comment.