Set Expressions (with Chainable Format) for Arithmetic and Relational Operations #1107
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Set Expressions (with Chainable Format) for Arithmetic and Relational Operations
I've been working with
SetVar
inchoco-Solver
. Unlike other types of variables, such asRealVar
orIntVar
, there was no direct way to compose operations such as equals (eq), not equals (ne), contains, not contains (nc), subset (subSet), union, or intersection directly in the model definition. While propagators allow for constructing such constraints, it was not as intuitive as it is for other variable types likeIntVar
andRealVar
. These features are extremely useful in facilitating the composition of constraints.Thus, I've worked on adding this feature for
SetVar
, based on the operations already available for other variable types. For example:However, such expressions were not possible for
SetVar
:Improvement
I've implemented a new expression framework for
SetVar
, allowing the use of familiar operations directly on set variables. This change introduces support for the following operations:Relational Operations:
eq
(Equals):setA.eq(1, 2, 3)
orsetA.eq(setB)
.ne
(Not Equals):setA.ne(1, 2)
orsetA.ne(setB)
.contains
:setA.contains(1, 2)
.nc
(notContains):setA.notContains(1, 2)
.subSet
:setA.subSet(1, 2, 3)
orsetA.subSet(setB)
.Arithmetic Set Operations:
union
:setA.union(setB)
intersection
:setA.intersection(setB)
This enhancement allows for the expression of complex set relations with simplified syntax, enabling combinations such as:
Example Usage:
Internal Changes:
This change is implemented through three main components:
UnReSetExpression
): Handle relations between a set and a list of values, likesetVar.eq(1,2,3).post()
.BiReSetExpression
): Manage operations between twoSetVar
instances, such assetA.eq(setB).post()
.BiArSetExpression
): Handle arithmetic operations like unions and intersections, e.g.,setA.eq(setB.union(setC)).post()
.A new interface,
SetExpression
, inspired by other Choco-Solver expressions, allowsSetVarExpression
to be used in conjunction with these expression composition objects (UnReSetExpression
,BiReSetExpression
, andBiArSetExpression
).Backward Compatibility:
This improvement is fully backward-compatible with existing constraints and models. It adds considerable flexibility when working with
SetVar
, and I believe it will make modeling with sets much more intuitive.Future Improvements:
I believe that additional operations, such as disjoint, could also be implemented to further enhance set operations in Choco-Solver.
Finally, I've included a comprehensive suite of tests in
SetVarExpressionTest
, which I hope will be useful to validate this enhancement.I would greatly appreciate any feedback or suggestions for optimizations, as well as any edge cases that could further improve this approach!