Skip to content

Multi dimensional element constraint#926

Open
Dimosts wants to merge 42 commits into
masterfrom
Multi-Dim-Element
Open

Multi dimensional element constraint#926
Dimosts wants to merge 42 commits into
masterfrom
Multi-Dim-Element

Conversation

@Dimosts

@Dimosts Dimosts commented Apr 17, 2026

Copy link
Copy Markdown
Collaborator

Adding Multi-Dimensional Element constraint separately than 1D Element. The main reason is to allow us to safen the indices, as the way we have it now we lose information for the different indices.

@Dimosts Dimosts self-assigned this Apr 17, 2026

@IgnaceBleukx IgnaceBleukx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I have some (mostly minor) comments on the code, but the CI seems to be failing for PySAT, not sure why...

Other thing is the name, personally I like MultiDElement, what do you think?

Comment thread cpmpy/expressions/globalfunctions.py Outdated
Comment thread cpmpy/expressions/globalfunctions.py Outdated
Comment thread cpmpy/expressions/globalfunctions.py
Comment thread cpmpy/expressions/globalfunctions.py
Comment thread cpmpy/expressions/globalfunctions.py Outdated
Comment thread cpmpy/expressions/variables.py Outdated
Comment thread tests/test_globalconstraints.py
@Dimosts Dimosts linked an issue Apr 20, 2026 that may be closed by this pull request
@Dimosts

Dimosts commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator Author

Fixed the issue with pysat, it was a memory thing, just reduced the size of the array in the test and done. Also resolved the comments, improving based on the suggestions, and adding additional tests.

About the name, I do not have any strong preference, will change it to MultiDElement.

@Dimosts Dimosts requested a review from IgnaceBleukx April 20, 2026 13:29

@IgnaceBleukx IgnaceBleukx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Not sure if the helper function is really required?
Also, MultiD element is also supported in MiniZinc, right? So we should also add it to our interface.

I quickly checked for other solvers, they don't seem to support it.

Comment thread cpmpy/expressions/globalfunctions.py Outdated
Comment thread cpmpy/expressions/globalfunctions.py Outdated
@tias tias mentioned this pull request Apr 23, 2026
19 tasks
@tias

tias commented Apr 23, 2026

Copy link
Copy Markdown
Collaborator

NDElement, like we have NDVarArray?

(no real objection to MultiDElement though)

@Dimosts

Dimosts commented Apr 23, 2026

Copy link
Copy Markdown
Collaborator Author

No real objection or preference to the name on my side either.

On another note though, I did some experiments on using minizinc's multi dimensional element, or decomposing it ourselves.

And it seems that our decomposition is quite better. So, although minizinc does support it, I think we should not put it in its supporting list.

My experiments used the Quadratic Assignment Problem, using a 2D element constraint in defining the objective. The main parameter is n, the number of locations, which also defines that the array (to be accessed) is nxn. I experimented with n = 6...10.

Note that in the figure, the time is in log scale.

image

@IgnaceBleukx

Copy link
Copy Markdown
Collaborator

Interesting, is this with gecode or OR-Tools as backend? I would still prefer to not decompose it, as users may want to use a solver with minizinc that does support native 2d element constraints.

@IgnaceBleukx

Copy link
Copy Markdown
Collaborator

Renamed to NDElement, and merged current master into this one.
Should be ready for final review by @tias

@IgnaceBleukx IgnaceBleukx requested a review from tias May 19, 2026 14:08
@tias tias added this to the v0.20 milestone Jun 15, 2026

@tias tias left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some stricter typecheck to the constructor; well, an assert actually, and had to update a test.

our safen has a 'safen_toplevel' that multiple solvers have to fill in with 'element', 'div', mod'... should we also add nd_element there?
More generally, safening docs don't mention nd_element yet

we now have a decompose_linear for element, do we also need one for nd_element?

I also followed an agent recommendation to extend a test...

also small bugfix in choco's added objective safening

other than that its looking good so far...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2d-element constraint can have out-of-bounds indices

3 participants