How to modify variables and constraints of an existing CP-model

1,397 views
Skip to first unread message

Devarshi Raval

unread,
Jul 1, 2021, 9:15:03 AM7/1/21
to or-tools-discuss
Hey!

I have written a CP model in Python, which uses CP-SAT 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 every time?

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.

  1. Change the upper bound of variable c to 5
  2. Delete the constraint a==b
  3. Create a new constraint a==c

How can this be achieved without building the model from scratch?

Xiang Chen

unread,
Jul 1, 2021, 9:22:16 AM7/1/21
to or-tools...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/893ca21b-1fc4-4be3-98bb-687d7eda7d5fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages