简体   繁体   English

获得Optimal之后的次佳解决方案

[英]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. CBC号 CPLEX, Gurobi do support keeping more solutions, but this is available in OR-Tools only for Gurobi through the NextSolution() method. CPLEX,Gurobi确实支持保留更多解决方案,但这仅可通过NextSolution()方法在Gurobi的OR-Tools中使用。

If your model is purely integral, you can have a look at the CP-SAT solver. 如果您的模型是完全积分的,则可以查看CP-SAT求解器。

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. 因此,在添加附加约束之后,通过解决该问题将获得不同的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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