18.4. 在规划解决方案中收集域对象
TimeTable 实例会打包单个 数据集 的所有 TimeTable、Room 和 lesson 实例。另外,因为它包含所有课程,每个课程都有特定的规划变量状态,所以它是一个 规划解决方案,它分数如下:
-
如果尚未分配课程,则它是一个 未初始化的 解决方案,例如,分数为
-4init/0hard/0soft的解决方案。 -
如果它中断了硬限制,则它是一个 不可避免的 解决方案,例如分数为
-2hard/-3soft的解决方案。 -
如果它遵循所有硬限制,则它是一个可行的解决方案,例如分数为
0hard/-7soft的解决方案。
TimeTable 类具有 @PlanningSolution 注解,因此红帽构建的 OptaPlanner 知道此类包含所有输入和输出数据。
特别是,这个类是问题的输入:
包含所有时间插槽的
timeslotList字段- 这是问题事实列表,因为它们在解决过程中不会改变。
带有所有房间的
roomList字段- 这是问题事实列表,因为它们在解决过程中不会改变。
具有所有课程的
lessonList字段- 这是规划实体列表,因为它们在解决过程中发生了变化。
每个 lesson
:-
timeslot和room字段的值通常仍然为null,因此未分配。它们是规划变量。 -
其他字段(如
主题、sonr和studentGroup)已填写。这些字段是问题属性。
-
但是,这个类也是解决方案的输出:
-
在解决解决后,每个
Lesson实例都有非null时间slot和room字段的lessonList字段 -
代表输出解决方案的质量的
score字段,例如0hard/-5soft
流程
创建 src/main/java/com/example/domain/TimeTable.java 类:
package com.example.domain;
import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
@PlanningSolution
public class TimeTable {
@ValueRangeProvider(id = "timeslotRange")
@ProblemFactCollectionProperty
private List<Timeslot> timeslotList;
@ValueRangeProvider(id = "roomRange")
@ProblemFactCollectionProperty
private List<Room> roomList;
@PlanningEntityCollectionProperty
private List<Lesson> lessonList;
@PlanningScore
private HardSoftScore score;
private TimeTable() {
}
public TimeTable(List<Timeslot> timeslotList, List<Room> roomList,
List<Lesson> lessonList) {
this.timeslotList = timeslotList;
this.roomList = roomList;
this.lessonList = lessonList;
}
// ********************************
// Getters and setters
// ********************************
public List<Timeslot> getTimeslotList() {
return timeslotList;
}
public List<Room> getRoomList() {
return roomList;
}
public List<Lesson> getLessonList() {
return lessonList;
}
public HardSoftScore getScore() {
return score;
}
}值范围供应商
timeslotList 字段是一个值范围供应商。它保存 OptaPlanner 可以从中选择的 Timeslot 实例,以分配给 lesson 实例的 timeslot 字段。timeslotList 字段具有一个 @ValueRangeProvider 注释,用于将 id 与 lesson 中 @PlanningVariable 的 @PlanningVariable 的 id 匹配。
遵循相同的逻辑后,roomList 字段也具有 @ValueRangeProvider 注释。
问题事实和规划实体属性
此外,OptaPlanner 需要知道它可以更改哪些更少的实例,以及如何检索用于您的 TimeTableConstraintProvider 分数计算的 Timeslot 和 Room 实例。
timeslotList 和 roomList 字段具有一个 @ProblemFactCollectionProperty 注释,因此您的 TimeTableConstraintProvider 可以从这些实例中选择。
lessonList 有一个 @PlanningEntityCollectionProperty 注释,因此 OptaPlanner 可以在解决期间更改它们,您的 TimeTableConstraintProvider 也可以从它们中选择。