# Score calculation

## 1. Score terminology

### 1.1. What is a score?

Every `@planning_solution` class has a score. The score is an objective way to compare two solutions. The solution with the higher score is better. The `Solver` aims to find the solution with the highest `Score` of all possible solutions. The best solution is the solution with the highest `Score` that `Solver` has encountered during solving, which might be the optimal solution.

OptaPy cannot automatically know which solution is best for your business, so you need to tell it how to calculate the score of a given `@planning_solution` instance according to your business needs. If you forget or are unable to implement an important business constraint, the solution is probably useless: ### 1.2. Formalize the business constraints

To implement a verbal business constraint, it needs to be formalized as a score constraint. Luckily, defining constraints in OptaPy is very flexible through the following score techniques:

• Score signum (positive or negative): maximize or minimize a constraint type

• Score weight: put a cost/profit on a constraint type

• Score level (hard, soft, …​): prioritize a group of constraint types

Take the time to acquaint yourself with the first three techniques. Once you understand them, formalizing most business constraints becomes straightforward.

 Do not presume that your business knows all its score constraints in advance. Expect score constraints to be added, changed or removed after the first releases.

### 1.3. Score constraint signum (positive or negative)

All score techniques are based on constraints. A constraint can be a simple pattern (such as Maximize the apple harvest in the solution) or a more complex pattern. A positive constraint is a constraint you want to maximize. A negative constraint is a constraint you want to minimize The image above illustrates that the optimal solution always has the highest score, regardless if the constraints are positive or negative.

Most planning problems have only negative constraints and therefore have a negative score. In that case, the score is the sum of the weight of the negative constraints being broken, with a perfect score of 0. For example in n queens, the score is the negative of the number of queen pairs which can attack each other.

Negative and positive constraints can be combined, even in the same score level.

When a constraint activates (because the negative constraint is broken or the positive constraint is fulfilled) on a certain planning entity set, it is called a constraint match.

### 1.4. Score constraint weight

Not all score constraints are equally important. If breaking one constraint is equally bad as breaking another constraint x times, then those two constraints have a different weight (but they are in the same score level). For example in vehicle routing, you can make one unhappy driver constraint match count as much as two fuel tank usage constraint matches: Score weighting is easy in use cases where you can put a price tag on everything. In that case, the positive constraints maximize revenue and the negative constraints minimize expenses, so together they maximize profit. Alternatively, score weighting is also often used to create social fairness. For example, a nurse, who requests a free day, pays a higher weight on New Years eve than on a normal day.

The weight of a constraint match can depend on the planning entities involved. For example in cloud balancing, the weight of the soft constraint match for an active `Computer` is the maintenance `cost` of that `Computer` (which differs per computer).

Putting a good weight on a constraint is often a difficult analytical decision, because it is about making choices and trade-offs against other constraints. Different stakeholders have different priorities. A non-accurate weight is less damaging than mediocre algorithms: Most use cases use a `Score` with `int` weights, such as HardSoftScore.

### 1.5. Score constraint level (hard, soft, …​)

Sometimes a score constraint outranks another score constraint, no matter how many times the latter is broken. In that case, those score constraints are in different levels. For example, a nurse cannot do two shifts at the same time (due to the constraints of physical reality), so this outranks all nurse happiness constraints.

Most use cases have only two score levels, hard and soft. The levels of two scores are compared lexicographically. The first score level gets compared first. If those differ, the remaining score levels are ignored. For example, a score that breaks `0` hard constraints and `1000000` soft constraints is better than a score that breaks `1` hard constraint and `0` soft constraints. If there are two (or more) score levels, for example HardSoftScore, then a score is feasible if no hard constraints are broken.

 By default, OptaPy will always assign all planning variables a planning value. If there is no feasible solution, this means the best solution will be infeasible. To instead leave some of the planning entities unassigned, apply overconstrained planning.

For each constraint, you need to pick a score level, a score weight and a score signum. For example: `-1soft` which has score level of `soft`, a weight of `1` and a negative signum. Do not use a big constraint weight when your business actually wants different score levels. That hack, known as score folding, is broken: Your business might tell you that your hard constraints all have the same weight, because they cannot be broken (so the weight does not matter). This is not true because if no feasible solution exists for a specific dataset, the least infeasible solution allows the business to estimate how many business resources they are lacking. For example in cloud balancing, how many new computers to buy. Furthermore, it will likely create a score trap. For example in cloud balance if a `Computer` has seven CPU too little for its `Process`es, then it must be weighted seven times as much as if it had only one CPU too little.

Three or more score levels are also supported. For example: a company might decide that profit outranks employee satisfaction (or vice versa), while both are outranked by the constraints of physical reality.

 To model fairness or load balancing, there is no need to use lots of score levels (even though OptaPy can handle many score levels).

Most use cases use a `Score` with two or three weights, such as HardSoftScore and HardMediumSoftScore.

### 1.6. Combining score techniques

All the score techniques mentioned above, can be combined seamlessly: ### 1.7. `Score` interface

A score is represented by the `Score` interface, which naturally extends `Comparable`:

``````class Score:
def compareTo(self, other):
...
...``````

The `Score` implementation to use depends on your use case. Your score might not efficiently fit in a single `long` value. OptaPy has several built-in `Score` implementations, but you can implement a custom `Score` too. Most use cases tend to use the built-in `HardSoftScore`. All Score implementations also have an `initScore` (which is an `int`). It is mostly intended for internal use in OptaPy: it is the negative number of uninitialized planning variables. From a user’s perspective this is `0`, unless a Construction Heuristic is terminated before it could initialize all planning variables (in which case `Score.isSolutionInitialized()` returns `false`).

The `Score` implementation (for example `HardSoftScore`) must be the same throughout a `Solver` runtime. The `Score` implementation is configured in the solution domain class:

``````@planning_solution
class CloudBalance:
...
@planning_score(HardSoftScore)
def get_score(self):
return self.score

def set_score(self, score):
self.score = score``````

### 1.8. Avoid floating point numbers in score calculation

Avoid the use of `float` in score calculation. Use `BigDecimal` or scaled `long` instead.

Floating point numbers cannot represent a decimal number correctly. For example: a `float` cannot hold the value `0.05` correctly. Instead, it holds the nearest representable value. Arithmetic (including addition and subtraction) with floating point numbers, especially for planning problems, leads to incorrect decisions: ``````# prints False
print(f'{((0.01 + 0.02) + 0.03) == (0.01 + (0.02 + 0.03))}')``````

Decimal numbers (`BigDecimal`) have none of these problems.

 BigDecimal arithmetic is considerably slower than `int`, `long` or `double` arithmetic. In experiments we have seen the score calculation take five times longer. Therefore, in many cases, it can be worthwhile to multiply all numbers for a single score weight by a plural of ten, so the score weight fits in a scaled `int` or `long`. For example, if we multiply all weights by `1000`, a fuel_cost of `0.07` becomes a fuel_cost_millis of `70` and no longer uses a decimal score weight.

## 2. Choose a score type

Depending on the number of score levels and type of score weights you need, choose a `Score` type. Most use cases use a `HardSoftScore`. All score types are available in the `optapy.score` package:

``from optapy.score import HardSoftScore``

### 2.1. `SimpleScore`

A `SimpleScore` has a single `int` value, for example `-123`. It has a single score level.

``````    @planning_score(SimpleScore)
def get_score(self):
...``````

### 2.2. `HardSoftScore` (Recommended)

A `HardSoftScore` has a hard `int` value and a soft `int` value, for example `-123hard/-456soft`. It has two score levels (hard and soft).

``````    @planning_score(HardSoftScore)
def get_score(self):
...``````

### 2.3. `HardMediumSoftScore`

A `HardMediumSoftScore` which has a hard `int` value, a medium `int` value and a soft `int` value, for example `-123hard/-456medium/-789soft`. It has three score levels (hard, medium and soft). The hard level determines if the solution is feasible, and the medium level and soft level score values determine how well the solution meets business goals. Higher medium values take precedence over soft values irrespective of the soft value.

``````    @planning_score(HardMediumSoftScore)
def get_score(self):
...``````

### 2.4. `BendableScore`

A `BendableScore` has a configurable number of score levels. It has an array of hard `int` values and an array of soft `int` values, for example with two hard levels and three soft levels, the score can be `[-123/-456]hard/[-789/-012/-345]soft`. In that case, it has five score levels. A solution is feasible if all hard levels are at least zero.

A BendableScore with one hard level and one soft level is equivalent to a HardSoftScore, while a BendableScore with one hard level and two soft levels is equivalent to a HardMediumSoftScore.

``````    @planning_score(BendableScore, bendable_hard_levels_size=2, bendable_soft_levels_size=3)
def get_score(self):
...``````

The number of hard and soft score levels need to be set at compilation time. It is not flexible to change during solving.

 Do not use a `BendableScore` with seven levels just because you have seven constraints. It is extremely rare to use a different score level for each constraint, because that means one constraint match on soft 0 outweighs even a million constraint matches of soft 1. Usually, multiple constraints share the same level and are weighted against each other. Use explaining the score to get the weight of individual constraints in the same level.

## 3. Calculate the `Score`

### 3.1. Score calculation types

There are several ways to calculate the `Score` of a solution:

Every score calculation type can work with any Score definition (such as `HardSoftScore` or `HardMediumSoftScore`). All score calculation types are Object Oriented and can reuse existing Python code.

 The score calculation must be read-only. It must not change the planning entities or the problem facts in any way. For example, it must not call a setter method on a planning entity in the score calculation. OptaPy does not recalculate the score of a solution if it can predict it (unless an environmentMode assertion is enabled). For example, after a winning step is done, there is no need to calculate the score because that move was done and undone earlier. As a result, there is no guarantee that changes applied during score calculation actually happen. To update planning entities when the planning variable change, use shadow variables instead.

### 3.2. Easy Python score calculation

An easy way to implement your score calculation in Python.

• Plain old Python: no learning curve

• Opportunity to delegate score calculation to an existing code base or legacy system

Create a function that takes a solution and return a score, and decorate it with `@easy_score_calculator`:

``````from optapy import easy_score_calculator

@easy_score_calculator
def fun(solution: SolutionType) -> Score:
...``````

For example in n queens:

``````from optapy import easy_score_calculator
from optapy.score import SimpleScore

@easy_score_calculator
def n_queens_easy_score_calculator(n_queens: NQueens) -> SimpleScore:
n = n_queens.get_n()
queen_list = n_queens.get_queen_list()

score = 0
for i in range(n):
for j in range(i + 1, n):
left_queen = queen_list[i]
right_queen = queen_list[j]
if left_queen.row is not None and right_queen.row is not None:
if left_queen.row_index == right_queen.row_index:
score -= 1
if left_queen.get_ascending_diagonal_index() == right_queen.get_ascending_diagonal_index():
score -= 1
if left_queen.get_descending_diagonal_index() == right_queen.get_descending_diagonal_index():
score -= 1
return SimpleScore.valueOf(score)``````

Configure it in the solver configuration:

``````  <scoreDirectorFactory>
<easyScoreCalculatorClass>n_queens_easy_score_calculator</easyScoreCalculatorClass>
</scoreDirectorFactory>``````

### 3.3. Incremental Python score calculation

A way to implement your score calculation incrementally in Java.

• Very fast and scalable

• Currently the fastest if implemented correctly

• Hard to write

• A scalable implementation heavily uses maps, indexes, …​ (things Constraint Streams can do for you)

• You have to learn, design, write and improve all these performance optimizations yourself

• Regular score constraint changes can lead to a high maintenance cost

Implement all the methods of the interface `IncrementalScoreCalculator`:

``````class IncrementalScoreCalculator:
def resetWorkingSolution(self, working_solution: SolutionType) -> None:
...

...

...

def beforeVariableChanged(self, entity, variable_name: str) -> None:
...

def afterVariableChanged(self, entity, variable_name: str) -> None:
...

def beforeEntityRemoved(self, entity) -> None:
...

def afterEntityRemoved(self, entity) -> None:
...

def calculateScore(self) -> Score:
...

}`````` For example in n queens:

``````from optapy import incremental_score_calculator
from optapy.score import SimpleScore

@incremental_score_calculator
class NQueensIncrementalScoreCalculator:
score: int
row_index_map: dict
ascending_diagonal_index_map: dict
descending_diagonal_index_map: dict

def resetWorkingSolution(self, working_solution: Solution):
n = working_solution.n
self.row_index_map = dict()
self.ascending_diagonal_index_map = dict()
self.descending_diagonal_index_map = dict()
for i in range(n):
self.row_index_map[i] = list()
self.ascending_diagonal_index_map[i] = list()
self.descending_diagonal_index_map[i] = list()
if i != 0:
self.ascending_diagonal_index_map[n - 1 + i] = list()
self.descending_diagonal_index_map[-i] = list()
self.score = 0
for queen in working_solution.queen_list:
self.insert(queen)

pass

self.insert(entity)

def beforeVariableChanged(self, entity: any, variable_name: str):
self.retract(entity)

def afterVariableChanged(self, entity: any, variable_name: str):
self.insert(entity)

def beforeEntityRemoved(self, entity: any):
self.retract(entity)

def afterEntityRemoved(self, entity: any):
pass

def insert(self, queen: Queen):
row = queen.row
if row is not None:
row_index = queen.row
row_index_list = self.row_index_map[row_index]
self.score -= len(row_index_list)
row_index_list.append(queen)
ascending_diagonal_index_list = self.ascending_diagonal_index_map[queen.getAscendingDiagonalIndex()]
self.score -= len(ascending_diagonal_index_list)
ascending_diagonal_index_list.append(queen)
descending_diagonal_index_list = self.descending_diagonal_index_map[queen.getDescendingDiagonalIndex()]
self.score -= len(descending_diagonal_index_list)
descending_diagonal_index_list.append(queen)

def retract(self, queen: Queen):
row = queen.row
if row is not None:
row_index = queen.row
row_index_list = self.row_index_map[row_index]
row_index_list.remove(queen)
self.score += len(row_index_list)
ascending_diagonal_index_list = self.ascending_diagonal_index_map[queen.getAscendingDiagonalIndex()]
ascending_diagonal_index_list.remove(queen)
self.score += len(ascending_diagonal_index_list)
descending_diagonal_index_list = self.descending_diagonal_index_map[queen.getDescendingDiagonalIndex()]
descending_diagonal_index_list.remove(queen)
self.score += len(descending_diagonal_index_list)

def calculateScore(self) -> SimpleScore:
return SimpleScore.of(self.score)``````

Configure it in the solver configuration:

``````  <scoreDirectorFactory>
<incrementalScoreCalculatorClass>NQueensIncrementalScoreCalculator</incrementalScoreCalculatorClass>
</scoreDirectorFactory>``````
 A piece of incremental score calculator code can be difficult to write and to review. Assert its correctness by using an `EasyScoreCalculator` to fulfill the assertions triggered by the `environmentMode`.

#### 3.3.1. `ConstraintMatchAwareIncrementalScoreCalculator`

Optionally, also implement the `ConstraintMatchAwareIncrementalScoreCalculator` interface to:

• Explain a score by splitting it up per score constraint with `ScoreExplanation.getConstraintMatchTotalMap()`.

• Visualize or sort planning entities by how many constraints each one breaks with `ScoreExplanation.getIndictmentMap()`.

• Receive a detailed analysis if the `IncrementalScoreCalculator` is corrupted in `FAST_ASSERT` or `FULL_ASSERT` `environmentMode`,

``````from optapy.constraint import DefaultConstraintMatchTotal

class ConstraintMatchAwareIncrementalScoreCalculator:

def resetWorkingSolution(self, working_solution: SolutionType, constraintMatchEnabled: bool) -> None:
...

def getConstraintMatchTotals(self) -> list[DefaultConstraintMatchTotal]:
...

def getIndictmentMap(self) -> dict | None:
return None # Calculate it non-incrementally from getConstraintMatchTotals()``````

For example in machine reassignment, create one `ConstraintMatchTotal` per constraint type and call `addConstraintMatch()` for each constraint match:

``````from optapy import incremental_score_calculator
from optapy.score import HardSoftScore
from optapy.constraint import DefaultConstraintMatchTotal

@incremental_score_calculator
class MachineReassignmentIncrementalScoreCalculator:
...
def resetWorkingSolution(self, working_solution: MachineReassignment, constraint_match_enabled: bool) {
# code to reset working solution
...
# ignore constraintMatchEnabled, it is always presumed enabled

def getConstraintMatchTotals(self):
maximum_capacity_match_total = DefaultConstraintMatchTotal('MachineReassignment',
"maximumCapacity", HardSoftScore.ZERO)
...
for (machine, machine_score_part) in self.machine_score_part_map.items():
for machine_capacity_score_part in machine_score_part.machine_capacity_score_part_list:
if (machine_capacity_score_part.maximum_available < 0) {
[machine_capacity_score_part.machine_capacity),
HardSoftScore.valueOf(machine_capacity_score_part.maximum_available, 0))
}
}
}
...
return [
maximum_capacity_match_total,
...
]

def get_indictment_map(self):
# Calculate it non-incrementally from getConstraintMatchTotals()
return None``````

That `getConstraintMatchTotals()` code often duplicates some of the logic of the normal `IncrementalScoreCalculator` methods. Constraint Streams don’t have this disadvantage, because they are constraint match aware automatically when needed, without any extra domain-specific code.

### 3.4. `InitializingScoreTrend`

The `InitializingScoreTrend` specifies how the Score will change as more and more variables are initialized (while the already initialized variables do not change). Some optimization algorithms (such Construction Heuristics and Exhaustive Search) run faster if they have such information.

For the Score (or each score level separately), specify a trend:

• `ANY` (default): Initializing an extra variable can change the score positively or negatively. Gives no performance gain.

• `ONLY_UP` (rare): Initializing an extra variable can only change the score positively. Implies that:

• There are only positive constraints

• And initializing the next variable cannot unmatch a positive constraint that was matched by a previous initialized variable.

• `ONLY_DOWN`: Initializing an extra variable can only change the score negatively. Implies that:

• There are only negative constraints

• And initializing the next variable cannot unmatch a negative constraint that was matched by a previous initialized variable.

Most use cases only have negative constraints. Many of those have an `InitializingScoreTrend` that only goes down:

``````  <scoreDirectorFactory>
<constraintProviderClass>optapy.examples.cloudbalancing.score.CloudBalancingConstraintProvider</constraintProviderClass>
<initializingScoreTrend>ONLY_DOWN</initializingScoreTrend>
</scoreDirectorFactory>``````

Alternatively, you can also specify the trend for each score level separately:

``````  <scoreDirectorFactory>
<constraintProviderClass>optapy.examples.cloudbalancing.score.CloudBalancingConstraintProvider</constraintProviderClass>
<initializingScoreTrend>ONLY_DOWN/ONLY_DOWN</initializingScoreTrend>
</scoreDirectorFactory>``````

### 3.5. Invalid score detection

When you put the `environmentMode` in `FULL_ASSERT` (or `FAST_ASSERT`), it will detect score corruption in the incremental score calculation. However, that will not verify that your score calculator actually implements your score constraints as your business desires. For example, one constraint might consistently match the wrong pattern. To verify the constraints against an independent implementation, configure a `assertionScoreDirectorFactory`:

``````  <environmentMode>FAST_ASSERT</environmentMode>
...
<scoreDirectorFactory>
<constraintProviderClass>optapy.examples.nqueens.score.n_queens_constraint_provider</constraintProviderClass>
<assertionScoreDirectorFactory>
<easyScoreCalculatorClass>optapy.examples.nqueens.score.n_queens_easy_score_calculator</easyScoreCalculatorClass>
</assertionScoreDirectorFactory>
</scoreDirectorFactory>``````

This way, the `n_queens_constraint_provider` implementation is validated by the `EasyScoreCalculator`.

## 4. Score calculation performance tricks

### 4.1. Overview

The `Solver` will normally spend most of its execution time running the score calculation (which is called in its deepest loops). Faster score calculation will return the same solution in less time with the same algorithm, which normally means a better solution in equal time.

### 4.2. Score calculation speed

After solving a problem, the `Solver` will log the score calculation speed per second. This is a good measurement of Score calculation performance, despite that it is affected by non score calculation execution time. It depends on the problem scale of the problem dataset. Normally, even for high scale problems, it is higher than `1`, except if you are using an `EasyScoreCalculator`.

 When improving your score calculation, focus on maximizing the score calculation speed, instead of maximizing the best score. A big improvement in score calculation can sometimes yield little or no best score improvement, for example when the algorithm is stuck in a local or global optima. If you are watching the calculation speed instead, score calculation improvements are far more visible. Furthermore, watching the calculation speed allows you to remove or add score constraints, and still compare it with the original’s calculation speed. Comparing the best score with the original’s best score is pointless: it’s comparing apples and oranges.

### 4.3. Incremental score calculation (with deltas)

When a solution changes, incremental score calculation (AKA delta based score calculation) calculates the delta with the previous state to find the new `Score`, instead of recalculating the entire score on every solution evaluation.

For example, when a single queen A moves from row `1` to `2`, it will not bother to check if queen B and C can attack each other, since neither of them changed: Similarly in employee rostering: This is a huge performance and scalability gain. Constraint Streams give you this huge scalability gain without forcing you to write a complicated incremental score calculation algorithm. Just let the rule engine do the hard work.

Notice that the speedup is relative to the size of your planning problem (your n), making incremental score calculation far more scalable.

### 4.4. Avoid calling remote services during score calculation

Do not call remote services in your score calculation (except if you are bridging `EasyScoreCalculator` to a legacy system). The network latency will kill your score calculation performance. Cache the results of those remote services if possible.

If some parts of a constraint can be calculated once, when the `Solver` starts, and never change during solving, then turn them into cached problem facts.

### 4.5. Pointless constraints

If you know a certain constraint can never be broken (or it is always broken), do not write a score constraint for it. For example in n queens, the score calculation does not check if multiple queens occupy the same column, because a `Queen`'s `column` never changes and every solution starts with each `Queen` on a different `column`.

 Do not go overboard with this. If some datasets do not use a specific constraint but others do, just return out of the constraint as soon as you can. There is no need to dynamically change your score calculation based on the dataset.

### 4.6. Built-in hard constraint

Instead of implementing a hard constraint, it can sometimes be built in. For example, if `Lecture` A should never be assigned to `Room` X, but it uses `@value_range_provider` on Solution, so the `Solver` will often try to assign it to `Room` X too (only to find out that it breaks a hard constraint). Use a ValueRangeProvider on the planning entity or filtered selection to define that Course A should only be assigned a `Room` different than X.

This can give a good performance gain in some use cases, not just because the score calculation is faster, but mainly because most optimization algorithms will spend less time evaluating infeasible solutions. However, usually this is not a good idea because there is a real risk of trading short term benefits for long term harm:

• Many optimization algorithms rely on the freedom to break hard constraints when changing planning entities, to get out of local optima.

• Both implementation approaches have limitations (feature compatibility, disabling automatic performance optimizations), as explained in their documentation.

### 4.7. Other score calculation performance tricks

• Verify that your score calculation happens in the correct `Number` type. If you are making the sum of `int` values, do not sum it in a `float` which takes longer.

• For optimal performance, set the `JAVA_HOME` environment variable to the latest JDK. For example, in the past we have seen performance increases of 30% by switching from java 1.5 to 1.6.

• Always remember that premature optimization is the root of all evil. Make sure your design is flexible enough to allow configuration based tweaking.

### 4.8. Score trap

Make sure that none of your score constraints cause a score trap. A trapped score constraint uses the same weight for different constraint matches, when it could just as easily use a different weight. It effectively lumps its constraint matches together, which creates a flatlined score function for that constraint. This can cause a solution state in which several moves need to be done to resolve or lower the weight of that single constraint. Some examples of score traps:

• You need two doctors at each table, but you are only moving one doctor at a time. So the solver has no incentive to move a doctor to a table with no doctors. Punish a table with no doctors more than a table with only one doctor in that score constraint in the score function.

• Two exams need to be conducted at the same time, but you are only moving one exam at a time. So the solver has to move one of those exams to another timeslot without moving the other in the same move. Add a coarse-grained move that moves both exams at the same time.

For example, consider this score trap. If the blue item moves from an overloaded computer to an empty computer, the hard score should improve. The trapped score implementation fails to do that: The Solver should eventually get out of this trap, but it will take a lot of effort (especially if there are even more processes on the overloaded computer). Before they do that, they might actually start moving more processes into that overloaded computer, as there is no penalty for doing so.

 Avoiding score traps does not mean that your score function should be smart enough to avoid local optima. Leave it to the optimization algorithms to deal with the local optima. Avoiding score traps means to avoid, for each score constraint individually, a flatlined score function.
 Always specify the degree of infeasibility. The business will often say "if the solution is infeasible, it does not matter how infeasible it is." While that is true for the business, it is not true for score calculation as it benefits from knowing how infeasible it is. In practice, soft constraints usually do this naturally and it is just a matter of doing it for the hard constraints too.

There are several ways to deal with a score trap:

• Improve the score constraint to make a distinction in the score weight. For example, penalize `-1hard` for every missing CPU, instead of just `-1hard` if any CPU is missing.

• If changing the score constraint is not allowed from the business perspective, add a lower score level with a score constraint that makes such a distinction. For example, penalize `-1subsoft` for every missing CPU, on top of `-1hard` if any CPU is missing. The business ignores the subsoft score level.

• Add coarse-grained moves and union select them with the existing fine-grained moves. A coarse-grained move effectively does multiple moves to directly get out of a score trap with a single move. For example, move multiple items from the same container to another container.

### 4.9. Fairness score constraints

Some use cases have a business requirement to provide a fair schedule (usually as a soft score constraint), for example:

• Fairly distribute the workload amongst the employees, to avoid envy.

• Evenly distribute the workload amongst assets, to improve reliability.

Implementing such a constraint can seem difficult (especially because there are different ways to formalize fairness), but usually the squared workload implementation behaves most desirable. For each employee/asset, count the workload `w` and subtract `w²` from the score. As shown above, the squared workload implementation guarantees that if you select two employees from a given solution and make their distribution between those two employees fairer, then the resulting new solution will have a better overall score. Do not just use the difference from the average workload, as that can lead to unfairness, as demonstrated below. Instead of the squared workload, it is also possible to use the variance (squared difference to the average) or the standard deviation (square root of the variance). This has no effect on the score comparison, because the average will not change during planning. It is just more work to implement (because the average needs to be known) and trivially slower (because the calculation is a bit longer).

When the workload is perfectly balanced, the user often likes to see a `0` score, instead of the distracting `-34soft` in the image above (for the last solution which is almost perfectly balanced). To nullify this, either add the average multiplied by the number of entities to the score or instead show the variance or standard deviation in the UI.

## 5. Explaining the score: which constraints are broken?

The easiest way to explain the score during development is to print the return value of `explainScore()`, but only use that method for diagnostic purposes:

``print(score_manager.explainScore(solution))``

For example in conference scheduling, this prints that talk `S51` is responsible for breaking the hard constraint `Speaker 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)
...
...```
 Do not attempt to parse this string or use it in your UI or exposed services. Instead use the ConstraintMatch API below and do it properly.

### 5.1. Using score calculation outside the `Solver`

If other parts of your application, for example your web UI, need to calculate the score of a solution, use the `ScoreManager` API:

``````from optapy import score_manager_create

score_manager = score_manager_create(solver_factory)
score_explanation = score_manager.explain(cloud_balance)``````

Then use it when you need to calculate the `Score` of a solution:

``score = score_explanation.getScore();``

Furthermore, the `ScoreExplanation` can help explain the score through constraint match totals and/or indictments: ### 5.2. Constraint match total: break down the score by constraint

To break down the score per constraint, get the `ConstraintMatchTotal`s from the `ScoreExplanation`:

``````constraint_match_totals = score_explanation.getConstraintMatchTotalMap().values()
for constraint_match_total in constraint_match_totals:
constraint_name = constraint_match_total.getConstraintName()
# The score impact of that constraint
total_score = constraint_match_total.getScore()

for constraint_match in constraint_match_total.getConstraintMatchSet():
justification_list = constraint_match.getJustificationList()
score = constraint_match.getScore()
...``````

Each `ConstraintMatchTotal` represents one constraint and has a part of the overall score. The sum of all the `ConstraintMatchTotal.getScore()` equals the overall score.

 Constraint streams supports constraint matches automatically, but incremental Python score calculation requires adding additional methods.

### 5.3. Indictment heat map: visualize the hot planning entities

To show a heat map in the UI that highlights the planning entities and problem facts have an impact on the `Score`, get the `Indictment` map from the `ScoreExplanation`:

``````indictment_map = score_explanation.getIndictmentMap()
for process in cloud_balance.process_list:
indictment = indictment_map.get(process);
if indictment is None:
continue
# The score impact of that planning entity
total_score = indictment.getScore()

for constraint_match in indictment.getConstraintMatchSet():
constraint_name = constraint_match.getConstraintName()
score = constraint_match.getScore()
...``````

Each `Indictment` is the sum of all constraints where that justification object is involved with. The sum of all the `Indictment.getScoreTotal()` differs from the overall score, because multiple `Indictment`s can share the same `ConstraintMatch`.

 Constraint streams supports indictments automatically, but incremental Python score calculation requires adding additional methods.