Using the Python interface to the OR-Tools CP-CAT solver (reference), I would like to be able to save a cp_model, load it at a later time or from a different process, and continue interacting with it.
I'm able to serialize a model into a Protubuf, and then load and solve it:
from google.protobuf import text_format
from ortools.sat.python import cp_model
def create_model():
model = cp_model.CpModel()
a = model.NewIntVar(0, 10, "var_a")
b = model.NewIntVar(0, 10, "var_b")
model.Maximize(a + b)
return model
def clone_model(model):
new_model = cp_model.CpModel()
text_format.Parse(str(model), new_model.Proto())
return new_model
def solve_model(model):
solver = cp_model.CpSolver()
status = solver.Solve(new_model)
print(solver.StatusName(status))
print(solver.ObjectiveValue())
# Works fine
model = create_model()
new_model = clone_model(model)
solve_model(new_model)
However, I would like to keep interacting with the model after loading it. For example, I want to be able to do something like:
model = create_model()
new_model = clone_model(model)
c = new_model.NewIntVar(0, 5, "var_c")
new_model.Add(a < c)
The problem is that this last line does not work because a is not defined; and I could not find any way to access the existing model's variables.
I am looking for something like: a = new_model.getExistingVariable("var_a") which will allow me to keep interacting with preexisting variables in the model after loading it.