Removing a constraint in CP-SAT

594 views
Skip to first unread message

Emmanuel Seve

unread,
Feb 15, 2022, 4:35:54 AM2/15/22
to or-tools-discuss
Hi all,

Is there a command to remove some constraints after the running the solver and re-run the same solver ?

Or the only possibility is to create a new solver ?

Emmanuel


Laurent Perron

unread,
Feb 15, 2022, 4:45:18 AM2/15/22
to or-tools-discuss
get the underlying protobuf from the constraint object and call Clear()/clear() on it.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
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/7a764055-de03-4874-b4b8-a2179a7a6686n%40googlegroups.com.

christoph...@ksb.com

unread,
Feb 15, 2022, 4:45:46 AM2/15/22
to or-tools-discuss
For certain types of constraints, you can add a clause to the constraint with the "OnlyEnforceIf" function and provide a parameter that is false, or can be made false with another constraint you add, to deactivate the first constraint. This does not work for all types of contraints, however; the AllowedAssigments and ForbiddenAssignments are among those for which it does not work.

Emmanuel Seve

unread,
Feb 15, 2022, 5:19:23 AM2/15/22
to or-tools-discuss
Thanks Laurent and Christoph

I think the first solution suit more to my case.

I have seen this example :

# 2. delete a == b # 
assuming that you did a_eq_b = model.Add(a == b) a_eq_b.Proto().Clear()

It is what you mean Laurent ?

a_eq_b should be declared somehow , right ? how ?

what to write a the beginning of the declaration of the constraint ?

"constraint_type"  a_eq_b = model.Add(a == b);

constraint_type = ?

Laurent Perron

unread,
Feb 15, 2022, 5:24:05 AM2/15/22
to or-tools-discuss

Emmanuel Seve

unread,
Feb 15, 2022, 5:44:39 AM2/15/22
to or-tools-discuss
Thanks .

I understood that the type is Constraint. So i wrote something like this:

Constraint  a_eq_b = model.Add(a == b); // (In Java)

Which sounds logical for me.

But i get the compilation error message :

cannot find symbol [ERROR]   symbol:   class Constraint

Something missing ?

Laurent Perron

unread,
Feb 15, 2022, 5:50:31 AM2/15/22
to or-tools-discuss
using/import ?

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


Emmanuel Seve

unread,
Feb 15, 2022, 7:35:23 AM2/15/22
to or-tools-discuss
Indeed, the compilation error disappear if i add :

import com.google.ortools.sat.Constraint;

Emmanuel Seve

unread,
Feb 15, 2022, 7:58:50 AM2/15/22
to or-tools-discuss
But after when i tried to remove a constraint, the name of the constraint is not known

i tried :

Elim2.Proto().Clear();
model.Elim2.Proto().Clear();
model.Constraint.Elim2.Proto().Clear();
model.constraint.Elim2.Proto().Clear();

nothing works

Emmanuel Seve

unread,
Feb 15, 2022, 8:00:16 AM2/15/22
to or-tools-discuss
Elim2 is the naùme of my constraint

Constraint Elim2 = model.addLessThan(LinearExpr.scalProd(FT_elim2,CoeffFT2),N_U);

Laurent Perron

unread,
Feb 15, 2022, 8:01:58 AM2/15/22
to or-tools-discuss
what does it mean the name is not known?

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


Emmanuel Seve

unread,
Feb 15, 2022, 8:05:47 AM2/15/22
to or-tools-discuss
cannot find symbol
[ERROR]   symbol:   variable Elim2
[ERROR]   location: variable model of type com.google.ortools.sat.CpModel

Emmanuel Seve

unread,
Feb 15, 2022, 9:27:06 AM2/15/22
to or-tools-discuss
The variable Elim2 was not created in my program. 

But i still have a compilation errors even if the constraint Elim2 exists

Elim2.Proto().Clear(); => with or without capital letters : 
cannot find symbol [ERROR]   symbol:   method Proto()  (same with the method Clear if i remove .Proto()

Reply all
Reply to author
Forward
0 new messages