I have written a constraint programming model using Google OR Tools in Python, which uses CP solver. I need to run that model multiple times, and in each run I modify constraints. Currently, I create the model object from scratch, every time I want to run the model. Is there anyway, I can modify variables/constraints of an existing model, so that I don't need to build the model from scratch everytime?
To give better context, please consider the below sample model.
from ortools.sat.python import cp_model
model = cp_model.CpModel()
num_vals = 3
a = model.NewIntVar(0, num_vals -1, 'a')
b = model.NewIntVar(0, num_vals -1, 'b')
c = model.NewIntVar(0, num_vals -1, 'c')
model.Add(a == b)
solver = cp_model.CpSolver()
solver.Solve(model)
Now, in the 2nd run of the problem, I want to do following changes.
- Change the upper bound of variable c to
5
- Delete the constraint
a==b
- Create a new constraint
a==c
How can this be achieved without building the model from scratch?