Skip to content

Commit 80f2747

Browse files
docs: add note on optimizing performance of declarative shadow variables (#1728)
Co-authored-by: Lukáš Petrovický <[email protected]>
1 parent 1a1909c commit 80f2747

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

docs/src/modules/ROOT/pages/using-timefold-solver/modeling-planning-problems.adoc

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,13 +1011,10 @@ Constraint penalizeLoopedJobs(ConstraintFactory factory) {
10111011
10121012
====
10131013
1014-
[IMPORTANT]
1015-
====
1016-
Do not use a @ShadowVariableLooped property in a method annotated with @ShadowSources.
1017-
@ShadowVariableLooped properties can be updated after the @ShadowSources marked method is called, causing score corruption.
1018-
@ShadowSources marked methods do not need to check @ShadowVariableLooped properties, since they are only called if all their dependencies are not looped.
1019-
====
1014+
NOTE: `@ShadowSources`-annotated methods do not need to check `@ShadowVariableLooped`-annotated properties.
1015+
These methods are only called if neither of their dependencies are looped, and therefore the value of such properties at that time is guaranteed to be `false`.
10201016
1017+
[#aligningShadowVariables]
10211018
==== Aligning Shadow Variables
10221019
10231020
`@ShadowSources` has an optional `alignmentKey` parameter that can be used to reuse calculation results to increase performance.
@@ -1150,6 +1147,24 @@ public class Job {
11501147
----
11511148
====
11521149
1150+
==== Optimizing Shadow Variables
1151+
1152+
Each custom Shadow Variable on each planning entity constitutes one node of a reference graph.
1153+
The Solver analyzes this graph to decide what is the most optimal strategy for updating these variables.
1154+
In general, the less variables and the less dependencies between them, the faster the processing.
1155+
For the best performance, follow these guidelines:
1156+
1157+
- Keep all custom Shadow Variables on the same entity class.
1158+
If one entity is grouping another entity, you might be able to use <<aligningShadowVariables,custom Shadow Variable alignment>> to keep custom Shadow Variables on the same entity class.
1159+
1160+
- Do not use both `@PreviousElementShadowVariable` and `@NextElementShadowVariable` in your sources.
1161+
This includes using `@PreviousElementShadowVariable` as a source in one custom Shadow Variable while using `@NextElementShadowVariable` as a source in another custom Shadow Variable.
1162+
1163+
- Compute as much as possible within a single custom Shadow Variable supplier method.
1164+
Do not create intermediate custom Shadow Variables when it is possible to compute the value directly from another custom Shadow Variable.
1165+
1166+
NOTE: These are tips and tricks for optimal performance and needn't be followed to the letter. The solver will work correctly even if you decide not to follow any of the advice, albeit with slightly lesser performance.
1167+
11531168
=== Shadow variable cloning
11541169
11551170
A shadow variable's value (just like a genuine variable's value)

0 commit comments

Comments
 (0)