modelingor-toolsinteger-programmingoperations-researchcp-sat

Ortools setting constraints while solving


I am using CP-SAT solver from ortools https://developers.google.com/optimization/cp/cp_solver

I am executing the solver with a callback object

solver = cp_model.CpSolver()
solution_agg = SolutionCollector(data, self.variables, self.products, self.vehicles)
status = solver.SearchForAllSolutions(self.model, callback=solution_agg)

Solution agg is supposed to filter out all solutions that have some wrong assignments, I could not model these as linear inequalities.

What I know is that generated solutions can be converged faster and the "hits" on the verifier can be made less. If I can add constraints on-the-go inside the callback.

I tried to do this inside the callback, adding a constraint to look for solutions in lesser volume than minimum volume till now.

self.__model.Add(volume_expression <= min_found_yet)

This doesn't give error, but the number of times the verifier rejected a solution is still same.

Is it possible to form constraints while solving? If not in Ortools, then any other solver which provides?


Solution

  • Not directly. The solver is stateless and read the cp_model once at the start of the solve.

    What you are describing seems just a minimization property. Why don't you just minimize volume_expression?