Ahmad-prog-mach
Ahmad-prog-mach

Reputation: 41

Optaplanner skipping Phases and returning empty solution

I'm working on scheduling project for classes (Teachers, Lessons, time). I'm using optaplanner as part of spring-boot application, the test code is compiling and running correctly however the result contain empty solution, in the log output I see this message:

rted: time spent (11), best score (0hard/0soft), environment mode (REPRODUCIBLE), move thread count (NONE), random (JDK with seed 0). 2021-09-28 22:39:26.619 INFO 2579 --- [pool-1-thread-1] o.o.core.impl.solver.DefaultSolver : Skipped all phases (2): out of 0 planning entities, none are movable (non-pinned). 2021-09-28 22:39:26.620 INFO 2579 --- [pool-1-thread-1] o.o.core.impl.solver.DefaultSolver : Solving ended: time spent (16), best score (0hard/0soft), score calculation speed (62/sec), phase total (2), environment mode (REPRODUCIBLE), move thread count (NONE).

The problem is in the test calculator I wrote I'm trying to loop on the possible solution and actually decrease the cost a bit sometimes or even increase it, but it doesn't taking effect because I'm looping and trying to log the objects but nothing is being logged, this is the code of the calculator:

public class ScheduleScoreCalculator implements EasyScoreCalculator<ScheduleTable, HardSoftScore>
{
@Override
public HardSoftScore calculateScore(ScheduleTable scheduleTable) {
    int hardScore = 0;
    int softScore = 0;

    List<ScheduledClass> scheduledClassList = scheduleTable.getScheduledClasses();
    System.out.println(scheduleTable);
    System.out.println("Hmmmmm ---"); // this is logged but the score is not changing
    for(ScheduledClass a: scheduledClassList) {
        for (ScheduledClass b : scheduledClassList) {
            if (a.getTeacher().getTeacherId() > 17000L) {
                hardScore+=18;
            }
            if (a.getTimeslot() != null && a.getTimeslot().equals(b.getTimeslot())
                    && a.getId() < b.getId()) {
                if (a.getTeacher() != null && a.getTeacher().equals(b.getTeacher())) {
                    hardScore--;
                }
                if (a.getTeacher().equals(b.getTeacher())) {
                    hardScore--;
                }
            } else {
                hardScore++;
                softScore+=2;
            }
        }
    }

    return HardSoftScore.of(hardScore, softScore);
}
}

So Please any idea why optaplanner might skip creating possible solutions?

Upvotes: 2

Views: 310

Answers (1)

Ahmad-prog-mach
Ahmad-prog-mach

Reputation: 41

The issue was simpler than I thought, The Solution class annotated with "PlanningSolution" has property "scheduledClasses" annotated with "PlanningEntityCollectionProperty" my mistake that this property was initialized with empty List (ArrayList), the solution was to initialize a solution class! In retrospect I think the documentation is to be blamed on this, the provided example didn't mention that we need to have this, so it should not be null (otherwise and exception will be raised) and it shouldn't be empty List. You need to initialize it with class without setting any value for the movable properties (annotated with "PlanningVariable"). Thanks for @Lukáš Petrovický as his comment helped me do the correct investigation!

Upvotes: 2

Related Questions