我正在尝试使用 Python 中的 or-tools 来解决具有多个最佳解决方案的混合整数线性程序。然而,NextSolution()
总是返回False
,所以我无法检索多个解决方案。我知道这个函数使用约束求解器工作,但我想使用 MILP 求解器。
相关的或工具文档 https://google.github.io/or-tools/cpp_linear/classoperations__research_1_1MPSolver.html#a18daa488abaa904f23c8f74158290883 states:
截至 2020 年 2 月 10 日,只有 Gurobi 和 SCIP 支持 NextSolution(),有关如何为多个解决方案配置这些求解器的示例,请参阅 Linear_solver_interfaces_test。其他求解器无条件返回 false。
但是,我在源代码库、文档或通过网络搜索中找不到任何此类 Linear_solver_interfaces_test 。我正在使用 ortools 版本 7.8.7959 和附带的 SCIP 7.0.1 和 Python 3.6.9。
下面是我的示例代码,它说明了我想要解决的类型的简单示例。它应该产生三个独特的解决方案,但目前产生零个解决方案。
from ortools.linear_solver import pywraplp
def main():
solver = pywraplp.Solver("multiple_solution_test", pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
x = solver.IntVar(0, 2, "x")
y = solver.IntVar(0, 2, "y")
z = solver.IntVar(0, 2, "z")
solver.Add(x + y <= 2)
solver.Maximize(x + y + z) # should be 4, which can be obtained by (2,0,2), (1,1,2), or (0,2,2)
solver.Solve()
print_solutions(solver, x, y, z)
def print_solutions(solver, x, y, z):
count = 0
while solver.NextSolution(): # <-- NextSolution() always returns False!
count += 1
print("x =", x.solution_value(), "y =", y.solution_value(), "z =", z.solution_value())
print("\nNumber of solutions found:", count)
if __name__ == "__main__":
main()