Obtain list of SAT solutions from ortools

时光怂恿深爱的人放手 提交于 2019-12-11 14:55:40

问题


I'm trying to figure out how to get the complete list of possible solutions from the ortools.sat.python.cp_model. I understand I can print them, as in the example below, but it is not clear to me how to get the values, e.g. as a nested list or list of dicts. I tried writing my own callback class by modifying the VarArraySolutionPrinter and appending solutions to a list attribute, but for some reason this consistently caused the python kernel to crash. Anyway, there must be a more direct way. I don't consider parsing the printed output an option.

from ortools.sat.python import cp_model

model = cp_model.CpModel()

x00 = model.NewBoolVar('x00')
x01 = model.NewBoolVar('x01')
x02 = model.NewBoolVar('x02')

model.AddBoolOr([x00, x01, x02.Not()])
model.AddBoolOr([x00.Not(), x02.Not()])

# Create a solver and solve.
solver = cp_model.CpSolver()
solution_printer = cp_model.VarArraySolutionPrinter([x00, x01, x02])
solver.SearchForAllSolutions(model, solution_printer)

## Prints:
Solution 0, time = 0.00 s
  x00 = 0   x01 = 1   x02 = 0 
Solution 1, time = 0.01 s
  x00 = 0   x01 = 0   x02 = 0 
Solution 2, time = 0.01 s
  x00 = 0   x01 = 1   x02 = 1 
Solution 3, time = 0.01 s
  x00 = 1   x01 = 1   x02 = 0 
Solution 4, time = 0.01 s
  x00 = 1   x01 = 0   x02 = 0 

回答1:


Turns out writing a custom callback class works after all:

from ortools.sat.python import cp_model

model = cp_model.CpModel()

x00 = model.NewBoolVar('x00')
x01 = model.NewBoolVar('x01')
x02 = model.NewBoolVar('x02')

model.AddBoolOr([x00, x01, x02.Not()])
model.AddBoolOr([x00.Not(), x02.Not()])

class VarArraySolutionCollector(cp_model.CpSolverSolutionCallback):

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

    def on_solution_callback(self):
        self.solution_list.append([self.Value(v) for v in self.__variables])

# Create a solver and solve.
solver = cp_model.CpSolver()
solution_collector = VarArraySolutionCollector([x00, x01, x02])
solver.SearchForAllSolutions(model, solution_collector)

solution_collector.solution_list

Out[33]: [[0, 1, 0], [0, 0, 0], [0, 1, 1], [1, 1, 0], [1, 0, 0]]


来源:https://stackoverflow.com/questions/58934609/obtain-list-of-sat-solutions-from-ortools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!