Is there workaround solution to remove one hard constraint from model

63 views
Skip to first unread message

YINXIONG CHEN

unread,
Nov 13, 2025, 3:16:53 AM11/13/25
to or-tools-discuss
Dear team,

I use ortools cpmodel to build cp-sat model successfully. In solver, there are multiple steps to optimize different objective functions. 
Here is the problem, I want to remove one hard constraint in the 2nd step. But to keep the solution from step1 and pass it to step2, I cannot simply rebuild the model. Is there a work around solution to remove that constraint in the 2nd step, and continue solve the solver?

Thanks.

Louie Hext

unread,
Feb 7, 2026, 2:29:46 AM (4 days ago) Feb 7
to or-tools-discuss
You can use a Boolean variable that represents if the constraint is active or not. When defining your constraint use the 'OnlyEnforceIf()' method. This way the constraint  is only applies when your Boolean variable is true. 

Simply set the variable to true or false as needed for each step.
Message has been deleted

Alberto Manzini

unread,
7:09 AM (16 hours ago) 7:09 AM
to or-tools-discuss
You can delete a constraint from the list of constraints in the model

You can list them with model.Proto().constraints

Note that the list is ordered based on the order you added the constraints to the model, so I would suggest you to log somehow the index of the constraints every time you add them
Note also that if you use Interval variables and not one of the last version of ortools, some "hidden" constraints are added to the model, so that each time you use AddIntervalVar, two constraints are added in the list

Once you have the list of constraints, you can manipulate them popping one or more of them from the list and then solve the model
Reply all
Reply to author
Forward
0 new messages