第 13 章 OptaPlanner 和 Java:一个可插入时间快速启动指南
本指南指导您完成创建具有 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 遵循硬和软调度 限制,将自动将附件实例分配给 Timeslot
和 Room
实例,例如:
- 空间最多可以同时有一个小时间。
- 一个架构师可以同时参与一个小时间。
- 每天最多可同时参与一个小时间。
- 指导人员更喜欢在同一房间所有较少的活动。
- 指导人员更喜欢参与较少的活动,并排解少量间的差距。
- 同一主题上连续的 小项是 一 个 一 个。
mathematly 认为是 NP 硬性问题。这意味着难以扩展。简单地强制迭代所有可能的组合,对于非平量数据集也需要数百万年,即使在超级计算器上也是如此。AI 约束解析器(如 OptaPlanner)有高级算法,可在合理的时间内提供最接近的解决方案。
先决条件
- 已安装 OpenJDK (JDK) 11。Red Hat build of Open JDK 位于红帽客户门户网站中的 Software Downloads 页面中(需要登录)。
- 已安装 Apache Maven 3.6 或更高版本。Maven 可从 Apache Maven 项目网站 获得。
- IDE,如 IntelliJ IDEA、VSCode 或 Eclipse
13.1. 创建 Maven 或 Gradle 构建文件并添加依赖项 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以将 Maven 或 Gradle 用于 OptaPlanner Evolution 时间应用程序。创建构建文件后,添加以下依赖项:
-
OptaPlanner-core
(复合范围)来解决 IANA 时间问题 -
optaPlanner-test
(测试范围)测试 JUnit 计时限制 -
一个实现,如
logback-classic
(runtime 范围)来查看 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.29.0.Final-redhat-00009</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.29.0.Final-redhat-00009</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