Gather the domain objects in a planning solution
A Timetable wraps all Timeslot, Room, and Lesson instances of a single dataset.
Furthermore, because it contains all lessons, each with a specific planning variable state,
it is a planning solution and it has a score:
-
If lessons are still unassigned, then it is an uninitialized solution,
-
If it breaks hard constraints, then it is an infeasible solution, for example, a solution with the score
-2hard/-3soft. -
If it adheres to all hard constraints, then it is a feasible solution, for example, a solution with the score
0hard/-7soft.
-
Java
-
Kotlin
Create the src/main/java/org/acme/schooltimetabling/domain/Timetable.java class:
package org.acme.schooltimetabling.domain;
import java.util.List;
import ai.timefold.solver.core.api.domain.solution.PlanningEntityCollectionProperty;
import ai.timefold.solver.core.api.domain.solution.PlanningScore;
import ai.timefold.solver.core.api.domain.solution.PlanningSolution;
import ai.timefold.solver.core.api.domain.solution.ProblemFactCollectionProperty;
import ai.timefold.solver.core.api.domain.valuerange.ValueRangeProvider;
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
@PlanningSolution
public class Timetable {
@ValueRangeProvider
@ProblemFactCollectionProperty
private List<Timeslot> timeslots;
@ValueRangeProvider
@ProblemFactCollectionProperty
private List<Room> rooms;
@PlanningEntityCollectionProperty
private List<Lesson> lessons;
@PlanningScore
private HardSoftScore score;
public Timetable() {
}
public Timetable(List<Timeslot> timeslots, List<Room> rooms, List<Lesson> lessons) {
this.timeslots = timeslots;
this.rooms = rooms;
this.lessons = lessons;
}
public List<Timeslot> getTimeslots() {
return timeslots;
}
public List<Room> getRooms() {
return rooms;
}
public List<Lesson> getLessons() {
return lessons;
}
public HardSoftScore getScore() {
return score;
}
}
Create the src/main/kotlin/org/acme/schooltimetabling/TimetableApp.kt class:
package org.acme.schooltimetabling.domain
import ai.timefold.solver.core.api.domain.solution.PlanningEntityCollectionProperty
import ai.timefold.solver.core.api.domain.solution.PlanningScore
import ai.timefold.solver.core.api.domain.solution.PlanningSolution
import ai.timefold.solver.core.api.domain.solution.ProblemFactCollectionProperty
import ai.timefold.solver.core.api.domain.valuerange.ValueRangeProvider
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore
import ai.timefold.solver.core.api.solver.SolverStatus
@PlanningSolution
data class Timetable (
@ProblemFactCollectionProperty
@ValueRangeProvider
val timeslots: List<Timeslot>,
@ProblemFactCollectionProperty
@ValueRangeProvider
val rooms: List<Room>,
@PlanningEntityCollectionProperty
val lessons: List<Lesson>,
@PlanningScore
var score: HardSoftScore? = null) {
// No-arg constructor required for Timefold
constructor() : this(emptyList(), emptyList(), emptyList())
}
The Timetable class has an @PlanningSolution annotation,
so Timefold Solver knows that this class contains all of the input and output data.
Specifically, these classes are the input of the problem:
-
The
timeslotsfield with all time slots-
This is a list of problem facts, because they do not change during solving.
-
-
The
roomsfield with all rooms-
This is a list of problem facts, because they do not change during solving.
-
-
The
lessonsfield with all lessons-
This is a list of planning entities, because they change during solving.
-
Of each
Lesson:-
The values of the
timeslotandroomfields are typically stillnull, so unassigned. They are planning variables. -
The other fields, such as
subject,teacherandstudentGroup, are filled in. These fields are problem properties.
-
-
However, this class is also the output of the solution:
-
The
lessonsfield for which eachLessoninstance has non-nulltimeslotandroomfields after solving. -
The
scorefield that represents the quality of the output solution, for example,0hard/-5soft.
The value range providers
The timeslots field is a value range provider.
It holds the Timeslot instances which Timefold Solver can pick from to assign to the timeslot field of Lesson instances.
The timeslots field has an @ValueRangeProvider annotation to connect the @PlanningVariable with the @ValueRangeProvider,
by matching the type of the planning variable with the type returned by the value range provider.
Following the same logic, the rooms field also has an @ValueRangeProvider annotation.
The problem fact and planning entity properties
Furthermore, Timefold Solver needs to know which Lesson instances it can change
as well as how to retrieve the Timeslot and Room instances used for score calculation
by your TimetableConstraintProvider.
The timeslots and rooms fields have an @ProblemFactCollectionProperty annotation,
so your TimetableConstraintProvider can select from those instances.
The lessons has an @PlanningEntityCollectionProperty annotation,
so Timefold Solver can change them during solving
and your TimetableConstraintProvider can select from those too.