Projection-Based Constrained Policy Optimization
Tsung-Yen Yang, Justinian Rosca, Karthik Narasimhan, Peter J. Ramadge
Introduction
Recent advances in deep reinforcement learning (RL) have demonstrated excellent performance on several domains ranging from games like Go (Silver et al., 2017) and StarCraft (AlphaStar, 2019) to robotic control (Levine et al., 2016). In these settings, agents are allowed to explore the entire state space and experiment with all possible actions during training. However, in many real-world applications such as self-driving cars and unmanned aerial vehicles, considerations of safety, fairness and other costs prevent the agent from having complete freedom to explore. For instance, an autonomous car, while optimizing its driving policies, must not take any actions that could cause harm to pedestrians or property (including itself). In effect, the agent is constrained to take actions that do not violate a specified set of constraints on state-action pairs. In this work, we address the problem of learning control policies that optimize a reward function while satisfying predefined constraints.
The problem of policy learning with constraints is more challenging since directly optimizing for the reward, as in Q-Learning (Mnih et al., 2013) or policy gradient (Sutton et al., 2000), will usually violate the constraints. One approach is to incorporate constraints into the learning process by forming a constrained optimization problem. Then perform policy updates using a conditional gradient descent with line search to ensure constraint satisfaction (Achiam et al., 2017). However, the base optimization problem can become infeasible if the current policy violates the constraints. Another approach is to add a hyperparameter weighted copy of the constraints to the objective function (Tessler et al., 2018). However, this incurs the cost of extensive hyperparameter tuning.
To address the above issues, we propose projection-based constrained policy optimization (PCPO). This is an iterative algorithm that performs policy updates in two stages. The first stage maximizes reward using a trust region optimization method (e.g., TRPO (Schulman et al., 2015a)) without constraints. This might result in a new intermediate policy that does not satisfy the constraints. The second stage reconciles the constraint violation (if any) by projecting the policy back onto the constraint set, i.e., choosing the policy in the constraint set that is closest to the selected intermediate policy. This allows efficient updates to ensure constraint satisfaction without requiring a line search (Achiam et al., 2017) or adjusting a weight (Tessler et al., 2018). Further, due to the projection step, PCPO offers efficient recovery from infeasible (i.e., constraint-violating) states (e.g., due to approximation errors), which existing methods do not handle well.
We analyze PCPO theoretically and derive performance bounds for the algorithm. Specifically, based on information geometry and policy optimization theory, we construct a lower bound on reward improvement, and an upper bound on constraint violations for each policy update. We find that with a relatively small step size for each policy update, the worst-case constraint violation and reward degradation are tolerable. We further analyze two distance measures for the projection step onto the constraint set. We find that the convergence of PCPO is affected by the smallest and largest singular values of the Fisher information matrix used during training. By observing these singular values, we can choose the appropriate projection best suited to the problem.
Empirically, we compare PCPO with state-of-the-art algorithms on four different control tasks, including two Mujoco environments with safety constraints introduced by Achiam et al. (2017) and two traffic management tasks with fairness constraints introduced by Vinitsky et al. (2018). In all cases, the proposed algorithm achieves comparable or superior performance to prior approaches, averaging more reward with fewer cumulative constraint violations. For instance, across the above tasks, PCPO achieves 3.5 times fewer constraint violations and around 15% more reward. This demonstrates the ability of PCPO robustly learn constraint-satisfying policies, and represents a step towards reliable deployment of RL in real problems.
Preliminaries
We aim to learn a policy that maximizes a cumulative discounted reward, denoted by
while satisfying constraints, i.e., making a cumulative discounted cost constraint below a desired threshold , denoted by
where is the discount factor, is the trajectory (), and is shorthand for showing that the distribution over the trajectory depends on where is the initial state distribution.
Kakade & Langford (2002) give an identity to express the performance of policy in terms of the advantage function over another policy
Projection-Based Constrained Policy Optimization
To robustly learn constraint-satisfying policies, we develop PCPO – a trust region method that performs policy updates corresponding to reward improvement, followed by projections onto the constraint set. PCPO, inspired by projected gradient descent, is composed of two steps for each update, a reward improvement step and a projection step (This is illustrated in Fig. 1).
Reward Improvement Step. First, we optimize the reward function by maximizing the reward advantage function subject to a Kullback-Leibler (KL) divergence constraint. This constraints the intermediate policy to be within a -neighbourhood of :
Projection Step. Second, we project the intermediate policy onto the constraint set by minimizing a distance measure between and :
The projection step ensures that the constraint-satisfying policy is close to We consider two distance measures : norm and KL divergence. In contrast, using KL divergence projection in the probability distribution space allows us to provide provable guarantees for PCPO.
In safety-critical applications such as autonomous cars, one cares about how worse the performance of a system evolves when applying a learning algorithm. To this end, for PCPO with KL divergence projection, we analyze the worst-case performance degradation for each policy update when the current policy satisfies the constraint. The following theorem provides a lower bound on reward improvement, and an upper bound on constraint violation for each policy update.
where is the step size in the reward improvement step.
Theorem 3.1 indicates that if is small, the worst-case performance degradation is tolerable.
Due to approximation errors or the random initialization of policies, PCPO may have a constraint-violating update. Theorem 3.1 does not give the guarantee on updating a constraint-violating policy. Hence we analyze worst-case performance degradation for each policy update when the current policy violates the constraint. The following theorem provides a lower bound on reward improvement, and an upper bound on constraint violation for each policy update.
where is the step size in the reward improvement step.
Theorem 3.2 indicates that when the policy has greater constraint violation ( increases), its worst-case performance degradation increases. Note that Theorem 3.2 reduces to Theorem 3.1 if the current policy satisfies the constraint (). The proofs of Theorem 3.1 and Theorem 3.2 follow from the fact that the projection of the policy is non-expansive, i.e., the distance between the projected policies is smaller than that of the unprojected policies. This allows us to measure it and bound the KL divergence between the current policy and the new policy.
PCPO Updates
For a large neural network policy with many parameters, it is impractical to directly solve for the PCPO update in Problem 2 and Problem 3 due to the computational cost. However, with a small step size , we can approximate the reward function and constraints with a first order expansion, and approximate the KL divergence constraint in the reward improvement step, and the KL divergence measure in the projection step with a second order expansion. We now make several definitions:
Reward Improvement Step. We linearize the objective function at subject to second order approximation of the KL divergence constraint in order to obtain the following updates:
Projection Step. If the projection is defined in the parameter space, we can directly use norm projection. On the other hand, if the projection is defined in the probability space, we can use KL divergence projection. This can be approximated through the second order expansion. Again, we linearize the cost constraint at This gives the following update for the projection step:
where for norm projection, and for KL divergence projection. One may argue that using linear approximation to the constraint set is not enough to ensure constraint satisfaction since the real constraint set is maybe non-convex. However, if the step size is small, then the linearization of the constraint set is accurate enough to locally approximate it.
We solve Problem (4) and Problem (5) using convex programming (See the supplemental material for the derivation). For each policy update, we have
We assume that does not have as an eigenvalue and hence it is invertible. PCPO requires to invert , which is impractical for huge neural network policies. Hence we use the conjugate gradient method (Schulman et al., 2015a). Algorithm 1 shows the pseudocode. (See supplemental material for a discussion of the tradeoff between the approximation error and computational efficiency of the conjugate gradient method.)
Related Work
Policy Learning with Constraints. Learning constraint-satisfying policies has been explored in the context of safe RL (Garcia & Fernandez, 2015).
The agent learns policies either by (1) exploration of the environment (Achiam et al., 2017; Tessler et al., 2018; Chow et al., 2017) or (2) through expert demonstrations (Ross et al., 2011; Rajeswaran et al., 2017; Gao et al., 2018). However, using expert demonstrations requires humans to label the constraint-satisfying behavior for every possible situation. The scalability of these rule-based approaches is an issue since many real autonomous systems such as self-driving cars and industrial robots are inherently complex. To overcome this issue, PCPO uses the first approach in which the agent learns by trial and error. To prevent the agent from having constraint-violating behavior during exploring the environment, PCPO uses the projection onto the constraint set to ensure constraint satisfaction throughout learning.
Constraint satisfaction by Projections. Using a projection onto a constraint set has been explored for general constrained optimization in other contexts. For example, Akrour et al. (2019) projects the policy from a parameter space onto the constraint. This ensures the updated policy stays close to the previous policy. In contrast, we examine constraints that are defined in terms of states and actions. Similarly, Chow et al. (2019) proposes -projection. This approach projects the policy parameters onto the constraint set. However, no provide provable guarantees are provided. Moreover, the problem is formulated by adding the weighted constraint to the reward objective function. Since the weight must be tuned, this incurs the cost of hyperparameter tuning. In contrast, PCPO eliminates the cost of the hyperparameter tuning, and provides provable guarantees on learning constraint-satisfying policies.
Comparison to CPO (Achiam et al., 2017). Perhaps the closest work to ours is the approach of Achiam et al. (2017), who proposes the constrained policy optimization (CPO) algorithm to solve the following:
CPO simultaneously considers the trust region and the constraint, and uses the line search to select a step size (This is illustrated in Fig. 2). The update rule of CPO becomes infeasible when the current policy violates the constraint (). CPO recovers by replacing Problem (7) with an update to purely decrease the constraint value: This update rule may lead to a slow progress in learning constraint-satisfying policies. In contrast, PCPO first optimizes the reward and uses the projection to satisfy the constraint. This ensures a feasible solution, allowing the agent to improve the reward while ensuring constraint satisfaction simultaneously.
Experiments
Tasks. We compare the proposed algorithm with existing approaches on four control tasks in total: two tasks with safety constraints ((a) and (b) in Fig. 3), and two tasks with fairness constraints ((c) and (d) in Fig. 3). These tasks are briefly described in the caption of Fig. 3. The first two tasks – Gather and Circle – are Mujoco environments with state space constraints introduced by Achiam et al. (2017). The other two tasks – Grid and Bottleneck – are traffic management problems where the agent controls either a traffic light or a fleet of autonomous vehicles. This is especially challenging since the dimensions of state and action spaces are larger, and the dynamics of the environment are inherently complex.
Baselines. We compare PCPO with four baselines outlined below.
(1) Constrained Policy Optimization (CPO) (Achiam et al., 2017).
(2) Primal-dual Optimization (PDO) (Chow et al., 2017). In PDO, the weight (dual variables) is learned based on the current constraint satisfaction. A PDO policy update solves:
where is updated using Here is a fixed learning rate.
(3) Fixed-point Policy Optimization (FPO). A variant of PDO that solves Eq. (8) using a constant .
(4) Trust Region Policy Optimization (TRPO) (Schulman et al., 2015a). The TRPO policy update is an unconstrained one:
Note that TRPO ignores any constraints. We include it to serve as an upper bound baseline on the reward performance.
Since the main focus is to compare PCPO with the state-of-the-art algorithm, CPO, PDO and FPO are not shown in the ant circle, ant gather, grid and bottleneck tasks for clarity.
Overall Performance. The learning curves of the discounted reward and the undiscounted constraint value (the total number of constraint violation) over policy updates are shown for all tested algorithms and tasks in Fig. 4. The dashed line in the constraint figure is the cost constraint threshold . The curves for baseline oracle, TRPO, indicate the reward and constraint value when the constraint is ignored. Overall, we find that PCPO is able to improve the reward while having the fastest constraint satisfaction in all tasks. In particular, PCPO is the only algorithm that learns constraint-satisfying policies across all the tasks. Moreover we observe that (1) CPO has more constraint violation than PCPO, (2) PDO is too conservative in optimizing the reward, and (3) FPO requires a significant effort to select a good value of .
We also observe that in Grid and Bottleneck task, there is slightly more constraint violation than the easier task such as point circle and point gather. This is due to complexity of the policy behavior and non-convexity of the constraint set. However, even with a linear approximation of the constraint set, PCPO still outperforms CPO with 85.15% and 5.42 times less constraint violation in Grid and Bottleneck task, respectively.
These observations suggest that projection step in PCPO drives the agent to learn the constraint-satisfying policy within few policy updates, giving PCPO an advantage in applications. To show that PCPO achieves the same reward with less constraint violation, we examine the reward versus the cumulative constraint value for the tested algorithms in point circle and point gather task shown in Fig. 5. We observe that PCPO outperforms CPO significantly with 66 times and 15 times less constraint violation under the same reward improvement in point circle and point gather tasks, respectively. This observation suggests that PCPO enables the agent to cautiously explore the environment under the constraints.
Comparison of PCPO with KL Divergence vs. Norm Projections. We observe that PCPO with norm projection is more constraint-satisfying than PCPO with KL divergence projection. In addition, PCPO with norm projection tends to have reward fluctuation (point circle, ant circle, and ant gather tasks), while with KL divergence projection tends to have more stable reward improvement (all the tasks).
The above observations indicate that since the gradient of constraint is not multiplied by the Fisher information matrix, the gradient of the constraint is not aligned with the gradient of the reward. This reduces the reward improvement. However, when the Fisher information matrix is ill-conditioned or not well-estimated, especially in a high dimensional policy space, a bad constraint update direction may hinder constraint satisfaction (ant circle, ant gather, grid and bottleneck tasks). In addition, since the stationary points of KL divergence and norm projections are different, they converge to policies with different reward (observe that PCPO with norm projection has higher reward than the one with KL divergence projection around 2250 iterations in ant circle task, and has less reward in point gather task).
Discussion of PDO and FPO. For the PDO baseline, we see that its constraint values fluctuate especially in the point circle task. This phenomena suggests that PDO is not able to adjust the weight quickly enough to meet the constraint threshold, which hinders the efficiency of learning constraint-satisfying policies. If the learning rate is too big, the agent will be too conservative in improving the reward. For FPO, we also see that it learns near constraint-satisfying policies with slightly larger reward improvement compared to PDO. However, in practice FPO requires a lot of engineering effort to select a good value of . Since PCPO requires no hyperparameter tuning, it has the advantage of robustly learning constraint-satisfying policies over PDO and FPO.
Conclusion
We address the problem of finding constraint-satisfying policies. The proposed algorithm – projection-based constrained policy optimization (PCPO) – optimizes for the reward function while using the projections to ensure constraint satisfaction. This update rule allows PCPO to maintain the feasibility of the optimization problem of each update, addressing the issue of state-of-the-art approaches. The algorithm achieves comparable or superior performance to state-of-the-art approaches in terms of reward improvement and constraint satisfaction in all cases. We further analyze the convergence of PCPO, and find that certain tasks may prefer either KL divergence projection or norm projection. Future work will consider the following: (1) examining the Fisher information matrix to iteratively prescribe the choice of projection for policy update, and hence robustly learn constraint-satisfying policies with more reward improvement, and (2) using expert demonstration or other domain knowledge to reduce the sample complexity.
The authors would like to thank the anonymous reviewers and the area chair for their comments. Tsung-Yen Yang thanks Siemens Corporation, Corporate Technology for their support.
References
Appendix S Supplementary Materials
To prove the policy performance bound when the current policy is feasible (i.e., constraint-satisfying), we prove the KL divergence between and for the KL divergence projection. We then prove our main theorem for the worst-case performance degradation.
By the Bregman divergence projection inequality, being in the constraint set, and being the projection of the onto the constraint set, we have
The derivation uses the fact that KL divergence is always greater than zero. We know that KL divergence is asymptotically symmetric when updating the policy within a local neighbourhood. Thus, we have
Now we use Lemma S.1 to prove our main theorem.
where is the step size in the reward improvement step.
By the theorem in Achiam et al. (2017) and Lemma S.1, we have the following reward degradation bound for each policy update:
Again, we have the following constraint violation bound for each policy update:
S.2 Proof of Theorem 3.2: Performance Bound on Updating the Constraint-violating Policy
To prove the policy performance bound when the current policy is infeasible (i.e., constraint-violating), we prove the KL divergence between and for the KL divergence projection. We then prove our main theorem for the worst-case performance degradation.
We define the sublevel set of cost constraint function for the current infeasible policy :
By the Three-point Lemma, for these three polices and , with (this is illustrated in Fig. 6), we have
And since is small, we have given . Thus, the third term in Eq. (11) can be eliminated.
Now we use Lemma S.3 to prove our main theorem.
where is the step size in the reward improvement step.
Following the same proof in Theorem S.2, we complete the proof. ∎
Note that the bounds we obtain for the infeasibe case; to the best of our knowledge, are new results.
S.3 Proof of Analytical Solution to PCPO
Consider the PCPO problem. In the first step, we optimize the reward:
and in the second step, we project the policy onto the constraint set:
assuming that is invertible to get a unique solution.
For the first problem, since is the Fisher Information matrix, which automatically guarantees it is positive semi-definite. Hence it is a convex program with quadratic inequality constraints. Hence if the primal problem has a feasible point, then Slater’s condition is satisfied and strong duality holds. Let and denote the solutions to the primal and dual problems, respectively. In addition, the primal objective function is continuously differentiable. Hence the Karush-Kuhn-Tucker (KKT) conditions are necessary and sufficient for the optimality of and We now form the Lagrangian:
And we have the following KKT conditions:
By Eq. (13), we have And by plugging Eq. (13) into Eq. (14), we have Hence we have our optimal solution:
which also satisfies Eq. (15), Eq. (16), and Eq. (17).
Following the same reasoning, we now form the Lagrangian of the second problem:
And we have the following KKT conditions:
By Eq. (19), we have And by plugging Eq. (19) into Eq. (20) and Eq. (22), we have Hence we have our optimal solution:
which also satisfies Eq. (21) and Eq. (23). Hence by Eq. (18) and Eq. (24), we have
Since the right hand side of Eq. (25) can be made arbitrarily small for a given , and hence we have:
Let be such that We show that must be the optimal solution. Let and Then we have
Based on Lemma S.6, we have the following theorem.
The proof of the theorem is based on working in a Hilbert space and the non-expansive property of the projection. We first prove stationary points for PCPO with the KL divergence and norm projections, and then prove the change of the objective value.
When in stationary points , we have
For the KL divergence projection (), Eq. (28) boils down to and for the norm projection (), Eq. (28) is equivalent to
Now we prove the second part of the theorem. Based on Lemma S.6, for the KL divergence projection, we have
By Eq. (29), and -smooth continuous function we have
By the definition of the condition number and Eq. (31), we have
S.5 Additional Computational Experiments
For detailed explanation of the task in Achiam et al. (2017), please refer to the appendix of Achiam et al. (2017). For detailed explanation of the task in Vinitsky et al. (2018), please refer to Vinitsky et al. (2018).
We use GAE- approach (Schulman et al., 2015b) to estimate and . For the simulations in the gather and circle tasks, we use neural network baselines with the same architecture and activation functions as the policy networks. For the simulations in the grid and bottleneck tasks, we use linear baselines.
The hyperparameters of each task for all algorithms are as follows (PC: point circle, PG: point gather, AC: ant circle, AG: ant gather, Gr: grid, and BN: bottleneck tasks):
Note that we do not use a learned model to predict the probability of entering an undesirable state within a fixed time horizon as CPO did for cost shaping.
S.5.2 Experiment Results
To examine the performance of the algorithms with different metrics, we provide the learning curves of the cumulative constraint value over policy update, and the reward versus the cumulative constraint value for the tested algorithms and task pairs in Section 6 shown in Fig. 8. The second metric enables us to compare the reward difference under the same number of cumulative constraint violation.
CPO has more cumulative constraint violation than PCPO.
PCPO with norm projection has less cumulative constraint violation than KL divergence projection except for the point circle and point gather tasks. This observation suggests that the Fisher information matrix is not well-estimated in the high dimensional policy space, leading to have more constraint violation.
PCPO has more reward improvement compared to CPO under the same number of cumulative constraint violation in point circle, point gather, ant circle, ant gather, and bottleneck task.
S.5.3 CPO without Line Search
Due to approximation errors, CPO performs line search to check whether the updated policy satisfies the trust region and cost constraints. To understand the necessity of line search in CPO, we conducted the experiment with and without line search shown in Fig. 9. The step size is set to We find that CPO without line search tends to (1) have large reward variance especially in the point circle task, and (2) learn constraint-satisfying policies slightly faster. These observations suggest that line search is more conservative in optimizing the policies since it usually take smaller steps. However, we conjecture that if using smaller , the effect of line search is not significant.
S.5.4 The Tasks with Harder Constraints
To understand the stability of PCPO and CPO when deployed in more constraint-critical tasks, we increase the difficulty of the task by setting the constraint threshold to zero and reduce the safe area. The learning curve of discounted reward and constraint value over policy updates are shown in Fig. 10.
We observe that even with more difficult constraint, PCPO still has more reward improvement and constraint satisfaction than CPO, whereas CPO needs more feasible recovery steps to satisfy the constraint. In addition, we observe that PCPO with norm projection has high constraint variance in point circle task, suggesting that the reward update direction is not well aligned with the cost update direction. We also observe that PCPO with norm projection converges to a bad local optimum in terms of reward in point gather task, suggesting that in order to satisfy the constraint, the cost update direction destroys the reward update direction.
S.5.5 Smaller Batch Samples
To learn policies under constraints, PCPO and CPO require to have a good estimation of the constraint set. However, PCPO may project the policy onto the space that violates the constraint due to the assumption of approximating the constraint set by linear half space constraint. To understand whether the estimation accuracy of the constraint set affects the performance, we conducted the experiments with batch sample size reducing to of the previous experiments (only 500 samples for each policy update) shown in Fig. 11.
We find that smaller training samples affects the performance of the algorithm, creating more reward and cost fluctuation. However, we observe that even with smaller training samples, PCPO still has more reward improvement and constraint satisfaction than CPO.
S.6 Analysis of the Approximation Error and the Computational Cost of the Conjugate Gradient Method
In the Grid task, we observe that PCPO with KL divergence projection does worse in reward than TRPO, which is expected since TRPO ignores constraints. However, TRPO actually outperforms PCPO with KL divergence projection in terms of constraint, which is unexpected since by trying to consider the constraint, PCPO with KL divergence projection has made constraint satisfaction worse.
To solve this issue, one can have more epochs of conjugate gradient method. This is because that the convergence of conjugate gradient method is controlled by the condition number (Shewchuk, 1994); the larger the condition number is, the more epochs the algorithm needs to get accurate approximation. In our experiments, we set the number of iteration of conjugate gradient method to be 10 to tradeoff between the computational efficiency and the accuracy across all tested algorithms and task pairs.
To verify our observation, we compare the condition number of the Fisher information matrix, and the approximation error of the constraint update direction over training epochs with different number of iteration of the conjugate gradient method shown in Fig. 12.
We observe that the Fisher information matrix is ill-conditioned, and the one with larger number of iteration has less error and more constraint satisfaction. This observation confirms our discussion.
Theorem 4.1 states that a stationary point of PCPO with KL divergence projection is different from the one of PCPO with norm projection. See Fig. 13 for illustration. To compare both stationary points, we consider the following example shown in Fig. 14. We maximize a non-convex function subject to the constraint where and is an all-one vector. An optimal solution to this constrained optimization problem is infinity. Fig. 14(a) shows the update direction that combines the objective and the cost constraint update directions for both projections. It shows that PCPO with KL divergence projection has stationary points with in the boundary of the constraint set (observe that the update direction is zero for PCPO with KL divergence projection at and ), whereas PCPO with norm projection does not have stationary points in the boundary of the constraint set. Furthermore, Fig. 14(b) shows the optimization paths for both projections with one initial starting point. It shows that starting at the initial point PCPO with KL divergence projection with the initial point converges to a local optimum, whereas norm projection converges to infinity. However, the above example does not necessary means that PCPO with norm projection always find a better optimum. For example, if the gradient direction of the objective is zero in the constraint set or in the boundary, then both projections may converge to the same stationary point.