Use Global State in Constraints #577
Replies: 1 comment 5 replies
-
Hello there, @ulrich-grossmann. Yours is an interesting question. I'm going to approach this question from an entirely different perspective. Since you've already decided you want to get performance at whatever cost, why not look into Incremental calculator is the fastest you can possibly be with Timefold Solver. Essentially, the limit there is how fast your hand-rolled Java code can be. There is even a variant ( But let's assume you want to stick with Constraint Streams. If we're talking about IncrementalCountingConstraint, what you have there should be doable by a custom constraint collector and therefore a single I'm not saying whether or not you'll keep your 7x improvement. Most likely you will land somewhere in the middle. But I will argue that you have not actually achieved a 7x improvement, because your code has score corruptions. Choosing performance over correctness is not a choice I can recommend. |
Beta Was this translation helpful? Give feedback.
-
Problem background
I am solving real-world school-timetable problems with Timefold and have many complex constraints like students of one grade having lessons together with students of another grade, lessons that have to span multiple consecutive timeslots and grades beeng divided into any number of subgroups that are taught different subjects by different teachers at the same time.
It is often difficult to express such constraints with the constraints-stream language of Timefold yet it is possible but it is even more difficult to express it in a performant way.
The global state pattern and its advantages
So I came across a different design pattern where I use global state in a singleton. This global state is updated before and after any variable change and thus supports fast and scalable incremental score calculation.
But this incremental score calculation goes even two steps ahead of the incremental score calculation provided by Timefold.
Another advantage is that any number of such constraint can be merged into one constraint if speed is important.
This design pattern improves score calculation significantly.
In my real world scenario I managed to improve score calculation speed from 30.000 up to 200.000.
In the POC scenario I provide below the speed improves even in the most basic scenario from 175.000 to 400.000.
A simple POC
In order to explain the pattern I have forked the school-timetable-helloworld quickstart:
https://github.com/ulrich-grossmann/timefold-quickstarts
The problems
There are significant problems with this pattern at the moment.
My questions
Still I want to use this approach and wonder if it could be helpful in other scenarios too. So I have some questions:
Beta Was this translation helpful? Give feedback.
All reactions