用于车辆路线选择的OptaPlanner

时间:2020-04-21 19:09:25

标签: optaplanner vehicle-routing

我正在尝试使用OptaPlanner解决VRP。我用约束提供者,计划实体和计划解决方案等编写了所有代码。问题是,我试图通过以下方式获取解决方案

        SolverConfig solverConfig = new SolverConfig();
        solverConfig.withSolutionClass(Solution.class);
        solverConfig.withEntityClasses(Stop.class, Standstill.class);
        ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
        scoreDirectorFactoryConfig.setConstraintProviderClass(VrpConstraintProvider.class);

        solverConfig.withScoreDirectorFactory(scoreDirectorFactoryConfig);

        SolverFactory<Solution> solverFactory = SolverFactory.create(solverConfig);
        Solver<Solution> solver = solverFactory.buildSolver();

        Solution solution = solver.solve(solutionDef);

但这会导致无休止的等待解决方案。有什么原因的想法吗?预先感谢。

1 个答案:

答案 0 :(得分:2)

您尚未配置求解器终止条件。例如,如果您希望求解器求解60秒,则添加以下内容:

solverConfig.withTerminationConfig(new TerminationConfig().withSecondsSpentLimit(60L));

请注意,在本示例中,您提供的求解器在主线程上运行并阻塞,直到满足终止条件,并且它自动停止求解。由于您的示例中没有这种条件,因此它将永远解决并阻塞线程。

或者,使用SolverManager API异步(在工作线程上)解决问题:

int problemId = 1;
SolverManager<Solution, Integer> solverManager = SolverManager.create(
        solverConfig, new SolverManagerConfig());
SolverJob<Solution, Integer> solverJob = solverManager.solve(problemId, solutionDef);
// wait some time, then terminate solver manager
solverManager.terminateEarly(problemId);
Solution bestSolution = solverJob.getFinalBestSolution();