2

我正在使用drools-planner-5.4.0.CR1,我想获得最终最佳解决方案的破坏约束列表

并查看了 5.4.0.CR1 的示例

我已经像示例中那样实现了,但它返回空 list,换句话说 getScoreDetailList().size() 等于0,但 solution.getScore() 等于-54

有什么建议吗?

public class SomeClass {
...
private volatile Solver solver;
private ScoreDirector scoreDirector;
...
public void someMethod() {
    SolverFactory solverFactory = new XmlSolverFactory(SOLVER_CONFIG);
    solver = solverFactory.buildSolver();
    scoreDirector = solver.getScoreDirectorFactory().buildScoreDirector();
    ...
    this.scoreDirector.setWorkingSolution(solution);
    this.solver.setPlanningProblem(this.scoreDirector.getWorkingSolution());
    this.solver.solve();

    SomeSolution solution = (SomeSolution) solver.getBestSolution();
    this.scoreDirector.setWorkingSolution(solution);
    System.out.println( "Score: " + solution.getScore());
    System.out.println( "getScoreDetailList size:" + getScoreDetailList().size());
}

public List<ScoreDetail> getScoreDetailList() {
    if (!(scoreDirector instanceof DroolsScoreDirector)) {
        return null;
    }
    Map<String, ScoreDetail> scoreDetailMap = new HashMap<String, ScoreDetail>();
    WorkingMemory workingMemory = ((DroolsScoreDirector) scoreDirector).getWorkingMemory();
    if (workingMemory == null) {
        return Collections.emptyList();
    }
    Iterator<ConstraintOccurrence> it = (Iterator<ConstraintOccurrence>) workingMemory.iterateObjects(
            new ClassObjectFilter(ConstraintOccurrence.class));
    while (it.hasNext()) {
        ConstraintOccurrence constraintOccurrence = it.next();
        ScoreDetail scoreDetail = scoreDetailMap.get(constraintOccurrence.getRuleId());
        if (scoreDetail == null) {
            scoreDetail = new ScoreDetail(constraintOccurrence.getRuleId(), constraintOccurrence.getConstraintType());
            scoreDetailMap.put(constraintOccurrence.getRuleId(), scoreDetail);
        }
        scoreDetail.addConstraintOccurrence(constraintOccurrence);
    }
    List<ScoreDetail> scoreDetailList = new ArrayList<ScoreDetail>(scoreDetailMap.values());
    Collections.sort(scoreDetailList);
    return scoreDetailList;
}
}
4

1 回答 1

2

this.scoreDirector.setWorkingSolution(solution);

你忘了打电话

this.scoreDirector.calculateScore();

我将介绍有关在 5.4.0.Final 中使用 Solver.getScoreDirectorFactory() 的文档。

于 2012-04-21T09:25:28.743 回答