CpSolverSolutionCallback and num_workers

262 views
Skip to first unread message

alireza...@gmail.com

unread,
May 7, 2024, 1:45:04 PM5/7/24
to or-tools-discuss
Hi Laurent, 
I have a simple CP model (/python3.10/dist-packages (9.9.3963)) as follows ( I need to find all solutions):
X1 + 2X2 + 3X3 =6 

it works fine but when I specify the number of num_workers
it does not work anymore 
Could you please advise ? 
Thanks in advance 
Alireza 

from ortools.sat.python import cp_model
class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
def __init__(self, variables):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variables = variables
self.__solution_count = 0
def on_solution_callback(self):
self.__solution_count += 1
print(f"---------Solution {self.__solution_count} ------------------")
for i,v in self.__variables.items():
print(i, self.Value(v) )
def solution_count(self):
return self.__solution_count


def main() -> None:
model = cp_model.CpModel()
N = 3
coef = [ 1,2,3]
x = {i:model.NewIntVar(0,3, f"x_{i}") for i in range(N)}
model.Add( sum( coef[i]*x[i] for i in range(N) ) == 6)
solver = cp_model.CpSolver()
#solver.parameters.num_workers = 4
solver.parameters.enumerate_all_solutions = True
solution_printer = VarArraySolutionPrinter(x)
status = solver.solve(model, solution_printer)
model.ExportToFile('simplemodel.txt')
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
print(f"Maximum of objective function: {solver.objective_value}\n")
else:
print("No solution found.")
if __name__ == "__main__":
main()
simplemodel.txt

Laurent Perron

unread,
May 7, 2024, 2:16:10 PM5/7/24
to or-tools-discuss
Enable the log or read the Doc.

Enumerating all solutions does not work in parallel. 

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/3d49ddf4-dc15-47be-8550-17436182596fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages