OptaPlanner:如何在没有 .xml 配置文件的情况下构建求解器?

OptaPlanner: How can I build a solver without .xml config file?

默认情况下,我是 OptaPlanner 的初学者,我发现所有求解器都是由 OptaPlanner 中的 xml 配置文件构建的。所以我想知道如何只使用纯 java 代码构建我的求解器,就像

SolverFactory.setScoreCalculator(new NQueensEasyScoreCalculator()).setSearch(new ***()).build();

换句话说,我们不需要其他 xml 文件,可以使用上面的求解器来解决问题。 所以任何人都可以告诉我如何处理它?

非常感谢。

是的,您可以使用 SolverConfig fluent API 构建配置。例如:

SolverConfig solverConfig = new SolverConfig()
        .withEnvironmentMode(EnvironmentMode.REPRODUCIBLE)
        .withSolutionClass(VehicleRoutingSolution.class)
        .withEntityClasses(Standstill.class, PlanningVisit.class)
        .withScoreDirectorFactory(new ScoreDirectorFactoryConfig().withScoreDrls("org/optaweb/vehiclerouting/solver/vehicleRoutingScoreRules.drl"))
        .withTerminationConfig(new TerminationConfig().withSecondsSpentLimit(60L))
        .withPhases(
                new ConstructionHeuristicPhaseConfig().withConstructionHeuristicType(ConstructionHeuristicType.FIRST_FIT_DECREASING),
                new LocalSearchPhaseConfig().withLocalSearchType(LocalSearchType.TABU_SEARCH)
        );

然后您可以使用配置通过 SolverFactory:

构建求解器
SolverFactory<VehicleRoutingSolution> solverFactory = SolverFactory.create(solverConfig);
Solver<VehicleRoutingSolution> solver = solverFactory.buildSolver();

在您的情况下,配置可能更简单。我包含了更多选项来说明可能性。