'Solving a sequence of cp_models

I need to solve a sequence of Google or-tools cp_model SAT problems, in which only one constraint of many changes on each step through the sequence. Building a new model for each case is extremely inefficient. Is there a way to vary a parameter in the model, like Parameter() in cvxpy? Otherwise, is there a way to remove constraint and add a new one in its place?



Solution 1:[1]

The underlying storage of the model is a protobuf:

The CpModel storage

You need to keep a reference to the Constraint objet. To clear a constraint, just access the underlying proto object (Proto() in python, getBuilder() in java for instance) and call Clear() or clear() on it.

You can overwrite a constraint in place. But you will need to understand the process to rewrite model object into constraint (look at the cp_model.cc, cp_model.py, CpModel.java, CpModel.cs files). The simpler is to append a new constraint using the standard model.AddXXX APIs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laurent Perron