【问题标题】:Specify the solution printed in OR-Tools指定在 OR-Tools 中打印的解决方案
【发布时间】:2020-10-21 10:47:25
【问题描述】:

当我得到下面的解决方案时,例如在轮班安排坐问题中,我如何指定我想用 SolveWithSolutionCallback 方法打印的解决方案?

Solution 13, time = 37.58 s, objective = 82
Solution 14, time = 37.71 s, objective = 81
Solution 15, time = 37.87 s, objective = 80
Solution 16, time = 37.96 s, objective = 76

假设我想看看解决方案 13 和 14 产生了什么,有什么办法吗?

【问题讨论】:

  • 不确定如何在方法中传递该列表,我尝试用列表替换printer_solution变量但没有用,但也许我做错了什么,您能详细说明一下吗?
  • 你好!请问您可以将这些惊人的细节编辑到您的帖子中吗?谢谢!

标签: python constraints scheduling constraint-programming or-tools


【解决方案1】:

您可以将解决方案存储在回调的列表中或将它们记录到文件等:

from ortools.sat.python import cp_model


class Callback(cp_model.CpSolverSolutionCallback):

    def __init__(self, variables):
        cp_model.CpSolverSolutionCallback.__init__(self)
        self.variables = variables
        self.solutions = []

    def on_solution_callback(self):
        self.solutions.append([self.Value(v) for v in self.variables])


if __name__ == "__main__":
    model = cp_model.CpModel()

    num_vals = 3
    x = model.NewIntVar(0, num_vals - 1, "x")
    y = model.NewIntVar(0, num_vals - 1, "y")
    z = model.NewIntVar(0, num_vals - 1, "z")

    model.Add(x != y)

    solver = cp_model.CpSolver()
    callback = Callback([x, y, z])
    solver.SearchForAllSolutions(model, callback)
    print(callback.solutions)

【讨论】:

  • 当您有最小化目标时,这是无法做到的。
  • 搜索所有解决方案只定义在可满足性问题上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 2019-10-13
  • 2019-08-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多