Policy Search and Optimization Techniques
Policy Search and Optimization Techniques
Policy search involves searching the space of policies without directly computing a
value function. The policy space is often lower-dimensional than the state space
and can often be searched more efficiently. Policy optimization optimizes the
parameters in a parameterized policy in order to maximize utility. This parameter-
ized policy can take many forms, such as neural networks, decision trees, and
computer programs. This chapter begins by discussing a way to estimate the value
of a policy given an initial state distribution. We will then discuss search methods
that do not use estimates of the gradient of the policy, saving gradient methods
for the next chapter. Although local search can be quite effective in practice, we
will also discuss a few alternative optimization approaches that can avoid local
optima.1 1
There are many other optimiza-
tion approaches, as discussed
by M. J. Kochenderfer and
10.1 Approximate Policy Evaluation T. A. Wheeler, Algorithms for
Optimization. MIT Press, 2019.
U (π ) = ∑ b ( s )U π ( s ) (10.1)
s
50
U (π )
We will use πθ to denote a policy parameterized by θ. For convenience, we Figure 10.2. The effect of the depth
will use U (θ ) as shorthand for U (πθ ) in cases where it is not ambiguous. The and sample count for Monte Carlo
parameter θ may be a vector or some other more complex representation. For policy evaluation of a uniform ran-
dom policy on the cart-pole prob-
example, we may want to represent our policy using a neural network with a lem (appendix F.3). The variance
particular structure. We would use θ to represent the weights in the network. decreases as the number of sam-
ples increases. The blue regions in-
Many optimization algorithms assume that θ is a vector with a fixed number of dicate the 5 % to 95 % and 25 % to
75 % empirical quantiles of U (π ).
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 2. l oc a l se a rc h 215
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
216 c ha p te r 10 . p ol i c y se a rc h
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 3. g e n e ti c a l g ori thms 217
Suppose we want to optimize a policy for the simple regulator problem Example 10.1. Using a policy opti-
mization algorithm to optimize the
described in appendix F.5. We define a stochastic policy π parameterized by parameters of a stochastic policy.
θ such that the action is generated according to
−2 0 2 −2 0 2 −2 0 2 −2 0 2
θ1 θ1 θ1 θ1
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
218 c ha p te r 10 . p ol i c y se a rc h
6
S. Mannor, R. Y. Rubinstein, and Y.
Gat, “The Cross Entropy Method
10.4 Cross Entropy Method for Fast Policy Search,” in Interna-
tional Conference on Machine Learn-
The cross entropy method (algorithm 10.4) involves updating a search distribution ing (ICML), 2003.
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
220 c ha p te r 10 . p ol i c y se a rc h
function optimize(M, π, U)
return [Link](optimize_dist(M, π, U))
end
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 5 . e vol u ti on stra te g i e s 221
Z
∇ψ E [U (θ )] = ∇ψ U (θ ) p(θ | ψ) dθ (10.6)
θ ∼ p(·|ψ)
Z
= U (θ )∇ψ p(θ | ψ) dθ (10.7)
p ( θ | ψ)
Z
= U (θ )∇ψ p(θ | ψ) dθ (10.8)
p ( θ | ψ)
Z
(10.9)
= U (θ )∇ψ log p(θ | ψ) p(θ | ψ) dθ
(10.10)
= E U (θ )∇ψ log p(θ | ψ)
θ ∼ p(·|ψ)
The introduction of the logarithm above comes from what is called the log deriva-
tive trick, which observes that ∇ψ log p(θ | ψ) = ∇ψ p(θ | ψ)/p(θ | ψ). This
computation requires knowing ∇ψ log p(θ | ψ), but we can often compute this
analytically, as discussed in example 10.2.
The search gradient can be estimated from m samples: θ (1) , . . . , θ (m) ∼ p(· | ψ):
m
1
∇ψ E [U (θ )] ≈ ∑ U (θ (i) )∇ψ log p(θ (i) | ψ) (10.11)
θ ∼ p(·|ψ) m i =1
This estimate depends on the evaluated expected utility, which itself can vary
widely. We can make our gradient estimate more resilient with rank shaping,
which replaces the utility values with weights based on the relative performance
of each sample to the other samples in its iteration. The m samples are sorted in
descending order of expected utility. Weight w(i) is assigned to sample i according
to a weighting scheme with w(1) ≥ · · · ≥ w(m) . The search gradient becomes
m
∇ψ E [U (θ )] ≈ ∑ w(i) ∇ψ log p(θ (i) | ψ) (10.12)
θ ∼ p(·|ψ) i =1
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
222 c ha p te r 10 . p ol i c y se a rc h
The multivariate normal distribution N (µ, Σ), with mean µ and covariance Example 10.2. A derivation of the
log likelihood gradient equations
Σ, is a common distribution family. The likelihood in d dimensions takes the for the multivariate Gaussian dis-
form tribution. For the original deriva-
tion and several more sophisticated
solutions for handling the positive
d 1 1
p(x | µ, Σ) = (2π )− 2 |Σ|− 2 exp − (x − µ)⊤ Σ−1 (x − µ) definite covariance matrix, see D.
2
Wierstra, T. Schaul, T. Glasmach-
ers, Y. Sun, J. Peters, and J. Schmid-
where |Σ| is the determinant of Σ. The log likelihood is huber, “Natural Evolution Strate-
gies,” Journal of Machine Learning
d 1 1 Research, vol. 15, pp. 949–980, 2014.
log p(x | µ, Σ) = − log(2π ) − log |Σ| − (x − µ)⊤ Σ−1 (x − µ)
2 2 2
The parameters can be updated using their log likelihood gradients:
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 5 . e vol u ti on stra te g i e s 223
−0.5
1 2 3 4 5 6 7 8 9 10
index in order by decreasing expected utility, i
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
224 c ha p te r 10 . p ol i c y se a rc h
The previous section introduced evolutionary strategies that can work with gen- 13
An example of this approach ap-
eral search distributions. This section will make the assumption that the search plied to policy search is explored
distribution is a spherical or isotropic Gaussian, where the covariance matrix takes by T. Salimans, J. Ho, X. Chen, S.
Sidor, and I. Sutskever, “Evolution
the form σ2 I.13 Under this assumption, the expected utility of the distribution Strategies as a Scalable Alternative
introduced in equation (10.5) simplifies to14 to Reinforcement Learning,” 2017.
arXiv: 1703.03864v2.
E [U (θ)] = E [U (ψ + σǫ)] (10.14) 14
In general, if A⊤ A = Σ, then θ =
θ∼N (ψ,σ2 I) ǫ∼N (0,I) µ + A⊤ ǫ transforms ǫ ∼ N (0, I)
into a sample θ ∼ N (µ, Σ).
The search gradient reduces to
(10.15)
h i
∇ψ E [U (θ)] = E U (θ)∇ψ log p(θ | ψ, σ2 I)
θ∼N (ψ,σ2 I) θ∼N (ψ,σ2 I)
15
D. Brockhoff, A. Auger, N.
1 Hansen, D. Arnold, and T.
= E U (θ) 2 (θ − ψ) (10.16)
2
θ∼N (ψ,σ I) σ Hohm, “Mirrored Sampling and
Sequential Selection for Evolution
1 Strategies,” in International Confer-
= E U (ψ + σǫ) 2 (σǫ) (10.17)
ǫ∼N (0,I) σ ence on Parallel Problem Solving from
Nature, 2010.
1
= E [U (ψ + σǫ)ǫ] (10.18)
σ ǫ∼N (0,I) 16
This technique was implemented
by T. Salimans, J. Ho, X. Chen, S.
Algorithm 10.6 provides an implementation of this strategy. This implemen-
Sidor, and I. Sutskever, “Evolution
tation incorporates mirrored sampling.15 We sample m/2 values from the search Strategies as a Scalable Alterna-
distribution and then generate the other m/2 samples by mirroring them about tive to Reinforcement Learning,”
2017. arXiv: 1703 . 03864v2. They
the mean. Mirrored samples reduce the variance of the gradient estimate.16 The included other techniques as well,
benefit of using this technique is shown in figure 10.8. including weight decay.
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 6 . i sotrop i c e vol u ti on a ry stra te g i e s 225
0 10 20 30 40 50 60
iteration
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
226 c ha p te r 10 . p ol i c y se a rc h
10.7 Summary
• Monte Carlo policy evaluation involves computing the expected utility associ-
ated with a policy using a large number of rollouts from states sampled from
an initial state distribution.
• The cross entropy method iteratively improves a search distribution over policy
parameters by refitting the distribution to elite samples at each iteration.
• Isotropic evolutionary strategies make the assumption that the search distribu-
tion is an isotropic Gaussian.
10.8 Exercises
Exercise 10.1. In Monte Carlo policy evaluation, how is the variance of the utility estimate
affected by the number of samples?
Solution: The variance of Monte Carlo policy evaluation is the variance of the mean of m
samples. These samples are assumed to be independent, and so the variance of the mean
is the variance of a single rollout evaluation divided by the sample size:
" #
m
(i ) 1
Var[Û (π )] = Var ∑ R(τ ) = Varτ [ R(τ )]
i =1
m
where Û (π ) is the utility from Monte Carlo policy evaluation and R(τ ) is the trajectory
reward for a sampled trajectory τ. The sample variance, therefore, decreases with 1/m.
Exercise 10.2. What effect does varying the number of samples m and the number of elite
samples melite have on cross entropy policy search?
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 8 . e x e rc i se s 227
Solution: The computational cost per iteration scales linearly with the number of samples.
More samples will better cover the search space, resulting in a better chance of identifying
better elite samples to improve the policy. The number of elite samples also has an effect.
Making all samples elite provides no feedback to the improvement process. Having too
few elite samples can lead to premature convergence to a suboptimal solution.
Exercise 10.3. Consider using evolution strategies with a univariate Gaussian distribution,
θ ∼ N (µ, ν). What is the search gradient with respect to the variance ν? What issue arises
as the variance becomes small?
1 ( x − µ )2
=− +
2ν 2ν2
log p(1 | 0, ν)
0.4
0.2
0
∂ν
∂
1 2 3 4 5
ν
We find that the gradient goes to infinity as the variance approaches zero. This is a
problem because the variance should be small when the search distribution converges.
Very large gradients can cause simple ascent methods to overshoot optima.
Exercise 10.4. Equation (10.14) defines the objective in terms of a search distribution
θ ∼ N (ψ, Σ). What advantage does this objective have over directly optimizing θ using
the expected utility objective in equation (10.1)?
Solution: The added Gaussian noise around the policy parameters can smooth discontinu-
ities in the original objective, which can make optimization more reliable.
Exercise 10.5. Which of the methods in this chapter are best suited to the fact that multiple
types of policies could perform well in a given problem?
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
228 c ha p te r 10 . p ol i c y se a rc h
Exercise 10.6. Suppose we have a parameterized policy πθ that we would like to optimize
using the Hooke-Jeeves method. If we initialize our parameter θ = 0 and the utility function
is U (θ ) = −3θ 2 + 4θ + 1, what is the largest step size α that would still guarantee policy
improvement in the first iteration of the Hooke-Jeeves method?
Solution: The Hooke-Jeeves method evaluates the objective function at the center point ±α
along each coordinate direction. In order to guarantee improvement in the first iteration of
Hooke-Jeeves search, at least one of the objective function values at the new points must
improve the objective function value. For our policy optimization problem, this means
that we are searching for the largest step size α such that either U (θ + α) or U (θ − α) is
greater than U (θ ).
Since the underlying utility function is parabolic and concave, the largest step size that
would still lead to improvement is slightly less than the width of the parabola at the current
point. Thus, we compute the point on the parabola opposite the current point, θ ′ at which
U ( θ ′ ) = U ( θ ):
The point on the parabola opposite the current point is thus θ ′ = 34 . The distance be-
tween θ and θ ′ is 43 − 0 = 43 . Thus, the maximal step size we can take and still guarantee
improvement in the first iteration is just under 43 .
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]
10 . 8 . e x e rc i se s 229
p ( θ | ψ ) = ψ θ (1 − ψ )1− θ
log p(θ | ψ) = log ψθ (1 − ψ)1−θ
Exercise 10.8. Compute the sample weights for search gradient estimation with rank
shaping given m = 3 samples.
Solution: We first compute the numerator of the first term from equation (10.13), for all i:
i=1 max 0, log 32 + 1 − log 1 = log 25
i=2 max 0, log 23 + 1 − log 2 = log 45
i=3 max 0, log 23 + 1 − log 3 = 0
log 25 1
w (1) = 5 5
− = 0.47
log 2 + log + 04
3
log 45 1
w (2) = 5
− = −0.14
log + log 45 + 0 3
2
0 1
w (3) = 5 5
− = −0.33
log 2 + log 4 + 0 3
© 2022 Massachusetts Institute of Technology, shared under a Creative Commons CC-BY-NC-ND license.
2022-05-07 21:55:16-07:00, comments to bugs@[Link]