# 2. Response Curves Part of [[Utility AI]]. Previous: [[1. Utility AI Concepts and When to Use It]]. Next: [[3. Considerations Actions and Compensation]]. ## 2.1 Why curves A raw fact rarely maps straight to "how much should I care". Losing the first half of your health is not alarming; losing the last 20 % is. A curve encodes that judgment. Inputs and outputs are both 0 to 1, so every curve is interchangeable and every consideration comes out on the same scale. ## 2.2 ResponseCurve.cs **`ResponseCurve.cs`** ```csharp using System; public enum CurveType { Linear, Polynomial, Logistic, Logit, Step, Constant } /// <summary> /// Maps a normalized input (0..1) to a score (0..1). The four parameters follow the usual utility-AI /// convention: m = slope/steepness, k = exponent/height, b = vertical shift, c = horizontal shift. /// Pure C# and serializable, so designers can edit it in the Inspector and tests can run it outside Unity. /// </summary> [Serializable] public struct ResponseCurve { public CurveType type; public float m; public float k; public float b; public float c; public bool invert; public static ResponseCurve Linear(float slope = 1f, float yShift = 0f) => new ResponseCurve { type = CurveType.Linear, m = slope, k = 1f, b = yShift, c = 0f }; public static ResponseCurve Polynomial(float exponent, float slope = 1f) => new ResponseCurve { type = CurveType.Polynomial, m = slope, k = exponent, b = 0f, c = 0f }; public static ResponseCurve Logistic(float steepness = 10f, float midpoint = 0.5f) => new ResponseCurve { type = CurveType.Logistic, m = steepness, k = 1f, b = 0f, c = midpoint }; public static ResponseCurve Logit(float steepness = 10f) => new ResponseCurve { type = CurveType.Logit, m = steepness, k = 1f, b = 0f, c = 0f }; public static ResponseCurve Step(float threshold) => new ResponseCurve { type = CurveType.Step, m = 1f, k = 1f, b = 0f, c = threshold }; public static ResponseCurve Constant(float value) => new ResponseCurve { type = CurveType.Constant, b = value }; public ResponseCurve Inverted() { var r = this; r.invert = !invert; return r; } public float Evaluate(float x) { if (float.IsNaN(x)) return 0f; x = Clamp01(x); if (invert) x = 1f - x; float y; switch (type) { case CurveType.Linear: y = m * (x - c) + b; break; case CurveType.Polynomial: var basis = x - c; y = basis <= 0f ? b : m * MathF.Pow(basis, k) + b; break; case CurveType.Logistic: y = k / (1f + MathF.Exp(-m * (x - c))) + b; break; case CurveType.Logit: var p = Math.Clamp(x - c, 0.0001f, 0.9999f); y = 0.5f + MathF.Log(p / (1f - p)) / (m == 0f ? 1f : m) + b; break; case CurveType.Step: y = (x >= c ? 1f : 0f) + b; break; default: y = b; break; } return float.IsNaN(y) ? 0f : Clamp01(y); } static float Clamp01(float v) => v < 0f ? 0f : v > 1f ? 1f : v; } ``` The parameters follow the convention most utility-AI talks and books use (Dave Mark's "infinite axis" model): **m** slope or steepness, **k** exponent or height, **b** vertical shift, **c** horizontal shift. Every curve's output is clamped to 0..1 and a `NaN` input or output becomes 0, so one bad sensor reading can't poison a score. | Type | Formula (x already 0..1) | Shape | |---|---|---| | Linear | m·(x − c) + b | straight line | | Polynomial | m·(x − c)^k + b | k = 2 or 3: slow start, fast finish; k = 0.5: fast start | | Logistic | k / (1 + e^(−m·(x − c))) + b | S-curve; c = the midpoint, m = how sharp | | Logit | 0.5 + ln(x / (1 − x)) / m + b | the inverse S: steep at the ends, flat in the middle | | Step | 1 if x ≥ c, else 0 (+ b) | a yes/no gate | | Constant | b | ignores the input | `invert` flips the input (x becomes 1 − x) before the formula, so "low health" is just the health curve inverted. ## 2.3 Picking a curve | Fact | Curve | Reading | |---|---|---| | "Am I hurt?" (health) | Polynomial k = 3, inverted | Barely matters until health is low, then matters a lot | | "Healthy enough to fight?" | Logistic m = 12, c = 0.3 | Fine above 40 %, drops off sharply around 30 % | | "Is the target close?" (distance over 0–30 m) | Linear, inverted | Closer is steadily better | | "Do I have ammo?" | Step c = 0.01 | Any ammo at all: yes | | "Low on ammo?" | Polynomial k = 2, inverted | Urgent only near empty | | "Can I see the player?" (0 or 1) | Step c = 0.5 | A gate: 0 vetoes the action | | "Time since I saw them" (0–10 s) | Logistic c = 0.4 | Short gaps don't count; after 4 s, safe to heal | Rules of thumb: - **Normalize first** (part 3): each consideration maps its raw range (0 to 30 metres) to 0..1 before the curve sees it. - **Use Step curves as gates**, not as scores. They make an action impossible, which is clearer than a low score. - **Keep curves gentle** for anything that's always present. A very steep logistic acts like a step and makes agents switch abruptly. - **Write the sentence first** ("the guard should only care about reloading when nearly empty"), then pick the curve that draws it. Plot curves in a spreadsheet if in doubt: sample x from 0 to 1 in steps of 0.1. Unity's `AnimationCurve` is an alternative that designers can draw by hand. It needs `UnityEngine`, though, so the scoring code couldn't run in plain tests. If you want it, add a curve type that holds an `AnimationCurve` in a Unity-only subclass and keep the formula curves for tests. Next: [[3. Considerations Actions and Compensation]].