OptaPlanner返回空解决方案

问题描述 投票:0回答:1

我试图用OptaPlanner解决一个问题,但程序返回我和空的解决方案。我按照这个例子来解决它。https:/github.comge0ffreygetting-started-guidestreegs-constraint-solving-ai-optaplanner完整的。

我使用的是SpringBoot JPA集成。

这里有实现的代码。

  • 规划方案
@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;
...
}

  • OptaPlanner服务
@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);
    }
}
  • HorarioComidaService。

@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);
        }
    }
}

  • 调用OptaPlanner服务的功能。
    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);
        }
    }
  • Constraint provider, but with no restrictions at the moment. 但目前没有限制。
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).
java spring-data-jpa optaplanner
1个回答
1
投票

我想最好是添加一个简单的约束条件(奖励惩罚),以允许求解器计算分数,这将有助于它猜测是否会有变化,你能不能更新你的帖子,从你的@PlanningSolution规划实体(PlanningEntityCollectionProperty在你的情况下)在解决之前和之后的一些日志? 这将有助于记录分数也。

PS:尝试在保存之前覆盖 "HorarioComidasService::save "来记录。

© www.soinside.com 2019 - 2024. All rights reserved.