Active Inference InstituteArcade

Chapters 2, 4 and 7Planning as inferenceArcade

Hill: go back to go forward

The car's engine is too weak to drive straight up the hill. It has to roll back the other way first, to gain momentum. An agent that only tries to be nearer the summit at each moment never arrives. An agent that weighs whole futures against where it expects to end up finds the way, and the further ahead it looks, the more it will put up with going the wrong way now.

One step is a tenth of a second. Greedy is a horizon of one step.
The width σ of the prior over outcomes, as a length of track. A setting.

Keys: left and right arrows, or A and D, to push; R to restart; P to pause. A gamepad stick or D-pad also works.

Learn by doing

Try this

  1. Hold right and see what happens. Before you press: will full power get the car to the flag? Hold the right arrow and watch the height it reaches. The page works out the answer from the energy alone: the engine can add only so much on the way up.
  2. Work out the trick yourself. Look at the lower panel. It plots position against velocity, and the bright loop is the separatrix: the path of a car with exactly enough energy to crest the hill. Inside the loop you stay in the valley. Find a way to get outside it. Coach mode shows the agent's plan in gold from wherever you are.
  3. Watch the agent. It has no reward and no rule about rocking. It holds a prior expectation that it will be at the summit, and it chooses the future that best fits. Predict which way it pushes first.
  4. Turn the horizon down to one, then up until it succeeds. With a horizon of one step the map below the hill is one colour almost everywhere: push towards the flag. Raise the horizon slowly. Predict when a patch of the other colour will appear near the floor of the valley, and check your guess against the sentence under the stage, which gives the horizon at which the patch appears and the horizon at which the agent first arrives.
  5. Sharpen and flatten the preference. A sharp preference makes every moment spent far from the flag costly, so the detour looks worse and a longer view is needed to accept it. Predict which way the critical horizon moves, then drag the preference slider and read the new value.
  6. Drive into the fog. Here the agent does not know where it started, and its readings of position are poor except on a lit stretch of the far slope. Its belief is the blue cloud. Watch whether it rolls back far enough to reach the light, then try the level yourself with only the green readings to go on.

The mathematics

What is being computed

Positions are distances along the track, p, and h(p) is the height of the track there. Velocity is v. The driver chooses an action a: push left (−1), coast (0) or push right (+1). All numbers below are the model's own, for the car as it is now.

Motion along the track (function step) dvdt=ka−gh′(p)+w(p)−cv,dpdt=v

k is the engine's thrust, g gravity, w the wind and c a drag coefficient. Because p is measured along the track, h′ is the rise per unit of track and this is the exact motion of a unit mass on the curve. On level 1 the profile is h = sin(3p)/3 with g = 0.0025 and k = 0.001, which gives the standard mountain-car equation v ← v + 0.001a − 0.0025 cos(3p), p ← p + v. Updating the velocity first and then moving with the new velocity is the symplectic Euler method, with a fixed step.

Energy bookkeeping (functions energy, residual) 12v2+gh(p)=E0+Wengine+Wwind−L+ε

Kinetic plus potential energy equals the starting energy, plus the work done by the engine and the wind, minus what drag, the speed limit and the wall have removed (L). The remainder ε is the error of the integrator, which is of first order in the time step. For a car rolling freely it stays bounded and does not drift. With the engine in use it wanders: in the tests it stayed under 15% of the climb over whole attempts of random thrust, and under 5% in the agent's own runs. With the engine held on, the quantity gh(p) − kap + ½v² is conserved, and that settles step 1 of "Try this": from rest at the bottom of level 1, full power to the right stops at – of the height.

Expected free energy, looking T steps ahead (functions setPrefs, evalActions) GT(s,u)=DKL[B(·|s,u)‖C]+∑s′B(s′|s,u)minu′GT−1(s′,u′),G0=0

Hidden states s are the nodes of a grid of positions and velocities, plus one more state, "at the summit". B(u) says where each node goes in one decision step (three steps of the physics) under action u; the result is shared between the sixteen nearest nodes, which is the model's small process noise. C is the agent's prior over outcomes: half of it sits on the summit and the rest falls away along the track with width σ. There is no reward. The first term is risk: the divergence between the outcomes an action leads to and the outcomes the agent expects. On these levels the state is observed, so the second term of expected free energy, ambiguity, is zero. The recursion is the one used in sophisticated inference, and because beliefs here are single states it can be solved exactly for every node by working backwards from the horizon. This is planning as inference; in this fully observed limit it is the same computation as KL control, and for a noiseless B it is a shortest-path problem.

If one removes ambiguity, the resulting scheme corresponds to risk-sensitive or KL control in control theory.Parr, Pezzulo & Friston (2022), Active Inference, chapter 2, p. 36
The choice now (function evalActions, with the library's policyPosterior) Q(u)=σ(−γGT(s,u))

σ is the softmax function and γ is the precision of beliefs about what to do (set to 1 here). The agent takes the most probable action, then plans again at the next step. The coloured map in the lower panel is the most probable action at every node; its brightness is GT, brighter where less free energy is expected.

In the fog: beliefs and ambiguity (functions fogUpdate, fogEfe, fogEvaluate) Q(s)∝Q(s)A(o|s),G(π)=∑τ(DKL[Q(oτ|π)‖C]+EQ(sτ|π)H[A(·|sτ)])

On the foggy level the hidden state is which of 31 possible cars is the real one: they start at different places and are moved by the same physics. A gives the probability of each position reading: wide in the fog, narrow under the lamp. The first formula is Bayes' rule for a new reading o. The second scores a policy π (hold one action for a number of steps, then the other) by risk plus ambiguity, where ambiguity is the expected entropy H of the readings. A stretch of track where readings are sharp has low ambiguity, so an uncertain agent is drawn to it. In the test file, over 24 seeded starts, the agent spends more time on the lit stretch when it scores ambiguity than when it scores risk alone, and no time there when it starts out certain of its position.

Reading

Where this comes from

What is established

The mountain-car problem was set by Moore (1990) and is a standard test in reinforcement learning (Sutton & Barto 2018). Its lesson is that the direct route fails: the car must first move away from the goal.

Friston, Daunizeau & Kiebel (2009) solved it with an agent that has no reward function. The agent has prior expectations about the path it will follow, and acts to fulfil them; Friston and colleagues (2010) set out the same formulation of action. Later work treats the task with planning by expected free energy and learned models (Çatal and colleagues 2020; Tschantz and colleagues 2020).

In discrete time, policies are scored by expected free energy, which separates into risk and ambiguity (Parr, Pezzulo & Friston 2022, chapters 2, 4 and 7; Da Costa and colleagues 2020). Sophisticated inference evaluates it recursively over future beliefs (Friston and colleagues 2021).

The free energy principle is a modelling framework. This game is an illustration of it, not evidence for it.

What this model leaves out

The 2009 paper works in continuous time and its agent learns the expected path by first being shown it. The agent here is given its model of the hill and plans on a grid, with the summit as a prior over outcomes. It does not learn.

The grid is coarse (about – nodes) and sharing each move between neighbouring nodes adds noise that the real car does not have. The critical horizons quoted are properties of this model at this resolution and with a decision every tenth of a second; a finer model would give slightly different numbers.

Future choices are taken at their best, which is the limit of high precision. In the fog the agent considers a limited family of policies and looks at most 60 steps ahead; a full search over future beliefs is far larger. The fog is also kinder than real fog: the agent's hypotheses always include the true starting place.

The picture draws height against distance along the track, with the vertical scale chosen to fit the screen, so drawn slopes are not the true angles. Medal times are multiples of the agent's best time, chosen as settings.