Skip to content

refactoring decompose in typed pattern#929

Merged
IgnaceBleukx merged 24 commits into
masterfrom
decomp_typed
May 27, 2026
Merged

refactoring decompose in typed pattern#929
IgnaceBleukx merged 24 commits into
masterfrom
decomp_typed

Conversation

@tias

@tias tias commented Apr 19, 2026

Copy link
Copy Markdown
Collaborator

a better-typed decompose (inspired by the new pattern in negate).

It does show we need to continue typing our expressions #913 :
[ ] Element decompose can return int (even in our test suite there is a case for element), to be changed (see inline comment)
[ ] global constraint decompose can return bool, should only return Expressions

and this one should probably go after the CSEMAP #917

This typed version should be a good basis for the positive-decompose, e.g. we only want to support that in the for loop in decompose_in_tree (which can also check if expr.name == "->" and expr.args[1] isinstance GlobalConstraint... for the halfreified positives)

@tias tias marked this pull request as draft April 19, 2026 21:24
@tias tias mentioned this pull request Apr 20, 2026
@IgnaceBleukx IgnaceBleukx self-requested a review May 21, 2026 08:38
@tias tias mentioned this pull request May 21, 2026
32 tasks

@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, had some trouble merging master into this one, but should be done now.

A couple of open questions still

Comment thread cpmpy/transformations/decompose_global.py Outdated
Comment thread cpmpy/transformations/decompose_global.py Outdated
Comment thread cpmpy/transformations/decompose_global.py
Comment thread cpmpy/transformations/decompose_global.py Outdated
Comment thread cpmpy/transformations/decompose_global.py Outdated
@IgnaceBleukx IgnaceBleukx marked this pull request as ready for review May 21, 2026 10:33
@tias tias mentioned this pull request May 21, 2026
18 tasks
@tias

tias commented May 25, 2026

Copy link
Copy Markdown
Collaborator Author

I've changed, it but

  • the code was consistent: in both paths, it was first doing decompose and only then recurse over the args (so it was consistently different from what it used to be)
  • the reason was indeed that after decompose, you need to decompose what you get again (for nested decompositions)
  • it also made the csemap easier: you could check it, decompose the global, decompose it args, then insert it; now, you have to check the map, recurse over the args, decompose, decompose the args, then only insert it

I think the current code is harder to read, it also requires an additional 'cast', and with the csemap, the cost of 're-decomposing' globals in the args was actually non-existing... the whole reason of the map was to avoid that.

So, I would be in favor of going back...

@tias

tias commented May 25, 2026

Copy link
Copy Markdown
Collaborator Author

actually two casts...

now that it is still fresh in my mind, I am going to revert it and document why we do it the way we do it (and there are some minor cleanups possible I now realize)

@tias

tias commented May 25, 2026

Copy link
Copy Markdown
Collaborator Author

hmm, it was a rather extensive refactor for the args part; that looks quite a bit like what I reverted; its still a bit cleaner.

Also compatible with the other PR about 'decompose_custom_positive', I would expect, as it only touches decompose_in_tree().

This is compatible with the 'supported_implication' for choco, needs a name == '->' check and a few lines in decompose_in_tree() (non-urgent?)

@tias tias requested a review from IgnaceBleukx May 25, 2026 22:03

@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.

Ok so if I get it, the pattern now is:

if unsupported global cons/func: decompose.
then treat the decomposition as any expression: either recursively decompose, or check the args of the decomp.

I think that's clean, easy to follow and better than what we had before : )
but some comments on the code itself, where cleanup is possible.

Comment thread cpmpy/transformations/decompose_global.py Outdated
Comment thread cpmpy/transformations/decompose_global.py Outdated
Comment thread cpmpy/transformations/decompose_global.py
Comment thread cpmpy/transformations/decompose_global.py Outdated
@tias tias requested a review from IgnaceBleukx May 26, 2026 23:28

@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 think we're there, and with a nice runtime improvement too!
dev/time_transformations has a decompose time doing down from 43s -> 34s on my machine.
Also memory usage is decreased by ±10%

I've added a type alias for the custom decomposition, its somewhat cryptic so fine to remove again if we don't like it.

Good to merge for me.

@IgnaceBleukx IgnaceBleukx merged commit 0fbf920 into master May 27, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants