The method used here is backwards induction: a single pass over the grid that solves for the optimal policy parameters analytically, without any sampling.
Solving for the Optimal Parameters
Recall that the value function at state is,
(1)
where is the expected return from taking action in state and following thereafter. To simplify the expressions that follow, we introduce the shorthand
(2)
which absorbs the immediate transition reward and the discounted future value into a single quantity. With this notation, and the value function becomes . The optimal policy is the one that maximises at every state. Rather than following gradients towards this maximum, we can find it directly by setting and solving.
Working through the derivative (which involves the sigmoid identity and the entropy contribution from the reward), the condition simplifies to a remarkably clean result:
(3)
which, expanding , is,
(4)
The interpretation is intuitive: the optimal parameter at each state is simply the difference in expected future reward between stepping up and stepping down. A large positive means going up is strongly preferred; means the two directions are equally valuable. The entropy regularisation in the reward ensures this solution is always finite, the policy never degenerates to a hard deterministic rule.
Why we can Iterate Backwards
What makes backwards induction work is that the excursion problem has a defined Terminal boundary condition: at the terminal time , the value of every state is fixed by the problem definition (the boundary reward), with no future steps to consider.
Starting from the boundary, the values there are known. One step back from the boundary, Equation 4 can be evaluated exactly, the next-state values are already in hand. Those states then have known values, and so on, peeling the grid back towards the starting point one column at a time.
t = T t = T-1 t = T-2 ... t = 0
[known] → [compute θ] → [compute θ] → ... → [compute θ]
The two special conditions that make this work are:
- Finite horizon: the walk has a fixed terminal time , so the backwards sweep always terminates.
- Known boundary values: at is determined entirely by the boundary reward , with no policy-dependent terms remaining.
Neither condition holds in general RL problems, which is precisely why exact backwards induction cannot replace the policy gradient methods discussed earlier. We return to this limitation in the conclusion.
Computing the Value Function Stably
To run the backwards sweep, we need to evaluate at each state as we go. Substituting the optimal policy back into Equation 1 and writing , the optimal value function reduces to,
(5)
where the terms cancel exactly in the sum (a consequence of the softmax structure of the optimal policy). This log-sum-exp form is compact and exact, but directly computing the exponentials is numerically dangerous.
When either is large, overflows to inf in floating-point arithmetic, even though the final answer is perfectly finite. For example, and should give a value around , but naively evaluating immediately overflows.
The Log-Sum-Exp Trick
The fix is to factor out the larger of the two exponentials before taking the logarithm. Letting ,
(6)
so the stable expression for the value function is,
(7)
Since , both shifted exponents and are non-positive, so both exponentials lie safely in . The larger equals exactly, and overflow is impossible by construction.
Let and . Direct evaluation overflows. The stable form gives , and
a finite result computed entirely from numbers in .
What the Exact Solution Gives Us
The backwards induction solution provides the ground truth optimal policy for the excursion problem, a fixed reference against which both the tabular and neural network approaches can be evaluated. In the KL divergence section that follows, we use this exact solution as the target distribution when measuring how closely the learned policies have converged. Crucially, the exact solution requires no sampling and no iterative training: it is a single backwards sweep over the grid, in the number of states.
This is powerful, but its applicability is narrow. It requires finite horizon, known boundary conditions, and a state space small enough to sweep explicitly. Additionally, there should be no cycles in the state space - we can apply this method as the state contains time which only flows in one direction. For the generalisation goals of this project, and for real physical systems where the Doob -transform cannot be computed directly, the RL methods remain essential. We use this exact solution to benchmark our RL approach on this simple toy problem.
← Back to the main article: Finding an exact solution