Skip to main content

How to find all solutions to a mixed integer programming problem?

Answered

Comments

5 comments

  • Simranjit Kaur
    • Gurobi Staff Gurobi Staff

    Hi Aleksandrs,

    You can use Gurobi's Solution Pool feature. Specifically, check out the usage of the parameter PoolSearchMode=2.

    You may find our KB article How do I find additional solutions to a model? helpful too.

    Best regards,
    Simran

    1
  • Aleksandrs Smilgins
    • Gurobi-versary
    • Conversationalist
    • Curious

    Hi Simranjit,

    Thank you very much for the answer. I am new to gurobi, and even after reading the manual I cannot figure out where I should write PoolSearchMode=2 . Could you please help.

     

    In my Python code I have the following lines:

    my_model = gp.Model(...

    my_model.addVars(...

    my_model.setObjective(...

    my_model.addConstrs(...

    my_model.setParam('TimeLimit', 60)

    my_model.optimize()

     

    and then in order to see the solution on the screen I have:

    for v in my_model.getVars():
        print(v.varName, v.x)

     

    So, my question is where I should write PoolSearchMode=2 ? And also, if there are several solutions, how can I choose to print solution1, solution2, etc. ? 

     

    Thank you in advance!

     

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    PoolSearchMode is a parameter. You can set it as follows:

    my_model.Params.PoolSearchMode = 2

    See the Python Parameter Examples section of the documentation for more information. Be sure to also set the PoolSolutions parameter to the number of solutions you want to find.

    The number of stored solutions is given by the SolCount model attribute. You can retrieve multiple solutions and their objective values by using the SolutionNumber parameter, Xn variable attribute, and PoolObjVal model attribute. For example:

    for i in range(my_model.SolCount):
      my_model.Params.SolutionNumber = i
        sol = my_model.getAttr("Xn")
        objval = my_model.PoolObjVal
    1
  • Aleksandrs Smilgins
    • Gurobi-versary
    • Conversationalist
    • Curious

    Thank you, it works!

    However, after testing this, it looks like it finds 10 best solutions. I am only interested in those solutions that maximize the objective value. That is, suppose I find the optimal solution. Then I am only interested in seeing other solutions if the give me exactly the same value of the objective function. Right now I am using your code to get 10 solutions, and then I check manually when a solution has a lower value of the objective function and I stop there. But is there an easier way such that it gives me all solutions automatically? I have read some articles on the website, and it looks like PoolObjBound could be useful. However, before running the code I do not know what the value should be. What should I write in Python such that pooled solutions only include solutions with the maximal possible value of the objective function? Thank you!

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    You can set the PoolSolutions parameter to the number of solutions you want to find. If you're only interested in optimal solutions, you can set the PoolGap or PoolGapAbs parameter to a very small value.

    0

Please sign in to leave a comment.