java.lang.Object
ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore
All Implemented Interfaces:
Score<HardSoftScore>, Serializable, Comparable<HardSoftScore>

public final class HardSoftScore extends Object implements Score<HardSoftScore>
This Score is based on 2 levels of int constraints: hard and soft. Hard constraints have priority over soft constraints. Hard constraints determine feasibility.

This class is immutable.

See Also:
  • Field Details

  • Method Details

    • parseScore

      public static @NonNull HardSoftScore parseScore(@NonNull String scoreString)
    • ofUninitialized

      public static @NonNull HardSoftScore ofUninitialized(int initScore, int hardScore, int softScore)
    • of

      public static @NonNull HardSoftScore of(int hardScore, int softScore)
    • ofHard

      public static @NonNull HardSoftScore ofHard(int hardScore)
    • ofSoft

      public static @NonNull HardSoftScore ofSoft(int softScore)
    • initScore

      public int initScore()
      Description copied from interface: Score
      The init score is the negative of the number of genuine planning variables set to null, unless null values are specifically allowed by PlanningVariable.allowsUnassigned() or PlanningListVariable.allowsUnassignedValues() Nulls are typically only allowed in over-constrained planning. In that case, there is no way how to tell a fully initialized solution with some values left unassigned, from a partially initialized solution where the initialization of some values wasn't yet attempted.

      During Comparable.compareTo(Object), init score is considered more important than the hard score. If the init score is 0 (which it usually is), the score's Object.toString() does not mention it.

      Specified by:
      initScore in interface Score<HardSoftScore>
      Returns:
      higher is better, always negative (except in statistical calculations); 0 if all planning variables are non-null, or if nulls are allowed.
    • hardScore

      public int hardScore()
      The total of the broken negative hard constraints and fulfilled positive hard constraints. Their weight is included in the total. The hard score is usually a negative number because most use cases only have negative constraints.
      Returns:
      higher is better, usually negative, 0 if no hard constraints are broken/fulfilled
    • getHardScore

      @Deprecated(forRemoval=true) public int getHardScore()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use hardScore() instead.
      As defined by hardScore().
    • softScore

      public int softScore()
      The total of the broken negative soft constraints and fulfilled positive soft constraints. Their weight is included in the total. The soft score is usually a negative number because most use cases only have negative constraints.

      In a normal score comparison, the soft score is irrelevant if the 2 scores don't have the same hard score.

      Returns:
      higher is better, usually negative, 0 if no soft constraints are broken/fulfilled
    • getSoftScore

      @Deprecated(forRemoval=true) public int getSoftScore()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use softScore() instead.
      As defined by softScore().
    • withInitScore

      public @NonNull HardSoftScore withInitScore(int newInitScore)
      Description copied from interface: Score
      For example 0hard/-8soft with -7 returns -7init/0hard/-8soft.
      Specified by:
      withInitScore in interface Score<HardSoftScore>
      Parameters:
      newInitScore - always negative (except in statistical calculations), 0 if all planning variables are initialized
      Returns:
      equals score except that Score.initScore() is set to newInitScore
    • isFeasible

      public boolean isFeasible()
      Description copied from interface: Score
      A PlanningSolution is feasible if it has no broken hard constraints and Score.isSolutionInitialized() is true. Simple scores (SimpleScore, SimpleLongScore, SimpleBigDecimalScore) are always feasible, if their Score.initScore() is 0.
      Specified by:
      isFeasible in interface Score<HardSoftScore>
      Returns:
      true if the hard score is 0 or higher and the Score.initScore() is 0.
    • add

      public @NonNull HardSoftScore add(@NonNull HardSoftScore addend)
      Description copied from interface: Score
      Returns a Score whose value is (this + addend).
      Specified by:
      add in interface Score<HardSoftScore>
      Parameters:
      addend - value to be added to this Score
      Returns:
      this + addend
    • subtract

      public @NonNull HardSoftScore subtract(@NonNull HardSoftScore subtrahend)
      Description copied from interface: Score
      Returns a Score whose value is (this - subtrahend).
      Specified by:
      subtract in interface Score<HardSoftScore>
      Parameters:
      subtrahend - value to be subtracted from this Score
      Returns:
      this - subtrahend, rounded as necessary
    • multiply

      public @NonNull HardSoftScore multiply(double multiplicand)
      Description copied from interface: Score
      Returns a Score whose value is (this * multiplicand). When rounding is needed, it should be floored (as defined by Math.floor(double)).

      If the implementation has a scale/precision, then the unspecified scale/precision of the double multiplicand should have no impact on the returned scale/precision.

      Specified by:
      multiply in interface Score<HardSoftScore>
      Parameters:
      multiplicand - value to be multiplied by this Score.
      Returns:
      this * multiplicand
    • divide

      public @NonNull HardSoftScore divide(double divisor)
      Description copied from interface: Score
      Returns a Score whose value is (this / divisor). When rounding is needed, it should be floored (as defined by Math.floor(double)).

      If the implementation has a scale/precision, then the unspecified scale/precision of the double divisor should have no impact on the returned scale/precision.

      Specified by:
      divide in interface Score<HardSoftScore>
      Parameters:
      divisor - value by which this Score is to be divided
      Returns:
      this / divisor
    • power

      public @NonNull HardSoftScore power(double exponent)
      Description copied from interface: Score
      Returns a Score whose value is (this ^ exponent). When rounding is needed, it should be floored (as defined by Math.floor(double)).

      If the implementation has a scale/precision, then the unspecified scale/precision of the double exponent should have no impact on the returned scale/precision.

      Specified by:
      power in interface Score<HardSoftScore>
      Parameters:
      exponent - value by which this Score is to be powered
      Returns:
      this ^ exponent
    • abs

      public @NonNull HardSoftScore abs()
      Description copied from interface: Score
      Returns a Score whose value is the absolute value of the score, i.e. |this|.
      Specified by:
      abs in interface Score<HardSoftScore>
    • zero

      public @NonNull HardSoftScore zero()
      Description copied from interface: Score
      Returns a Score, all levels of which are zero.
      Specified by:
      zero in interface Score<HardSoftScore>
    • toLevelNumbers

      public Number @NonNull [] toLevelNumbers()
      Description copied from interface: Score
      Returns an array of numbers representing the Score. Each number represents 1 score level. A greater score level uses a lower array index than a lesser score level.

      When rounding is needed, each rounding should be floored (as defined by Math.floor(double)). The length of the returned array must be stable for a specific Score implementation.

      For example: -0hard/-7soft returns new int{-0, -7}

      The level numbers do not contain the Score.initScore(). For example: -3init/-0hard/-7soft also returns new int{-0, -7}

      Specified by:
      toLevelNumbers in interface Score<HardSoftScore>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(@NonNull HardSoftScore other)
      Specified by:
      compareTo in interface Comparable<HardSoftScore>
    • toShortString

      public @NonNull String toShortString()
      Description copied from interface: Score
      Like Object.toString(), but trims score levels which have a zero weight. For example 0hard/-258soft returns -258soft.

      Do not use this format to persist information as text, use Object.toString() instead, so it can be parsed reliably.

      Specified by:
      toShortString in interface Score<HardSoftScore>
    • toString

      public String toString()
      Overrides:
      toString in class Object