Ejemplo n.º 1
0
def main():
    model, variables = puzzle_constraints()

    # Set up the solver
    solver = cp_model.CpSolver()
    solution_printer = cp_model.VarArraySolutionPrinter(
        [item for item in variables])

    # Solve the puzzle
    status = solver.SearchForAllSolutions(model, solution_printer)
    print()
    print('Statistics')
    print('  - status          : %s' % solver.StatusName(status))
    print('  - conflicts       : %i' % solver.NumConflicts())
    print('  - branches        : %i' % solver.NumBranches())
    print('  - wall time       : %f s' % solver.WallTime())
    print('  - solutions found : %i' % solution_printer.solution_count())
Ejemplo n.º 2
0
def BooleanProductSampleSat():
    """Encoding of the product of two Boolean variables.

  p == x * y, which is the same as p <=> x and y
  """
    model = cp_model.CpModel()
    x = model.NewBoolVar('x')
    y = model.NewBoolVar('y')
    p = model.NewBoolVar('p')

    # x and y implies p, rewrite as not(x and y) or p
    model.AddBoolOr([x.Not(), y.Not(), p])

    # p implies x and y, expanded into two implication
    model.AddImplication(p, x)
    model.AddImplication(p, y)

    # Create a solver and solve.
    solver = cp_model.CpSolver()
    solution_printer = cp_model.VarArraySolutionPrinter([x, y, p])
    solver.SearchForAllSolutions(model, solution_printer)