第 18 章 红帽构建的 OptaPlanner 和 Java:中等时间的快速入门指南
本指南指导您完成使用 OptaPlanner 约束创建简单 Java 应用程序的过程,用于解决人工智能(AI)。您将创建一个命令行应用程序,为教育人员和教员优化了院校的时间:
... 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 的问题。这意味着扩展比较困难。简单地简单地强制对所有可能的组合进行迭代,需要数百万年时间用于非公平的数据集,即使在超级计算机上也是如此。satisfy, AI 约束解析器(如 OptaPlanner)具有在合理的时间内提供接近最佳解决方案的高级算法。
先决条件
- 已安装 OpenJDK (JDK) 11。红帽构建的 Open JDK 可通过红帽客户门户网站中的 Software Downloads 页面(需要登录)。
- 已安装 Apache Maven 3.6 或更高版本。Maven 位于 Apache Maven Project 网站。
- IDE,如 IntelliJ IDEA、VSCode 或 Eclipse
18.1. 创建 Maven 或 Gradle 构建文件并添加依赖项 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以将 Maven 或 Gradle 用于 OptaPlanner school timetable 应用程序。创建构建文件后,添加以下依赖项:
-
OptaPlanner-core(编译范围)以解决中等问题 -
对 JUnit 的 OptaPlanner
-test(测试范围)测试 CLASS timetabling 约束 -
logback-classic(运行时范围)等实现,以查看 OptaPlanner 采取的步骤
流程
- 创建 Maven 或 Gradle 构建文件。
在您的构建文件中添加
和optaplanner-core、Optaplanner-testlogback-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.38.0.Final-redhat-00004</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.38.0.Final-redhat-00004</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 platform("org.optaplanner:optaplanner-bom:${optaplannerVersion}") implementation "org.optaplanner:optaplanner-core" testImplementation "org.optaplanner:optaplanner-test" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" }dependencies { implementation platform("org.optaplanner:optaplanner-bom:${optaplannerVersion}") implementation "org.optaplanner:optaplanner-core" testImplementation "org.optaplanner:optaplanner-test" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" }Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以下示例显示了已完成的
gradle.build文件。plugins { id "java" id "application" } def optaplannerVersion = "{optaplanner-version}" def logbackVersion = "1.2.9" group = "org.acme" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation platform("org.optaplanner:optaplanner-bom:${optaplannerVersion}") implementation "org.optaplanner:optaplanner-core" testImplementation "org.optaplanner:optaplanner-test" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" } 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 = "{optaplanner-version}" def logbackVersion = "1.2.9" group = "org.acme" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation platform("org.optaplanner:optaplanner-bom:${optaplannerVersion}") implementation "org.optaplanner:optaplanner-core" testImplementation "org.optaplanner:optaplanner-test" runtimeOnly "ch.qos.logback:logback-classic:${logbackVersion}" } 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