第 13 章 红帽构建的 OptaPlanner 和 Java: a button Timetable Quickstart 指南
本指南指导您完成使用 OptaPlanner 的约束解决人工智能(AI)创建简单 Java 应用程序的过程。您将构建一种针对学生和教师优化中学时间的命令行应用程序:
... INFO Solving ended: time spent (5000), best score (0hard/9soft), ... INFO INFO | | Room A | Room B | Room C | INFO |------------|------------|------------|------------| INFO | MON 08:30 | English | Math | | INFO | | I. Jones | A. Turing | | INFO | | 9th grade | 10th grade | | INFO |------------|------------|------------|------------| INFO | MON 09:30 | History | Physics | | INFO | | I. Jones | M. Curie | | INFO | | 9th grade | 10th grade | | INFO |------------|------------|------------|------------| INFO | MON 10:30 | History | Physics | | INFO | | I. Jones | M. Curie | | INFO | | 10th grade | 9th grade | | INFO |------------|------------|------------|------------| ... INFO |------------|------------|------------|------------|
...
INFO Solving ended: time spent (5000), best score (0hard/9soft), ...
INFO
INFO | | Room A | Room B | Room C |
INFO |------------|------------|------------|------------|
INFO | MON 08:30 | English | Math | |
INFO | | I. Jones | A. Turing | |
INFO | | 9th grade | 10th grade | |
INFO |------------|------------|------------|------------|
INFO | MON 09:30 | History | Physics | |
INFO | | I. Jones | M. Curie | |
INFO | | 9th grade | 10th grade | |
INFO |------------|------------|------------|------------|
INFO | MON 10:30 | History | Physics | |
INFO | | I. Jones | M. Curie | |
INFO | | 10th grade | 9th grade | |
INFO |------------|------------|------------|------------|
...
INFO |------------|------------|------------|------------|
您的应用程序将使用 AI 遵循硬和软调度 限制,将 Lesson 实例分配给 Timeslot 和 Room 实例,例如:
- 房间最多可以有一节时间。
- 教师可以指导大多数课程同时进行学习。
- 学员最多可以同时参加一门课程。
- 教师更喜欢在同一房间教授所有课程。
- 教员更喜欢教授在课程之间的顺序课程和分解。
- 同一主题上的学员不喜欢顺序课程。
以数学方式讲,中立时间是 NP-hard 问题。这意味着很难扩展。简而言之,对于一个非三维数据集,即使是超级计算机,即使是超级计算器,所有可能的组合都要花数年时间。需要的是,AI 约束解决者(如 OptaPlanner)具有在合理的时间内提供接近优化解决方案的高级算法。
先决条件
- OpenJDK (JDK) 11 已安装。红帽构建的 Open JDK 可从红帽客户门户中的 Software Downloads 页面获得(需要登录)。
- 已安装 Apache Maven 3.6 或更高版本。Maven 可从 Apache Maven Project 网站获取。
- IDE,如 IntelliJ IDEA、VSCode 或 Eclipse
13.1. 创建 Maven 或 Gradle 构建文件并添加依赖项 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以将 Maven 或 Gradle 用于 OptaPlanner button application。创建构建文件后,添加以下依赖项:
-
OptaPlanner-core(编译范围)来解决中计时问题 -
OptaPlanner-test(test scope) to JUnit 测试中立时间稳定限制 -
logback-classic(runtime scope)等实现,用于查看 OptaPlanner 采取的步骤
流程
- 创建 Maven 或 Gradle 构建文件。
在您的构建文件中添加
optaplanner-core、Soptaplanner-test和logback-classic依赖项:对于 Maven,请在
pom.xml文件中添加以下依赖项:<dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-core</artifactId> </dependency> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency><dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-core</artifactId> </dependency> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以下示例显示了完整的
pom.xml文件。<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.acme</groupId> <artifactId>optaplanner-hello-world-school-timetabling-quickstart</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>11</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <version.org.optaplanner>8.11.1.Final-redhat-00006</version.org.optaplanner> <version.org.logback>1.2.3</version.org.logback> <version.compiler.plugin>3.8.1</version.compiler.plugin> <version.surefire.plugin>3.0.0-M5</version.surefire.plugin> <version.exec.plugin>3.0.0</version.exec.plugin> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-bom</artifactId> <version>${version.org.optaplanner}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${version.org.logback}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-core</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <scope>runtime</scope> </dependency> <!-- Testing --> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${version.surefire.plugin}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>${version.exec.plugin}</version> <configuration> <mainClass>org.acme.schooltimetabling.TimeTableApp</mainClass> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>jboss-public-repository-group</id> <url>https://repository.jboss.org/nexus/content/groups/public/</url> <releases> <!-- Get releases only from Maven Central which is faster. --> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.acme</groupId> <artifactId>optaplanner-hello-world-school-timetabling-quickstart</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>11</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <version.org.optaplanner>8.11.1.Final-redhat-00006</version.org.optaplanner> <version.org.logback>1.2.3</version.org.logback> <version.compiler.plugin>3.8.1</version.compiler.plugin> <version.surefire.plugin>3.0.0-M5</version.surefire.plugin> <version.exec.plugin>3.0.0</version.exec.plugin> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-bom</artifactId> <version>${version.org.optaplanner}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${version.org.logback}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-core</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <scope>runtime</scope> </dependency> <!-- Testing --> <dependency> <groupId>org.optaplanner</groupId> <artifactId>optaplanner-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${version.surefire.plugin}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>${version.exec.plugin}</version> <configuration> <mainClass>org.acme.schooltimetabling.TimeTableApp</mainClass> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>jboss-public-repository-group</id> <url>https://repository.jboss.org/nexus/content/groups/public/</url> <releases> <!-- Get releases only from Maven Central which is faster. --> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 对于 Gradle,将以下依赖项添加到
gradle.build文件中:dependencies { implementation "org.optaplanner:optaplanner-core:${optaplannerVersion}" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" testImplementation "org.optaplanner:optaplanner-test:${optaplannerVersion}" }dependencies { implementation "org.optaplanner:optaplanner-core:${optaplannerVersion}" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" testImplementation "org.optaplanner:optaplanner-test:${optaplannerVersion}" }Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以下示例显示了已完成的
gradle.build文件。plugins { id "java" id "application" } def optaplannerVersion = "{project-version}" def logbackVersion = "1.2.3" group = "org.acme" version = "0.1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation "org.optaplanner:optaplanner-core:${optaplannerVersion}" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" testImplementation "org.optaplanner:optaplanner-test:${optaplannerVersion}" } java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } compileJava { options.encoding = "UTF-8" options.compilerArgs << "-parameters" } compileTestJava { options.encoding = "UTF-8" } application { mainClass = "org.acme.schooltimetabling.TimeTableApp" } test { // Log the test execution results. testLogging { events "passed", "skipped", "failed" } }plugins { id "java" id "application" } def optaplannerVersion = "{project-version}" def logbackVersion = "1.2.3" group = "org.acme" version = "0.1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation "org.optaplanner:optaplanner-core:${optaplannerVersion}" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" testImplementation "org.optaplanner:optaplanner-test:${optaplannerVersion}" } java { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } compileJava { options.encoding = "UTF-8" options.compilerArgs << "-parameters" } compileTestJava { options.encoding = "UTF-8" } application { mainClass = "org.acme.schooltimetabling.TimeTableApp" } test { // Log the test execution results. testLogging { events "passed", "skipped", "failed" } }Copy to Clipboard Copied! Toggle word wrap Toggle overflow