Steer the snake to eat apples, and it grows by one segment with each. The game ends when the snake hits a wall or its own body, or when it goes 288 steps without an apple.
Steer with W A S D or the arrow keys, with the steering pad on a phone, or by swiping on the board. The snake cannot reverse, so the opposite key is ignored.
Press WATCH THE AGENT to see the selected learned agent play, and ONE STEP to advance it one move at a time. PLANNER HINT draws the route the planner would take.
A snake on a 12 × 12 board steers by choosing one of three actions each step. Its goal is to eat apples, and each apple adds a segment to its body. The game ends if it runs into a wall or its own body, or if it goes too long without food.
Two learned agents steer the snake, and both are shown here. The default is an evaluation function: it scores each safe move by a weighted sum of eight numbers computed from the position the move leads to, and its eight weights were tuned by the cross-entropy method. The other is a neural net, a function built from weighted sums, with 339 weights bred by a genetic algorithm. Two classic baselines, greedy and a breadth-first search planner, are measured on the same boards for comparison.
The board is a 12 × 12 grid of cells, each addressed by coordinates (x, y), where x is the column and y the row, growing downward. The snake is a list of segments. The first is its head and the last its tail. It starts in the middle of the board with three segments at (6, 6), (5, 6) and (4, 6), heading east. A heading is one of north, east, south or west, numbered 0 to 3 in clockwise order.
Each step the snake takes one action: left, straight or right, measured from its heading. The actions are relative, so the policy never needs compass directions. A left or right action turns the heading by a quarter turn, and straight leaves it alone. The snake cannot reverse, so these three actions are the only choices1.
Here h is the heading before the step and h' the heading after it, and \delta_a is the quarter turn of action a. Adding 1 turns clockwise, and the modulo keeps the result in 0 to 33. Example. In a new game the head is at (6, 6) heading east. Straight enters (7, 6), and left turns the head north and enters (6, 5).
The head then moves into the cell ahead. The move ends the game if that cell is outside the board (a wall death) or holds a body cell (a body death). The tail is treated with care. If the snake does not eat, the tail leaves its cell in the same step, so the tail's cell can be entered. If the snake eats, the tail stays in place and blocks its cell45.
Eating adds one apple to the score and one segment to the body, and the food moves to a uniformly random empty cell. Each empty cell is equally likely, so with |E| empty cells each has probability 1/|E|6. An idle counter counts the steps since the last apple. The game ends as starved when the counter reaches the cap of two laps of the board, 2N^2 = 288 steps. Filling the whole board ends the game as full, the only win78.
Here N = 12 is the side of the board. The cap stops a looping snake from playing forever, and it is the same for every agent in the benchmark below.
Both learned agents are trained on one number per game, the game score, which is also called fitness when it is used to rank candidates. A game that does not end by starvation scores its apples, plus a small survival bonus of at most 0.25 for lasting up to 300 steps. Beyond 300 steps each extra step costs 0.0002, so drifting for thousands of moves earns no bonus and pays a small cost. A starved game is penalised instead: it keeps its apples, loses 0.5, and gets no survival bonus, so looping is worse than dying cleanly910.
Here s(g) is the score of game g. The constants are the survival cap of 300 steps, the survival bonus of 0.25, the step cost of 0.0002 and the starvation penalty of 0.5, all set in the code11. A candidate's fitness is the mean of this score over the boards it played.
The default agent is an evaluation function, a rule that gives each position a number. For each move that does not end the game, it imagines the resulting position, computes eight features (numbers between 0 and 1 read from that position), and takes the move whose weighted sum of features is largest. A weight is the number that multiplies one feature. The features and the rules are hand-built, and the eight weights are the only part that was learned12.
A move is first applied in imagination. Let h' be the new head, and B' the new body: the new head followed by the old body, with the old tail removed unless the snake ate. The last cell of B' is the new tail t'. Every feature comes from one breadth-first search (BFS) that starts at h'. A BFS visits cells in order of their distance in steps, so the first time it reaches a cell it has the shortest route there. This search runs over empty cells and gives three values at once: the distance d_f in steps from the head to the food, the number R of empty cells it reaches, and whether it reaches a cell next to the tail. The tail t' is a goal the search may touch but not pass through, because it moves away on the next step13.
Here f_k(a) is feature k of the position after move a, w_k its weight, and A_{\text{safe}} the moves that do not end the game. If A_{\text{safe}} is empty, the snake goes straight. Ties keep the earlier move in the order straight, left, right, because a later move must score strictly higher to replace it15.
Here N = 12, so 2N = 24 and N^2 = 144. The food features use d_f, and R is the number of empty cells the search reaches, not counting the head. The room feature f_5 divides by the body length |B'|, and the area feature f_4 divides by the number of empty cells on the board after the move. f_7 counts the head's neighbours that are on the board and free, and the new tail counts as occupied19.
The committed weights are in snake/data/evaluator.json20. They were learned by the cross-entropy method described below.
| Feature | Weight | What it measures |
|---|---|---|
| apple | 0.7266 | 1 if the move eats |
| food near | 0.8261 | 1 if it eats; else 1 − d_f / 24 when the food is reachable; else 0 |
| food reachable | −0.6941 | 1 if it eats or the food is reachable; else 0 |
| area | −0.2007 | Empty cells reached, as a share of all empty cells |
| room for body | 0.3796 | Empty cells reached over the body length, capped at 1 |
| tail reachable | 1.5404 | 1 if a reached cell is next to the new tail |
| exits | 0.1065 | Free neighbours of the new head, over 4 |
| length | 0.7549 | Body length over 144 |
The tail-reachable weight is the largest. The two food terms together need a closer look. For food that is reachable and not eaten, they add 0.8261\,(1 - d_f/24) - 0.6941 = 0.1320 - 0.0344\,d_f. That is positive for food within 3 steps, and negative beyond it. Beyond 24 steps it is the full -0.6941. So from the food terms alone, reachable food farther than about 3 steps scores below food that cannot be reached at all, which scores 0. The tuner found this combination; the page does not claim it as a design goal. The other features, and the apple term, shape the choice as well.
Example. A move that eats has f_1 = f_2 = f_3 = 1. Its apple and food terms add 0.7266 + 0.8261 - 0.6941 = 0.8586 to its score, before the area, room, tail, exits and length terms are added.
The simple version below shows only the choice. The full version adds the feature computation and the safety test.
function CHOOSE(game, weights):
best ← none
for each action a in (straight, left, right): # ties keep the earlier action
if DIES(game, a):
continue # a move that ends the game is never chosen
score ← sum over k of weights[k] · FEATURES(game, a)[k]
if best is none or score > best.score:
best ← (a, score)
if best is none:
return straight # every move ends the game
return best.action
function CHOOSE(game, weights):
best ← none
for each action a in (straight, left, right): # ties keep the earlier action
if DIES(game, a):
continue # a move that ends the game is never chosen
f ← FEATURES(game, a)
score ← weights[1]·f[1] + weights[2]·f[2] + ... + weights[8]·f[8]
if best is none or score > best.score:
best ← (a, score)
if best is none:
return straight # every move ends the game
return best.action
function DIES(game, a):
cell ← NEXT-CELL(game, a) # where the head would go
if cell is outside the board:
return true
if cell = game.food: # eating keeps the whole body in place
return cell is in game.body
return cell is in game.body without its last cell # the tail leaves in the same step
function FEATURES(game, a):
head ← NEXT-CELL(game, a)
eats ← head = game.food
if eats:
body ← [head] + game.body # the tail stays
else:
body ← [head] + game.body without its last cell
tail ← last cell of body
blocked ← body without its last cell # the tail may be reached, not passed
n_empty ← 12 · 12 − |body|
dist ← {head: 0}; queue ← [head]; tail_ok ← false
while queue is not empty: # breadth-first search over empty cells
cell ← first cell of queue; remove it from queue
for each neighbour c of cell, in order north, east, south, west:
if c is outside the board or c in dist:
continue
if c = tail:
tail_ok ← true
continue
if c in blocked:
continue
dist[c] ← dist[cell] + 1
append c to queue
reach ← |dist| − 1 # reached empty cells, not the head
if eats:
food_near ← 1; food_reach ← 1
else if game.food in dist:
food_near ← max(0, 1 − dist[game.food] / 24); food_reach ← 1
else:
food_near ← 0; food_reach ← 0
exits ← 0
for each neighbour c of head:
if c is inside the board and c not in body:
exits ← exits + 1
return [
1 if eats else 0,
food_near,
food_reach,
reach / n_empty if n_empty > 0 else 0,
min(1, reach / |body|),
1 if tail_ok else 0,
exits / 4,
|body| / 144,
]
The search runs once and feeds five features: the food distance, the area, the room, the tail test and the food reachability. The tail is a goal, so the escape check can succeed at the tail without the search continuing through it. Distances are counted in whole steps and the food feature is scaled by 2N = 24, so food 24 steps away counts as 0 for f_2, while food on a reachable path still counts for f_3.
The order of the neighbours does not change any feature, since a BFS gives the same distances and reach in any order. The order matters only for the choice, through the tie rule.
The cross-entropy method (CEM) learns parameters by sampling. It keeps a Gaussian distribution, a bell curve with a mean and a standard deviation, for each parameter. Each generation it draws a population of candidate parameter vectors from that distribution, scores each candidate, keeps the best few (the elites), and moves the distribution toward them. Here the vector has eight entries, one per weight, and each weight has its own Gaussian23.
Each generation draws 48 candidates. Each candidate plays the same 6 boards, whose seeds are drawn anew each generation from 1000 up to 39,999, and its fitness is the mean game score over them. The 12 best are the elites. The Gaussian is then refit to them: its mean becomes the elites' mean, and its standard deviation becomes the elites' spread, floored at 0.05 so that the search never collapses to a single point2425.
Here \mu and \sigma are the mean and standard deviation vectors, \odot multiplies each weight by its own standard deviation, and \varepsilon^{(i)} is a standard normal draw. G = 6 is the number of boards, s_g(\theta) the game score of board g, and E the set of K = 12 elites out of 48 candidates. The start is \mu = 0 and \sigma = 1.0 for every weight. The standard deviation divides by K, which is numpy's default, not K - 1.
The champion is chosen on fixed boards, not on the training fitness, since the training boards change each generation. The best candidate of any generation and the top five of the last generation are rounded to four decimals. Each is scored on 32 validation seeds, 40000 to 40031, which lie above the training range and below the benchmark's 50000, and the highest validation score is committed28. The committed run used 30 generations. Its champion came from generation 30, and scored a mean of 96.344 apples and 2,777 steps per game on those 32 boards20.
function CROSS-ENTROPY(generations):
mean ← zeros(8); sigma ← 1.0 for every weight
repeat generations times:
population ← 48 samples from Normal(mean, sigma)
score each sample by its mean game score on 6 new boards
elites ← the 12 best samples
mean ← average of the elites
sigma ← max(spread of the elites, 0.05)
return the candidate that scores best on the validation boards
function CROSS-ENTROPY(settings):
mean ← zeros(8) # one mean per weight
sigma ← 1.0 for every weight # init_sigma
best ← none; candidates ← empty
for gen ← 1 to 30: # the generation count
population ← mean + sigma · NORMAL(0, 1) # 48 candidate vectors
seeds ← 6 integers drawn uniformly from 1000 to 39999
for each candidate θ in population:
score[θ] ← mean over seeds of GAME-SCORE(PLAY(θ, seed))
order ← candidates sorted by score, highest first (ties keep the earlier one)
if score[order[1]] > best.score:
best ← (order[1], gen)
append the record of this generation to the history
if gen = 30:
candidates ← order[1..5] with generation gen
break
elites ← order[1..12]
mean ← average of elites, weight by weight
sigma ← max(population standard deviation of elites, 0.05), weight by weight
append best to candidates
for each (θ, gen) in candidates:
θ ← θ rounded to four decimals
validation[θ] ← mean over seeds 40000 to 40031 of GAME-SCORE(PLAY(θ, seed))
return the θ with the largest validation score, and its generation
function PLAY(θ, seed):
game ← new game with food from seed
while game is alive:
step game with CHOOSE(game, θ)
return game
The simple version shows the idea: sample, keep the best, refit. The full version adds the seeded boards, the candidate ordering and the validation step. Ties in the ordering keep the candidate with the lower index, so the run is repeatable. The worker count changes only the speed, since each candidate's score depends only on its weights and seeds31.
| Setting | Value | Notes |
|---|---|---|
| Candidates per generation | 48 | Samples from the Gaussian |
| Games per candidate | 6 | All candidates share the same 6 boards in a generation |
| Generations | 30 | The committed run took about 55 minutes on two cores |
| Elites | 12 | The Gaussian is refit to them |
| Initial sigma, minimum sigma | 1.0, 0.05 | Start wide, never narrower than 0.05 per weight |
| Random seed | 1 | Makes the run repeatable: python -m snake cem |
The neural net maps 17 numbers, its senses, to 3 numbers, one score per action. It is a function built from weighted sums. The snake takes the action with the largest score. The senses are read in the snake's own frame: ahead is the direction of the heading, and left and right are a quarter turn either side. One learned rule therefore works whichever way the snake faces32.
Six of the senses come from a run, the three danger flags and the three free runs: the number of free cells walking from the head in one direction before the first obstacle, where an obstacle is a wall or any body cell, tail included. A flood fill is a breadth-first search over empty cells that counts how much room a direction opens up. The senses are the danger flag, the run, the room, the food and tail offsets, and a one-hot heading. A one-hot heading has a 1 for the current heading and 0 elsewhere3334.
Here \text{run}_k is the run in direction k, R_k the flood-fill count from the cell next to the head in that direction, capped at 30, h the head, f the food, t the tail, a the unit vector of the heading and r the unit vector a quarter turn to its right. The offsets are divided by N - 1 = 11, the largest possible distance across the board, and clipped to [-1, 1]3536. Example. The snake heads east at (6, 6) and the food is at (9, 6). Its forward offset is 3/11 \approx 0.273 and its right offset is 0.
The net is one hidden layer of 16 hidden units, followed by 3 outputs. Each hidden unit takes a weighted sum of the 17 senses plus a bias, a constant offset, and passes it through tanh, a squashing function that maps any number into the range (−1, 1). Each output is a weighted sum of the hidden units plus its own bias. The outputs are in the order left, straight, right, and the largest one is the action. The net does not check for danger itself, so a poor output can send the snake into a wall. Avoiding that has to be learned from the game score37.
Here W_1 is a 17 \times 16 matrix of input weights, b_1 the 16 hidden biases, W_2 a 16 \times 3 matrix of output weights and b_2 the 3 output biases. Ties in z go to the lower index, which is the order left, straight, right. The 339 numbers are stored as one flat vector called a genome, and the genetic algorithm works on that vector39.
function ACT(game):
x ← SENSE(game) # 17 inputs, in the snake's frame
h ← tanh(x · W1 + b1) # 16 hidden units
z ← h · W2 + b2 # left, straight, right
return the index of the largest z # ties go to the lower index
function ACT(game):
x ← SENSE(game) # 17 inputs
h ← tanh(x · W1 + b1) # 16 hidden units
z ← h · W2 + b2 # 3 outputs: left, straight, right
return the index of the largest z # ties go to the lower index
function SENSE(game):
occupied ← set of all body cells, tail included
head ← game.body[0]; heading ← game.heading
a ← DIRS[heading] # ahead
r ← DIRS[(heading + 1) mod 4] # right; left is −r
for each direction d in (a, −r, r), with index k = 0, 1, 2:
run ← 0; cell ← head + d
while cell is inside the board and cell is not in occupied:
run ← run + 1
cell ← cell + d
x[k] ← 1 if run = 0 else 0 # danger
x[3 + k] ← run / 11 # free run
x[14 + k] ← ROOM(occupied, head + d) / 30 # room
x[6], x[7] ← OFFSET(food)
x[8], x[9] ← OFFSET(tail)
x[10 + heading] ← 1 # heading one-hot
return x
function OFFSET(cell):
o ← cell − head
forward ← clip((o · a) / 11)
right ← clip((o · r) / 11)
return (forward, right)
function ROOM(occupied, start):
if start is outside the board or start is in occupied:
return 0
seen ← {start}; queue ← [start]
while queue is not empty and |seen| < 30: # the cap keeps the search cheap
cell ← first cell of queue; remove it from queue
for each neighbour c of cell:
if c is outside the board or c is in occupied or c is in seen:
continue
add c to seen and to queue
if |seen| ≥ 30:
break
return |seen|
The run counts only cells in a straight line, so it cannot show a pocket that the snake would enter by turning. The room counts every empty cell that a flood fill reaches from the adjacent cell, so it shows the size of the pocket. The cap of 30 keeps each flood fill cheap, since the net only needs to know whether a pocket is small42.
The same arithmetic runs in the browser, in web/js/snake-core.js, so the page's network view matches the benchmark43.
A genetic algorithm (GA) improves a population of genomes by three operators. Selection picks parents, favouring the fitter genomes. Crossover mixes two parents into a child. Mutation adds small random changes. No gradients are computed. The best genomes are copied unchanged each generation, a practice called elitism, so the best result never gets lost44.
The 60 starting genomes are random: each of their 339 weights is drawn from a normal distribution with mean 0 and standard deviation 0.545. Each generation, 8 boards are drawn from seeds 1000 up to 39,999, and every genome plays the same 8 boards. Its fitness is the mean game score over them. The 4 best genomes are copied unchanged, and the other 56 are children4647.
A parent is chosen by a tournament of size 3. Three genomes are drawn at random with replacement, and the fittest of the three is the parent. Two parents are chosen per child. Uniform crossover then takes each of the 339 weights from one parent or the other with probability ½. Gaussian mutation changes each weight with probability 0.1, by a random amount drawn from a normal distribution with standard deviation 0.1548.
Here a and b are the two parents, c the child, m_j the crossover bit of weight j, and F the fitness. Example. With 60 genomes and 4 elites, each generation makes 56 children, so the population size stays at 6051.
function EVOLVE(generations):
population ← 60 genomes, each weight drawn from Normal(0, 0.5²)
repeat generations times:
fitness ← each genome's mean game score on 8 new boards
next ← the 4 fittest genomes, unchanged # elitism
while |next| < 60:
a ← TOURNAMENT(fitness, 3); b ← TOURNAMENT(fitness, 3)
next ← next + [MUTATE(CROSSOVER(a, b))]
population ← next
return the best genome, chosen on the validation boards
function EVOLVE(settings):
population ← 60 genomes, each of 339 weights drawn from Normal(0, 0.5²)
best ← none; candidates ← empty
for gen ← 1 to 80: # the generation count
seeds ← 8 integers drawn uniformly from 1000 to 39999
for each genome g in population:
fitness[g] ← mean over seeds of GAME-SCORE(PLAY(g, seed))
order ← genomes sorted by fitness, highest first (ties keep the earlier one)
if fitness[order[1]] > best.fitness:
best ← (order[1], gen)
append the record of this generation to the history
if gen = 80:
candidates ← order[1..5] with generation gen
break
next ← copies of order[1..4] # elites, unchanged
while |next| < 60:
a ← population[TOURNAMENT(fitness, 3)]
b ← population[TOURNAMENT(fitness, 3)]
for each weight j in 1..339:
m_j ← true with probability 0.5
child_j ← a_j if m_j else b_j # uniform crossover
for each weight j in 1..339:
if a random draw is below 0.1:
child_j ← child_j + NORMAL(0, 0.15²) # Gaussian mutation
append child to next
population ← next
append best to candidates
for each genome g in candidates:
validation[g] ← mean over seeds 20000 to 20015 of GAME-SCORE(PLAY(g, seed))
return the genome with the largest validation score, and its generation
function TOURNAMENT(fitness, k):
contenders ← k indices drawn uniformly with replacement
return the contender with the highest fitness
Elitism keeps the top four genomes unchanged, so crossover and mutation cannot destroy them. Because the boards change each generation, a genome's fitness is measured again on new boards, so the fitness of one generation is not comparable with another's. The champion is therefore picked on fixed boards, as in the cross-entropy method. The best genome ever seen and the top five of the last generation are scored on seeds 20000 to 200155354. Those seeds fall inside the training range, so this check is same-distribution, not held-out.
Each genome's fitness is one mean over the 8 boards, so the same boards decide between genomes in a generation. A genome cannot memorise a fixed set of boards, because the next generation draws new ones55.
The random policy takes each action with the same probability. It uses its own seeded generator, so a game can be repeated exactly. It senses nothing, so it dies quickly56.
The greedy policy makes a one-step decision with no search. For each safe move it measures the Manhattan distance, the number of grid steps between two cells ignoring walls, from the new head to the food. It takes the move with the smallest distance. It has no learned part.
Here (x_a, y_a) is the head's cell after move a and (x_f, y_f) the food. Straight is tried first, so a tie keeps the snake going straight.
function GREEDY(game):
if game has no food:
return straight
best ← straight; best_distance ← none
for each action a in (straight, left, right): # straight first, so ties go straight
if DIES(game, a):
continue
cell ← the head's cell after a
distance ← |cell.x − food.x| + |cell.y − food.y|
if best_distance is none or distance < best_distance:
best ← a; best_distance ← distance
return best
The planner is the strongest classic baseline. It uses breadth-first search (BFS) for a shortest route to the food, then for a safety check, and, if needed, for a route to the tail. Its key idea is a safety check. After the snake eats, the tail stays in place, so the head must still be able to reach it. The planner takes the food route only if that holds5960.
The check is a second BFS, from the food to the tail, through the cells the snake would occupy after eating: its body and the food route. If the check fails, the planner follows its tail instead. The tail moves away each step, so a route to it is always open, and this is called tail-chasing. The planner re-plans every step, so it switches to the food route as soon as the check passes. If the planned step is unsafe or no route exists, it picks the safe move that leaves the largest flood-fill region, and if no move is safe it goes straight61.
The page's PLANNER HINT calls the same code on the server, and the route it returns is labelled as food, tail or none62.
function PLANNER(game):
path ← PLAN-ROUTE(game)
if path is not empty and its first cell is a legal, safe step:
return the action that turns the head toward the first cell of path
# boxed in: take the safe move with the most room
return the safe action whose cell opens the largest flood-fill region, else straight
function PLAN-ROUTE(game):
if the food route passes the tail-chasing check:
return the food route
return the route to the tail
function PLANNER(game):
path ← PLAN-ROUTE(game)
if path is not empty:
a ← action that turns the head toward the first cell of path # relative to the heading; none if it is a reversal
if a is not none and not DIES(game, a):
return a
# boxed in: choose the safe move that leaves the largest empty region
best ← straight; best_room ← −1
for each action a in (straight, left, right):
if DIES(game, a):
continue
room ← REACHABLE(the cell after a, blocked = body without its tail)
if room > best_room:
best ← a; best_room ← room
return best
function PLAN-ROUTE(game):
body ← game.body; head ← the first segment of body; tail ← the last segment of body
blocked ← body without its last cell # the tail will have moved away
if game.food exists:
path ← BFS(head, game.food, blocked)
if path exists:
new_blocked ← (body ∪ path) without tail
if BFS(game.food, tail, new_blocked) exists: # the tail-chasing check
return path
return BFS(head, tail, blocked)
function BFS(start, goal, blocked):
if start = goal:
return empty path
parent ← {start: none}; queue ← [start]
while queue is not empty: # cells in order of distance
cell ← first cell of queue; remove it from queue
for each direction d in (north, east, south, west):
c ← cell + d
if c is outside the board or c is in parent:
continue
if c ≠ goal and c is in blocked:
continue # the goal may be entered even if blocked
parent[c] ← cell
if c = goal:
follow parent links from goal back to start
return the cells after start, in order
append c to queue
return none # no route
function REACHABLE(start, blocked):
seen ← {start}; queue ← [start]
while queue is not empty:
cell ← first cell of queue; remove it from queue
for each neighbour c of cell:
if c is outside the board or c is in seen or c is in blocked:
continue
add c to seen and to queue
return |seen|
The core of the planner is BFS, which finds a shortest route in steps because it reaches each cell first along a shortest path. The planned move is the first step of the route, and the room rule replaces it only when that step is unsafe or no route exists64.
The table compares every agent on the same 200 seeded games, seeds 50000 to 50199 on the 12 × 12 board. These seeds are never used for training or for validation. The median is the middle value of the apples counts, and the death rate is the share of games that ended in a wall or body collision. A starved game is different: the snake is still alive, but it has looped for two laps without an apple6566.
| Agent | Mean apples | Median | Max | Death rate | Starved (of 200) |
|---|---|---|---|---|---|
| evaluation function | 90.13 | 86 | 133 | 0.0% | 200 |
| planner (BFS) | 56.13 | 49.5 | 137 | 0.0% | 200 |
| greedy | 20.12 | 19 | 47 | 100.0% | 0 |
| neural net (v2) | 13.69 | 13 | 26 | 38.5% | 123 |
| random | 0.16 | 0 | 2 | 100.0% | 0 |
The table is measured on 200 games per agent, seeds 50000 to 50199, and is committed in results/snake_benchmark.md67. The evaluation function eats the most apples, and it never dies. But every one of its 200 games ends by starvation, so it stalls instead of losing. The planner is the same: it never dies, and it starves on all 200 boards, not on some of them.
The evolved net (v2) eats 13.69 apples on average, which beats random by a wide margin and shows that it learned to eat. It does not beat greedy's 20.12, and it dies in 38.5% of games. Greedy dies in every game, but it eats more before it does. Treat the evolved net's row as a result, not a win.
The mean score hides the turning pattern. Over the first 40 benchmark boards, seeds 50000 to 50039, the net made 19,829 moves: 17,033 straight (85.9%), 2,793 left (14.1%), and 3 right (0.02%). The net almost never turns right, and it turns left about one move in seven. The counts are committed in results/snake_net_actions.json, from python -m snake actions --write6869.
Here n_a is the number of moves the net made with action a. The shares are 85.9% straight, 14.1% left and 0.02% right, from the counts above.
Each row is one version of the net. The first version was trained on three fixed boards. The live champion, v2, uses fresh boards each generation. The two longer runs were scratch runs that were not committed, so no file in results/ backs their numbers.
| Version | What changed | Evolved mean apples | Median | Death rate | Starved (of 200) |
|---|---|---|---|---|---|
| v1 | 3 fixed training boards, 14 senses, 291 weights, 100 generations of 80 × 3 games | 2.05 | 1 | 5.0% | 190 |
| v2 (live) | Fresh boards each generation, 8 games, starvation penalty 0.5, room senses, 339 weights, 80 generations of 60 | 13.69 | 13 | 38.5% | 123 |
| v3a | Starvation penalty 2.0, population 100, 200 generations, 8 games. Scratch run, not committed: no file in results/ backs these numbers. | 0.93 | 1 | 100.0% | 0 |
| v3b | Starvation penalty 0.5, population 100, 200 generations, 8 games. Scratch run, not committed: no file in results/ backs these numbers. | 13.22 | 13 | 53.5% | 93 |
The v2 run is logged in results/snake_train_log.jsonl. Its 80 generations took 583 seconds, about ten minutes on two cores, and its best generation was the 80th71. The v1 log is results/snake_train_log_v1.jsonl, and the evaluation function's log is results/snake_eval_train_log.jsonl7273.
The genetic algorithm's settings are listed below. Its defaults are the committed run, and python -m snake evolve --help lists the flags74.
| Parameter | Value | Notes |
|---|---|---|
| Board | 12 × 12 | Fixed by the rules. The snake starts with 3 segments heading east. |
| Starvation | 2 laps (288 steps) without food | Ends the game. Same rule for every agent in the benchmark. |
| Population | 60 | Sized to fit about 10 minutes on two cores. |
| Generations | 80 | Time budget. The 200-generation runs (v3a, v3b) were still improving at the end. |
| Games per genome | 8 | Every genome in a generation plays the same 8 boards. |
| Training boards | Seeds drawn from 1000 to 39999, new each generation | Never overlaps the benchmark seeds (50000 and up). |
| Fitness per game | apples + up to 0.25 survival bonus; −0.0002 per step beyond 300 | Survival bonus breaks ties between early learners. |
| Starvation penalty | 0.5, and no survival bonus | Looping is worse than dying cleanly. A penalty of 2.0 was tried in v3a and made the population learn to die early (see the history). |
| Elites | 4 | Copied unchanged each generation. |
| Tournament size | 3 | Parents are the best of 3 random genomes. |
| Crossover | Uniform, p = 1/2 per weight | Each weight comes from either parent. |
| Mutation | rate 0.10 per weight, sigma 0.15 | A first try at sigma 0.3 on 8% of weights learned slowly. |
| Initial weights | Normal, sigma 0.5 | Random start. |
| Champion | Best of the best-ever genome and the last generation's top 5 | Re-scored on 16 fixed validation seeds (20000 to 20015), not on the benchmark. These seeds fall inside the training range (1000 to 39999), so this check is same-distribution, not held-out. |
| Random seed | 1 | Makes the run repeatable: python -m snake evolve. |
The evaluation function's cross-entropy settings are in the table in its section above.
How these were chosen: by trial, not by a systematic sweep. The first version used three fixed training boards and learned to memorise them: its best generation scored 6.0 apples per game on those boards, against 2.05 on the benchmark boards. Fresh boards each generation fixed most of that. The starvation cap was first set to four laps, then cut to two to keep looping games cheap; the benchmark uses the same cap for every agent. The population and generation counts were set by the time budget. Two longer runs (v3a, v3b: 200 generations, population 100) did not beat v2. v3a's penalty of 2.0 collapsed into early death. v3b, with the penalty back at 0.5, reached 13.22 apples, within noise of v2's 13.69 and not better, so v2 stays the live champion. No parameter was tuned on the benchmark seeds.