Search code examples
optaplannertimefold

May planning entites reference other planning entities?


May one planning entity reference another planning entity and depend on the other entity in score calculations?

Example: A Lecture either has a planning variable Timeslot startTime set, or it has a reference to another lecture, Lecture sameStartTimeAsOtherLecture.

When score calculation (Bavet) is performed, the conflict rule may pick up the start time from the referenced Lecture.

Is such setup ok?

Reason for the question: In my head I have assumed that incremental score calculation depends on having knowledge of what entities are changed, and I figured the setup suggested above might be problematic since the validity of a constraints of a Lecture may change based on updates of a different Lecture.

If not ok, is there a recommended workaround?


Solution

  • The situation you are describing will cause a score corruption. Consider the following constraint:

    forEach(Lecture.class)
        .filter(lecture -> lecture.otherLecture().getStartTime() > ...)
        ...
    

    The filter will only be re-evaluated when the outer lecture changes, but it will not be re-evaluated if only the inner lecture changes.

    The solution to this problem is to clearly mark the other lecture for re-evaluation, using a join:

    forEach(Lecture.class)
        .join(Lecture.class,
             Joiners.equal(Lecture::otherLecture(), Function.identity())
        .filter((lecture, otherLecture) -> otherLecture.getStartTime() > ...)
        ...