英文:
Optaplanner integration
问题
我正在尝试在我的项目中集成OptaPlanner。我正在使用Spring JPA、Maven和MySQL数据库进行开发。
我已经在我的Maven文件中添加了所需的依赖项,这样我就可以使用OptaPlanner的注解了,但我不知道如何使用它。我已经阅读了文档和示例,但我仍然不知道如何使用它。
我需要将食谱(recipes)和用户(user)分配给一个名为FoodList的类。每个FoodList对象都有一个id、两个枚举、一个食谱、一个用户和一个日期,如下所示:
- FoodList类:
@PlanningEntity
@Entity
public class ListaComida {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private Comida comida;
@Enumerated(EnumType.STRING)
private Plato plato;
@PlanningVariable
@ManyToOne
private Receta receta;
@PlanningVariable
@ManyToOne
private Usuario usuario;
@Column(nullable = false)
private LocalDate fecha;
// ...
}
@PlanningSolution // OptaPlanner注解
@TypeDef(defaultForType = HardSoftScore.class, typeClass = HardSoftScoreHibernateType.class) // Hibernate注解
public class ListaComidaSolution {
@Columns(columns = {@Column(name = "hardScore"), @Column(name = "softScore")})
private HardSoftScore score;
@PlanningScore
public HardSoftScore getScore() {
return score;
}
public void setScore(HardSoftScore score) {
this.score = score;
}
}
- XML配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<solver>
<!-- 领域模型配置 -->
<scanAnnotatedClasses/>
<!-- 分数配置 -->
<scoreDirectorFactory>
<easyScoreCalculatorClass>src/main/java/es.uca.AutomaticFoodList/GenerarComidaEasyScoreCalculator</easyScoreCalculatorClass>
<!-- <scoreDrl>org/optaplanner/examples/cloudbalancing/solver/cloudBalancingScoreRules.drl</scoreDrl> -->
</scoreDirectorFactory>
<!-- 优化算法配置 -->
<termination>
<secondsSpentLimit>30</secondsSpentLimit>
</termination>
</solver>
- EasyScoreCalculator实现:
public class GenerarComidaEasyScoreCalculator implements EasyScoreCalculator<ListaComidaSolution> {
public HardSoftScore calculateScore(ListaComidaSolution listaComidaSolution){
int hardScore = 0, softScore = 0;
return HardSoftScore.of(hardScore, softScore);
}
}
- 下面这个类尚未实现,但我认为我需要实现它:
public static void generarListaComida(){
//SolverFactory<CloudBalance> solverFactory = SolverFactory.createFromXmlResource(
// "org/optaplanner/examples/cloudbalancing/solver/cloudBalancingSolverConfig.xml");
//Solver<CloudBalance> solver = solverFactory.buildSolver();
// Load a problem with 400 computers and 1200 processes
//CloudBalance unsolvedCloudBalance = new CloudBalancingGenerator().createCloudBalance(400, 1200);
// Solve the problem
//CloudBalance solvedCloudBalance = solver.solve(unsolvedCloudBalance);
// Display the result
//System.out.println("\nSolved cloudBalance with 400 computers and 1200 processes:\n"
// + toDisplayString(solvedCloudBalance));
}
这些是我在项目中实现这一部分所需的所有类和文件吗?还是我需要实现更多的类?
英文:
I am trying to integrate OptaPlanner in my project. I am working with Spring jpa, maven and mysql database.
I have implemented the dependencies on my maven file, so I can use the annotations of OptaPlanner, but I don't know how to use it. I have been reading the documentation and examples but i still don't know how to use it.
I have to assign recipes and an user to a class called FoodList. Each object of FoodList has id, 2 enums, the recipe, the user and a Date, i show:
- FoodList class:
@PlanningEntity()
@Entity
public class ListaComida {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private Comida comida;
@Enumerated(EnumType.STRING)
private Plato plato;
@PlanningVariable()
@ManyToOne
private Receta receta;
@PlanningVariable()
@ManyToOne
private Usuario usuario;
@Column(nullable = false)
private LocalDate fecha;
...
}
@PlanningSolution // OptaPlanner annotation
@TypeDef(defaultForType = HardSoftScore.class, typeClass = HardSoftScoreHibernateType.class) // Hibernate annotation
public class ListaComidaSolution {
@Columns(columns = {@Column(name = "hardScore"), @Column(name = "softScore")})
private HardSoftScore score;
@PlanningScore
public HardSoftScore getScore() {
return score;
}
public void setScore(HardSoftScore score) {
this.score = score;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<solver>
<!-- Domain model configuration -->
<scanAnnotatedClasses/>
<!-- Score configuration -->
<scoreDirectorFactory>
<easyScoreCalculatorClass>src/main/java/es.uca.AutomaticFoodList/GenerarComidaEasyScoreCalculator</easyScoreCalculatorClass>
<!--<scoreDrl>org/optaplanner/examples/cloudbalancing/solver/cloudBalancingScoreRules.drl</scoreDrl>-->
</scoreDirectorFactory>
<!-- Optimization algorithms configuration -->
<termination>
<secondsSpentLimit>30</secondsSpentLimit>
</termination>
</solver>
public class GenerarComidaEasyScoreCalculator implements EasyScoreCalculator<ListaComidaSolution> {
public HardSoftScore calculateScore(ListaComidaSolution listaComidaSolution){
int hardScore = 0, softScore = 0;
return HardSoftScore.of(hardScore, softScore);
}
}
- This class is not implemented, but I think I have to do it.
public static void generarListaComida(){
//SolverFactory<CloudBalance> solverFactory = SolverFactory.createFromXmlResource(
// "org/optaplanner/examples/cloudbalancing/solver/cloudBalancingSolverConfig.xml");
//Solver<CloudBalance> solver = solverFactory.buildSolver();
// Load a problem with 400 computers and 1200 processes
//CloudBalance unsolvedCloudBalance = new CloudBalancingGenerator().createCloudBalance(400, 1200);
// Solve the problem
//CloudBalance solvedCloudBalance = solver.solve(unsolvedCloudBalance);
// Display the result
//System.out.println("\nSolved cloudBalance with 400 computers and 1200 processes:\n"
// + toDisplayString(solvedCloudBalance));
}
Is this all classes and files I need to implement this in my project?, or I have to implement more classes?
答案1
得分: 0
在 https://www.optaplanner.org/ 上,您可以下载可执行演示。然而,这不仅是一个可执行演示,还包含了示例的源代码(在 examples/source
文件夹中)。在那里,您可以看到 optaplanner 在示例应用中的使用方式,您可以在您的应用程序中进行类似的操作。
一个很好的起点还有 https://docs.optaplanner.org/7.36.0.Final/optaplanner-docs/html_single/index.html#plannerConfiguration 第4章及以后部分。
英文:
On https://www.optaplanner.org/ you can download an executable demo. However it is not just an executable demo but also contains the source code of the examples ( in examples/source
folder). There you can see how optaplanner is used in the example applications, and you can do the same in your application.
A good starting point is also https://docs.optaplanner.org/7.36.0.Final/optaplanner-docs/html_single/index.html#plannerConfiguration chapter 4 ff.
专注分享java语言的经验与见解,让所有开发者获益!
评论