OptaPlanner笔记2

1.5.3 使用maven

修改pom.xml

导入optaplanner-bom以避免为每一个依赖项重复添加版本号

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.optaplanner</groupId>
        <artifactId>optaplanner-bom</artifactId>
        <type>pom</type>
        <version>...</version>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-persistence-jpa</artifactId>
    </dependency>
    ...
  </dependencies>
</project>

2.2.5 领域对象建模

在这里插入图片描述

2.2.5.1 Timeslot

Timeslot类表示教授课程的时间段,例如:星期一上午10:30-11:30或星期二13:30-14:30。为简单起见,所有时间段的持续时间相同,午餐或其他休息时间没有时间段。
时间段没有日期,因为高中的课程表每周都是一样的。 因此,无需进行连续规划(18.4)

因为在求解过程中Timeslot实例不会改变,所以称Timeslot为problem fact(问题事实)。 这种类不需要使用任何OptaPlanner注解。

注意:保证toString()方法的输出简短,这样OptaPlanner的DEBUG/TRACE日志会更易读。

2.2.5.2 Room

Room 类表示教授课程的地点,例如,Room A或 Room B。为简单起见,所有房间都没有容量限制,他们可以容纳所有课程。
Room实例在求解过程中不会改变,这也是一个问题事实。

2.2.5.3. Lesson

Lesson类表示一节课程(一名教师给一组学生教授一个主题),例如,A.Turing给9年纪学生教授的数学课 或 M.Curie为10年纪学生教授的化学课 。如果同一名教师每周向同一学生组多次教授相同一门科目,那么这样几个Lesson实例只能通过id来区分。 例如,9年级每周有六节数学课。

在求解过程中,OptaPlanner会更改Lesson类的timeslot和room字段,来为每节课分配到一个时间段和一个房间。因为OptaPlanner更改了这些字段,所以Lesson 是一个planning entity(计划实体)
在这里插入图片描述
上图中除了橙色字段的大多数字段都包含输入数据:在输入数据中,lesson的timeslot和room字段没有赋值,而是在输出数据中被赋值了。OptaPlanner在求解过程中会更改这些字段。此类字段称为planning variables(规划变量)。 为了让OptaPlanner识别规划变量, timeslot和room字段都需要添加**@PlanningVariable注解。包含规划变量的类Lesson,需要使用@PlanningEntity**注解。

import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.lookup.PlanningId;
import org.optaplanner.core.api.domain.variable.PlanningVariable;

// 标识这个类在求解过程中需要变更,因为他会包含至少一个规划变量。
@PlanningEntity
public class Lesson {
    
    
   @PlanningId
    private Long id;
    ...
    // 标识这个值可以变更
    @PlanningVariable
    private Timeslot timeslot;
    @PlanningVariable
    private Room room;
    ...
}

为了查找可以给规划变量字段赋值的潜在Timeslot实例,OptaPlanner会根据变量类型来链接一个提供List的*( value range provider)值域提供器*(4.3.5.2),并从中取值。

参考:领域建模指南

猜你喜欢

转载自blog.csdn.net/zhoudingding/article/details/132250081