13.11. 解释分数
有几种方法显示了 OptaPlanner 分数是如何派生的。这称为解释分数:
-
打印
getSummary ()
的返回值。这是在开发期间解释分数的最简单方法,但仅将此方法用于诊断目的。 -
在应用程序或 Web UI 中使用
ScoreManager
API。 划分每个约束的分数,以获得更精细的视图。
流程
使用以下方法之一解释分数:
打印
getSummary ()
的返回值:System.out.println(scoreManager.getSummary(solution));
以下会议调度示例显示,与
S51
通信负责破坏硬约束:Explanation of score (-1hard/-806soft): Constraint match totals: -1hard: constraint (Speaker required room tag) has 1 matches: -1hard: justifications ([S51]) -340soft: constraint (Theme track conflict) has 32 matches: -20soft: justifications ([S68, S66]) -20soft: justifications ([S61, S44]) ... ... Indictments (top 5 of 72): -1hard/-22soft: justification (S51) has 12 matches: -1hard: constraint (Speaker required room tag) -10soft: constraint (Theme track conflict) ... ...
重要不要尝试解析此字符串,或者在 UI 或公开的服务中使用它。反之,使用 ConstraintMatch API。
在应用程序或 Web UI 中使用
ScoreManager
API。输入类似以下示例的代码:
ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory); ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);
当您需要计算解决方案分数时使用此代码:
HardSoftScore score = scoreExplanation.getScore();
按约束划分分数:
从
ScoreExplanation
中获取ConstraintMatchTotal
值:Collection<ConstraintMatchTotal<HardSoftScore>> constraintMatchTotals = scoreExplanation.getConstraintMatchTotalMap().values(); for (ConstraintMatchTotal<HardSoftScore> constraintMatchTotal : constraintMatchTotals) { String constraintName = constraintMatchTotal.getConstraintName(); // The score impact of that constraint HardSoftScore totalScore = constraintMatchTotal.getScore(); for (ConstraintMatch<HardSoftScore> constraintMatch : constraintMatchTotal.getConstraintMatchSet()) { List<Object> justificationList = constraintMatch.getJustificationList(); HardSoftScore score = constraintMatch.getScore(); ... } }
每个
ConstraintMatchTotal
代表一个约束,它属于整体分数。所有ConstraintMatchTotal.getScore
() 的总和等于整个分数。注意约束流和 Drools 分数计算支持约束自动匹配,但增量 Java 分数计算需要实施额外的接口。