13.11. スコアの説明

OptaPlanner スコアがどのように生成されるかを示す方法はいくつかあります。これをスコアの説明といいます。

  • getSummary() の戻り値を出力します。これは開発中にスコアを説明する最も簡単な方法ですが、この方法は診断目的でのみ使用します。
  • アプリケーションまたは Web UI で ScoreManager API を使用します。
  • より詳細なビューを得るために、各制約のスコアを分析します。

    スコアの視覚化の図

手順

  • スコアを説明するには、次のいずれかの方法を使用します。

    • getSummary() の戻り値を出力します。

      System.out.println(scoreManager.getSummary(solution));

      次の会議スケジュールの例では、会議 S51Speaker required room tag というハード制約を破る原因であることを出力します。

      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 を使用します。

    1. 次の例のようなコードを入力します。

      ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory);
      ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);
    2. ソリューションのスコアを計算する必要がある場合は、このコードを使用します。

      HardSoftScore score = scoreExplanation.getScore();
  • スコアを制約ごとに分類します。

    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();
              ...
          }
      }

      ConstraintMatchTotal は 1 つの制約を表し、全体のスコアの一部です。すべての ConstraintMatchTotal.getScore() の合計が全体のスコアと等しくなります。

      注記

      制約ストリームと Drools スコア計算は制約一致を自動的にサポートしますが、Java インクリメント演算子によるスコア計算には追加のインターフェイスを実装する必要があります。