首页 > 解决方案 > OptaPlanner:如何在没有 .xml 配置文件的情况下构建求解器?

问题描述

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

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

也就是说,我们不需要其他的xml文件,就可以使用上面的solver来解决问题。所以任何人都可以告诉我我该如何处理它?

非常感谢。

标签: javaoptaplanner

解决方案


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();

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


推荐阅读