Barrier Functions Inspired Reward Shaping for
Reinforcement Learning
Abstract
Reinforcement Learning (RL) has progressed from simple control tasks to complex real-world challenges with large state spaces. While RL excels in these tasks, training time remains a limitation. Reward shaping is a popular solution, but existing methods often rely on value functions, which face scalability issues. This paper presents a novel safety-oriented reward-shaping framework inspired by barrier functions, offering simplicity and ease of implementation across various environments and tasks. To evaluate the effectiveness of the proposed reward formulations, we conduct simulation experiments on CartPole, Ant, and Humanoid environments, along with real-world deployment on the Unitree Go1 quadruped robot. Our results demonstrate that our method leads to 1.4-2.8 times faster convergence and as low as 50-60% actuation effort compared to the vanilla reward. In a sim-to-real experiment with the Go1 robot, we demonstrated better control and dynamics of the bot with our reward framework. We have open-sourced our code at https://github.com/Safe-RL-IISc/barrier_shaping.
I INTRODUCTION
Reinforcement Learning (RL) has demonstrated substantial success in various domains, including gaming (e.g., Minecraft [1] and Atari [2]), language model optimization (e.g., Sparrow [3] and InstructGPT [4]), and robotics [5, 6]. However, all of these methods including RLHF [7], which addresses RL’s sample efficiency issues in other domains, can be costly when applied to robotics, including sim-to-real transfer scenarios. Despite having its limitations, reward shaping provides a simpler, more accessible and efficient alternative to address these issues even now. Well-crafted reward functions guide the agent’s behaviour towards desired outcomes, facilitating successful learning of the intended task. Previous works ([8], [9]) have established reward-shaping to accelerate algorithm convergence. Potential-based reward shaping [10] is a well-known work that suggests adding a potential function term initialized to the value function for reward-shaping. However, it needs a good estimate of the value function, which is challenging in the case of sparse reward models and in complex environments due to dimensionality [11], [12], [13].

With a view to improve upon existing works on reward shaping methods, we introduce Barrier Function (BF) inspired reward shaping, a simple, safety-focused framework that enhances training efficiency and safety. Our approach uses BFs to supplement the base reward in an environment. Intuitively, when a BF is positive, the system’s state is within the safe region. Accordingly, we can construct an inequality by using the derivative of the barrier function, which is in turn encoded in the form of a reward. This reward-shaping term encourages the RL agent’s states to not only remain within the safe zone, but also ensure that undesirable behaviors are avoided at the limits. In this paper, we propose two BF-based reward formulations: the exponential barrier and the quadratic barrier. We assess our framework across various environments, from CartPole[14] to Humanoid[15]. Additionally, our approach improves the walking performance of agents, as evident in Fig 1. We also demonstrate sim-to-real transferability by applying our framework to the Unitree Go1 robot [16]. The key highlights of the proposed framework are:
-
•
A safety-oriented, intuitive and easy-to-implement barrier function-inspired reward shaping framework.
-
•
The framework leads to faster convergence towards the goal and efficient state exploration by enforcing the system within the safe set.
-
•
It leads to lesser energy expenditure as the barrier function constrains the states within desired limits, thus avoiding extreme actions.
II Related Work
Reward Shaping : Positive linear transformation [17], [18], [19], leverages the principles of utility theory to enhance agent performance by adding rewards for state transitions as the difference between the values of the arbitrary potential functions applied to the respective states. In [20], a Bayesian approach to reward shaping is presented, which incorporates prior beliefs and adapts with experience. While effective, this method is model-based and may not generalize well across different environments. Signal temporal logic, explored in [21] and [22], can serve as a formal specification for reward formulation but is also model-dependent. The safety and stability of the agent and environment remain unaddressed. To the best of our knowledge, for the first time, we explore the agent’s safety and stability using reward shaping that ensures faster convergence along with less energy consumption during training and testing.
It is worth mentioning that, in our formulation, we omit energy terms from the shaped reward as they often clash with the environment’s goals. For example, in CartPole, including an energy term would prioritize having the pendulum vertically down, which isn’t the intended objective. Adjusting energy terms requires extra tuning, complicating things for larger robot models. Similarly, alternative reward-shaping approaches like angle limits and velocity limits may lead to undesirable behavior, keeping the agent within bounds but failing to achieve the main objective. Intuitively, there is a relationship between the positions and velocities that must be respected at the limits. Violation of this can restrict exploration and learning, hindering effective policy development. The CBF based constraints encode this relationship, thereby ensuring that the position-velocity values are not in conflict.
Safety : [23] introduces a framework that combines model-free reinforcement learning with model-based CBF controllers and learned system dynamics to ensure safety during exploration. Since it is model-based, a good knowledge of the system dynamics is required. [24] proposes using reachability constraints to expand the feasible set, resulting in a less conservative policy compared to CBF-based approaches. [25] explores a more data-driven approach using two neural networks to learn the controller and safety barrier certificate simultaneously achieving a verification-in-the-loop synthesis, but requires formal verification (using SMT solvers) to ensure constraint satisfaction. Lastly, [26] discusses learning to restructure an MDP reward function for accelerated reinforcement learning, but it can be computationally intensive. Tan et al. [27] link CBF to a value function to enforce verifiable safety. Our approach aligns with the idea of leveraging model-based constraints for improved performance, but it doesn’t require learning system dynamics, making it more generalizable. We also allow for adjustable constraint levels (soft or hard) to prioritize returns or constraint satisfaction. Furthermore, we eliminate the need for formal verification, as our formulation inherently satisfies constraints.
III Preliminary
III-A Reinforcement Learning
Reinforcement learning (RL) can be described as a discounted Markov decision process (MDP), defined by the tuple = ( r, ), where is a set of states, is a set of actions, is the deterministic state transition function, is the reward function, and (0,1) is the discount factor. In RL, the goal of the learning algorithm is to converge on a policy : that maximizes the total (discounted) reward after performing actions on an MDP, i.e., the objective is to maximize , where is the output of the reward function for the sample at instance . Since a policy maps a state to an action, the value of a policy is evaluated according to the discounted cumulative reward. Given this MDP, we are interested in shaping the rewards by using barrier functions, described in the next section.
III-B Barrier Functions
For practical applications, we often want the system state to stay within a safe region, denoted as a set . The set is defined as the super-level set of a continuously differentiable function satisfying,
(1) | ||||
(2) | ||||
(3) |
where and denote the interior and the boundary of the set , respectively. It is assumed that is non-empty and has no isolated points, i.e. and . The set is said to be forward invariant (safe) if . We can mathematically verify the safety of the set by establishing the existence of a barrier function. We have the following definition of a barrier function (BF) from [28].
Definition III.1
Here is a strictly increasing continuous function, with (0) = 0. is widely called an extended class function. Note that , where is the time derivative of . Even if the MDP is in discrete time, , and consequently , can be calculated approximately as , where are the samples of the states obtained at time steps and , and is the time interval between two samples. It is worth mentioning that the classical definition has the actions or controls as one of the arguments along with model in (4), thereby making it a control barrier function (CBF). For this paper, we avoid the use of inputs and make estimates of the derivative of by using . This allows us to use barrier functions in a model-free way and is sufficient for the presented work as our focus is on reward shaping.
If we are able to restrict the states of the system in such a way that the inequality (4) is satisfied, then we know that the set is forward invariant (safe). We can use this idea to shape the reward functions in such a way that any violation of safety causes a loss of reward. We will formally show the reward-shaping methodology in the next section.
IV Reward Shaping Methodology
IV-A Reward Shaping using Barrier Functions
Having described BFs and their associated formal results, we now discuss BF-inspired reward shaping in the context of RL. Reward shaping is a method for engineering a reward function to provide more frequent feedback on appropriate behaviours. To illustrate our framework, we propose the shaped reward in (5), which we obtain by adding a term to the traditional vanilla reward .
(5) |
is our barrier function inspired reward shaping term. We emphasise that the new reward depends on . From Definition III.1, is forward invariant if and only if there exists a barrier function such that it satisfies (4). Thus we define as
(6) |
In order to satisfy (4) we have taken the extended class function as , . This gives our shaping term the desirable property of positively rewarding the agent when it is in a set of safe states while negatively rewarding otherwise.
The BF : is chosen to be a suitable function that constrains specific quantities in , such that it would lead to desirable properties like actuation safety and training efficiency. The following section provides specific examples of BF-based reward shaping.
Remark IV.1
Since, in RL task, our goal is to find a policy that maximizes total (discounted) reward, as we maximise the proposed reward (5) it will also enforce the condition (4) in Definition III.1 and thus implies safe execution of the task. However, it is essential to note that this approach does not provide a safety guarantee and does not imply safety during training.
IV-B Barrier Function Formulation
In an RL task, some state variables should ideally lie between a safe range of values. Violating these bounds can result in undesirable behaviours. To constrain them within their safe bounds, we must use an appropriate barrier function, parameterized by . We propose two barrier functions: a quadratic function (7) and an exponential function (8).
(7) | ||||
(8) |
where , is the vector parameter, and is the set of indices for the elements of the state variables of the model whose values we want to constrain. In particular, is element of state vector , which is the value of the state variable upon which we wish to enforce the bounds (, ). The choice of and the bounds (, ) is elaborated in the following section.
Since is positive for an only when , and negative otherwise, qualifies as a barrier function and can be computed by Eq. (6).
For appropriate values of , is flat within the bounds and tapers down sharply outside them, which allows for better exploration while staying within the bounds. In contrast, the shape of makes it more suitable for tasks that require to be constrained near the central values (Fig. 2).
The reward is a function of both and . Thus, it jointly constrains them depending on their values (Fig. 3). To get a more intuitive understanding, consider the following,
(9) |
Taking the partial derivative of with respect to gives,
(10) |
Now, if the barrier function is taken to be a concave function, then there exists an such that for all and negative otherwise. Thus, from (10) we can infer that when i.e. it is near the lower bound, is positive. As an RL policy wants to maximize , it promotes to be positive. Given that denotes the rate of change of over time, a positive causes to increase, moving it away from the lower bound . Conversely, the opposite effect applies when is near . In both cases, is promoted to move towards .
Thus, works with both and to promote safety. This effect is lacking with other reward shaping methods that constrain only within some bounds for safety. It can be verified that both of our proposed barriers and are concave functions with .
V Simulation Experiments
We experimentally evaluate our BF-based reward-shaping formulation on OpenAI Gym’s Cartpole [29], and MuJoCo environments like Half-Cheetah [30], Humanoid [15], and Ant [14]. We used the Twin Delayed DDPG (TD3) [31] algorithm with two variants of the BF-based reward shaping: (7) and (8), which yield the and policies respectively. The environment’s vanilla reward is used as the baseline. All experiments were performed on a Ryzen Threadripper CPU, 64GB RAM, and an NVIDIA RTX 3080.
V-A Cartpole
Reward Shaping: The state-space contains the pole angle , pole angular velocity , cart’s position , and velocity . The task consists of balancing a pole attached to a moving cart, i.e. we want . The vanilla reward is = +1 for every step taken. We define as the pole angle’s desired threshold range for the goal ( = 0) around which we want the pendulum to stabilize, hence . This bound is taken as the maximum angle deviation from which the pole can balance itself. According (5)-(6), the quadratic BF (7) shaped reward is given as, (taking = 1, )
(11) |
Results: Fig. 5 shows energy expended till stabilisation vs initial angle for a range of angles. As can be read, the performance of policy using vanilla reward is energy-expensive compared to that of with . For one unit of energy spent by the vanilla policy, policy spends 0.79 units, policy spends 0.59 units. Fig. 5 describes the control performance for the same initial state. In contrast to policy, the Vanilla policy exhibits chaotic behaviour with no convergence to the value. Thus our policy maintains safety during deployment. In Fig. 5 multiple barrier refers to constructed on and along with .
V-B MuJoCo Walker Environments
Reward Shaping: For walkers like Half-Cheetah [30], Ant [14], and Humanoid [15], the task is to learn to run as fast as possible. We use the same formulation for all these environments as essentially they all have a similar state space containing , and , where and are the set of agent’s joint angles and angular velocities, respectively. The vanilla reward given by (12) has multiple terms that guide the agent to move forward () without falling () and minimizing the contact forces ().
(12) |
Since the task involves running, we seek to constrain the agent’s joint angles within a range of angles . In this case, is the set of all joint angles. Thus, is analogous to . These bounds correspond to the range of angles within which a joint can safely turn, violating which could lead to damage to the actuators or collision with other parts of the body. These general bounds are specified in the robot description and do not require additional domain knowledge. The optimal parameters for (7)-(8) were found by performing a grid search.
Metrics: The metrics used to evaluate the policy performance are () Actuation Coefficient, computed from the episodic energy-velocity curve (Fig. 7), () Training Speed, computed from the velocity-timestep curve (Fig. 7). All the experiments were repeated for ten random seeds.
The energy used by a walker in a time-step is calculated by
(13) |
where is the set of all joints of the walker, is the torque exerted by the agent on the joint , and is the change in the joint angle. The episodic energy is the sum of over an episode. Since the task of these environments is to maximise the velocity , we define the Actuation Coefficient (given in Table. LABEL:tab:main-res) as
(14) |
This term represents the energy expended or the effort made by the agent to achieve a certain kinetic energy.
Results: Table LABEL:tab:main-res shows that for Humanoid, policy takes only about 49% actuation energy to achieve the same kinetic energy as the vanilla policy. Fig. 7 (Left) shows that the policies reach a higher maximum velocity for the same training time-steps. Fig. 7 (Left) shows that policy converges to a higher velocity in much fewer training time-steps. Table LABEL:tab:main-res shows that policy converges 1.56 times faster, while policy is no worse than vanilla policy. Continuing the trends from Table LABEL:tab:main-res, we see that the policies on Ant and Half-cheetah share similarities with the results in the Humanoid environment. These similarities include reduced kinetic energy, quicker convergence, and increased agent velocity within the same time-step frame.
VI SIM-TO-REAL ON HARDWARE
VI-A Implementation Details
Hardware: We deploy our trained policies on the Unitree Go1 robot [16], a quadruped with 12 DOFs. We rely on the SDK provided by [32] to communicate between our code and the low-level control SDK provided by Unitree. The control frequency is 50Hz for both simulation and hardware.
Simulation and Training: We use an open source implementation of the Go1 in the Isaac Gym simulator [33]. We train the policies for 2500 episodes on 4000 parallel environments using PPO [34] with both the Vanilla Reward and constructed using (8), having same formulations as given in V-B for other waslkers. The joint angle bounds were taken from the physical specifications of the robot.
Domain Randomization: To enhance sim-to-real transfer, we train a policy that remains robust across variations in robot attributes such as body mass, motor strength, joint position calibration, ground friction, restitution, and gravity orientation and magnitude by varying these quantities.
VI-B Simulation and Training Results
The Vanilla reward is comprised of three components: target velocity tracking, target angle tracking, and target height jump. As depicted in Fig. 9 (Top Row), our proposed policy trained with the shaping term added to the vanilla reward consistently outperforms the Vanilla policy across all three tasks. In Fig. 9 (Bottom Row), it’s evident that utilizes only 78% of the energy compared to Vanilla, as calculated using (13). Furthermore, exhibits improved action smoothness (negative of change in action) and significantly lower maximum action values, thereby enhancing safety for deployment.
VI-C Hardware Results
Vanilla Policy: The Vanilla policy (Fig. 9 Bottom) resulted in unstable movements and limb instability, particularly affecting the front right limb during basic commands like forward motion and re-direction (Fig. 9). This led to a fall, from which the robot autonomously recovered but at the cost of posing practical deployment risks. The robot’s joint movements lacked coordination and efficiency, frequently deviating from the intended path and causing the front hinge joints to approach dangerously close to the ground.
Policy: The policy trained with the new reward significantly enhanced the Go1 robot’s performance, with improved balanced and coordinated movements despite having half the training duration as the Vanilla policy (Fig. 9 Top). It flawlessly followed RC controller commands, ensuring safety without falls or risky behaviour. The advantages of the policy are further affirmed by Fig. 10 where policy leads to more consistent and rhythmic action values. This characteristic enhances safety for the actuators and contributes to increased task efficiency.
VII CONCLUSIONS
In this paper we propose barrier function (BF) inspired reward shaping, a safety-oriented, easy-to-implement reward shaping formulation for robotic platforms. This approach is based on theoretical principles of safety provided by barrier functions. The shaping term aims to encourage agents to remain within predefined safe states during training. This enhances training efficiency and ensures safer exploration. To illustrate our formulation process, we proposed two barrier functions: exponential and quadratic.
While prior works, e.g. by Cheng et al. [23] achieved high training efficiency and state safety, their framework needs the system dynamics model. In contrast, our method eliminates this need, thus being easy to implement in complex environments. However, a limitation of our study is that we have only tested with barrier functions of joint angles. Further investigation into other quantities such as joint angular velocities could offer a more comprehensive understanding of our reward’s effectiveness.
We employed the Unitree Go1 robot [16] as our hardware platform for sim-to-real experiments. Our reward-shaping methodology emerged superior through comparative analysis with [32], revealing smoother, more rhythmic control dynamics. The results indicate that our formulation is an easy way to introduce safety and efficiency in RL training.
References
- [1] D. Hafner, J. Pasukonis, J. Ba, and T. Lillicrap, “Mastering diverse domains through world models,” arXiv preprint arXiv:2301.04104, 2023.
- [2] V. Mnih, K. Kavukcuoglu, D. Silver, A. A. Rusu, J. Veness, M. G. Bellemare, A. Graves, M. Riedmiller, A. K. Fidjeland, G. Ostrovski et al., “Human-level control through deep reinforcement learning,” nature, vol. 518, no. 7540, pp. 529–533, 2015.
- [3] A. Glaese, N. McAleese, M. Trębacz, J. Aslanides, V. Firoiu, T. Ewalds, M. Rauh, L. Weidinger, M. Chadwick, P. Thacker et al., “Improving alignment of dialogue agents via targeted human judgements,” arXiv preprint arXiv:2209.14375, 2022.
- [4] L. Ouyang, J. Wu, X. Jiang, D. Almeida, C. Wainwright, P. Mishkin, C. Zhang, S. Agarwal, K. Slama, A. Ray et al., “Training language models to follow instructions with human feedback,” Advances in Neural Information Processing Systems, vol. 35, pp. 27 730–27 744, 2022.
- [5] S. Arimoto, S. Kawamura, and F. Miyazaki, “Bettering operation of robots by learning,” Journal of Robotic systems, vol. 1, no. 2, pp. 123–140, 1984.
- [6] J. Kober, J. A. Bagnell, and J. Peters, “Reinforcement learning in robotics: A survey,” The International Journal of Robotics Research, vol. 32, no. 11, pp. 1238–1274, 2013.
- [7] P. F. Christiano, J. Leike, T. Brown, M. Martic, S. Legg, and D. Amodei, “Deep reinforcement learning from human preferences,” Advances in neural information processing systems, vol. 30, 2017.
- [8] B. Marthi, “Automatic shaping and decomposition of reward functions,” in Proceedings of the 24th International Conference on Machine Learning, ser. ICML ’07. New York, NY, USA: Association for Computing Machinery, 2007, p. 601–608.
- [9] M. Grzes and D. Kudenko, “Plan-based reward shaping for reinforcement learning,” in 2008 4th International IEEE Conference Intelligent Systems, vol. 2, 2008, pp. 10–22–10–29.
- [10] A. Y. Ng, D. Harada, and S. Russell, “Policy invariance under reward transformations: Theory and application to reward shaping,” in Icml, vol. 99. Citeseer, 1999, pp. 278–287.
- [11] Y. Dong, X. Tang, and Y. Yuan, “Principled reward shaping for reinforcement learning via lyapunov stability theory,” Neurocomputing, vol. 393, pp. 83–90, 2020.
- [12] A. G. Barto and S. Mahadevan, “Recent advances in hierarchical reinforcement learning,” Discrete event dynamic systems, vol. 13, no. 1-2, pp. 41–77, 2003.
- [13] R. Bellman, “Dynamic programming,” science, vol. 153, no. 3731, pp. 34–37, 1966.
- [14] J. Schulman, P. Moritz, S. Levine, M. Jordan, and P. Abbeel, “High-dimensional continuous control using generalized advantage estimation,” arXiv preprint arXiv:1506.02438, 2015.
- [15] Y. Tassa, T. Erez, and E. Todorov, “Synthesis and stabilization of complex behaviors through online trajectory optimization,” in 2012 IEEE/RSJ International Conference on Intelligent Robots and Systems. IEEE, 2012, pp. 4906–4913.
- [16] Unitree, “Go1,” 2022. [Online]. Available: https://www.unitree.com/products/go1
- [17] M. Grzes and D. Kudenko, “Plan-based reward shaping for reinforcement learning,” in 2008 4th International IEEE Conference Intelligent Systems, vol. 2. IEEE, 2008, pp. 10–22.
- [18] S. M. Devlin and D. Kudenko, “Dynamic potential-based reward shaping,” in Proceedings of the 11th international conference on autonomous agents and multiagent systems. IFAAMAS, 2012, pp. 433–440.
- [19] M. Grzes, “Reward shaping in episodic reinforcement learning,” 2017.
- [20] O. Marom and B. Rosman, “Belief reward shaping in reinforcement learning,” in Proceedings of the AAAI conference on artificial intelligence, vol. 32, no. 1, 2018.
- [21] A. Balakrishnan and J. V. Deshmukh, “Structured reward shaping using signal temporal logic specifications,” in 2019 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS). IEEE, 2019, pp. 3481–3486.
- [22] N. Saxena, G. Sandeep, and P. Jagtap, “Funnel-based reward shaping for signal temporal logic tasks in reinforcement learning,” IEEE Robotics and Automation Letters, 2023.
- [23] R. Cheng, G. Orosz, R. M. Murray, and J. W. Burdick, “End-to-end safe reinforcement learning through barrier functions for safety-critical continuous control tasks,” Proceedings of the AAAI Conference on Artificial Intelligence, vol. 33, no. 01, pp. 3387–3395, Jul. 2019.
- [24] D. Yu, H. Ma, S. E. Li, and J. Chen, “Reachability constrained reinforcement learning,” 2022.
- [25] H. Zhao, X. Zeng, T. Chen, Z. Liu, and J. Woodcock, “Learning safe neural network controllers with barrier certificates,” 2020.
- [26] B. Marthi, “Automatic shaping and decomposition of reward functions,” in Proceedings of the 24th International Conference on Machine learning, 2007, pp. 601–608.
- [27] D. C. Tan, F. Acero, R. McCarthy, D. Kanoulas, and Z. A. Li, “Your value function is a control barrier function: Verification of learned policies using control theory,” arXiv preprint arXiv:2306.04026, 2023.
- [28] A. D. Ames, S. Coogan, M. Egerstedt, G. Notomista, K. Sreenath, and P. Tabuada, “Control barrier functions: Theory and applications,” in 2019 18th European control conference (ECC). IEEE, 2019, pp. 3420–3431.
- [29] A. G. Barto, R. S. Sutton, and C. W. Anderson, “Neuronlike adaptive elements that can solve difficult learning control problems,” IEEE transactions on systems, man, and cybernetics, no. 5, pp. 834–846, 1983.
- [30] P. Wawrzyński, “A cat-like robot real-time learning to run,” in Adaptive and Natural Computing Algorithms: 9th International Conference, ICANNGA 2009, Kuopio, Finland, April 23-25, 2009, Revised Selected Papers 9. Springer, 2009, pp. 380–390.
- [31] S. Fujimoto, H. Hoof, and D. Meger, “Addressing function approximation error in actor-critic methods,” in International conference on machine learning. PMLR, 2018, pp. 1587–1596.
- [32] G. B. Margolis and P. Agrawal, “Walk these ways: Tuning robot control for generalization with multiplicity of behavior,” Conference on Robot Learning, 2022.
- [33] V. Makoviychuk, L. Wawrzyniak, Y. Guo, M. Lu, K. Storey, M. Macklin, D. Hoeller, N. Rudin, A. Allshire, A. Handa et al., “Isaac gym: High performance gpu-based physics simulation for robot learning,” arXiv preprint arXiv:2108.10470, 2021.
- [34] J. Schulman, F. Wolski, P. Dhariwal, A. Radford, and O. Klimov, “Proximal policy optimization algorithms,” arXiv preprint arXiv:1707.06347, 2017.