Automated Synthesis of Safe Digital Controllers for Sampled-Data Stochastic Nonlinear Systems

We present a new method for the automated synthesis of digital controllers with formal safety guarantees for systems with nonlinear dynamics, noisy output measurements, and stochastic disturbances. Our method derives digital controllers such that the corresponding closed-loop system, modeled as a sampled-data stochastic control system, satisfies a safety specification with probability above a given threshold. The proposed synthesis method alternates between two steps: generation of a candidate controller pc, and verification of the candidate. pc is found by maximizing a Monte Carlo estimate of the safety probability, and by using a non-validated ODE solver for simulating the system. Such a candidate is therefore sub-optimal but can be generated very rapidly. To rule out unstable candidate controllers, we prove and utilize Lyapunov's indirect method for instability of sampled-data nonlinear systems. In the subsequent verification step, we use a validated solver based on SMT (Satisfiability Modulo Theories) to compute a numerically and statistically valid confidence interval for the safety probability of pc. If the probability so obtained is not above the threshold, we expand the search space for candidates by increasing the controller degree. We evaluate our technique on three case studies: an artificial pancreas model, a powertrain control model, and a quadruple-tank process.


INTRODUCTION
Digital control [30] is essential in many cyber-physical and embedded systems applications, ranging from aircraft autopilots to biomedical devices, due to its superior flexibility and scalability, and lower cost compared to its analog counterpart. The synthesis of analog controllers for linear systems is well-studied [29], but its extension to nonlinear and stochastic systems has proven much more challenging. Furthermore, digital control adds extra layers of complexity, e.g., time discretization and signal quantization. A common problem in both digital and analog control is the lack of automated synthesis techniques with provable guarantees, especially for properties beyond stability (e.g., safety) for nonlinear stochastic systems.
In this paper we address this problem by introducing a new method for the synthesis of probabilistically safe digital controllers for a large class of stochastic nonlinear systems, viz. sampled-data stochastic control systems. In such systems, the plant is a set of nonlinear differential equations subject to random disturbances, and the digital controller samples the noisy plant output, generating the control input with a fixed frequency.
Controllers are usually designed to achieve stability of the closedloop system. Lyapunov's indirect method provides conditions under which the stability of an equilibrium point of a nonlinear system follows from the stability of that point for the linearized version of the system [24]. Lyapunov's method for sampled-data nonlinear systems is much more involved. Previous work [27] provides sufficient conditions on the sampled-data linearized system that ensure stability of the sampled-data nonlinear system. Unfortunately, it is difficult to verify these conditions algorithmically. In this paper, we instead prove necessary conditions for stability that are easy to verify, and use them to restrict the controller synthesis domain. However, a stable system is not necessarily safe, as during the transient the system might reach an unsafe, catastrophic state. The synthesis approach that we propose overcomes this issue by deriving controllers that are safe.
Given an invariant ϕ (i.e., a correctness specification), and a nonlinear plant with stochastic disturbances and noisy outputs, our method synthesizes a digital controller such that the corresponding closed-loop system satisfies ϕ with probability above a given threshold ϑ . The synthesis algorithm (Algorithm 1 in Section 5) is illustrated in Figure 1. It works by alternating between two steps: generation of a candidate controller p c , and verification of the candidate. p c is generated via the optimize procedure (see Algorithm 2), which maximizes a Monte Carlo estimate of the satisfaction probability by simulating a discrete-time approximation of the system with a non-validated ODE solver. Such a candidate is, therefore, sub-optimal but very rapid to generate. To rule out unstable controller candidates, we prove and utilize Lyapunov's indirect method for instability of sampled-data nonlinear systems. Along with p c , optimize returns an approximate confidence interval (CI) [a, b] for the satisfaction probability.
Next, in the verification step (procedure verify), we use a validated solver based on SMT (Satisfiability Modulo Theories) to compute a numerically and statistically valid CI [a ′ , b ′ ] for the satisfaction probability of p c . If the deviation between the approximate CI [a, b] and the precise CI [a ′ , b ′ ] is too large, indicating that the candidates generated by optimize are not sufficiently accurate, we increase the precision of the non-validated, fast solver (procedure update_discretization). If instead the precise probability is not above the threshold ϑ , we expand the search space for candidates by increasing the controller degree.
Summarizing, the novel contributions of this paper are: • we synthesize digital controllers for nonlinear systems subject to stochastic disturbances and measurement noise, while state-of-the-art approaches consider linear systems only; • we prove Lyapunov's indirect method for instability of nonlinear systems in closed-loop with digital controllers; • we present a novel algorithm that synthesizes digital controllers with guaranteed probabilistic safety properties.

SAMPLED-DATA STOCHASTIC SYSTEMS
We consider sampled-data stochastic control systems (SDSS), a rich class of control systems where the plant is specified as a nonlinear system subject to random disturbances. The controller periodically samples the plant output subject to random noise generating, using the plant output's history, a control input that is kept constant during the sampling period with a zero-order hold; see Figure 2. The controller is characterized by a number of unknown parameters, which are the target of our synthesis algorithm.

Definition 2.1 (Sampled-data Stochastic Control System
). An SDSS can be described in the following state-space notation: d dt where x(·) ∈ R n is the state of the plant; x 0 is the initial state at time t = 0; d(·) ∈ R q is the disturbance; y(·) is the plant output, which is a function of the state with additive i.i.d. noise η ∼ N (0,W ) with covariance matrix W ; u(·) ∈ R m is the control input, updated at every sampling period τ > 0 by the digital controller h (defined in Section 3); and p ∈ P ⊂ R 2L+1 is the vector of unknown controller parameters, where P is a hyperbox (i.e., a product of closed intervals). The dynamics of the plant is governed by the vector field f , which is assumed to be in C 1 , hence Lipschitz-continuous. We also assume that the output map o(·) is in C 1 .
We assume that there are no time lags for transmitting the plant output to the controller and the control input to the plant. The disturbance d(·) is a piecewise-continuous function having, for a time horizon T , a finite number of discontinuities. The discontinuity points and the value of d(·) at each sub-domain can be defined in terms of a finite number of random parameters drawn from arbitrary distributions. Note that these assumptions on d(·) are reasonably mild and allow us to define very general classes of systems, which subsume, for instance, numerical solutions of stochastic differential equations [35]. 1

DIGITAL CONTROLLERS
The operation of a digital controller is succinctly indicated in Equation (1). These computations are generally performed using current and past output samples and past input samples. Definition 3.1 (Digital Controller for SDSS). Given an SDSS, we denote y(k) = y(t k ) and u(k) = u(t k ) and define the tracking error as where r (·) is the reference signal. The output of the controller is where u(j) = e(j) = 0 for j < 0 2 and L is the controller degree.
Controller design amounts to finding a degree L and coefficients that ensure the desired behavior of the closed-loop system. The vector of parameters p defined in (1) is recovered by T . An alternative description of the controller is via the state-space representation where x c (k) is the state of the controller and matrices (G c , H c , C c , D c ) need to be designed. The above two representations are equivalent. Given a controller in the form of (3), one can transform it to the representation (4), for instance by taking states as memories that store previous values of inputs/outputs. Given matrices (G c , H c , C c , D c ), one can easily compute coefficients {a i , b i } in (3) using matrix multiplications [30].

Stability of the closed-loop system
A necessary requirement of any controller is stability of the closedloop system. In our setting, we have the influence of both external inputs (d, η, r ) and initial states (x 0 , x c 0 ). A suitable notion is inputto-state stability (ISS) [40], which implies that bounded input signals must result in bounded outputs and, at the same time, that the effect of initial states must disappear as time goes to infinity. A necessary requirement of ISS is Lyapunov stability of the system 'without' external inputs, stated in the next definition, a requirement that can be applied to both continuous-and discrete-time systems.
Definition 3.2. Consider a dynamical system with state space D ⊂ R n and without any external inputs, where x e ∈D is an equilibrium point and D is open. Then x e is called Lyapunov stable if for every ϵ > 0 there exists a δ > 0 such that for all It is very easy to verify stability for discrete-time linear systems. 1 Such numerical solutions rely on computing the value of the Wiener process at discrete time points, which makes it a special case of our disturbances. 2 Note that if the controller has been previously deployed, i.e., it starts from a non-empty history, then u(j), e(j) may be nonzero for j < 0.   In the remainder of this section, we consider a version of SDSS in Definition 2.1 controlled by (4) without any external input, i.e., when (d, η, r ) are identically zero. We study Lyapunov stability of the closed-loop system without external inputs, which is necessary for having input-to-state stability. Let us put (d, η, r ) ≡ 0 and define x 1 := x − x e , u 1 := u − u e , with (x e , u e ) being the equilibrium point of SDSS (1), i.e., f (x e , u e , 0) = 0. Similarly, define y 1 := y − y e with y e = o(x e ) and x c 1 := x c − x c e with x c e being the equilibrium point for the controller. We then denote the plant dynamics after eliminating external inputs based on shifted version of variables by d dt , wheref Thus x 1 = 0, u 1 = 0 is an equilibrium point for (5). The digital controller dynamics is likewise given by where the minus sign is due to r (·) = 0 and negative feedback in (2). The nonlinear system (5) is controlled with the digital controller (6) by setting u 1 (t) = u 1 (kτ ) for all t ∈ [kτ , (k + 1)τ ), k ∈ Z ≥0 . Ensuring stability of the sampled-data nonlinear control system (5)-(6) is difficult in general. Sufficient conditions for preserving stability under linearization are provided in [27,28], but they are hard to verify automatically. Rather, we provide an easy-to-check necessary condition for Lyapunov stability to reject unsuitable controllers. This necessary condition is based on Lyapunov's indirect method developed here for sampled-data nonlinear systems. In the following we prove that if the linearized closed-loop system has an eigenvalue outside the unit circle, the nonlinear closed-loop system (5)-(6) is not Lyapunov stable, thus the system (1)-(4) is not input-to-state stable.
The upper bound (9) enables us to study the effect of the nonlinear terms д(·) and l(·) in the sampled version of the dynamics, which can be written as Next, we derive a bound forд(·) in terms of x 1 and x c 1 .
Lemma 3.5. For any γ > 0, there exist continuous functionsĥ 1 ,ĥ 2 such that the following inequality holds forд(·) defined in (10), The explicit form ofĥ 1 ,ĥ 2 is provided, along with the proof, in the appendix. We are now ready to state our main result of this section.
Theorem 3.6. The continuous-time nonlinear system (5) controlled with the digital controller (6) is unstable if the linearized continuoustime system controlled by the same digital controller has a pole outside the unit circle.
Sketch of the proof. We prove the theorem by contradiction. We show that there is an r > 0 such that for any δ > 0 we can find an initial state for the system and the controller with We construct a Lyapunov function for the linear system that is strictly increasing on a suitable set of initial states. By a proper selection of r , we show that this Lyapunov function is also strictly increasing on the nonlinear system if the trajectory remains inside the ball with radius r , which is a contradiction.
Proof. The closed-loop linearized system will have the following dynamics in discrete time with G = e Aτ and H = ∫ τ 0 e Aλ Bdλ. This gives the following state transition matrixĜ which is assumed to have at least one eigenvalue outside the unit circle. We cluster the eigenvalues ofĜ into a group of eigenvalues outside the unit circle and a group of eigenvalues on or inside the unit circle. Then there is a nonsingular matrix T such that where G −1 1 is stable. In other words, G 1 contains all of the eigenvalues ofĜ from the first group. (The matrix T can be found for instance by transformingĜ into its real Jordan form.) Let us define where the partitions of z and T are compatible with the dimensions of G 1 and G 2 . The dynamics of z becomes Then both G 2 /(1 + δ ) and (1 + δ )G −1 1 are stable matrices. According to Proposition 3.3, there are positive definite matrices M 1 , M 2 and Q 1 , Q 2 such that the following matrix equalities hold where the positive constant is obtained using definition ofд 1 ,д 2 as a function ofд and Lemma 3.5 as Similarly, we use the Cauchy-Schwarz inequality for Take 0 < γ 0 ≤ 1 sufficiently small such that c 0 − c 1 γ 0 − c 2 γ 2 0 ≥ 0 with its associated radius r 0 = r (γ 0 ). Note that this is always possible sinceĥ 1 ,ĥ 2 , thus c 1 , c 2 , are bounded on the interval γ ∈ (0, 1].
For the proof of instability, take radius We claim that the trajectory starting from y(0) will always leave the ball with radius r 1 . Suppose this is not true, i.e., which contradicts the boundedness of y(t). □ Stability test. The characteristic polynomial P(s) of the linearized system (11) is which is a polynomial whose coefficients depend on the choice of parameters p for the digital controller (1) in either of the representations (3)-(4). As we have shown in Theorem 3.6, if this polynomial has a root s outside unit circle, i.e., ∥s ∥ > 1, then the closed-loop sampled-data nonlinear system is unstable, so we can eliminate that controller from the synthesis domain.

DIGITAL CONTROLLER SYNTHESIS
In this section we define the digital controller synthesis problem for SDSSs, using the same notation of Definition 2.1. Recall that we consider controller parameters p ∈ P (where P is a hyperbox in R 2L+1 ), a finite time horizon T , and a safety invariant ϕ(x) defined as a predicate over the SDSS state vector x (a quantifier-free FOL formula over the theory of nonlinear real arithmetic). The synthesized controller should ensure safety with respect to the probability measure induced by the stochastic disturbance d(·) and the measurement noise η. In particular, the probability of satisfying ϕ(x(t)) for all t ∈ [0,T ] should be above a given, user-defined threshold ϑ . We further constrain the controller search by limiting the maximum controller degree to L (see (3) in Definition 2.1).
Below, we denote by θ the stochastic uncertainty due to the SDSS disturbances d and the measurement noise η up to time T . Definition 4.1 (Digital Controller Synthesis). Given an SDSS, a time bound T < ∞, a maximum controller degree L, and a probability threshold ϑ ∈ (0, 1), the digital controller synthesis problem is finding the degree l * l * = min{ l | C(l) ∅, l ≤ L} and controller parameters p * ∈ C(l * ) where and P l is the parameter space of controllers of degree l. For clarity we indicate that x depends (indirectly) on the controller parameters p and on the stochastic uncertainty θ . If C(l) = ∅ for all l ≤ L, then we say the problem is infeasible.
In general the above synthesis problem is very hard to solve exactly due to the presence of nonlinearities, ordinary differential equations (ODEs) introduced by the SDSS dynamics, and multidimensional integration for computing probabilities. In fact, a decision version of the digital controller synthesis problem (i.e., given l ≤ L decide whether C(l) is nonempty), is easily shown to be undecidable. While Satisfiability Modulo Theory (SMT) approaches, e.g., [11], can now in principle handle nonlinear arithmetics via a sound numerical relaxation, their computational complexity is exponential in the number of variables. In particular, our stochastic optimization problem is high-dimensional and currently infeasible for fully formal approaches, but it can be tackled using the mixed SMT-statistical approach of [39], which computes statistically and numerically sound confidence intervals. As such, we replace (exact) probability with empirical mean, thereby obtaining a Monte Carlo version of Definition 4.1.

Definition 4.2 (Digital Controller Synthesis -Monte Carlo).
Given an SDSS H , a time bound T < ∞, a maximum controller degree L, a probability threshold ϑ ∈ (0, 1), and a confidence value c ∈ (0, 1). The Monte Carlo digital controller synthesis problem is findinĝ Note that by the law of large numbers when K → ∞ and the confidence value c is sufficiently close to one, we have thatĈ(l) approximates C(l) arbitrarily well. Also, note that the constraints in Definition 4.2 are simpler to solve than those in 4.1, as they do not involve absolutely precise integration of the probability measure -we only require to decide the constraints with some statistical confidence c. However, even with this simplification a decision version of the Monte Carlo digital controller synthesis problem (i.e., deciding whetherĈ(l) = ∅) remains undecidable when plants with nonlinear ODEs are involved. Intuitively, that is because evaluating the elements ofĈ(l) amounts to solving reachability, which is well known to be an undecidable problem for general nonlinear systems. Hence, one can only solve the Monte Carlo controller synthesis problem approximately, and that is what we aim to do in the next Section. Finally, note that we can generate sample realizations of θ : recall from Section 2 that the disturbance d(·) is defined from a finite number of random variables. This implies that θ is finitedimensional as the number of random noise variables is also finite due to the sampling period.

SYNTHESIS ALGORITHM
In this section we present an algorithm for approximately solving the Monte Carlo controller synthesis problem of Definition 4.2. Our synthesis algorithm starts from controllers with degree l = 0 and iteratively increase l until the constraintĈ(l) ∅ is satisfied or l reaches a maximum value.
The synthesis algorithm, summarized in Algorithm 1, consists of two nested loops. The inner loop (lines 4-9) consists of two main stages: optimization and verification. Procedure optimize (line 5) aims at finding controller parameters p that (approximately) maximizes the empirical probability that the closed-loop system with a discrete-time version of the plant satisfies property ϕ over the finite time horizon [0,T ]; optimize also returns an approximate confidence interval (CI) [a, b] for such probability. Optimization is based on the cross-entropy algorithm, but our approach could work with different black-box optimization algorithms too, such as Gaussian process optimization [33] and particle swarm optimization [23]. Then, procedure verify (line 6) checks the candidate controller p in closed-loop with the original (continuous-time) plant model and computes a precise CI [a ′ , b ′ ] for Prob θ K {X (p, θ K ) ≥ ϑ }. The reason for using the continuous-time plant only in verify is due to the high computational complexity of validated numerical ODE solving compared to solving its discrete-time approximation. The interval returned by verify is compared against the current best verified interval, which is then updated accordingly (line 7).
The procedures optimize and verify are iterated until the approximate CI (for the discrete-time plant) [a, b] and the verified CI (for the continuous-time plant) [a ′ , b ′ ] overlap to a certain length, or a maximum number of iterations is reached (line 9). In the outer loop, if a ′ ≥ ϑ (i.e., the LHS of the verified confidence interval is larger than ϑ ) then p is a witness forĈ(l) ∅, with probability at least c. Therefore, p approximately solves the digital controller synthesis problem of Definition 4.2, and the algorithm terminates. (The approximation lies in the fact that we cannot guarantee that the synthesized controller has minimum degree.) Otherwise (a ′ < ϑ ), we increase the controller degree l up to a maximum degree L.
In the inner loop, line 8 improves the approximation of the closedloop system used in optimize. This can be any adjustment to the ODE solver complexity (e.g., increasing the Taylor series order). In our case, it corresponds to increasing the number of time points used for ODE integration. We next explain both optimize and verify in more detail.
Procedure optimize. In Algorithm 2 we give the pseudocode for the optimize function (line 5 of Algorithm 1). It implements a modified cross-entropy (CE) optimization algorithm [34] that repeatedly samples (from the CE distribution of) controller parameters, evaluates their performance, and guides the search towards parameters that increase the safety probability (i.e., probability of satisfying ϕ over [0,T ]). Sample performance is computed first by the stability check using Theorem 3.6 (line 6 of Algorithm 2). If a controller does not pass the test, i.e., it is necessarily unstable, it is rejected. Otherwise we compute a CI for the probability (over θ ) of the closed-loop system to satisfy the invariant ϕ (line 7). For this purpose, we consider a discrete-time version of the plant, simulated via an approximate ODE solver based on the first term of the Taylor series expansion. The time steps 0 = t 1 < · · · < t G = T for ODE integration are obtained by discretizing the time between the controller sampling points (defined by τ -see Definition 2.1) using the discretization parameter m.
To compute the CI, our implementation uses sequential Bayesian estimation for efficiency reasons [38], but other standard statistical techniques may also be employed (e.g., the Chernoff-Hoeffding bound). After an adequate number of controller parameters are sampled and evaluated, the best performing sample is chosen (line 11), and the CE distribution is updated accordingly (line 12, see [34] for more details). This is repeated until a maximum number of iterations is reached. Procedure verify. We use the ProbReach tool [38] to compute a CI for the probability that a candidate digital controller in closedloop with the plant satisfies the time-bounded invariant ϕ (line 6 of Algorithm 1). This step is necessary since the candidate controller has been obtained using an approximate, discrete-time solver for simulating the (continuous) plant dynamics, while ProbReach uses instead an SMT solver [12] to handle the plant dynamics in a sound manner. In particular, ProbReach allows to derive a guaranteed confidence interval for Prob θ K {X (p, θ K ) ≥ ϑ } with confidence c, where K is the number of samples of the Monte Carlo synthesis problem (see Definition 4.2). (We note that in general K will depend on the size of the interval and the confidence c.) Procedure verify consists of two steps. The first step builds a hybrid system (the model format accepted by ProbReach) representing the closed-loop system under the candidate controller. In the second step, verify invokes ProbReach with three parameters: the hybrid system, and the required minimum size ξ and confidence c of the confidence interval to compute. We remark that the size of the confidence interval cannot be guaranteed in general [39] because of the undecidability of reasoning about nonlinear arithmetics. As such, the confidence interval returned by ProbReach via verify can be fully trusted from both the statistical and numerical viewpoints: while the interval size might be larger than ξ , the confidence is guaranteed to be at least c, as the sampled controllers are evaluated by SMT and verified numerical techniques.
Theorem 5.1. Let S be an SDSS for which the synthesis problem of Definition 4.1 is feasible for a given ϑ ∈ (0, 1) and controller degree l ≤ L. Suppose that Algorithm 1, with parameters S, L, ϑ and c ∈ (0, 1), returns a controller p and an interval [a, b] such that a ≥ ϑ . Then p is a solution of the Monte Carlo problem of Definition 4.2 for S, ϑ , and c.
Proof (sketch). It suffices to note that Algorithm 1 terminates either by finding a controller of minimal degree whose safety probability is at least ϑ , with confidence c, or by finding a controller of degree l with the best confidence interval produced by verify. By hypothesis a controller of degree l that satisfies ϕ with probability larger than ϑ exists, and Algorithm 1 returns an interval whose LHS is larger than ϑ . Therefore, by verify, we know that this interval has confidence c. □ We remark that Algorithm 1 can output sub-optimal controllers: this is unavoidable when using stochastic optimization methods such as cross-entropy. However if the controller design problem is feasible, multiple restarts of Algorithm 1 will eventually find the minimal controller with probability 1.

CASE STUDIES AND EVALUATION
We evaluate our approach on three case studies: a model of insulin control for Type 1 diabetes (T1D) [17], also known as the artificial pancreas (AP), a model of a powertrain control system [19], and a quadruple-tank process. For all case studies we use the following input parameters for Algorithm 1: ξ = 0.05, c = 0.99 and α = 0.5. The experiments were performed on a 32-core Intel 2.90GHz system running Ubuntu.
Digital PID controllers. While our algorithm can synthesize any digital controller as per Definition 3.1, we here exemplify its use via proportional-integral-derivative (PID) controllers, one of the most popular control techniques. A PID controller output is the weighted sum of three terms: the error itself weighted with K P , its rate of change weighted with K D , and accumulated error weighted with K I . The input/output equation of a digital PID controller is Essentially, the controller needs to store the previous value of the input and the previous two values of the error. In the following case studies, we focus on the synthesis of controllers in the PID form, hence we consider a maximum degree L = 2.

Artificial pancreas
The AP is a system for the automated delivery of insulin therapy that is required to keep blood glucose (BG) levels of diabetic patients within safe ranges, typically between 4-11 mmol/L. A so-called continuous glucose monitor (CGM) sends BG measurements to a control algorithm that computes the adequate insulin input. PID control is one of the main techniques [41], and is also found in commercial devices [21]. Meals are the major disturbance in insulin control, which make full closed-loop control challenging. Our approach is therefore well suited to solve this problem because it can synthesize controllers attaining arbitrary safety probability by minimizing the impact of such disturbances. To model insulin and glucose dynamics, we employ the nonlinear model of Hovorka et al. [18], considered as one of the most faithful models. The plant has nine state variables describing insulin and glucose concentration in different physiological compartments. We evaluate the system for a time bound of 24 hours. In our SDSS model, we consider three meals (respectively represent breakfast, lunch and dinner) with random timing and random amount, expressed by the following normally-distributed parameters: the amount of carbohydrates (CHO) of each meal in grams, D G 0 ∼ N (50, 100), D G 1 ∼ N (70, 100) and D G 2 ∼ N (60, 100), and the waiting times between meals, T 1 ∼ N (300, 100) and T 2 ∼ N (300, 100). The corresponding disturbance input is given by: The system output y(t) is the CGM measurement (performed every 5 minutes), given by the equation y(t) = C(t)+η(t), where C is the state variable for interstitial glucose and η(t) is white Gaussian sensor noise with standard deviation 0.25.
The control input u(t) is the insulin infusion rate computed by the PID controller. The tracking error is defined as e(t) = r (t) −y(t) with the constant reference signal r (t) = 6.11 mmol/L. The total infusion rate is given by u(t) + u b where u b (≈ 0.05548) is the basal insulin, i.e., a low and continuous dose to regulate glucose outside meals. The value of u b is chosen to guarantee a steady-state BG value equals to r (t) in absence of meals. This steady state is used as the initial state of the system. Safety property. Insulin control seeks to prevent hyperglycemia (BG above 11 mmol/L) and hypoglycemia (BG below 4 mmol/L). Hypoglycemia happens when the controller overshoots its dose and has more severe health effects than hyperglycemia, which is tolerated to a small extent after meals. For this reason we consider a safe BG range of [4,16] mmol/L, which strictly avoids hypoglycemia and allows for some post-meal hyperglycemia tolerance. In addition, we want that the glucose level stays close to the reference signal   towards the end of the 24 hours (1440 minutes). Our invariant is given by: where G is the state variable for the BG concentration.
In the synthesis algorithm, we use a probability threshold of ϑ = 0.95 (we want to satisfy the above invariant with probability not below 95%), confidence c = 0.99, confidence interval size ξ = 0.05, and α = 0.5.
To better understand the performance of the controllers, we analyze their behavior on 1,000 Monte Carlo executions of the system. Results, reported in Figure 3, evidence that hyper-and hypo-glycemia episodes are never sustained.

Powertrain system
We consider the automotive air-fuel control system adapted from the powertrain control benchmark in [19]. The plant model consists of a system of three nonlinear ODEs describing the dynamics of the engine in relation to the throttle air dynamics, intake manifold and air-fuel path.
The engine is controlled by a PID controller that seeks to maintain a constant air/fuel ratio equals to the stoichiometric valuē λ = 14.7, that is when the engine performs optimally. The tracking error is thus given by e(t) = y(t) −λ. The control signal u(t) determines the amount of fuel entering the system. Safety property. We consider the following invariant where µ(t) = (λ(t) −λ)/λ is the relative error from the setpoint. The first conjunct states that the air/fuel ratio should constantly be within ±100% of the ideal ratioλ. The second conjunct states that whenever the input throttle angle θ in rises (at time t = 0) or falls (t = ζ /2), the plant should settle within time ζ /8 and remain in the settling region (±5% aroundλ) until the next rise or fall (happening after time ζ /2). We set the probability threshold to ϑ = 0.96.
Synthesis results. Table 2 shows the PID controllers synthesized at each iteration of the algorithm. The domain of controller parameters was chosen as follows: and K d ∈ [−0.05, 0.05]. With our algorithm, we could synthesize a degree-2 controller (PID) satisfying the threshold. The optimal degree-1 controller has similar performance (both yield confidence intervals with RHS equals to 1), albeit below the threshold.
Compared to the AP case study, we observe that the powertrain model requires generating (and verifying) fewer candidate parameters. At the same time the dynamics of the powertrain system appear more challenging to control as the model requires more ODE integration steps (see column # o (# 0 o ) of Tables 2 and 1).

Quadruple-tank process
We consider a quadruple-tank process adapted from [20], which consists of four interconnected water tanks. The process is illustrated in Figure 4. This model is an example of a multiple-input and multiple-output (MIMO) system with multivariable right halfplane zeros [13] (such zeros bring performance limitations in control problems). We extended the deterministic model of [20] to include uncertainties in the valve settings and random disturbances in the process that remove water from the tanks. The process is controlled in a decentralized fashion, by which two digital controllers are designed for the input-output pairs (u 1 , y 1 ) and (u 2 , y 2 ), where u 1 and u 2 are the input voltages for the pumps, and y 1 and y 2 are the water level measurements obtained as y 1 = 0.5 · h 1 and y 2 = 0.5 · h 2 , where h 1 and h 2 are the water levels in tanks 1 and 2, respectively. In this case study we assume that the pumps can only add water to the tanks (and cannot pump it out).
We consider a scenario where at time 0 and then twice after every minute we remove a random amount of water (which is model through reducing the corresponding water levels by a random value ∼ U (0, 3)) from tanks 1 and 2. Every time such a disturbance happens, the valves parameters are randomly reset to γ 1 ∼ N (0.7, 0.223) Tank 1   Tank 2   Tank 3  Tank 4 Pump 1 Valve 1 Pump 2 Valve 2 Figure 4: The diagram of the quadruple-tank model.
-number of candidates (unstable) sampled by the optimization algorithm, CPU(opt) -total (only optimize procedure) runtime in minutes, * -the number of points in discretisation was increased from 1 to 2. and γ 2 ∼ N (0.6, 0.223). The system is subject to a measurement noise modeled as a white Gaussian noise with variance 0.33.
Safety property. After each disturbance, we require that the system reaches the desired water levels in tanks 1 and 2 (within 1 centimeter above or below the corresponding set points r 1 = 12.4 and r 2 = 12.7) within 5 seconds, and that the water levels stay close to the setpoints for the remaining 55 seconds, before the next disturbance occurs. Also, all four water levels h 1 ,  Table 3, which shows that we can obtain a confidence interval of up to [0.94, 0.99] for the safety probability by using two PI controllers (see third row of Table 3). Note that the performance of the controller is not improved by including the derivative terms K D 1 , K D 2 (see last two rows). This is due to the optimization algorithm which works by sampling a finite number of controller parameters and thus, might fail to explore parameter regions with better safety probability.

RELATED WORK
Although recent papers [1,2,8] have addressed the synthesis of safe digital controllers for linear and deterministic systems, synthesis for the class of stochastic nonlinear systems that we consider has not yet been tackled.
While the approach proposed in this paper can be applied in principle to any digital controller, our case studies focus on PID controllers. Several methods have been proposed for the synthesis of PID controller for nonlinear and stochastic plants [3,9,10,14,16,37,42]. However, none of these methods can provide safety guarantees beside the work of [37] (discussed at the end of the section) .
We pursue a different direction with respect to the classical solutions proposed in the literature, by providing a framework for the efficient synthesis of discrete-time digital controller for nonlinear and stochastic plants that are safe and robust (with respect to a probabilistic reachability property) by construction.
The control synthesis problem considered here requires to solve a parameter synthesis problem over a closed-loop system modeled as a stochastic nonlinear system. In contrast with existing parameter synthesis techniques for stochastic and continuous nonlinear systems [4,5,15], our approach, which targets specifically digital controllers, takes advantage of a computationally efficient stability check that rules out unstable controller candidates, thereby reducing the computational effort.
The problem of controller synthesis under safety requirements has been investigated mostly for Model Predictive Control (MPC) [6] whose goal is to find the control input that optimizes the predicted performance of the closed-loop system up to a finite horizon. The work of [22,25,26,31,32,36,43] consider safety requirements expressed as temporal logic formulas, and synthesize MPC controllers that optimize the robust satisfaction of the formula [7] (i.e., a quantitative measure of satisfaction). MPC is an online method that requires solving at runtime often computationally expensive optimization problems. In contrast, our approach performs controller synthesis at design time.
The closest paper to our own is the work of [37], where the authors have recently proposed a method to synthesize continuoustime PID controllers for nonlinear stochastic plants such that the resulting closed-loop system satisfies safety and performance requirements specified as bounded reachability properties. That method works under the assumptions that the system can measure the output of the plant continuously and without sensing noise. However, this is not realistic in the majority of embedded systems whose operations are governed by a discrete-time clock and where sensor noise is unavoidable.

A STABILITY REQUIREMENT
The following lemma provides a bound on exponent of a matrix and is is used in proving our main theorem.
Lemma A.1. For any matrix A and any ϵ > 0, there is a constant C(ϵ) such that Proof. We use the Jordan normal form J of A that satisfies A = P −1 JP for some invertible matrix P: where J i is the i th block of J with size k 0 ∈ N and can be written as J i = λ i I + N . Matrix N is a matrix of all zeros except identities on the first superdiagonal. Since N m = 0 for all m > k 0 , we have the following for J i : The claim is true by taking C(ϵ) We write this inequality in terms of σ (t) := W (t) as Rename L := ∥A∥ + γ , L 1 := ∥BC c ∥ + γ ∥C c ∥ and L 2 : where L, L 1 , L 2 depend on γ as defined above. □ Proof of Lemma 3.5. Using the assumption of ∥(x 1 (t), u 1 (kτ ))∥ ≤ r (γ ) for all t ∈ [kτ , (k + 1)τ ], we get Then we employ Lemma 3.4 to get ∥д(kτ )∥ ≤γ The claim holds with anyĥ 1 ,ĥ 2 witĥ which can be selected as the following according to Lemma A.1, with Γ := C(1) and α := a + 1,

B GLUCO-REGULATORY ODE MODEL
The model consists of three subsystems: • Glucose Subsystem: it tracks the masses of glucose (in mmol) in the accessible (Q 1 ) and non-accessible (Q 2 ) compartments, G (mmol/L) represents the glucose concentration in plasma, EGP 0 (mmol/min) is the endogenous glucose production rate and U G (t) (mmol/min) is the glucose absorption rate from the gut. • Gut absorption: this subsystem uses a chain of two compartments, G 1 and G 2 (mmol), to model the absorption dynamics of ingested food, given by the disturbance D G (t). A д is the CHO bio-availability. t maxG (min) is the time of maximum appearance rate of glucose. • Interstitial glucose: C is the subcutaneous glucose concentration (mmol/L) detected by the CGM sensor and has a delayed response w.r.t. the blood concentration G. • Insulin Subsystem: it represents absorption of subcutaneously administered insulin. It is defined by a two-compartment chain, S 1 and S 2 measured in U (units of insulin), where u(t) (U/min) is the administration of insulin computed by the PID controller, u b (U/min) is the basal insulin infusion rate and I (U/L) indicates the insulin concentration in plasma.
par value par value par value w 100 k e 0.138 k 12 0.066 k a 1 0.006 k a 2 0.06 k a 3 0.03 k b 1 0.0034 k b 2 0.056 k b 3 0.024 t max I 55 V I 0.12 · w V G 0.16 · w F 01 0.0097 · w t maxG 40 F R 0 EGP 0 0.0161 · w A G 0.8 k int 0.025 Table 4: Parameter values for the glucose-insulin regulatory model. w (kg) is the body weight.
• Insulin Action Subsystem: it models the action of insulin on glucose distribution/transport, x 1 , glucose disposal, x 2 , and endogenous glucose production, x 3 (unitless).
The model parameters are given in Table 4.
− k e I x i (t) = −k a i · x i (t) + k b i · I (t), i = 1, 2, 3

C FUEL CONTROL SYSTEM MODEL
The dynamics of the engine (plant) are given by the following set of ODEs: where p (bar) is the intake manifold pressure; θ (degrees) is the throttle angle; λ is the air/fuel ratio; θ in (degrees) is the throttle angle input disturbance;θ is the throttle plate angle and is defined by:θ = c 6 + c 7 θ + c 8 θ 2 + c 9 θ 3 ; m c (g/s) is the air inflow rate to cylinder and is defined by: m c = c 12 (c 2 + c 3 ωp + c 4 ωp 2 + c 5 ω 2 p); ω (rad/s) is the engine speed disturbance; m af is the inlet air mass flow rate, defined by:

D MODEL OF THE QUADRUPLE-TANK PROCESS
The dynamics of the Quadruple-Tank Process are given by the following set of ODEs [20]: where h i , a i , A i , i ∈ {1, 2, 3, 4} are the water level, cross-section of the outlet hole, and cross-section of tank i, respectively. Inputs u 1 , u 2 indicate the voltages applied to the pumps and the corresponding flows are k 1 u 1 , k 2 u 2 . The parameters γ 1 , γ 2 ∈ (0, 1) show the settings of the valves. The flow to tank 1 is γ 1 k 1 u 1 and the flow to tank 4 is (1 − γ 1 )k 1 u 1 (similarly for the other two tanks). The acceleration of gravity is denoted by д. The water levels of tanks 1, 2 are measured by sensors as k c h 1 , k c h 2 . The parameter values are: A 1 = A 3 = 28 cm 2 , A 2 = A 4 = 32 cm 2 , a 1 = a 3 = 0.071 cm 2 , a 2 = a 4 = 0.057 cm 2 , k c = 0.5 V/cm, д = 9.81 m/s 2 . We have chosen the steady state values h 0 1 = 12.4 cm, h 0 2 = 12.7 cm, h 0 3 = 1.8 cm, h 0 4 = 1.4 cm, u 0 1 = 3.00 V, u 0 2 = 3.00 V, k 1 = 3.33 cm 3 /Vs, and k 2 = 3.35 cm 3 /Vs.