Coroneal
Coroneal

Reputation: 1

Cloning PlanningSolution in OptaPlanner

I am trying to solve timetabling problem with OptaPlanner but I am facing with problem which may be connected to cloning issue. Why when my planning entity collection method looks like that

    @PlanningEntityCollectionProperty
public List<Lesson> getLessons() {
    return getProject().getLessons();
}

I am getting error

java.lang.IllegalStateException: The solutionProperty (lessons) was not cloned as expected. The FieldAccessingSolutionCloner failed to recognize that property's field, probably because its field name is different.

but, when the method looks like

    @PlanningEntityCollectionProperty
public List<Lesson> getLessons() {
    if (lessons == null) {
        lessons = getProject().getLessons();
    }
    return lessons;
}

everything is ok. What could be a reason? Why I am getting this error? Thanks for any help.

Upvotes: 0

Views: 513

Answers (1)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27312

The generic cloner (to planning clone the best solution so the working solution can degrade) can't currently handle that first piece of code unfortunately.

There are 2 workarounds:

  • Implement a custom planning cloner (this is not simple, but it's explained in the docs section "4.3.7.6. Cloning a Solution".
  • Make the canonical field for lessons on that planning solution instead of Project (or refactor Project to implement Solution).

I wouldn't do it with that lazy initialization trick, as you might not always be able to guarantee that the getter has been called at least once before a planning clone happened.

Upvotes: 1

Related Questions