Strategy analysis¶
A result says who won. Strategy analysis says how well each agent played, move by move: - how often it picked the best move; - how much each mistake cost; - where its blunders were; - how it opens a game; - whether it learns over a series of games.
The rules are in SPEC-022.
Score every move¶
Add the strategy evaluator to a scenario:
agentplay: 1
name: ttt-strategy
world: {use: tic-tac-toe}
agents:
- {id: x, use: scripted/random}
- {id: o, use: scripted/minimax}
evaluators:
- use: strategy
agentplaykit play ttt-strategy.yaml
finished: o wins in 7 steps, cost $0.0000 -> runs/ttt-strategy-1.jsonl
strategy: o accuracy 1.00, 0 blunders; x accuracy 0.75, 1 blunder (exact)
The evaluator works after the run, from the log alone: agents never see the analysis.
For each accepted move, it asks the world's analyst for the value of every legal move, then compares the move played with the best one:
| Measure | What it means |
|---|---|
| accuracy | the share of moves that were best (or within best_margin of the best) |
| regret | how much worse the move played was than the best move (0 for a best move) |
| blunder | a move whose regret is at least the world's blunder_threshold |
Settings:
| Setting | Default | What it does |
|---|---|---|
depth |
the world's | how far ahead the analyst searches, for analysts that search (chess: 1 to 4) |
max_ms_per_move |
2000 |
skip a move whose analysis takes longer, and say so |
Analysis results are cached by observation, so the same position is only analysed once.
Worlds with an analyst¶
| World | Analyst | Values | Blunder |
|---|---|---|---|
tic-tac-toe |
exact (minimax) | 1 win, 0 draw, -1 loss with perfect play | 1: a won game drawn, or a drawn game lost |
chess |
approximate (a short search) | pawns for the moving player | 3: a knight, a bishop or more |
examples/guess |
exact | minus the guesses still needed in the worst case | 1 |
An exact analyst knows the value of every move with perfect play. An approximate analyst looks a fixed number of moves ahead and estimates the rest. Reports always say which, for example "approximate, depth 2".
Chess¶
The chess analyst looks two half-moves ahead: the move, then the opponent's reply. After that it follows captures and promotions only, up to four in a row. It values a position by material (pawn 1, knight and bishop 3, rook 5, queen 9) plus small bonuses for central and advanced pieces. A forced mate it finds is worth ±100, minus the half-moves needed to reach it.
What it finds: - a piece left where it can be taken for free; - a capture that loses material; - mate in one, and a move that allows mate in one.
What it misses: - deep plans, and threats more than two half-moves away; - long-term weaknesses, such as a weak pawn structure.
So read a chess "blunder" as "loses about a piece or more within two half-moves". The analysis is deterministic: the same game always gets the same numbers, on any machine.
depth: 3 looks further, but takes about ten times longer.
In reports¶
agentplaykit report runs adds a strategy section for each world:
- Strategy: accuracy, mean regret and blunders per move, each with a 95% interval, and each competitor's five worst blunders. Each blunder links to its turn in the replay.
- Openings: the first move each competitor made in each role, with counts.
- Learning: for a series of games between the same competitors, the results in the first third of the games against the last third. It says "no clear change" when the intervals overlap.
- Exploiting a weakness: for worlds that declare
best_responses, as rock-paper-scissors does, how often a competitor played the move that beats its opponent's favourite move, in each third of the series. Againstscripted/biased, a good player learns to exploit the bias.
strategy (exact): accuracy, mean regret, blunders/move
scripted/minimax 1.00 (0.86-1.00) 0.00 (0.00-0.00) 0.00 (0.00-0.14) (23 moves)
scripted/random 0.74 (0.54-0.87) 0.26 (0.08-0.44) 0.26 (0.13-0.46) (23 moves)
The replay page lists the marked turns. On a blunder's turn it says what was played and what was best, and on a board it outlines the best move's squares.
Write an analyst for your world¶
An analyst is a class with two methods:
- legal(observation) returns the legal actions the observation shows. Logs do not store the
list of legal actions, so the analyst works them out.
- values(observation, legal_actions) returns each action's value for the agent that acts.
Higher is better.
It may add cells(observation, action): the grid cells a move touches, so the viewer can
outline the best move.
It must be deterministic and have no side effects. It sees only what the agent saw, never the running world.
The guess-the-number example (examples/guess)
has one. The best guess splits the numbers that are still possible in half:
from typing import Any, Dict, List
from agentplaykit.core import Analyst
class GuessAnalyst(Analyst):
def legal(self, observation: Dict[str, Any]) -> List[Any]:
return list(range(1, int(observation["high"]) + 1))
def values(self, observation: Dict[str, Any], legal_actions: List[Any]) -> Dict[Any, float]:
low, high = 1, int(observation["high"])
for guess, hint in observation["history"]: # what the hints have ruled out
if hint == "higher":
low = max(low, guess + 1)
elif hint == "lower":
high = min(high, guess - 1)
def needed(n: int) -> int: # worst-case guesses for n numbers, always splitting in half
return n.bit_length()
out: Dict[Any, float] = {}
for g in legal_actions:
if low <= g <= high: # the hint leaves g - low or high - g numbers
out[g] = -float(1 + max(needed(g - low), needed(high - g)))
else: # a number already ruled out: a wasted guess
out[g] = -float(1 + needed(high - low + 1))
return out
Then name it in the world, and say what the values mean:
class Guess(Environment):
name = "guess"
version = "1"
analyst = "guess_analyst.py:GuessAnalyst" # or the class itself
analysis = {"kind": "exact", "blunder_threshold": 1, "values": "minus guesses still needed"}
analysis takes:
| Field | Meaning |
|---|---|
kind |
exact or approximate |
values |
what a value means, in plain words |
blunder_threshold |
the regret that makes a move a blunder |
best_margin |
optional: a move this close to the best still counts as best (default 0) |
depth |
optional, for approximate analysts: how far they search |
A world may also declare best_responses, a map from an opponent's move to the move that beats
it, for the "exploiting a weakness" table.
A package can provide an analyst for a world it does not own. It registers the analyst under
the entry-point group agentplaykit.analysts, named <world name>@<world version>. An
installed analyst takes precedence over the world's own analyst.
[project.entry-points."agentplaykit.analysts"]
"tic-tac-toe@1" = "my_package.analysts:BetterTicTacToe"
See also behaviour metrics, which measure how agents behave rather than how well they play.