您所说的“所有非最佳解决方案”是指特定的非最佳解决方案吗?搜索空间会很快变得非常大,OptaPlanner 本身可能不会评估大多数解决方案(仅仅是因为搜索空间太大)。
如果提供给 Solver 的问题/解决方案已经是最佳解决方案(因为根据定义,没有比它更好的解决方案),BestSolutionChanged 事件将不会再次触发,这是正确的。
特别感兴趣的是ScoreManager,它允许您计算和解释任何问题/解决方案的分数:
(示例取自https://www.optaplanner.org/docs/optaplanner/latest/score-calculation/score-calculation.html#usingScoreCalculationOutsideTheSolver)
要创建它并获取ScoreExplanation,请执行以下操作:
ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory);
ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);
cloudBalance 是您要解释的问题/解决方案。随着
分数说明你可以:
获取分数
HardSoftScore score = scoreExplanation.getScore();
按约束分解分数
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();
...
}
}
并获得单个实体和问题事实的影响:
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();
...
}
}