简体   繁体   中英

Getting the next best solutions after Optimal

I have a simple solver which I am using to solve a knapsack-like problem. I am looking to maximize a value while keeping constraints in mind

    self.solver = pywraplp.Solver(
                'FD',
                pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING
            )
        self.objective = self.solver.Objective()
        self.objective.SetMaximization()
self.solver.solve()

I left out the code to define variables, but my question is: Running this code will get me the optimal lineup. Is there a way to find the 2nd, 3rd, etc. best solution?

With CBC no. CPLEX, Gurobi do support keeping more solutions, but this is available in OR-Tools only for Gurobi through the NextSolution() method.

If your model is purely integral, you can have a look at the CP-SAT solver.

The trick is the unless you explore all solutions, the second best solution is heuristic at best.

In a knapsack like problem it is straight forward to obtain the next best solution in an iterative procedure.

After the problem was solved for the first time, you can add a constraint where the left hand side sums over all items included in the optimal solution and the right hand side limits this sum to one less than the number of items included in the optimal solution.

This is essentially a cut which excludes the first optimal solution from the solution space. Thus, a different solution will be obtained by solving the problem after adding the additional constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM