1

我正在尝试使用 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);

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

4

1 回答 1

2

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

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

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

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

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();
于 2020-04-21T20:40:35.373 回答