本文介绍了optaplanners toList ConstraintCollector 有错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 optaplanner 8.1 中使用 toList() ConstraintCollector 时:

When using toList() ConstraintCollector in optaplanner 8.1 like:

factory.from(Lesson.class)
          .groupBy(Lesson::getCourse, ConstraintCollectors.toList()).penalize(...);

我遇到了:

Exception executing consequence for rule "foo" in model: java.lang.ClassCastException: class model.Lesson cannot be cast to class java.util.List (model.Lesson is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
    at org.drools.core.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)

问题:这是 optaplanner 的错误,还是我的代码有误?

我找到的最简单的完整复制器是:

The most simple complete reproducer I find is:

@PlanningSolution
public class OptaplannerIssue2 implements ConstraintProvider {

  @Override
  public Constraint[] defineConstraints(ConstraintFactory factory) {
    return new Constraint[] {factory.from(IssueEntity.class)
        .groupBy(IssueEntity::getValue, ConstraintCollectors.toList())
        .penalize("x", HardSoftScore.ofHard(1), (entity, enityList) -> 2)};
  }

  @PlanningScore
  private HardSoftScore score = HardSoftScore.ZERO;

  @PlanningEntityCollectionProperty
  private final List<IssueEntity> entities = new ArrayList<IssueEntity>();

  public List<IssueEntity> getEntities() {
    return entities;
  }

  @ValueRangeProvider(id = "valueRange")
  public CountableValueRange<Integer> getValueRange() {
    return ValueRangeFactory.createIntValueRange(0, 4);
  }

  public static void main() {
    // create Entity
    OptaplannerIssue2 issue = new OptaplannerIssue2();
    IssueEntity e1 = new IssueEntity();
    issue.entities.add(e1);

    // solve
    SolverFactory<OptaplannerIssue2> solverFactory = SolverFactory.create(new SolverConfig()
        .withEnvironmentMode(EnvironmentMode.FULL_ASSERT).withSolutionClass(OptaplannerIssue2.class)
        .withEntityClasses(IssueEntity.class)
        .withScoreDirectorFactory(
            new ScoreDirectorFactoryConfig().withConstraintProviderClass(OptaplannerIssue2.class))
        .withTerminationConfig(new TerminationConfig().withSecondsSpentLimit(5L)).withPhases(
            new ConstructionHeuristicPhaseConfig()
                .withConstructionHeuristicType(ConstructionHeuristicType.FIRST_FIT),
            new LocalSearchPhaseConfig().withLocalSearchType(LocalSearchType.LATE_ACCEPTANCE)));
    Solver<OptaplannerIssue2> solver = solverFactory.buildSolver();
    solver.solve(issue);
  }
}

具有以下实体类:

@PlanningEntity
public class IssueEntity {

  @PlanningVariable(valueRangeProviderRefs = {"valueRange"})
  Integer value;

  public Integer getValue() {
    return value;
  }
}


在相关线程中:Optaplanner GroupBy with toList not working as expected 提问者没有向评论员提供所有信息试图提供帮助,当我在那里提供复制器时,我被删除了,所以我不得不提出新问题.


In the related thread: Optaplanner GroupBy with toList not working as expected the questioner didn't provide all information to commentators trying to help and when I provided reproducer there I got deleted, so I had to ask new question.

推荐答案

您描述的行为是 OptaPlanner 中的一个错误,我们现已修复.请升级到 OptaPlanner 的下一个版本,在撰写此答案时将是 OptaPlanner 8.2.0.

The behavior you describe is a bug in OptaPlanner, which we have now fixed. Please upgrade to the next release of OptaPlanner, which at the time of writing this answer will be OptaPlanner 8.2.0.

有关详细信息,请参阅 PLANNER-2305.

For details, see PLANNER-2305.

这篇关于optaplanners toList ConstraintCollector 有错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 17:05