Learning Hybrid Control Barrier Functions from Data
Lars Lindemann, Haimin Hu, Alexander Robey, Hanwen Zhang, Dimos V. Dimarogonas, Stephen Tu, Nikolai Matni
Introduction
Consider the following safety-critical scenarios: autonomous vehicles in urban areas , exoskeletons for improving mobility of lower-body impaired users , and robots navigating a warehouse using semantic logic . These systems are all described by hybrid dynamics, i.e., states and transitions are both continuous and discrete, due to either their physics, or to higher level logical decision making and importantly, share that: 1) data exhibiting safe behavior is readily available or easily collected, and 2) in most cases, their hybrid system dynamics are well understood and can be identified. Based on these observations, we propose an optimization-based approach to learning provably safe controllers for hybrid systems using hybrid control barrier functions (HCBF).
Related work: Barrier functions (BFs) were introduced in to certify the safety of continuous-time systems. Nonsmooth and hybrid BFs have also been defined. Different to our work, these focus on BFs with jumps for discontinuous systems. Control barrier functions (CBFs) for continuous-time control systems appeared in for synthesizing feedback control laws that ensure safety by enforcing forward invariance of a desired safe set. Reciprocal and zeroing CBFs were proposed as less restrictive alternatives that do not enforce forward invariance on subsets of the safe set. Such CBFs can be used as a safety guard via convex quadratic programs , a feature that has been used for safe learning . CBFs for discrete-time control systems can be found in . Related to this paper, hybrid BFs were proposed in in the hybrid systems framework of as a means of certifying the safety of hybrid systems. Analogous hybrid BFs are introduced in for the hybrid automata modeling framework of .
The underlying challenge and bottleneck in ensuring safety using (control) BFs is the construction of these functions. In , the authors propose a sum-of-squares programming approach for finding polynomial (hybrid) barrier functions for polynomial (hybrid) systems and semi-algebraic sets. For finding CBFs, bilinear sum-of-squares programming and analytic approaches are proposed in ; such methods, however, only apply to a restrictive class of systems and have limited scalability. Recent approaches attempt to circumvent these limitations by treating the CBF synthesis task as a machine learning problem. In , a deep neural network is trained to imitate the control law obtained from a CBF, whereas in , a CBF is synthesized from safe and unsafe data samples using support vector machines. In , the authors cluster observed data and learn a linear CBF for each cluster. While the aforementioned works present detailed empirical validation of their methods, no formal correctness proofs are provided. In , a Lyapunov, barrier, and a policy function is learned from data: the validity of the learned certificates are then verified post-hoc using Lipschitz arguments. In , a method is proposed that learns a provably correct neural net safety guard for kinematic bicycle models. Finally, propose a data-driven approach for learning CBFs for smooth nonlinear systems assuming known Lipschitz dynamics, as well as availability of expert demonstrations illustrating safe behavior. Sufficient conditions ensuring correctness of the learned CBF using a Lipschitz argument are also given.
Contributions: For a class of hybrid control systems, we define hybrid control BFs as a means to enforce forward invariance of a safe set. We provide sufficient conditions in terms of a HCBF that, if satisfied by a control law, ensure safety. We then show that learning HCBFs from data can be cast as a constrained optimization problem, and provide conditions under which a feasible solution is a valid HCBF. Finally, we present simulations showcasing the benefits of our framework.
Preliminaries and Problem Formulation
The next result follows mainly from . We provide a slightly modified version to account for [19, Remark 5] and to not require that when by using the Comparison Lemma [28, Lemma 3.4].
Assume that is a valid control barrier function on and that is a continuous function with . Then implies for all . If is compact, it follows that is forward invariant under , i.e., .
First note that with admits a unique solution that is such that for all [28, Lemma 4.4]. Each solution to (1) under is now, due to the chain rule and since , such that for all . Using the Comparison Lemma [28, Lemma 3.4] and assuming that , it follows that for all , i.e., implies for all . Note next that (1) is defined on since is only defined for . Since for all and when is compact, it follows by [28, Theorem 3.3] that , i.e., is forward invariant. ∎
Hybrid Systems
We model and analyze hybrid systems using the formalism of .
A function is a hybrid solution to if and
for each s.t. , and .
Problem Formulation
The class of hybrid control systems that we consider is
We are additionally given a set of expert trajectories consisting of and discretely sampled data-points along flows and jumps as
is a subset of the geometric safe set , and that can be made forward invariant by appropriate control actions and .
Learning Hybrid Control Barrier Functions from Data
We begin by defining a suitable notion of hybrid control barrier functions, and show that they provide a mechanism for safe control of the hybrid control system (3). We then show how such HCBFs can be learned from data via a constrained optimization problem, and provide sufficient conditions under which a feasible solution is a valid HCBF.
so that This follows as we assume that , which results in , and since by the choices of the sets and . , which ensures that the set fully covers – see Fig. 1(middle) and (right). The sets and are the equivalent to the set in Section 2, but now considered separately for flows and jumps.
Based on the above definition, we define the sets of HCBF consistent inputs to be
We further assume for the remainder of the paper that the set is open. If is not open, one can instead assume that is strictly contained within . This will allow us, in the next result, to establish forward invariance of the set under control laws and when the set is compact.
Assume that is a valid hybrid control barrier function on and that and are continuous functions with and . Then implies for all . If is compact and satisfies , then the set is forward invariant under and , i.e., is unbounded.
During flows with not being a singleton, and if , we infer that for all due to (5) and as in the proof of Lemma 1. By continuity of and and since is closed, it also holds that if , i.e., the right end point is included in . After each jump, it holds that as a consequence of (6). Consequently, implies for all .
We next show that is forward invariant under and , i.e., is unbounded, if is compact and if . First recall that due to and since and . Assume now that the hybrid solution to the system (3) under control laws and is maximal A hybrid solution is maximal if there exists no other hybrid solution with and with for all . and that the hybrid time domain is bounded. By defining , we distinguish between the two cases: 1) and 2) . 1) Note that can only happen when is not a singleton and when has left the set by flowing without entering , which would enable a successive jump that is not possible since it is assumed that the solution is maximal. Since the set is compact and since is open, there has to exist a time such that according to [28, Theorem 3.3], which does not hold since and since as shown previously. Since is maximal, it follows by contradiction that is unbounded. 2) If , it holds that so that either or . In the former case, the solution can be extended by a jump. In the latter case, note that is strictly contained within the set so that the solution can be extended into or into by flowing. By contradiction, it again follows that is unbounded. ∎
Consider again the bouncing ball example from Example 1, here artificially equipped with continuous and discrete control inputs for illustrative purposes. The dynamics are
An Optimization Based Approach
Towards the goal of learning a valid HCBF, we now define, for and , the data sets
that need to be such that , which can be easily achieved even when data-points are close to by adjusting and or by omitting . Note that the set is open by definition. For , define
where is a ring of diameter that surrounds the set (see the golden ring in Figure 1). We will use the set to enforce that the value of the learned HCBF is negative on to ensure that the set is contained within the set , which is a necessary condition for to be valid. Hence, also assume that points
The optimization problem: We now propose an optimization problem for learning a valid local HCBF, and then prove its correctness. We solve
where is a normed function space and where the positive constants , , ,, , , and are hyperparameters determined by the data-sets , , and , which must be sufficiently dense, as quantified by , , and (conditions given below).
Lipschitz bounds: The constraints in (9c), (9e), and (9g) assume a function that returns an upper bound on the Lipschitz constant of its argument with respect to the state in an neighborhood of using the -norm as detailed in Appendix A. It may be difficult to enforce the constraints (9c), (9e), and (9g) in the optimization problem (9). One can resort to bootstrapping the values of , , and by iteratively solving the optimization problem (9), calculating the values of , , and to verify if constraints (9c), (9e), and (9g) hold, and adjusting regularization hyperparameters accordingly. Appendix A explains how to compute Lipschitz constants for DNNs and RKHS.
Should we trust the experts? Optimization problem (9) approximates the supremums over continuous and discrete inputs in the constraints (5) and (6) with the expert actions – while computationally expedient, this may prove conservative. We note that in many cases of interest, the supremum over the continuous input in constraint (5) admits a closed form expression. For example, when is an -norm ball, the left hand side of constraint (5) reduces to , for the dual norm. This in turn can be used to simplify constraint (9d) in optimization problem (9), and in particular eliminates the dependency on . Nevertheless, the availability of expert demonstrations is still valuable as they indicate that a safe action exists, and we therefore expect a feasible HCBF and control action to exist.
Guaranteeing Safety
We next show correctness of the learned HCBF obtained from (9) in two steps by:
showing that the certified safe set (8) is contained within the geometric safe set, i.e., that , and
proving that is a valid local HCBF by ensuring that the set is forward invariant under control laws and .
1) Guaranteeing : First assume that is an -net of , i.e., for all , there exists such that . Using a standard covering and Lipschitz argument, we next show that if satisfies constraint (9b) for all , we have that for all .
Let be Lipschitz continuous with local constant By local constant, we here mean a Lipschitz constant in an neighborhood of ., and be an -net of with
Then, if satisfies constraint (9b), we have that for all .
Note first that, for all , it follows that there exists a point satisfying due to the assumption that is an -net of . For any , we can now select a point satisfying for which it follows that
In particular, inequality follows from the constraint (9b) which says that for all . Inequality follows by the local Lipschitz constant on in an neighborhood of , while inequality follows again by the assumption that forms an -net of . The strict inequality follows simply by the assumption that for all . ∎
We can use a similar argument on constraint (9a), which ensures that the set over which , as defined in equation (8), has non-empty interior.
Let be Lipschitz continuous with local constant , and and be - and -nets of and , respectively, with
and . Then, if satisfies constraint (9a), we have that for all .
Note first that, for all , it again follows that there exists a point satisfying due to the assumption that is an -net of . For any , we can now select a point satisfying for which it follows that
In particular, inequality follows from the constraint (9a) which says that for all . Inequality follows by the local Lipschitz constant on in an neighborhood of , while inequality follows again by the assumption that is an -net of . The inequality follows simply by the assumption that for all . The same analysis holds for all , so that for all . ∎
Propositions 1 and 2 then ensure that , i.e., the zero level-set of is contained within the geometric safe set . Note that the constraints (9a) and (9b) may, in practice, lead to infeasibility of (9). We resort to the same remedy as proposed in [27, equation (3.4)], i.e., enforcing constraint (9a) on smaller sets and with and to allow for smoother HCBFs to be learned at the expense of a smaller invariant safe set , i.e., replace (9a) by
2) Guaranteeing a valid local HCBF: For a state , recall the definition of the function
where the control sample is associated with the sample that is such that . Note that such a pair is guaranteed to exist when the set is an -net of . The function is Lipschitz continuous in with local constant denoted by , as we have assumed to be twice continuously differentiable. Recall also that
and note similarly that is Lipschitz continuous with local constant denoted by . We next provide conditions guaranteeing that the learned HCBF satisfies the constraint (5) for all and the constraint (6) for all .
Suppose and are Lipschitz continuous with local constants and , respectively. Let , and assume that (i) is an -net of with
and (ii) is an -net of with
Then, if satisfies constraints (9d) and (9f), we have that for all and for all .
Note first that, for all , it follows that there exists a pair satisfying due to the assumption that is an -net of . For any , we can now select a pair satisfying for which it follows that
In particular, inequality follows from the constraint (9d) which says that for all . Inequality follows by the local Lipschitz constant on in an neighborhood of , while inequality follows again by the assumption that is an -net of . The inequality follows simply by the assumption that for all . This implies that for all . The same analysis holds for all , so that for all . ∎
By combining the previous arguments, we can ensure that the learned function defines an appropriate safe set, as captured by the condition , that can be rendered forward invariant by choosing control actions satisfying the constraints (5) and (6). The next theorem summarizes these results and guarantees that from (9) is a valid HCBF.
Let be a twice continuously differentiable function and let the sets , , , , , , and the data-sets , , , , and be defined as above. Suppose that forms an -net of satisfying for all , and that and are - and -nets of and , respectively, satisfying the conditions of Propositions 2 & 3. Let , , and be Lipschitz continuous with local constants , , and , respectively. Then if satisfies constraints (10), (9b), (9d), and (9f), the set is non-empty, , and the function is a valid local hybrid control barrier function on with domain .
Case Studies
The code for both case studies is available at https://github.com/unstable-zeros/learning-hcbfs. In both our simulations, we numerically integrate the hybrid dynamics of interest using an integrator implementation inspired by Drake . We build our implementation on top of scipy.integrate.solve_ivp’s event detection API. This allows us to get precise notifications for when a system makes a discrete jump.
We parametrize the HCBF candidate as a two-hidden-layer fully-connected DNN with tanh activation functions and 64 neurons in each hidden layer. The training is implemented using jax and the Adam algorithm with a cosine decay learning rate. We craft a loss function by relaxing the constraints in (9) – hyperparameter choices and other training details can be found in Appendix B. The set of the learned HCBF is shown as green dots in the middle subplot of Figure 2. One may clearly observe that is strictly contained within the geometric safe set, i.e. .
We now test if the learned HCBF is able to produce safe control inputs that keep the system within the set . We consider a nominal control law that looks to track the reference path shown in dash-dotted blue in Fig. 2(middle) and (right): as the system has full control, it can exactly track the reference path, but without correction, this would lead to violation of the velocity constraint. Starting from an initial state , we simulate the controlled system for seconds. During flow, we solve the CBF-QP problem for the continuous input (see Appendix B.2). During jump, we perform line search with a decay factor to find the scalar discrete input that satisfies (6). The closed-loop trajectory produced by the learned HCBF is plotted in Figure 2 (middle). As a comparison, we also plot in Figure 2 (right) the trajectory obtained using the analytical HCBF. Albeit slightly more conservative, the learned HCBF keeps the ball within at all times during both flow and jump.
Compass gait
We consider the compass gait walker . Originally introduced by , the compass gait dynamics describe a passive bipedal robot walking down an inclined plane at a constant velocity. This system is described by a four-dimensional continuous state
consisting of the angle and angular velocity of each leg. In this notation, the “stance” foot corresponds to the foot that is in contact with the ground as the compass gait walker makes its descent down the ramp; hence, the “swing” foot refers to the foot that is not in contact with the ramp at a particular instant in time. In our simulations, the walker’s initial stance leg is its left leg, and therefore its initial swing leg is its right leg. To improve the walking capabilities, we add actuation to hip joint and the ankle of the stance leg. To collect expert trajectories, we use the energy-based controller of . All implementation details for the compass gait walker can be found in Appendix C.
Learning and analyzing an HCBF for the compass gait walker hybrid system poses several challenges, including the well-known sensitivity of the compass gait walker to its initial conditions and the inherent difficulties in visualizing a four-dimensional state space. To facilitate a meaningful visualization of the four-dimensional state space, when collecting expert data, we fix the stance leg initial condition to the point on the passive limit cycle, and vary the initial condition of the swing leg by adding uniform noise to corresponding passive limit cycle state of the swing leg.
We again parameterize the candidate HCBF with a two-hidden-layer fully-connected neural network with tanh activations and with 32 and 16 neurons in the first and second hidden layers, respectively; as before, we determine the hyperpameters by grid search. However, unlike the previous example, the task of identifying boundary points is complicated by the higher dimensionality of the state space. Therefore, we propose a novel algorithm that can be used to identify boundary points by sampling from the space of expert trajectories. The algorithm, described in Appendix C, identifies boundary points by computing the pairwise distances between all of the expert states and thresholding based on the number of neighbors a point has within an -norm ball.
In Figure 3, we show the constraint satisfaction rates, the training loss, and the state separation attained by the learned HCBF. Note that these figures highlight the challenges of multi-objective optimization, i.e., trying to achieve all of the constraints in the optimization problem (9) via its unconstrained relaxation. Nevertheless, we emphasize that the use of robust optimization by including slack variables , , , and leads to HCBFs that perform well in practice.
In the left panel of Fig. 4, we visualize the learned HCBF. Starting from an initial condition with the same left leg state as the expert trajectories, we identify the values for at which in green, i.e. where the HCBF controller corrects the nominal controller. In red, we plot the phase portrait for the right leg corresponding to a trajectory using the HCBF-QP controller. To demonstrate the physical interpretation of this learned HCBF, in the right top panel of Fig. 4, we show the motion of the compass gait walker down the ramp, marking in green where the HCBF-based controller takes over; in the right lower panel of Fig. 4 we see the failure of a zero-valued controller from the same initial condition, showing that the learned HCBF preserves safety. Videos of both the safe HCBF and unsafe nominal controller trajectories can be found in the supplementary material.
To visualize the safe set of the learned HCBF, in Figure 5, we show that despite the fact that our nominal controller is not providing any control inputs, the HCBF-based controller has a similar safe set to that of the energy-based controller. In this way, the HCBF causes the uncontrolled system to match the safety characteristics of the expert energy-based controller. In particular, it can be observed that the safe set , which is described by the zero superlevel set of , under-approximates the safe expert behavior. As can further be observed, there are regions in the state space where our HCBF-based controller provides safe system trajectories while the energy-based controller results in unsafe system trajectories (e.g., the bottom right corner of both plots shown in Figure 5). We suspect that the safe set enjoys asymptotic stability properties similar to those of the continuous case , allowing for an expanded region of safety as compared to that of the expert. A more detailed investigation of this observation is, however, subject to future work.
Conclusion
We presented a framework for learning safe control laws for hybrid systems. We introduced HCBFs and sufficient conditions under which an HCBF based control law guarantees safety, i.e., a desired safe set is made forward invariant. We then showed how to learn such HCBFs from data using an optimization-based framework. We gave sufficient conditions under which feasible solutions to the optimization problem are valid HCBFs, and illustrated our methods on two case studies. Future work will look to extend this approach to hybrid systems described by differential inclusions, so as to be applicable to systems with friction and stiction, as well as to develop statistical guarantees of correctness to alleviate the sampling burden of our method.
References
Appendix A Appendix A
To verify (9c), (9e), and (9g), the Lipschitz constant of the learned function as well the Lipschitz constant of its gradient need to be calculated first. The following discussion is mainly taken from [27, Section 3.4.2]. When consists of twice differentiable functions, as is the case when is parametrized by a Deep Neural Net with twice differentiable activation functions or a Reproducing Kernel Hilbert Space, the following is shown to hold.
an upper bound on the Lipschitz constant of is provided as with probability at least where is as explained in [27, Section 3.4.2]. An upper bound on the Lipschitz constant of can be derived by the bound that holds with probability at least .
Deep Neural Net: When is a DNN, the problem of exactly computing the Lipschitz constant of is known to be NP-hard. Because most commonly-used activation functions are known to be 1-Lipschitz (e.g., ReLU, tanh, sigmoid), a naive upper bound on the Lipschitz constant of is given by the product of the norms of the weight matrices; that is, . However, this bound is known to be quite loose . Recently, the authors of proposed a semidefinite-programming based approach to efficiently compute an accurate upper bound on . On the other hand, there are relatively few results that provide accurate upper bounds for the Lipschitz constant of the gradient of . The only general method for computing an upper bound on is through post-hoc sampling.
Now, using these Lipschitz constants and of and , respectively, it can be seen that the functions and in (9d) and (9f) are locally Lipschitz continuous in since , , , , , and are locally Lipschitz continuous and since function composition preserves Lipschitz continuity. Upper bounds of the Lipschitz constants and follow immediately.
Appendix B Appendix B: Bouncing Ball
The code for the bouncing ball case study can be found at https://github.com/unstable-zeros/learning-hcbfs.
The control goal of the bouncing ball case study is to track a desired reference path shown as dash-dotted blue lines in Fig. 2 (middle and right). This reference path is obtained by dropping the ball from without applying any control input, i.e., , until it hits the ground. The discrete control input is set to when this happens. Reversing the velocity in sign while keeping the position unchanged gives the reference path in the right half plane.
In order to track the reference path, we design a linear error-feedback control law with via solving the Riccati equation. The reference state is selected as the state on the reference path that is the closet to in Euclidean distance. We set the nominal discrete input to be such that it amplifies the velocities after collisions and thus is more likely to cause constraint violation. Note that neither the reference path nor the closed-loop trajectory obtained by applying the nominal controllers and complies with the geometric safe set .
B.2 Training data
To obtain training data during flows, we use the above reference controller together with the analytical HCBF in Example 2, which, per solving the CBF-QP problem , gives a safe controller during flow. The CBF-QP basically consists of solving a convex quadratic program with decision variable , cost function , and the constraint . The safe control input used to obtain training data during jumps is essentially a thresholding function and can be found analytically as mentioned in Example 2. Based on these safe controllers and , we obtain data-sets and containing 5000 safe states and associated expert demonstrations and , which are shown as the green and magenta dots in the left subplot of Figure 2.
B.3 Hyperparameters for training the neural network
We train the neural network for epochs using the loss given by unconstrained relaxation of Problem (9) in Section 3. The hyperparameters, shown in Table 1, are found by grid search.
B.4 Visualization of the learned HCBF
We visualize the learned and analytical HCBFs in -space in Figure 6 by plotting their level sets. The green dots indicate where on , while the purple dots additionally indicate where on , i.e. the possible set of states after an impact with the ground.
B.5 Validness of the learned HCBF
We performed post-verification of the learned HCBF satisfying Propositions 1, 2 and 3, which serve as sufficient conditions for learning a valid HCBF as stated in Theorem 2. Since all of our data sets were gridded evenly on the state space, densities of the -nets are equal to the gridding resolutions, which are and . The local Lipschitz constants were approximated using the norm of their gradients, e.g. , which gives a tight estimation of the Lipschitz constants due to our dense gridding scheme. The percentages of data points that satisfy those conditions are summarized in Table 2.
Appendix C Appendix C: Compass Gait Walker
The code for the compass gait walker case study can be found at https://github.com/unstable-zeros/learning-hcbfs.
The approach we have described crucially relies on being able to sample from “unsafe” regions of state space, i.e. to sample states . For low-dimensional state spaces, it is often possible to take advantage of the underlying geometry of the hybrid system to sample from this region as for instance in the bouncing ball case study. However, for higher-dimensional state spaces such as the compass gait walker, it may not be possible to easily leverage the underlying geometry to obtain these states.
Before demonstrating how we used this algorithm in the compass gait case study, we provide some simple examples to illustrate the efficacy of this algorithm toward identifying the boundaries of different sets of points. In Figure 7(c), we show the result of running Algorithm 1 on mixtures of two Gaussians. In Figure 8, we show a three-dimensional example in the unit cube. Finally, we show the result of running Algorithm 1 on 100,000 states taken from the expert compass gait walker in Figure 9.
C.2 Implementation details
Our implementation of the compass gait walker relies heavily on the C++ implementation in the Drake package . In particular, we re-implement the compass gait walker in Python with Jax : our implementation is publicly available on the project Github repository. We use the same default settings for the compass gait walker as are used in the Drake implementation; these values are detailed in Table 3.
The hyperparameters used for training a HCBF for the compass gait walker are given in Table 4. These hyperparameters were chosen via grid search.
C.3 Collecting expert trajectories
To collect expert trajectories, we use the energy-based controller described in eq. (24) in . This controller applies actuation to the hip joint, leaving the ankle joints unactuated. In particular, this controller takes the following form
where we set and where is the reference energy from the passive limit cycle, and is the current total mechanical energy of the compass gait walker. Throughout, we use a reference energy of as suggested by .
As described in the main text, we collect expert trajectories by fixing the initial condition of the left leg to and varying the initial condition of the right foot where and . In Figure 5 (right), we show the successful right foot initial conditions for the energy-based controller corresponding to this initial condition. In particular, to train the HCBF, we collected 500 rollouts using this scheme. For each rollout, we used a horizon of steps and a time interval of .