用于车辆路线规划的 OptaPlanner
OptaPlanner for 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);
但这会导致无休止的等待解决方案。有什么原因的想法吗?提前致谢。
您尚未配置求解器终止条件。例如,如果您希望求解器求解 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();
我正在尝试使用 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);
但这会导致无休止的等待解决方案。有什么原因的想法吗?提前致谢。
您尚未配置求解器终止条件。例如,如果您希望求解器求解 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();