如何在OptaPlanner中以编程方式配置移动选择过滤器?
编程方式配置OptaPlanner移动选择过滤器(SpringBoot环境)
核心思路
无需XML配置,直接通过Java API给OptaPlanner的移动选择器绑定自定义过滤器,过滤掉不符合业务规则的移动操作,提升求解效率。
步骤1:实现自定义移动过滤器
创建类实现MoveFilter接口,在accept方法中编写过滤逻辑:
import org.optaplanner.core.impl.heuristic.move.Move; import org.optaplanner.core.impl.heuristic.selector.move.filter.MoveFilter; import org.optaplanner.core.impl.score.director.ScoreDirector; public class CustomMoveFilter implements MoveFilter<YourPlanningSolution> { @Override public boolean accept(ScoreDirector<YourPlanningSolution> scoreDirector, Move<YourPlanningSolution> move) { // 示例:禁止已锁定的规划实体执行移动操作 if (move instanceof ChangeMove) { ChangeMove<YourPlanningSolution> changeMove = (ChangeMove<YourPlanningSolution>) move; YourPlanningEntity entity = (YourPlanningEntity) changeMove.getEntity(); return !entity.isLocked(); } return true; // 其他移动默认允许 } }
步骤2:在Solver配置中绑定过滤器
方式一:直接构建SolverConfig
适用于手动创建Solver的场景:
import org.optaplanner.core.config.solver.SolverConfig; import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig; import org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig; import org.optaplanner.core.config.heuristic.selector.move.MoveSelectorConfig; import org.optaplanner.core.config.heuristic.selector.move.filter.MoveFilterConfig; import org.optaplanner.core.config.solver.termination.TerminationConfig; // 构建Solver配置 SolverConfig solverConfig = new SolverConfig() .withSolutionClass(YourPlanningSolution.class) .withEntityClasses(YourPlanningEntity.class) // 配置终止条件(示例:运行60秒后停止) .withTerminationConfig(new TerminationConfig().withSecondsSpentLimit(60L)) // 给局部搜索阶段绑定移动过滤器 .withPhaseList(List.of( new ConstructionHeuristicPhaseConfig(), // 构造启发式阶段 new LocalSearchPhaseConfig() .withMoveSelectorConfig( new MoveSelectorConfig() .withMoveFilterConfig( new MoveFilterConfig().withFilterClass(CustomMoveFilter.class) ) ) )); // 创建Solver并使用 SolverFactory<YourPlanningSolution> solverFactory = SolverFactory.create(solverConfig); Solver<YourPlanningSolution> solver = solverFactory.buildSolver();
方式二:SpringBoot自动配置扩展
如果使用SpringBoot的OptaPlanner自动配置,通过SolverConfigurer自定义配置:
import org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig; import org.optaplanner.core.config.heuristic.selector.move.MoveSelectorConfig; import org.optaplanner.core.config.heuristic.selector.move.filter.MoveFilterConfig; import org.optaplanner.spring.boot.autoconfigure.SolverConfigurer; import org.springframework.context.annotation.Configuration; @Configuration public class OptaPlannerCustomConfig { @Bean public SolverConfigurer solverConfigurer() { return solverConfig -> { // 遍历所有求解阶段,给局部搜索阶段添加过滤器 solverConfig.getPhaseList().forEach(phaseConfig -> { if (phaseConfig instanceof LocalSearchPhaseConfig) { LocalSearchPhaseConfig localPhaseConfig = (LocalSearchPhaseConfig) phaseConfig; MoveSelectorConfig moveSelectorConfig = localPhaseConfig.getMoveSelectorConfig(); if (moveSelectorConfig == null) { moveSelectorConfig = new MoveSelectorConfig(); localPhaseConfig.setMoveSelectorConfig(moveSelectorConfig); } moveSelectorConfig.setMoveFilterConfig( new MoveFilterConfig().withFilterClass(CustomMoveFilter.class) ); } }); }; } }
额外说明
- 可以针对不同阶段(构造启发式、局部搜索)分别配置过滤器,也可以全局统一配置
- 若需要多个过滤规则,可使用
CompositeMoveFilter组合多个过滤器 - 过滤逻辑需根据业务场景调整,核心是判断移动操作是否符合规则,返回
true允许执行,false则过滤
内容的提问来源于stack exchange,提问作者Justin Phillips
相关产品推荐
相关产品推荐

