获取drools-planner中最终最佳解决方案的破坏约束列表

时间:2012-04-19 05:53:51

标签: drools-planner

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

还查看了5.4.0.CR1

的示例

我在示例中实现了但它返回空列表,换句话说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;
}
}

1 个答案:

答案 0 :(得分:2)

this.scoreDirector.setWorkingSolution(solution);

你忘了打电话了

this.scoreDirector.calculateScore();

我将在5.4.0.Final中使用Solver.getScoreDirectorFactory()。