# 3. Considerations Actions and Compensation
Part of [[Utility AI]]. Previous: [[2. Response Curves]]. Next: [[4. The Reasoner Momentum Commitment and Cooldowns]].
## 3.1 UtilityScoring.cs
**`UtilityScoring.cs`**
```csharp
using System;
using System.Collections.Generic;
/// <summary>Read-only world facts an agent reasons about: "health", "threat_distance", "ammo"...</summary>
public interface IAIContext
{
bool TryGet(string key, out float value);
}
/// <summary>A plain dictionary context, for tests and for agents that don't need a component.</summary>
public sealed class DictionaryContext : IAIContext
{
readonly Dictionary<string, float> _values = new Dictionary<string, float>();
public float this[string key] { set => _values[key] = value; }
public bool TryGet(string key, out float value) => _values.TryGetValue(key, out value);
}
/// <summary>
/// One question an action asks: "how much does my health matter right now?" Reads one input, maps it
/// from [min, max] to 0..1, then through a response curve.
/// </summary>
[Serializable]
public sealed class Consideration
{
public string name = "Consideration";
public string input = "health";
public float min = 0f;
public float max = 1f;
public ResponseCurve curve = ResponseCurve.Linear();
public float missingScore = 0f; // score when the input is missing from the context
public float Normalize(float raw)
{
if (max == min) return raw >= max ? 1f : 0f;
var t = (raw - min) / (max - min);
return t < 0f ? 0f : t > 1f ? 1f : t;
}
public float Score(IAIContext ctx) =>
ctx.TryGet(input, out var raw) ? curve.Evaluate(Normalize(raw)) : missingScore;
}
/// <summary>A thing the agent can do, scored by multiplying its considerations.</summary>
[Serializable]
public sealed class UtilityAction
{
public string id = "Idle";
public float weight = 1f; // 1 = normal; 2-3 for emergencies; 0.5 for idle fillers
public float cooldownSeconds = 0f; // after the action ends, it scores 0 for this long
public float commitSeconds = 0f; // once started, stays chosen at least this long while it scores above 0
public List<Consideration> considerations = new List<Consideration>();
}
public static class UtilityScoring
{
/// <summary>
/// Multiplies consideration scores and compensates for how many there are, so an action with
/// six considerations at 0.9 isn't punished against one with a single 0.9 (0.9^6 = 0.53).
/// Any consideration at 0 vetoes the action. Result is multiplied by the action's weight.
/// </summary>
public static float Score(UtilityAction action, IAIContext ctx, float[] breakdown = null)
{
var n = action.considerations.Count;
if (n == 0) return action.weight;
var score = 1f;
for (var i = 0; i < n; i++)
{
var s = action.considerations[i].Score(ctx);
if (breakdown != null && i < breakdown.Length) breakdown[i] = s;
score *= s;
if (score <= 0f)
{
if (breakdown != null) for (var j = i + 1; j < n && j < breakdown.Length; j++) breakdown[j] = float.NaN;
return 0f;
}
}
return Compensate(score, n) * action.weight;
}
public static float Compensate(float product, int count)
{
if (count <= 1) return product;
var modification = 1f - 1f / count;
var makeUp = (1f - product) * modification;
return product + makeUp * product;
}
}
```
## 3.2 Facts: IAIContext
Considerations read facts through `IAIContext.TryGet(key, out value)`. In the game the context is the `AIBlackboard` component (part 5); in tests it's `DictionaryContext`. Facts are plain floats, with booleans as 0 or 1. Naming them with snake_case strings (`threat_distance`) keeps them editable in the Inspector. For a large project, put the keys in a static class of constants to avoid typos.
## 3.3 Considerations
A consideration does three things:
1. Reads one fact. If the fact is missing it returns `missingScore`, 0 by default, which vetoes the action. That's the safe default: an agent that doesn't know where the enemy is shouldn't attack. Set it higher for optional facts.
2. **Normalizes** the raw value from `[min, max]` to 0..1. Distance 15 with min 0 and max 20 becomes 0.75. Values outside the range clamp. If min equals max, it acts as a threshold.
3. Passes the result through its curve.
Give each consideration a readable `name` ("Healthy enough"). It costs nothing and it's what you'll be reading in the debugger.
## 3.4 Actions and multiplication
`UtilityScoring.Score` multiplies an action's considerations. Multiplying (instead of averaging) has one big advantage: **any zero vetoes the action**. "Attack" with no ammo scores 0 no matter how close the player is. That's what makes Step curves work as gates. The loop also stops early at the first zero, so put cheap, frequently-zero gates first.
`breakdown`, when passed, receives each consideration's score (later ones are `NaN` after a veto). The debug view and your own tools can show why an action scored what it did.
### Weight
The product is multiplied by the action's `weight`. Keep most actions at 1. Use 1.2–2 for urgent actions (Flee, Reload when empty) so they win ties with ordinary ones, and 0.1–0.5 for fillers (Patrol, Idle) so anything meaningful beats them. A weight of 0 disables an action without deleting it. Weights are great candidates for [[Remote Config and Live Tuning]].
## 3.5 The compensation factor
Multiplying has one flaw: every extra consideration drags the score down. Three considerations at 0.8 give 0.8³ = 0.512, while an action with a single consideration at 0.6 would beat it, even though the first action is "pretty good on every count". Agents end up favouring simple actions.
The fix (from Dave Mark's talks on the infinite axis utility system) is to give back part of what each extra factor took away:
```text
modification = 1 − 1 / n
makeUp = (1 − product) × modification
final = product + makeUp × product
```
For three considerations at 0.8: modification = 0.667, makeUp = 0.325, final = 0.512 + 0.325 × 0.512 = **0.679**. A veto still vetoes (0 stays 0), and one consideration is left as it is. The test `CompensationLiftsManyConsiderations` checks that exact number.
## 3.6 Example: Attack
| Consideration | Input | Range | Curve |
|---|---|---|---|
| Can see threat | `threat_visible` | 0–1 | Step c = 0.5 |
| Healthy enough | `health` | 0–1 | Logistic m = 12, c = 0.3 |
| Has ammo | `ammo` | 0–1 | Step c = 0.01 |
| Close enough | `threat_distance` | 0–30 m | Linear m = 0.6, b = 0.4, inverted |
At full health and ammo with the player 8 m away, that's 1 × 1.00 × 1 × 0.84 = 0.84 before compensation and **0.94** after. The same guard at 28 m scores 0.62, still enough to keep fighting, just less eager.
Next: [[4. The Reasoner Momentum Commitment and Cooldowns]].