Java OptaPlanner返回空解决方案
我正试图用OptaPlanner解决一个问题,但程序返回我和空的解决方案。我按照这个例子来解决这个问题: 我正在使用SpringBoot JPA集成 这里是实现的代码:Java OptaPlanner返回空解决方案,java,spring-data-jpa,optaplanner,Java,Spring Data Jpa,Optaplanner,我正试图用OptaPlanner解决一个问题,但程序返回我和空的解决方案。我按照这个例子来解决这个问题: 我正在使用SpringBoot JPA集成 这里是实现的代码: 规划解决方案 调用OptaPlanner服务的函数: 约束提供程序,但目前没有任何限制: 控制台返回的内容: 我想最好添加一个简单的约束条件(奖励/惩罚),以允许解算器计算分数,这将有助于它猜测是否会有变化,请您使用@PlanningSolution/Planning实体(在您的案例中是PlanningEntit
- 规划解决方案
- 调用OptaPlanner服务的函数:
- 约束提供程序,但目前没有任何限制:
- 控制台返回的内容:
我想最好添加一个简单的约束条件(奖励/惩罚),以允许解算器计算分数,这将有助于它猜测是否会有变化,请您使用@PlanningSolution/Planning实体(在您的案例中是PlanningEntityCollectionProperty)的一些日志更新您的帖子解决之前和之后?记录分数也会有帮助
PS:在保存之前,尝试覆盖“HorarioComidasService::save”以记录日志。在
HorarioComidasService::save
方法中设置断点。解决方案参数是否为空?如果是这样,请在horarioComidasService::findByUsuario
方法中放置断点,返回的解决方案是否为空?我不知道函数为什么不调用horarioComidasService::save方法。我添加了一个名为horarioComidaService的新类,我仍然有同样的问题。将断点放在输入和输出方法中,检查解决方案的目的是确认optaplanner确实做错了您认为它做错的事情。通常,问题发生在输入之前或输出之后的数据处理过程中,与SolverManager的工作无关。目前,我不知道我能做些什么,我对此感到非常困惑,不知道如何解决此问题。我一直在遵循这个示例,但现在不知道该怎么做。如何在调试时设置java断点:我有它,但我删除了它,因为我有同样的问题,所以我没有条件地尝试。
@PlanningSolution
public class HorarioComidas {
@ValueRangeProvider(id = "recetaRange")
@ProblemFactCollectionProperty
private List<Receta> recetaList;
@ValueRangeProvider(id = "fechaSemanaRange")
@ProblemFactCollectionProperty
private List<FechaSemana> fechaSemanaList;
@ValueRangeProvider(id = "comidaRange")
@ProblemFactCollectionProperty
private List<Comida> comidaList;
@PlanningEntityCollectionProperty
private List<UsuarioReceta> usuarioRecetas;
@PlanningScore
private HardSoftScore score;
// Ignored by OptaPlanner, used by the UI to display solve or stop solving button
private SolverStatus solverStatus;
...
}
@Service
public class OptaPlannerService {
@Autowired
private HorarioComidasService horarioComidasService;
@Autowired
private SolverManager<HorarioComidas, Usuario> solverManager;
@Autowired
private ScoreManager<HorarioComidas> scoreManager;
public HorarioComidas getTimeTable(Usuario usuario) {
// Get the solver status before loading the solution
// to avoid the race condition that the solver terminates between them
SolverStatus solverStatus = getSolverStatus(usuario);
HorarioComidas solution = horarioComidasService.findByUsuario(usuario);
scoreManager.updateScore(solution); // Sets the score
solution.setSolverStatus(solverStatus);
return solution;
}
public void solve(Usuario usuario) {
horarioComidasService.usuario = usuario;
solverManager.solveAndListen(usuario,
horarioComidasService::findByUsuario,
horarioComidasService::save);
}
public SolverStatus getSolverStatus(Usuario usuario) {
return solverManager.getSolverStatus(usuario);
}
public void stopSolving(Usuario usuario) {
solverManager.terminateEarly(usuario);
}
}
@Service
public class HorarioComidasService {
@Autowired
private UsuarioRecetaService usuarioRecetaService;
@Autowired
private RecetaService recetaService;
public Usuario usuario;
public HorarioComidas findByUsuario(Usuario usuario) {
if (!usuarioRecetaService.findByUsuario(usuario).isEmpty()) {
throw new IllegalStateException("No hay una lista de comida para este usuario.");
}
// Occurs in a single transaction, so each initialized lesson references the same timeslot/room instance
// that is contained by the timeTable's timeslotList/roomList.
return new HorarioComidas(recetaService.findAll(), usuarioRecetaService.findByUsuario(usuario), Arrays.asList(FechaSemana.values()), Arrays.asList(Comida.values()));
}
public void save(HorarioComidas horarioComidas) {
for (UsuarioReceta usuarioReceta : horarioComidas.getUsuarioRecetas()) {
usuarioReceta.setUsuario(usuario);
usuarioRecetaService.create(usuarioReceta);
}
}
}
public void generarListaCompra(Usuario usuario) throws InterruptedException {
optaPlannerService.solve(usuario);
HorarioComidas horarioComidas = optaPlannerService.getTimeTable(usuario);
while (horarioComidas.getSolverStatus() != SolverStatus.NOT_SOLVING) {
// Quick polling (not a Test Thread Sleep anti-pattern)
// Test is still fast on fast machines and doesn't randomly fail on slow machines.
Thread.sleep(20L);
horarioComidas = optaPlannerService.getTimeTable(usuario);
}
}
public class HorarioComidasConstraintProvider implements ConstraintProvider {
@Autowired
private IntoleranciaUsuarioService intoleranciaUsuarioService;
@Autowired
private IntoleranciaRecetaService intoleranciaRecetaService;
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[]{};
}
}
2020-06-04 14:12:42.186 INFO 10130 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener :
Condition evaluation unchanged
2020-06-04 14:21:37.238 INFO 10130 --- [pool-5-thread-1] o.o.core.impl.solver.DefaultSolver :
Solving started: time spent (3), best score (0hard/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
2020-06-04 14:21:37.239 INFO 10130 --- [pool-5-thread-1] o.o.core.impl.solver.DefaultSolver : Skipped all phases (2): out of 0 planning entities, none are movable (non-pinned).
2020-06-04 14:21:37.239 INFO 10130 --- [pool-5-thread-1] o.o.core.impl.solver.DefaultSolver : Solving ended: time spent (4), best score (0hard/0soft), score calculation speed (250/sec), phase total (2), environment mode (REPRODUCIBLE).