Skip to content

Deep reinforcement learning and decision models · Method reference · DRL006 · Algorithm

Trust region policy optimization (TRPO)

Stabilize policy improvement by constraining the change between old and new action distributions. TRPO motivates practical updates through a trust-region surrogate, while distinguishing the theoretical…

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran here in one language.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Stabilize policy improvement by constraining the change between old and new action distributions. TRPO motivates practical updates through a trust-region surrogate, while distinguishing the theoretical monotonic-improvement result from approximations used in implementations.

Question / hypotheses

How much can a policy improve a local advantage surrogate while keeping policy divergence within a chosen budget? This is constrained optimization, not a test that a new policy is statistically superior.

Architecture / computational structure

A differentiable stochastic policy and value baseline consume observations. Fresh rollouts estimate advantages. Fisher-vector products and conjugate-gradient calculations approximate a natural-gradient direction; a backtracking line search checks surrogate improvement and a KL constraint.

Assumptions

The policy must assign well-defined probabilities to sampled actions, and sampled advantages should represent the old policy’s visitation distribution. The practical expected-KL constraint is weaker than some theoretical worst-state bounds; approximate line searches and critics further limit guarantees.

Formula / computational definition

max⁡θ E[ρt(θ)A^t]\max_\theta\ \mathbb E[\rho_t(\theta)\widehat A_t] subject to E[KL(πold(⋅∣st)∥πθ(⋅∣st))]≤δ\mathbb E[\mathrm{KL}(\pi_{\mathrm{old}}(\cdot\mid s_t)\|\pi_\theta(\cdot\mid s_t))]\le\delta, where ρt=πθ(at∣st)/πold(at∣st)\rho_t=\pi_\theta(a_t\mid s_t)/\pi_{\mathrm{old}}(a_t\mid s_t).

Parameters and notation

Learned parameters are policy and value-function weights. Hyperparameters include KL radius delta, conjugate-gradient iterations, damping, line-search fraction/steps, rollout size, discount, advantage-estimation settings, and critic optimization. Trust-region radius changes the update constraint rather than the network shape.

Fitting, fine-tuning, and adaptation

Warm-start policy features when observation/action contracts match, but refit the baseline under a new reward. Use smaller trust regions for an initial transfer stage and fresh rollouts to reassess. Frozen-encoder and full-network adaptation should be separated in evaluation.

How to use and calibrate

Track realized KL, line-search rejection, surrogate improvement, and held-out episode returns. Compare interaction budgets and seed distributions with simpler actor–critic baselines. Validate that logged action probabilities come from the old behavior policy, not a continually overwritten network.

Example application

Train a continuous locomotion policy with controlled update sizes. The code evaluates a candidate categorical policy’s importance-weighted surrogate and mean old-to-new KL, illustrating a feasibility check rather than solving the constrained optimization problem.

Interpretation / pitfalls

Watch out

A sample-average KL bound does not guarantee every state changes little. A line-search success is not a high-confidence improvement claim. Poor advantage estimates or action-density mistakes can defeat the intended stability, and second-order approximations add substantial computation.

Python and R code examples

Scope: computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. Dependencies/validation: Python with NumPy executed successfully; base R not executed. Targets and supplied network outputs are ordinary arrays here, so this does not validate automatic-differentiation boundaries.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop. · needs numpy

import numpy as np
old=np.array([[.7,.3],[.4,.6]]); new=np.array([[.75,.25],[.35,.65]])
actions=np.array([0,1]); advantage=np.array([1.,-.2])
ratio=new[np.arange(2),actions]/old[np.arange(2),actions]
surrogate=np.mean(ratio*advantage); kl=np.mean(np.sum(old*np.log(old/new),axis=1))
print(surrogate,kl,kl<=.01)
R

Component demo, not a trained model — computational component described in the application subsection; not a complete estimator, neural trainer, or environment loop.

old <- matrix(c(.7,.3,.4,.6),2,byrow=TRUE); new <- matrix(c(.75,.25,.35,.65),2,byrow=TRUE)
actions <- c(1,2); advantage <- c(1,-.2)
ratio <- new[cbind(1:2,actions)]/old[cbind(1:2,actions)]
surrogate <- mean(ratio*advantage); kl <- mean(rowSums(old*log(old/new)))
print(c(surrogate=surrogate,KL=kl,feasible=kl<=.01))

Sources

From the CodeSplash reference, entry DRL006.