etov
etov

Reputation: 3032

Or-Tools CP-SAT solver export/import: how to access vars after loading a model?

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)

(source)

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.

Upvotes: 4

Views: 2207

Answers (1)

etov
etov

Reputation: 3032

An approach that seems to be working, based on a comment by @Stradivari, is to simply pickle the model along with its variables.

For example:

from ortools.sat.python import cp_model
import pickle

class ClonableModel:
    def __init__(self):
        self.model = cp_model.CpModel()
        self.vars = {}
        
    def create_model(self):
        self.vars['a'] = self.model.NewIntVar(0, 10, "var_a")
        self.vars['b'] = self.model.NewIntVar(0, 10, "var_b")

        self.model.Maximize(self.vars['a'] + self.vars['b'])
        
    # Also possible to serialize via a file / over network 
    def clone(self):
        return pickle.loads(pickle.dumps(self))
    
    def solve(self):
        solver = cp_model.CpSolver()
        status = solver.Solve(self.model)

        return '%s: %i' % (solver.StatusName(status), solver.ObjectiveValue())

Now, the following works as expected:

model = ClonableModel()
model.create_model()

new_model = model.clone()
new_model.model.NewIntVar(0,5,"c")
new_model.model.Add(new_model.vars['a'] < c)

print('Original model: %s' % model.solve())
print('Cloned model: %s' % new_model.solve())

# Original model: OPTIMAL: 20
# Cloned model: OPTIMAL: 14

Upvotes: 4

Related Questions