在开发过程中查看约束分数的最简单方法是打印 explainScore()的返回值,但此方法只能看到大概的信息。
System.out.println(scoreManager.getSummary(solution));
例如,在会议安排中,这意味着Task S51打破了硬约束:Speaker required room tag:
分数说明(-1hard/-806soft):
约束匹配总数:
-1hard:约束(需要发言者的房间标签)有 1 个匹配项:
-1hard:理由([S51])
-340soft:约束(主题轨道冲突)有 32 个匹配项:
-20soft:理由([S68,S66])
-20soft:理由([S61,S44])
...
...
起诉书(72 项中的前 5 项):
-1hard/-22soft:对齐 (S51) 有 12 个匹配项:
-1hard:约束(演讲者需要的房间标签)
-10soft:约束(主题轨道冲突)
...
...
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)
...
...
这些信息都是概略性的信息,如果要查看每个约束的匹配结果及分值,则可以通过ConstraintMatch API进行查看。
6.1。在求解器外使用Solver
如果您的应用程序是webUI,需要呈现一个解决方案的分数和约束匹配信息,请使用ScoreManager API。
ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory);
ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);
获取求解结果的总分值:
HardSoftScore score = scoreExplanation.getScore();
此外,ScoreExplanation可以通过约束匹配总数and/or 来帮助查看分数:
6.2. 约束匹配总数:按约束分解得分
要查看每个约束条件的得分,可以从ScoreExplanation中获得ConstraintMatchTotals。
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 分数计算需要 实现一个额外的接口。
6.3. 规划实体热图
需要在用户界面中显示一个热图,可以查看每个PlanningEntity所匹配到的约束Score信息,突出PlanningEntity和ProblemFact对Score的影响,可以从ScoreExplanation中获取Indictment信息:
Map<Object, Indictment<HardSoftScore>> indictmentMap = scoreExplanation.getIndictmentMap();
for (CloudProcess process : cloudBalance.getProcessList()) {
Indictment<HardSoftScore> indictment = indictmentMap.get(process);
if (indictment == null) {
continue;
}
// The score impact of that planning entity
HardSoftScore totalScore = indictment.getScore();
for (ConstraintMatch<HardSoftScore> constraintMatch : indictment.getConstraintMatchSet()) {
String constraintName = constraintMatch.getConstraintName();
HardSoftScore score = constraintMatch.getScore();
...
}
}
每个Indictment是对象所涉及的所有约束的总和。所有Indictment.getScoreTotal()的总和与总体分数不同,因为多个Indictments可以共享同一个ConstraintMatch。
约束流和Drools 分数计算自动支持约束匹配,但增量 Java 分数计算需要 实现一个额外的接口。