13.11. 解释分数


有几种方法显示了 OptaPlanner 分数是如何派生的。这称为解释分数:

  • 打印 getSummary () 的返回值。这是在开发期间解释分数的最简单方法,但仅将此方法用于诊断目的。
  • 在应用程序或 Web UI 中使用 ScoreManager API。
  • 划分每个约束的分数,以获得更精细的视图。

流程

  • 使用以下方法之一解释分数:

    • 打印 getSummary () 的返回值:

      System.out.println(scoreManager.getSummary(solution));
      Copy to Clipboard Toggle word wrap

      以下会议调度示例显示,与 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)
                  ...
              ...
      Copy to Clipboard Toggle word wrap
      重要

      不要尝试解析此字符串,或者在 UI 或公开的服务中使用它。反之,使用 ConstraintMatch API。

  • 在应用程序或 Web UI 中使用 ScoreManager API。

    1. 输入类似以下示例的代码:

      ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory);
      ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);
      Copy to Clipboard Toggle word wrap
    2. 当您需要计算解决方案分数时使用此代码:

      HardSoftScore score = scoreExplanation.getScore();
      Copy to Clipboard Toggle word wrap
  • 按约束划分分数:

    1. 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();
              ...
          }
      }
      Copy to Clipboard Toggle word wrap

      每个 ConstraintMatchTotal 代表一个约束,它属于整体分数。所有 ConstraintMatchTotal.getScore () 的总和等于整个分数。

      注意

      约束流和 Drools 分数计算支持约束自动匹配,但增量 Java 分数计算需要实施额外的接口。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat