원시데이터 수집 시스템
선정목록으로

DiffTune-MPC: Closed-Loop Learning for Model Predictive Control

관련 수소연료전지 · (5) 연료전지용 수분제어장치(가습기)

기본 정보

저자
  1. Ran Tao — University of Illinois Urbana-Champaign, Champaign, IL, USA
  2. Sheng Cheng — University of Illinois Urbana-Champaign, Champaign, IL, USA
  3. Xiaofeng Wang — University of South Carolina, Columbia, SC, USA
  4. Shenlong Wang — University of Illinois Urbana-Champaign, Champaign, IL, USA
  5. Naira Hovakimyan — University of Illinois Urbana-Champaign, Champaign, IL, USA
DOI
10.1109/lra.2024.3422836
저널
IEEE Robotics and Automation Letters
발행년도
2024
발행기관
Institute of Electrical and Electronics Engineers
피인용수
13
라이선스
cc-by
논문 유형
–
언어
–
원문 페이지
https://doi.org/10.1109/lra.2024.3422836
토픽
Fuel Cells and Related Materials
OpenAlex ID
W4400275615

평가 내용

관련도
관련
사업분야
수소연료전지 · (5) 연료전지용 수분제어장치(가습기)
부 분야
–
R&D 측면
–
확신도
–
근거 용어
–
판단 근거
임베딩 유사도 기반 평가로 '수소연료전지 > 연료전지용 수분제어장치(가습기)' 분야에 선정된 논문입니다.
R&D 활용 포인트
–
평가 입력
–

초록

Model predictive control (MPC) has been applied to many platforms in robotics and autonomous systems for its capability to predict a system's future behavior while incorporating constraints that a system may have. To enhance the performance of a system with an MPC controller, one can manually tune the MPC's cost function. However, it can be challenging due to the possibly high dimension of the parameter space as well as the potential difference between the open-loop cost function in MPC and the overall closed-loop performance metric function. This letter presents DiffTune-MPC, a novel learning method, to learn the cost function of an MPC in a closed-loop manner. The proposed framework is compatible with the scenario where the time interval for performance evaluation and MPC's planning horizon have different lengths. We show the auxiliary problem whose solution admits the analytical gradients of MPC and discuss its variations in different MPC settings, including nonlinear MPCs that are solved using sequential quadratic programming. Simulation results demonstrate the learning capability of DiffTune-MPC and the generalization capability of the learned MPC parameters.

본문 78,374자

본문 펼치기
## DiffTune-MPC: Closed-Loop Learning for Model Predictive Control Ran Tao <sup>†</sup>, Sheng Cheng <sup>†</sup>, Xiaofeng Wang, Shenlong Wang, Naira Hovakimyan Abstract— Model predictive control (MPC) has been applied to many platforms in robotics and autonomous systems for its capability to predict a system’s future behavior while incorporating constraints that a system may have. To enhance the performance of a system with an MPC controller, one can manually tune the MPC’s cost function. However, it can be challenging due to the possibly high dimension of the parameter space as well as the potential difference between the open-loop cost function in MPC and the overall closed-loop performance metric function. This paper presents DiffTune-MPC, a novel learning method, to learn the cost function of an MPC in a closed-loop manner. The proposed framework is compatible with the scenario where the time interval for performance evaluation and MPC’s planning horizon have different lengths. We show the auxiliary problem whose solution admits the analytical gradients of MPC and discuss its variations in different MPC settings, including nonlinear MPCs that are solved using sequential quadratic programming. Simulation results demonstrate the learning capability of DiffTune-MPC and the generalization capability of the learned MPC parameters. SUPPLEMENTARY INFORMATION Code: https://github.com/RonaldTao/DiffTune-MPC I. INTRODUCTION With the advancement of computational power in embedded systems and the development of general-purpose solvers (e.g., acados, ACADO, CasADi), Model Predictive Control (MPC) is being applied to more and more platforms in robotics and autonomous systems. Effective implementation of MPC requires a well-designed cost function, a dynamical model with appropriate fidelity, realistic constraints accounting for the physical limits of the system, and sufficient computation power for real-time solutions of the MPC. When an MPC is applied for different tasks, one may need to design the cost function for each task to suit its individual characteristics (e.g., trajectory tracking tasks for hover or aggressive maneuvers for quadrotors). A common approach is to tune the parameters in a cost function with a given structure, e.g., with weight matrices Q and R following the convention in a linear quadratic regulator (LQR). The challenges in manually tuning the Q and R matrices come from the following two aspects: *This work is supported by NASA cooperative agreement (80NSSC22M0070), NASA ULI (80NSSC22M0070), AFOSR (FA955021-1-0411), NSF-AoF Robust Intelligence (2133656), and NSF SLES (2331878, 2331879). †These authors contributed equally to this work. R. Tao, S. Cheng, S. Wang, and N. Hovakimyan are with the University of Illinois Urbana-Champaign, USA. (email: {rant3, chengs, shenlong, nhovakim}@illinois.edu) X. Wang is with the University of South Carolina, USA. (email: wangxi@cec.sc.edu) 1. Dimension: In the simplest case where Q and R are reduced to diagonal matrices, the parameter space’s dimension equals the sum of the state’s and control’s dimensions, making manual tuning a challenging task, especially when the state is high-dimensional (≥ 12 for 3D rigid-body dynamics). 2. Difference between the cost function for MPC and loss function for MPC’s performance: The cost function in MPC reflects the predictive cost in an open-loop fashion: at each sample time, an optimal control problem is solved for a sequence of control actions into a future horizon, where all - except for the first control actions - are open-loop and not applied for controlling the system. On the contrary, the loss function is for evaluating the performance of the MPC as a function of the closed-loop states (and control actions), e.g., tracking error between the desired trajectory and the closed-loop trajectory. In practice, the relationship between the cost function parameters and the performance judged by the loss function is complex and nonlinear. Furthermore, the loss function can be defined over a long horizon specified by an arbitrary task, whereas the cost in MPC is defined over a shorter horizon to enable real-time solutions. To address the challenges in the manual tuning of MPC’s cost function, we propose to learn the cost function’s parameters for a given task using DiffTune [1], an auto-tuning method for closed-loop control systems. DiffTune formulates the auto-tuning problem as a parameter optimization problem that can handle different horizons in the loss function (for performance) and cost function (for control). It has been previously demonstrated on explicitly differentiable controllers whose Jacobian can be conveniently obtained by autodifferentiation. In this work, we extend DiffTune to implicitly differentiable MPC controllers. The analytical gradients of the MPC policy are obtained by the implicit differentiation proposed in [2], which is based on the implicit function theorem and Karush–Kuhn–Tucker (KKT) conditions. We first show how to differentiate a linear MPC problem (with quadratic cost, linear dynamics, and linear inequality constraints) by solving an auxiliary linear MPC. Then, we extend it to a nonlinear MPC and show how to differentiate it utilizing the auxiliary MPC. We validate our approach in simulations, where we show the efficacy of DiffTune for MPC subject to both linear and nonlinear dynamical systems. We also validated the generalization capability and applicability to real-world scenarios in a highfidelity simulator. We summarize our contributions as follows: - We extend the application of DiffTune [1] from autotuning explicitly differentiable controllers to MPC that is implicitly differentiable. - We propose a novel and flexible closed-loop MPC learning scheme using differentiable programming for the general scenario where the horizon for closed-loop performance evaluation is longer than an MPC’s openloop planning horizon. This is a significant improvement compared to existing open-loop MPC learning schemes [2], [3] that apply differentiable programming, where the horizon of the loss function cannot be longer than the MPC’s horizon because the loss function therein is defined with respect to the open-loop optimal solution. - Our differentiation of the MPC is based on a general formulation of a linear MPC that contains linear-inequality constraints of state and control, which covers a broader range of applications than the boxed-control-constraints only formulation in [2]. The remainder of the paper is organized as follows: Sections II and III review related work and background, respectively. Section IV describes the problem formulation, and section V describes our method for differentiating MPC based on a general linear MPC formulation. Section VI shows the simulation results on a unicycle model with linear inequality constraints, a quadrotor model, and a 1D double integrator model. Section VII presents the results on a quadrotor model using a high-fidelity simulator to show the generalization capability. Section VIII concludes the paper. II. RELATED WORK Auto-tuning, in general, can be formulated as a parameter optimization problem that looks for an optimal parameter to minimize a loss function subject to a given controller. It can be considered as a special case of learning for control, where the structure of the controller comes from physics (e.g., by Lyapunov or conventional design) rather than a generic neural network. If the system model is available, one can leverage the model information to infer optimal parameter choice, e.g., using gradient descent [1], [4]–[6]. If the model is unavailable, then one needs to query the system’s performance from a chosen set of parameters to determine the next choice of the parameter. For example, the authors of [7], [8] use Bayesian optimization to search for an optimal parameter by treating the mapping from parameter to loss as a Gaussian process. The parameters are iteratively chosen as the minimizer with the maximum likelihood. In [9], a policy search method is proposed to select highlevel decision variables or parameters for MPC automatically. The authors of [10] use the Metropolis-Hastings sampling to generate random parameters and selectively keep one parameter at each trial to continue the sampling procedure. A data-driven approach is proposed in [11], which consists of a system identification module, an objective specification module, and a feedback control synthesis module, where the controller is optimized end-to-end using Bayesian optimization. This method is applied to control a challenging soft robot platform [12]. Learning for MPC has been a trending topic in recent years owing to the hardware and software advancement in deploying and training MPC-related algorithms. The concept of differentiable MPC is initially discussed in [2] and later extended to general optimal control problems [3], [13] from the perspective of Pontryagin’s maximum principle and robust MPC [14]. The structural similarity between optimal control and reinforcement learning (RL) has made differentiable MPC a bridge to connect the two worlds [15]. In [16], a receding-horizon implementation of analytical policy gradient is proposed with a curriculum learning scheme to improve training stability for offline training of a controller. The authors of [17] propose to use the actor-critic method to learn the parameters of an MPC policy, which combines the long-term learning capability of the actor-critic RL and the short-term control capability by MPC. One particular direction for learning-based MPC uses learning to enhance the model’s quality for better prediction performance (and thus control performance). Various approaches [18]–[22] have been proposed to learn the residual dynamics and show promising performance for quadrotor control. In contrast to previous works [2], [13] that apply differentiable programming to parameter learning of optimal control (OC) problems in an open-loop fashion, our approach tackles parameter learning of an MPC problem in a closed-loop fashion. <sup>1</sup> In an open-loop setting, [2], [13] formulate the loss for training as a function of the optimal states and control actions that are obtained from solving the OC problem once. This design restricts the loss function’s horizon such that it cannot be longer than the MPC’s horizon, limiting suitable applications (e.g., imitation learning) because the loss’s horizon for an arbitrary task is usually much longer than the MPC’s planning horizon. In a closed-loop setting, our loss function is defined over a closed-loop system’s state and control actions, which allows for a broad set of learning applications where the loss’s horizon is way longer than the MPC’s horizon (which cannot be handled by openloop learning [2], [13]). The closed-loop learning of an MPC has been explored using probabilistic approaches like policy search [9] and Bayesian optimization [11]. These approaches do not employ first-order information of the MPC and, therefore, are less efficient than those that do (e.g., this work). In addition to the different problem formulation and application scenarios discussed above, our approach has novelties in terms of differentiating the OC problem. The differentiable OC presented in previous work [2] is based on an LQR with boxed constraints for control actions only. We extend it to a more general case of LQR with linear inequality constraints, which are capable of handling state constraints for safety requirements and likely coupled control(-state) constraints. Towards the incorporation of constraints, our OC problem’s formulation is similar to [3] albeit with different solution and differentiation techniques. The results in [3] augment the constraints (excluding the dynamics) to the cost function using barrier functions and thus resort to Pontryagin’s differentiable programming [13] to solve the 1Note that the difference between OC and MPC is that MPC refers to the practice of solving an OC but only applies the first optimal control action back to the system. OC as well as find the analytical gradients. In this procedure, one needs to manually select the barriers’ multipliers for a tradeoff between constraint tightness and cost minimization. In addition, augmenting the constraints to soft constraints makes the resulting solution only an approximate one for the original problem. In contrast, our method is based on the KKT condition of an MPC to satisfy the hard constraints. The KKT-based formulation is also general enough to use off-the-shelf MPC solvers (e.g., acados) to obtain both the MPC solution and the analytical gradient. III. BACKGROUND The following notation will be used throughout the paper: For a matrix A, we use A to denote its vectorized form (by column). We use [v]j to denote the j-th element of vector v. Similarly, [A]i,:, [A]:,j and [A]i,j denote the i-th row, j-th column, and element on i-th row and j-th column of the matrix A, respectively. The matrix In denotes an ndimensional identity matrix. The vector 1n denotes an ndimensional vector with all elements being 1. The vector e <sup>n</sup> i denotes an n-dimensional unit vector with the i-th element being 1. We briefly review controller auto-tuning and DiffTune in the sequel. Consider a discrete-time dynamical system xk+1 = f (xk, uk), (1) where xk ∈ R <sup>n</sup> and uk ∈ R <sup>m</sup> are the state and control, respectively, and the initial state x0 is known. The control is generated by a feedback controller that tracks a desired state x¯k ∈ R <sup>n</sup> such that uk = h(xk, ¯xk, θ), (2) where θ ∈ Θ denotes the controller’s parameters, and Θ represents a feasible set of parameters that can be analytically or empirically determined for a system’s stability. We assume that the state xk can be measured directly or, if not, an appropriate state estimator is used. Furthermore, we assume that the dynamics (1) and controller (2) are differentiable, i.e., the Jacobians ∇xf, ∇uf, ∇xh, and ∇θh exist, which widely applies to general systems. The controller learning/auto-tuning task adjusts θ to minimize an evaluation criterion, denoted by a loss function L(·), which is a function of the desired states, actual states, and control actions over a time interval of length N . An illustrative example is the quadratic loss of the tracking error and control-effort penalty, where L(x1:N, ¯x1:N, u0:N −1; θ) = k=0 <sup>λ∥uk∥2</sup> <sup>with</sup> <sup>λ</sup> <sup>></sup> <sup>0</sup> <sup>being</sup> <sup>the</sup> penalty coefficient. We will use the short-hand notation L(θ) �N for conciseness in the rest of the paper. Note that the usage k=1 <sup>∥xk</sup> <sup>−</sup> <sup>x¯k∥2</sup> <sup>+ �N</sup> <sup>−1</sup> of DiffTune is not limited to the above example of a tracking problem. As long as a loss function can be specified and is differentiable, we are able to apply DiffTune to learn the parameters and minimize the loss. To summarize, we formulate the controller learning/autotuning problem as the following parameter optimization xT ∈XT, (MPC) where Xt and Ut stand for feasible sets of states and control actions at time t, and XT denotes the terminal set of states. The simplest case of an MPC would be an LQR, i.e., Ct(xt, ut) = 12 <sup>(xt</sup> <sup>−</sup> <sup>x¯t)⊤Qt(xt</sup> <sup>−</sup> <sup>x¯t)</sup> <sup>+</sup> 21 <sup>(ut</sup> <sup>−</sup> <sup>u¯t)⊤Rt(ut</sup> <sup>−</sup> <sup>u¯t)</sup> <sup>and</sup> <sup>f</sup> <sup>(xt, ut)</sup> <sup>=</sup> <sup>Atxt</sup> <sup>+</sup> <sup>Btut</sup> in MPC, where Qt, Rt, At, Bt are matrices with appropriate dimensions. No other constraints are enforced in LQR. Remark 1: Note that we distinguish the time indices used in DiffTune (k and N for closed-loop systems) and in MPC problem minimize L(θ) θ∈Θ subject to xk+1 = f (xk, uk), x0 given, uk = h(xk, ¯xk, θ), k ∈{0, 1, . . ., N - 1}. (P) We solve the above problem by DiffTune, which uses a projected gradient descent algorithm [23] to update the parameters iteratively: θ ←PΘ(θ − α∇θL). (3) To obtain the gradient ∇θL, we start with the chain rule: N −1 k=0 k=0 ∂uk ∂θ <sup>.</sup> (4) ∂xk ∂θ <sup>+</sup> ∇θL = N k=1 ∂L ∂uk k=1 ∂L ∂xk The gradients ∂L/∂xk and ∂L/∂uk can be determined once L is chosen, and we use sensitivity propagation to obtain the sensitivity states ∂xk/∂θ and ∂uk/∂θ by: xk+1 =(∇xk f + ∇uk f ∇xk h) <sup>∂xk</sup> ∂θ ∂θ ∂xk+1 ∂uk (5b) ∂ <sup>x</sup> θ <sup>k</sup> <sup>+ ∇θh,</sup> ∂uθk <sup>=∇xk</sup> <sup>h∂</sup> ∂ <sup>x</sup> θ <sup>k</sup> <sup>xk</sup> (5a) ∂θ <sup>+ ∇uk</sup> <sup>f</sup> <sup>∇θh,</sup> with ∂x0/∂θ = 0. Here, the notations ∇xk f, ∇uk f, ∇xk h, and ∇θh mean the Jacobians ∂f/∂x, ∂f/∂u, ∂h/∂x, and ∂h/∂θ is evaluated at the state xk and control uk. We allow the state xk to come from sensor measurements or state estimation, which permits data collected from a real system or simulated noisy system (beyond the data collected by rolling out the dynamics (1)). IV. PROBLEM FORMULATION Model predictive control determines a sequence of control actions at every step that minimizes a cost criterion subject to system dynamics and other constraints on the states and control inputs. The problem is repeatedly solved when new state measurement becomes available, and the first optimal control action is implemented for the closed-loop control. In general, an MPC problem has the following form: minimize x1:T,u1:T T t=1 t=1 Ct(xt, ut) subject to xt+1 = f (xt, ut), x1 = xinit, xt ∈Xt, ut ∈Ut, t ∈{0, 1, . . ., T - 1}, (t and T for open-loop MPC solution). For the closed-loop system, at time k, the instantaneous state xk is set to xinit in (MPC). Once the (MPC) is solved, its initial optimal control action u <sup>⋆</sup> 1 <sup>is</sup> <sup>set</sup> <sup>as</sup> <sup>uk</sup> <sup>for</sup> <sup>the</sup> <sup>closed-loop</sup> <sup>system.</sup> Furthermore, the horizon N of the performance evaluation in DiffTune is generally much larger than the horizon T in (MPC). The goal of this paper is to apply DiffTune to solve the problem (P) when the controller h(·) is an MPC. More specifically, we want to learn the cost functions of the MPC controller to minimize the loss function defined over the entire closed-loop trajectory. Since MPC is an implicitly differentiable controller, the Jacobians ∂h/∂θ and ∂h/∂xk cannot be calculated directly like a simple feedback control law u = Kx. Thus, we will present our method to efficiently compute the target Jacobians in the following section. V. THE DIFFTUNE-MPC METHOD We learn an MPC controller following a given loss function by auto-tuning the parameter θ in the cost function (e.g., Q and R for quadratic cost) using DiffTune. Following the sensitivity propagation in (5), at each time k, we need to know the Jacobians ∂h/∂θ and ∂h/∂xk, which are the sensitivity of the controller to the parameter θ and instantaneous state xk. In the setting of using MPC as a controller h at step k, we have ∂h/∂θ = ∂u <sup>⋆</sup> 1 <sup>/∂θ</sup> <sup>and</sup> ∂h/∂xk = ∂u <sup>⋆</sup> 1 <sup>/∂xinit,</sup> <sup>where</sup> <sup>u⋆</sup> 1 <sup>is</sup> <sup>the</sup> <sup>first</sup> <sup>optimal</sup> <sup>control</sup> action obtained by solving problem (MPC) with the initial state xinit = xk. We will leverage the KKT conditions and implicit differentiation to obtain the gradients as illustrated in [2]. We start by differentiating a linear MPC problem and discuss the differentiation of a nonlinear MPC at the end. Considering the page limit, we only present the main results in this section. The complete derivation and analysis can be found in the Appendix. A. Differentiating a Linear MPC By linear MPC, we refer to the following problem each element of the control to be bounded by umax and umin, respectively. Problem (LMPC) is a quadratic program (QP) and can be solved efficiently with off-the-shelf solvers. Once we get the optimal solution {τt <sup>⋆}t=1:T,</sup> <sup>we</sup> <sup>can proceed to solve</sup> <sup>the</sup> the optimal solution {τt <sup>⋆}t=1:T,</sup> <sup>we</sup> <sup>can proceed to solve</sup> <sup>the</sup> following auxiliary problem for the gradients ∂u <sup>⋆</sup> 1 <sup>/∂[Ct]i,j,</sup> ∂u <sup>⋆</sup> 1 <sup>/∂[ct]i,</sup> <sup>and</sup> <sup>∂u⋆</sup> 1 <sup>/∂[xinit]i:</sup> <sup>⋆</sup> 1 <sup>/∂[ct]i,</sup> <sup>and</sup> <sup>∂u⋆</sup> 1 <sup>/∂[xinit]i:</sup> minimize τ˜1:T T t=1 t=1 1 2 <sup>τ˜</sup> t <sup>⊤Ct ˜τt + ˜c⊤</sup> t <sup>τ˜t</sup> subject to x˜t+1 = Ft ˜τt, x˜1 = x˜init, [Gt]It,act ˜τt = 0, (LMPC-Grad) where τ˜t = [ u <sup>x</sup> ˜ <sup>˜t</sup> t <sup>]</sup> <sup>∈</sup> <sup>Rn+m</sup> <sup>is</sup> <sup>the</sup> <sup>composite</sup> <sup>auxiliary</sup> <sup>state-</sup> control to be optimized, [Gt]It,act denote the rows of Gt where the inequality Gtτt ≤ lt is active at the optimal solution τt <sup>⋆,</sup> <sup>i.e.,</sup> <sup>[Gt]I</sup> t,act <sup>τ</sup> t <sup>⋆</sup> = [lt]It,act . Denote the first where τ˜t = [ u <sup>x</sup> ˜ <sup>˜t</sup> t t <sup>⋆,</sup> <sup>i.e.,</sup> <sup>[Gt]I</sup> t,act <sup>τ</sup> t <sup>⋆</sup> minimize τ1:T T t=1 t=1 1 2 <sup>τ</sup> t <sup>⊤Ctτt</sup> <sup>+ c</sup> t <sup>⊤τt</sup> subject to xt+1 = Ftτt + ft, x1 = xinit, Gtτt ≤ lt, (LMPC) solution τt <sup>⋆,</sup> <sup>i.e.,</sup> <sup>[Gt]I</sup> t,act <sup>τ</sup> t <sup>⋆</sup> = [lt]It,act . Denote the first optimal control action of (LMPC-Grad) by u˜ <sup>⋆</sup> 1 <sup>. The analytical</sup> gradients for (LMPC) can be obtained by computing u˜ <sup>⋆</sup> 1 from (LMPC-Grad) with the coefficients c˜t and x˜init listed in Table I. For details in the derivation of (LMPC-Grad) for computing the analytical gradient, see Appendix C. Once we obtain the target gradients, we can leverage (4) and (5) to obtain ∇θL and update the parameter using (3). Remark 2: The high-level idea of obtaining the analytical gradients of (LMPC) by solving (LMPC-Grad) is summarized as follows: Problem (LMPC) is solved by treating it as a QP, where the optimal solution satisfies a linear equation (33) following the KKT condition (including the dynamics, costate dynamics, boundary conditions, and constraints). One can take matrix differentials [24] of this linear equation, which governs the relation among the infinitesimal changes of the decision variables (state, control, Lagrange multipliers) and parameters (such as cost matrix Ct). Meanwhile, the resulting equation of matrix differentials (34) leads to the governing linear equation of the gradients of (LMPC), which coincides with the KKT condition of the auxiliary problem (LMPC-Grad). Hence, one can match the terms and coefficients in the original KKT condition with those in the one derived from the matrix differentials, which allows us to obtain the target gradients listed in the first column of Table I by solving (LMPC-Grad) with associated terms listed in the last two columns of Table I. TABLE I: To get target gradients of (LMPC) with (Ct, ct)parametrization in the left column, one can solve (LMPC-Grad) with coefficients c˜t and x˜init specified in the right column. The solution u˜ <sup>⋆</sup> 1 <sup>of (LMPC-Grad) equals the gradient in the left column.</sup> Note that the values in the column of c˜t are specifically for time step t from the target parameters Ct and ct, and for all other time steps t <sup>′̸</sup> = t, we have c˜t′ = 0. Target gradients in Coefficients in (LMPC-Grad) (LMPC) c˜t x˜init where τt = [ u <sup>xt</sup> t <sup>]</sup> <sup>∈</sup> <sup>Rn+m</sup> <sup>is</sup> <sup>the</sup> <sup>composite</sup> <sup>state-control</sup> to be optimized; Ct ∈ R <sup>(n+m)×(n+m)</sup> and ct ∈ R <sup>n+m</sup> are the cost function parameters; Ft = [At Bt] ∈ R <sup>n×(n+m)</sup> ; ft ∈ R <sup>n</sup> stands for residuals in the dynamics; and Gt ∈ R <sup>g×(n+m)</sup> and lt ∈ R <sup>g</sup> stand for g number of inequalities on the state and control actions. Although a more general form of inequality constraints for τt of all t ∈{1, 2, . . ., T } can be incorporated, we stick to the form displayed in (LMPC) where the linear inequality constraints are separable by t. The commonly used boxed constraint on the control action is a special case of the form used in Gtτt ≤ ht, where Gt = [ <sup>0</sup> 0 −IImm <sup>]</sup> <sup>and</sup> <sup>ht</sup> <sup>=</sup> <sup>[</sup> - <sup>u</sup> u <sup>max</sup> min <sup>1</sup> 1 <sup>m</sup> m <sup>]</sup> <sup>define</sup> <sup>the</sup> <sup>bounds</sup> <sup>of</sup> ∂u <sup>⋆</sup> 1 [Ct] ∂u <sup>⋆</sup> 1 ∂[ct] ∂u <sup>⋆</sup> 1 t <sup>⋆]j</sup> <sup>e</sup> i <sup>n+m</sup> ∂[∂Cut] <sup>⋆</sup> 1i,j [τt <sup>⋆</sup> i <sup>n+m</sup> 0 ∂u <sup>⋆</sup> 1 e <sup>n+m</sup> ∂[ct]i i i <sup>n+m</sup> 0 ∂[∂xuinit <sup>⋆</sup> 1]i 0 e <sup>n</sup> i i Remark 3: The analytical gradient obtained by solving (LMPC-Grad) is computationally cheaper than a numerical gradient obtained by finite difference. Since (LMPC-Grad) is a QP with equality constraints only (solving such a problem reduces to solving a linear system of equations [25]), (LMPC-Grad) takes less time to solve than the original problem (LMPC) which contains inequality constraints. If one wants to get the analytical gradient of u <sup>⋆</sup> 1 <sup>to</sup> <sup>M</sup> <sup>scalar</sup> <sup>parameters</sup> <sup>in</sup> <sup>(LMPC),</sup> <sup>then</sup> <sup>one</sup> <sup>needs</sup> <sup>to</sup> solve (LMPC-Grad) M times. However, if one wants to get the numerical gradient, then one has to solve at least M times (LMPC) with single-direction perturbations for M parameters and perform finite difference. Besides, the quality of the numerical gradient depends on the size of the perturbation, determining which can be an individual problem of study. In the problem setting of DiffTune, when differentiating (LMPC) through solving (LMPC-Grad), we only need lation: minimize τ1:T T t=1 t=1 Cθ,t(τt) subject to xt+1 = f (τt), x1 = xinit, g(τt) ≤ 0, (NMPC) where the nonlinear cost function Cθ,t is parameterized by θ, f is the nonlinear dynamics, and g is the nonlinear inequality constraint function. One common way to solve this nonlinear optimization problem is to treat it as nonlinear programming and apply sequential quadratic programming (SQP) [25], which iteratively generates and optimizes the following approximate problem: t <sup>p,</sup> <sup>x1</sup> <sup>= xinit,</sup> θ <sup>p</sup>,t <sup>τ</sup> t <sup>p</sup> t <sup>p)⊤τt</sup> minimize τ1:T T t=1 t=1 θ <sup>p</sup>,t <sup>τt + (∇C</sup> θ <sup>p</sup> θ <sup>p</sup>,t <sup>−∇2C</sup> θ <sup>p</sup> 1 2 <sup>τ</sup> t <sup>⊤∇2C</sup> θ <sup>p</sup> τ˜1 (more specifically u˜1). Hence, if u <sup>⋆</sup> 1 <sup>is</sup> <sup>active</sup> <sup>at</sup> <sup>some</sup> constraints (i.e., I1,act̸ = ∅), then the gradient of (LMPC) ∂u <sup>⋆</sup> 1 <sup>/∂θ</sup> <sup>(for</sup> <sup>θ</sup> <sup>being</sup> <sup>an</sup> <sup>arbitrary</sup> <sup>scalar</sup> <sup>element</sup> <sup>of</sup> <sup>Ct</sup> <sup>or</sup> ct) satisfies subject to xt+1 = ∇f <sup>p</sup> τt + f <sup>p</sup> −∇f <sup>p</sup> τt <sup>p</sup> ∇g <sup>p</sup> τt ≤∇g <sup>p</sup> τt <sup>p</sup> <sup>−</sup> <sup>gp,</sup> (L-NMPC) where the nonlinear cost function and constraints are Taylorexpanded at the p-th iteration’s optimal solution {τt <sup>p}t=1:T</sup> <sup>to</sup> expanded at the p-th iteration’s optimal solution {τt <sup>p}t=1:T</sup> <sup>to</sup> the second and first order, respectively. All the differentiation denoted by ∇ is with respect to τt, with superscript 2 indicating the Hessian. The superscript p indicates that the function is evaluated at the optimal solution of (L-NMPC) obtained in the p-th iteration. The solution to the displayed (L-NMPC), denoted by {τt <sup>p+1</sup> }t=1:T, is used in the Taylor expansion to = 0. (6) [G1]I1,act ˜τ1 = [G1]I1,act 1 ∂θ ∂u <sup>⋆</sup> 1 ∂θ - ∂x⋆ Consider a simple example of a scalar control u1 to be upper bounded by umax, where Gt = [0 1] and lt = umax for all t. If u <sup>⋆</sup> 1 <sup>= umax, then ∂u</sup> 1 <sup>⋆/∂θ = 0, indicating the inefficacy</sup> t. If u <sup>⋆</sup> 1 <sup>= umax, then ∂u</sup> 1 <sup>⋆/∂θ = 0, indicating the inefficacy</sup> of manipulating u <sup>⋆</sup> 1 <sup>by</sup> <sup>tweaking</sup> <sup>θ</sup> <sup>since</sup> <sup>u⋆</sup> 1 <sup>will</sup> <sup>remain</sup> denoted by {τt <sup>p+1</sup> }t=1:T, is used in the Taylor expansion to form the (p + 1)-th iteration of SQP. Problem (L-NMPC) has the identical form to that of (LMPC). When the SQP terminates at the N -th iteration, we take {τt <sup>N</sup> <sup>}t=1:T</sup> <sup>as</sup> <sup>the</sup> <sup>optimal</sup> <sup>solution</sup> <sup>to</sup> <sup>(NMPC)</sup> <sup>and</sup> of manipulating u <sup>⋆</sup> 1 <sup>by</sup> <sup>tweaking</sup> <sup>θ</sup> <sup>since</sup> <sup>u⋆</sup> 1 <sup>will</sup> <sup>remain</sup> umax when θ is perturbed locally. A detailed discussion on active constraints and corresponding gradients is available in Appendix F. Simulation results showing the degraded efficiency in learning when control saturation happens are included in Appendix E. B. Linear MPC with interpretable quadratic cost function Although the (Ct, ct)-parametrization is general enough, its physical meaning is not as clear as the conventional (Qt, Rt)-parametrization. By (Qt, Rt)-parametrization, we consider the cost function of the form Ct(xt, ut) = 2 <sup>1</sup> <sup>(xt −</sup> θ <sup>N</sup>,t <sup>τ</sup> t <sup>N</sup> consider the cost function of the form Ct(xt, ut) = 2 <sup>1</sup> <sup>(xt −</sup> x¯t) <sup>⊤</sup> Qt(xt - x¯t) + 2 <sup>1</sup> <sup>(ut</sup> <sup>−</sup> <sup>u¯t)⊤Rt(ut</sup> <sup>−</sup> <sup>u¯t),</sup> <sup>where</sup> <sup>x¯t</sup> −2[(Qt ¯x <sup>N</sup> t <sup>)⊤</sup> <sup>(Rt ¯uN</sup> t <sup>)⊤]⊤,</sup> <sup>respectively.</sup> <sup>Correspondingly,</sup> the computation of ∂u <sup>⋆</sup> 1 <sup>/∂[Qt]i,j</sup> <sup>and</sup> <sup>∂u⋆</sup> 1 <sup>/∂[Rt]i,j</sup> <sup>reduces</sup> <sup>N</sup> t <sup>)⊤</sup> <sup>(Rt ¯uN</sup> t θ <sup>N</sup>,t and (∇Cθ <sup>N</sup> θ <sup>N</sup>,t <sup>−∇2C</sup> θ <sup>N</sup>,t x¯t) <sup>⊤</sup> Qt(xt - x¯t) + 2 <sup>1</sup> <sup>(ut</sup> <sup>−</sup> <sup>u¯t)⊤Rt(ut</sup> <sup>−</sup> <sup>u¯t),</sup> <sup>where</sup> <sup>x¯t</sup> and u¯t are the reference state and control to be tracked. In this case, the parameters to be learned are Qt ∈ R <sup>n×n</sup> and Rt ∈ R <sup>m×m</sup>, and this cost function associates with the one in (LMPC) by Ct = diag(Qt, Rt) and ct = −[ <sup>Qt</sup> Rt <sup>] ¯τt</sup> we take {τt <sup>N</sup> <sup>}t=1:T</sup> <sup>as</sup> <sup>the</sup> <sup>optimal</sup> <sup>solution</sup> <sup>to</sup> <sup>(NMPC)</sup> <sup>and</sup> use (LMPC-Grad) to compute the gradients. Leveraging the chain rule, we can get the target gradients ∂u <sup>⋆</sup> 1 <sup>/∂θ</sup> <sup>and</sup> ∂u <sup>⋆</sup> 1 <sup>/∂xinit</sup> <sup>for</sup> <sup>DiffTune.</sup> Remark 4: For the cost function of (NMPC), the quadratic cost with (Qt, Rt)-parametrization in Section V-B is often used for an interpretable cost function. In this case, ∇ <sup>2</sup> Cθ <sup>N</sup>,t and (∇Cθ <sup>N</sup>,t <sup>−∇2C</sup> θ <sup>N</sup>,t <sup>τ</sup> t <sup>N</sup> <sup>)</sup> in the cost function of (L-NMPC) reduce to 2diag(Qt, Rt) and −2[(Qt ¯x <sup>N</sup> t <sup>)⊤</sup> <sup>(Rt ¯uN</sup> t <sup>)⊤]⊤,</sup> <sup>respectively.</sup> <sup>Correspondingly,</sup> the computation of ∂u <sup>⋆</sup> 1 <sup>/∂[Qt]i,j</sup> <sup>and</sup> <sup>∂u⋆</sup> 1 <sup>/∂[Rt]i,j</sup> <sup>reduces</sup> to the case covered in Section V-B. in (LMPC) by Ct = diag(Qt, Rt) and ct = −[ <sup>Qt</sup> Rt <sup>] ¯τt</sup> for τ¯t = [ u <sup>x¯</sup> ¯ <sup>t</sup> t <sup>].</sup> <sup>Similar</sup> <sup>to</sup> <sup>Table</sup> <sup>I,</sup> <sup>if</sup> <sup>one</sup> <sup>wants</sup> <sup>to</sup> <sup>solve</sup> <sup>for</sup> ∂u <sup>⋆</sup> 1 <sup>/∂[Qt]i,j</sup> <sup>and ∂u⋆</sup> 1 <sup>/∂[Rt]i,j, then one can simply plug in</sup> ∂u <sup>⋆</sup> 1 <sup>/∂[Qt]i,j</sup> <sup>and ∂u⋆</sup> 1 <sup>/∂[Rt]i,j, then one can simply plug in</sup> c˜t into problem (LMPC-Grad) as indicated in Table II and solve (LMPC-Grad) for the target gradients. C. Nonlinear MPC The linear MPC problem discussed above does not cover practical control problems in reality, where one can have general nonlinear dynamics, costs, and constraints. For such nonlinear MPC problems, we consider the following formu TABLE II: To get target gradients of (LMPC) with (Qt, Rt)parametrization in the left column, one can solve (LMPC-Grad) with coefficients c˜t and x˜init specified in the right column. The solution u˜ <sup>⋆</sup> 1 <sup>of</sup> <sup>(LMPC-Grad)</sup> <sup>equals</sup> <sup>the</sup> <sup>gradient</sup> <sup>of</sup> <sup>(LMPC)</sup> <sup>in</sup> <sup>the</sup> left column. Target gradients in Coefficients in (LMPC-Grad) (LMPC) c˜t x˜init ∂u <sup>⋆</sup> 1 ∂[Qt]i,j �[x <sup>⋆</sup> t <sup>⋆</sup> t <sup>−</sup> <sup>x¯t]j</sup> <sup>e</sup> i <sup>n</sup> <sup>x¯t]j</sup> <sup>e</sup> i 0 ∂u <sup>⋆</sup> 1 ∂[Rt]i,j 0 �[u <sup>⋆</sup> t <sup>−</sup> <sup>u¯</sup> <sup>⋆</sup> t <sup>−</sup> <sup>u¯t]j</sup> <sup>e</sup> i <sup>m</sup> i 0 0 VI. SIMULATION RESULTS In this section, we implement DiffTune to learn MPCs on different dynamical systems and different trajectories. We compare its learning performance with a baseline learning method [9] that uses policy search (PS-MPC) to determine the parameters for MPC. We implement [9, Alg. 1] with 10 samples of the MPC parameters in each episode. The samples follow a Gaussian distribution with the initial mean as the initial MPC parameters and the covariance matrix as 0.09I. We use β = 5 for the inverse temperature of the soft-max distribution. We use the following three dynamical systems: a 1D double-integrator system with box constraints for control actions and states, a differential wheeled robot with linear inequality constraints on the control actions, and a quadrotor with nonlinear dynamics. We use the interpretable quadratic cost function, i.e., (Q, R)-parametrization <sup>2</sup>, within the MPC formulation in all simulation. The loss function L is set to be the accumulated tracking error squared of the closed-loop system to the reference trajectory unless specified otherwise. The (Q, R) matrices are initialized as identity matrices of appropriate dimensions. We only learn the diagonal elements of Q and R matrices and restrict them to be bounded within [0.01,1000]. We solve the main MPC problem using acados [26]. For solving (LMPC-Grad), we use acados if the original MPC problem has linear (time-invariant) dynamics and use MATLAB’s quadprog otherwise (because acados does not support linear time-varying system of the form in (L-NMPC)). Both DiffTune and PS-MPC are set to use 20 episodes for the learning, which is sufficient for convergence with both methods (shown in Fig. 1). Double-integrator system: Consider the following double integrator with friction = 0 1 x(t) + 0 −0.05 - 0 1 u(t). (7) on the control action u apply: −2ωmr ≤ 2us + uwd ≤ 2ωmr, (9) −2ωmr ≤ 2us − uwd ≤ 2ωmr. (10) We use the following two reference trajectories for tracking: x¯1(t) =[1 − cos(0.5t), 0.5t, arctan(0.5/sin(0.5t))] <sup>⊤</sup>, x¯2(t) =[− sin(0.5t), 1 + cos(0.5t), 0.25t] <sup>⊤</sup> . The NMPC problem has a planning horizon of 0.5 s with 0.05 s sample time (T = 10). A total horizon of 10 s (N = 200) is considered for the closed-loop system. We set the learning rate to α = 0.01 for DiffTune. Quadrotor: Consider the following nonlinear dynamics: p˙ =v, v˙ = m <sup>−1</sup> fT zB + g, (11) q˙ = <sup>1</sup> ω˙ = J <sup>−1</sup> (M - ω × Jω), (12) 2 <sup>q ⊗</sup> <sup>[0</sup> <sup>ω⊤]⊤,</sup> where the state x = [p <sup>⊤</sup> v <sup>⊤</sup> q <sup>⊤</sup> ω <sup>⊤</sup> ] <sup>⊤</sup>, p ∈ R <sup>3</sup>, v ∈ R <sup>3</sup> denote the position and velocity of the quadrotor, both represented in the inertial frame, q ∈ S <sup>3</sup> is the quaternion representing the rotation from the inertial frame to the body frame (with ⊗ denoting the quaternion product), and ω ∈ R <sup>3</sup> represents the angular velocity in body frame. The gravitational acceleration is denoted by g = [0 0 - g] <sup>⊤</sup> (under the ENU coordinates). The vector zB is the unit vector aligning with the z-axis of the body frame, represented in the inertial frame. The control u contains four individual rotor thrusts and thus determines the total thrust fT and moment M ∈ R <sup>3</sup> . The quadrotor has parameters m = 0.03 kg and J = 10 <sup>−5</sup> diag(1.43, 1.43, 2.89) kgm <sup>2</sup> . We command two reference trajectories: one is a 3D Lissajous trajectory, and the other one is a polynomial trajectory generated using minimum-snap optimization [27]. For both trajectories, the NMPC problem has a planning horizon of 0.5 s with 0.05 s sample time (T = 10). A total horizon of 10 s (N = 200) is considered for the closed-loop system. We set the learning rate to α = 0.08 for DiffTune. The results are shown in Fig. 1, where we display the root-mean-squared error as an indicator of the systems’ performance through the learning episodes. Overall, DiffTuneMPC results in a better performance in fewer episodes than the PS-MPC, owing to the former’s usage of the first-order information of the loss function. On the contrary, PS-MPC requires sampled parameters and queries to the loss function to update its probabilistic model, where a relatively large variation of RMSE happens in the first few episodes in all test cases. For the linear system, DiffTune-MPC takes one episode to reduce the RMSE too close to the minimum (the parameters are updated with small changes in the following episodes, resulting in a minor reduction in the RMSE towards the end). VII. EXPERIMENTAL RESULTS WITH A HIGH-FIDELITY QUADROTOR SIMULATOR To show the applicability of the proposed DiffTuneMPC framework to real-world scenarios and its general x˙ (t) = p˙(t) v˙(t) We consider the boxed constraint |u| ≤ 0.9 for control constraint and speed limit |v| ≤ 0.8 as the state constraint. Two trajectories are used: x¯1(t) = [1+t−cos(t), 1+sin(t)] <sup>⊤</sup> and x¯2(t) = [0.8t, 0.8] <sup>⊤</sup> . A total horizon of 10 s is considered with time intervals of 0.01 s for discretization. The planning horizon of MPC is 0.2 s (T = 20). The learning rate is set to α = 0.5 for DiffTune. Differential wheeled robot: Consider the following model p˙x = cos(φ)us, p˙y = sin(φ)us, φ <sup>˙</sup> = uω, (8) where the system state x = [px, py, φ] <sup>⊤</sup> includes the horizontal position px, vertical position py, and heading angle φ, and the control action u = [us, uω] <sup>⊤</sup> includes the speed us and angular velocity uω. The differential wheels (radius r = 0.1 m) are placed with distance d = 0.5 m in between. The maximum angular velocity for each wheel is set to ωm = 2π rad/s. Considering the geometry and the angular velocity limit of each wheel above, the following linear inequalities 2We use time-invariant cost matrices in the simulation, i.e., Qt = Q and Rt = R for all t ∈{1, 2, . . ., T }. ization capability (not overfitting to any particular run or condition), we conduct experiments using the high-fidelity quadrotor simulator RotorPy [28], which provides realistic aerodynamic effects, motor dynamics, and sensor noise similar to what a quadrotor experiences in physical experiments. Using RotorPy, we apply DiffTune for batch learning an NMPC for a trajectory tracking task subject to trajectories with different speeds. In particular, the task is to track 3D Lissajous reference trajectories: p¯(t) = [γ sin(2πt/T ), γ sin(4πt/T ), γ sin(πt/T )] <sup>⊤</sup> for t ∈ [0, T ] with radius γ and total horizon T . We use a total of 11 such trajectories that share the same length (fixed γ) and shape but with various speeds. We manage different speeds by setting the total horizons T for the 11 trajectories to be evenly distributed from 5 to 15 s. In other words, each reference trajectory is designed to finish one round within the designated horizon to achieve different desired speeds. In each training episode, we control the quadrotor with an NMPC to run these reference trajectories, compute the gradient ∇θL for each run, and update the parameters (Q, R) using the averaged gradient from the 11 runs. The initial (Q, R) for this section can be found in Appendix G. In RotorPy, we set the simulation rate to 200 Hz and use the default vehicle and noise settings with a hummingbird profile. The NMPC problem has a planning horizon of 0.5 s with a sampling time of 0.05 s. We apply DiffTune for 15 episodes with a learning rate set to 0.01. The training results are shown in Fig. 2. It can be observed that the mean RMSE over the 11 reference trajectories decreases from 0.113 m to a final value of 0.082 m, indicating the learning capability of DiffTune in a batchsetting over multiple trajectories. We show the comparison of performance using learned and initial parameters in the same figure, where the improved tracking quality is clear. We evaluate the learned parameters over test trajectories that are not present in the training set to understand the generalization capability of the learned parameters. We also include the comparison to baseline hand-tuned parameters (these parameters can be found in Appendix G). The results are shown in Table III. The testing trajectories include 3D Lissajous trajectories with total horizons T outside the training set of [5,15], 3D Lissajous trajectories with different radius γ, 3D circle trajectories with different speeds, and two polynomial trajectories generated using minimum-snap optimization [27]. For the two polynomial trajectories, their average speeds are set to 1 and 2 m/s. The waypoints used to define these two trajectories can be found in Appendix G. Overall, the results show that the learned parameters by DiffTune-MPC outperform the hand-tuned parameters in all tested trajectories. Over the 3D Lissajous trajectories with the same radius (γ = 1), it can be observed that RMSE decreases when the total horizon increases. This tendency is expected because a larger value of the total horizon indicates a lower speed, and thus, the quadrotor can track the desired trajectory better. The same tendency can be consistently observed with 3D circle trajectories, where the RMSE decreases when the radius turns bigger (subject to a fixed total horizon), (a) Double integrator, traj. 1 (b) Double integrator, traj. 2 (c) Diff. wheeled robot, traj. 1 (d) Diff. wheeled robot, traj. 2 (e) Quadrotor, traj. 1 (f) Quadrotor, traj. 2 Fig. 1: Simulation results comparing the learning progress of DiffTune-MPC (ours) and the baseline PS-MPC [9]. The shaded areas show the range of RMSEs (min to max) achieved with a total of 10 sampled parameters by PS-MPC. indicating a faster speed. Based on the training and the evaluation results shown in Fig. 2 in Table III, we have validated the proposed framework’s generalization capability for not overfitting to a particular task or condition and its applicability subject to realistic conditions with sensor noise and unknown factors (aerodynamics and rotor dynamics). TABLE III: Evaluation of the trained parameters and the baseline hand-tuned parameters on various trajectories in RotorPy with different radius γ and total horizon T . The reported values are the RMSEs over 5 evaluations, displayed by mean ± standard deviation. Trajectories used in the training set are signified by *. DiffTune-MPC Trajectory γ [m] T [s] RMSE [m] Hand-tuned RMSE [m] 3D Lissajous 1 3 0.332 ± 0.002 0.365 ± 0.002 3D Lissajous* 1 10 0.069 ± 0.002 0.086 ± 0.002 3D Lissajous 1 20 0.033 ± 0.001 0.046 ± 0.002 3D Lissajous 3 10 0.186 ± 0.002 0.234 ± 0.003 3D Lissajous 5 10 0.301 ± 0.001 0.395 ± 0.004 3D Circle 1 10 0.194 ± 0.001 0.206 ± 0.002 3D Circle 3 10 0.583 ± 0.002 0.594 ± 0.001 Polynomial 1 N/A 10 0.050 ± 0.001 0.076 ± 0.001 Polynomial 2 N/A 10 0.086 ± 0.002 0.097 ± 0.001 VIII. CONCLUSION In this paper, we develop a closed-loop learning scheme for MPC using DiffTune. We learn the parameters in the cost function to improve the closed-loop system’s performance specified by a loss function that is different than the MPC’s cost function. To use DiffTune, we need the gradient of the first optimal control action with respect to the initial state and parameters of the cost function in the MPC problem. Our derivation is centered on a linear MPC Fig. 2: Left: Evolution of the tracking performance with DiffTune-MPC in RotorPy over a total of 11 3D Lissajous training trajectories. The curve shows the mean RMSE, whereas the shaded area shows the standard deviation. Right: Performance comparison over the 3D Lissajous trajectory with a total duration of 10 s. problem with linear inequality constraints (to incorporate safety requirements of physical limitations of the system). We show that the linear-MPC-based differentiation can be applied to a nonlinear MPC that is solved via SQP. We validate our approach in simulation for systems with linear and nonlinear dynamics subject to constraints on the state and control. Our approach shows more efficient learning compared with a baseline policy-search-based learning scheme due to our effective usage of first-order information. We also demonstrate the generalization capability of the proposed scheme to different trajectories and its applicability to realworld scenarios in a quadrotor high-fidelity simulator. Future work will investigate the usage of DiffTune for different evaluation criteria (e.g., robustness) and the application of DiffTune-MPC on challenging robotic tasks, e.g., designing cost functions for trajectory tracking with hybrid systems in contact-rich scenarios. ACKNOWLEDGEMENT We gratefully acknowledge Yuliang Gu and Chengyu Yang for sharing their NMPC implementation in RotorPy with us. REFERENCES [1] S. Cheng, M. Kim, L. Song, S. Wang, and N. Hovakimyan, “Difftune: Auto-tuning through auto-differentiation,” under revision for IEEE Transactions on Robotics, arXiv preprint arXiv:2209.10021, 2023. [2] B. Amos, I. Jimenez, J. Sacks, B. Boots, and J. Z. Kolter, “Differ entiable MPC for end-to-end planning and control,” in Proceedings of the 32nd Conference on Neural Information Processing Systems, vol. 31, Montreal, Canada, 2018. [3] W. Jin, S. Mou, and G. J. Pappas, “Safe Pontryagin differentiable programming,” Advances in Neural Information Processing Systems, vol. 34, pp. 16 034–16 050, 2021. [4] H. Parwana and D. Panagou, “Recursive feasibility guided optimal parameter adaptation of differential convex optimization policies for safety-critical systems,” arXiv:2109.10949, 2021. [5] S. Cheng, L. Song, M. Kim, S. Wang, and N. Hovakimyan, “Diff tune+: Hyperparameter-free auto-tuning using auto-differentiation,” in Learning for Dynamics and Control Conference. PMLR, 2023, pp. 170–183. [6] A. R. Kumar and P. J. Ramadge, “DiffLoop: Tuning PID controllers by differentiating through the feedback loop,” in Proceedings of the 55th Annual Conference on Information Sciences and Systems, Baltimore, MD, USA, 2021, pp. 1–6. [7] F. Berkenkamp, A. P. Schoellig, and A. Krause, “Safe controller optimization for quadrotors with Gaussian processes,” in Proceedings of IEEE International Conference on Robotics and Automation, Stockholm, Sweden, 2016, pp. 491–496. [8] R. R. Duivenvoorden, F. Berkenkamp, N. Carion, A. Krause, and A. P. Schoellig, “Constrained Bayesian optimization with particle swarms for safe adaptive controller tuning,” IFAC-PapersOnLine, vol. 50, no. 1, pp. 11 800–11 807, 2017. [9] Y. Song and D. Scaramuzza, “Policy search for model predictive control with application to agile drone flight,” IEEE Transactions on Robotics, vol. 38, no. 4, pp. 2114–2130, 2022. [10] A. Loquercio, A. Saviolo, and D. Scaramuzza, “Autotune: Controller tuning for high-speed flight,” IEEE Robotics and Automation Letters, vol. 7, no. 2, pp. 4432–4439, 2022. [11] W. Edwards, G. Tang, G. Mamakoukas, T. Murphey, and K. Hauser, “Automatic tuning for data-driven model predictive control,” in Proceedings of the IEEE International Conference on Robotics and Automation, Xi’an, China, 2021, pp. 7379–7385. [12] W. D. Null, W. Edwards, D. Jeong, T. Tchalakov, J. Menezes, K. Hauser et al., “Automatically-tuned model predictive control for an underwater soft robot,” IEEE Robotics and Automation Letters, 2023. [13] W. Jin, Z. Wang, Z. Yang, and S. Mou, “Pontryagin differentiable programming: An end-to-end learning and control framework,” in Proceedings of the 34th Conference on Neural Information Processing Systems, vol. 33, Vancouver, Canada, 2020, pp. 7979–7992. [14] A. Oshin and E. A. Theodorou, “Differentiable robust model predictive control,” arXiv preprint arXiv:2308.08426, 2023. [15] M. Zanon and S. Gros, “Safe reinforcement learning using robust MPC,” IEEE Transactions on Automatic Control, vol. 66, no. 8, pp. 3638–3652, 2020. [16] N. Wiedemann, V. W¨uest, A. Loquercio, M. M¨uller, D. Floreano, and D. Scaramuzza, “Training efficient controllers via analytic policy gradient,” in 2023 IEEE International Conference on Robotics and Automation (ICRA). IEEE, 2023, pp. 1349–1356. [17] A. Romero, Y. Song, and D. Scaramuzza, “Actor-critic model predic tive control,” arXiv preprint arXiv:2306.09852, 2023. [18] G. Torrente, E. Kaufmann, P. F¨ohn, and D. Scaramuzza, “Data-driven mpc for quadrotors,” IEEE Robotics and Automation Letters, vol. 6, no. 2, pp. 3769–3776, 2021. [19] K. Y. Chee, T. Z. Jiahao, and M. A. Hsieh, “KNODE-MPC: A knowledge-based data-driven predictive control framework for aerial robots,” IEEE Robotics and Automation Letters, vol. 7, no. 2, pp. 2819–2826, 2022. [20] K. Y. Chee, T. C. Silva, M. A. Hsieh, and G. J. Pappas, “En hancing sample efficiency and uncertainty compensation in learningbased model predictive control for aerial robots,” arXiv preprint arXiv:2308.00570, 2023. [21] T. Z. Jiahao, K. Y. Chee, and M. A. Hsieh, “Online dynamics learning for predictive control with an application to aerial robots,” in Conference on Robot Learning. PMLR, 2023, pp. 2251–2261. [22] A. Saviolo, G. Li, and G. Loianno, “Physics-inspired temporal learn ing of quadrotor dynamics for accurate model predictive trajectory tracking,” IEEE Robotics and Automation Letters, vol. 7, no. 4, pp. 10 256–10 263, 2022. [23] N. Parikh, S. Boyd et al., “Proximal algorithms,” Foundations and Trends® in Optimization, vol. 1, no. 3, pp. 127–239, 2014. [24] B. Amos and J. Z. Kolter, “OptNet: Differentiable optimization as a layer in neural networks,” in Proceedings of the 34th International Conference on Machine Learning, Sydney, Australia, 2017, pp. 136– 145. [25] P. T. Boggs and J. W. Tolle, “Sequential quadratic programming,” Acta numerica, vol. 4, pp. 1–51, 1995. [26] R. Verschueren, G. Frison, D. Kouzoupis, J. Frey, N. van Duijkeren, A. Zanelli, B. Novoselnik, T. Albin, R. Quirynen, and M. Diehl, “acados - a modular open-source framework for fast embedded optimal control,” Mathematical Programming Computation, Oct 2021. [27] D. Mellinger and V. Kumar, “Minimum snap trajectory generation and control for quadrotors,” in Proceedings fo the International Conference on Robotics and Automation, Shanghai, China, 2011, pp. 2520–2525. [28] S. Folk, J. Paulos, and V. Kumar, “Rotorpy: A python-based multi rotor simulator with aerodynamics for education and research,” arXiv preprint arXiv:2306.04485, 2023. [29] S. P. Boyd and L. Vandenberghe, Convex optimization. Cambridge university press, 2004. APPENDIX We show how the auxiliary problem (LMPC-Grad) is derived using the KKT condition in the Appendix. We start with an LQR and then cover the case with a general linear MPC (an LQR with linear inequality constraints). A. Differentiation of an LQR We start the illustration of obtaining the gradient of an LQR’s first optimal control action to the parameters of interest (cost coefficients and initial state). Consider the following LQR problem, where we stick to the same notation as in [2]: costate dynamics, and boundary conditions: dxt+1 =dFtxt + Ftdxt + dft, (18a)  <sup>⋆</sup> t <sup>=dF</sup> t <sup>⊤</sup> dλ <sup>⋆</sup> t <sup>F ⊤</sup> t+1,x <sup>dλt+1 + dCt+1,xτ</sup> t <sup>⋆</sup> +1 + Ct+1,xdτt <sup>⋆</sup> +1 <sup>+ dct+1,x,</sup> t <sup>⊤</sup> +1,x <sup>λt+1 + F</sup> t <sup>⊤</sup> +1 t <sup>⊤</sup> +1,x <sup>dλt+1 + dCt+1,xτ</sup> t <sup>⋆</sup> + Ct+1,xdτt <sup>⋆</sup> +1 <sup>+ dct+1,x,</sup> (18b) dx1 =dxinit, (18c) <sup>⋆</sup> T −1 <sup>=dCT,xx</sup> T <sup>⋆</sup> dλ <sup>⋆</sup> T T <sup>⋆</sup> <sup>+ CT,xdx</sup> T <sup>⋆</sup> T <sup>⋆</sup> <sup>+ dcT,x.</sup> (18d) The KKT factorization for (18) can be presented as follows: −df1 − dF1τ1 <sup>⋆</sup> −dc2 − dC2τ2 <sup>⋆</sup> <sup>−</sup> <sup>dF</sup>  =  −dxinit −dc1 − dC1τ1 <sup>⋆</sup> 1 − dC1τ1 <sup>⋆</sup> <sup>−</sup> <sup>dF</sup> 1 <sup>⊤λ</sup> 1 <sup>⋆</sup> −df1 − dF1τ1 <sup>⋆</sup> dλ <sup>⋆</sup> 0 dτ1 <sup>⋆</sup> dλ <sup>⋆</sup> 1 dτ2 <sup>⋆</sup>   . minimize τ1:T T t=1 t=1 1 2 <sup>τ</sup> t <sup>⊤Ctτt</sup> <sup>+ c</sup> t <sup>⊤τt</sup> 2 ... dλ <sup>⋆</sup> T − −dc2 − dC2τ2 <sup>⋆</sup> <sup>−</sup> <sup>dF</sup> 2 <sup>⊤λ</sup> 2 <sup>⋆</sup> ... −dcT −1 − dCT −1τT <sup>⋆</sup> −1 <sup>−</sup> <sup>dF</sup> T <sup>⊤</sup> subject to xt+1 = Ftτt + ft, x1 = xinit, (P1) K  where τt = [ u <sup>xt</sup> t <sup>]</sup> <sup>∈</sup> <sup>Rn+m</sup> <sup>is</sup> <sup>the</sup> <sup>composite</sup> <sup>state-control,</sup> Ft = [ At Bt ] ∈ R <sup>n×(n+m)</sup>, and ft ∈ R <sup>n</sup> stands for residuals in the dynamics. With the Lagrangian (19) We start with the Jacobian ∂u <sup>⋆</sup> 1 <sup>/∂xinit because it is simpler</sup>  T −1 dτT <sup>⋆</sup> 1 − dCT −1τT <sup>⋆</sup> −1 <sup>−</sup> <sup>dF</sup> T <sup>⊤</sup> −1 <sup>λ⋆</sup> T −1 −dfT −1 − dFT −1τT <sup>⋆</sup> −1 T −1 − dFT −1τT <sup>⋆</sup> −1 −dcT - dCT τT <sup>⋆</sup> T <sup>⋆</sup> −1 <sup>−</sup> <sup>dF</sup> T <sup>⊤</sup> T <sup>⊤</sup> −1 <sup>λ⋆</sup> T T  T L(τ, λ) = + T t=1 T −1 t=1 T −1 t=0 2 <sup>τ ⊤Ctτt + c</sup> t <sup>⊤τt</sup> - 1 λ <sup>⊤</sup> t <sup>(Ftτt</sup> <sup>+ ft</sup> <sup>−</sup> <sup>xt+1),</sup> (13) <sup>t</sup> We start with the Jacobian ∂u <sup>⋆</sup> 1 <sup>/∂xinit because it is simpler</sup> than the other two Jacobians. We set the differentials dCt, dct, dFt, and dft to zero for t ∈{1, 2, . . ., T } in (19) and we have   , (20) t=0 −In ∂λ <sup>⋆</sup> 0 ∂xinit ∂τ1 <sup>⋆</sup>    0 ... 0 0 where λt ∈ R <sup>n</sup> is the vector of the Lagrangian multipliers associated with the dynamics constraints, and F0 = 0 and f0 = xinit in the Lagrangian above. The solution to the LQR problem is obtained at the fixed point of the Lagrangian, i.e., when   K 1 ∂xinit ... ∂λ <sup>⋆</sup> T − = T −1 ∂xinit ∂τT <sup>⋆</sup>  T ∂xinit  ∂τ1 <sup>⋆</sup> ∂xinit t <sup>⋆</sup> <sup>+ ct</sup> <sup>+ F</sup> t <sup>⊤</sup> <sup>⋆</sup> t <sup>−</sup> ∇τt L(τ <sup>⋆</sup>, λ <sup>⋆</sup> ) = Ctτt <sup>⋆</sup> t <sup>⊤λ⋆</sup> t = 0. (14) where the target Jacobian ∂u <sup>⋆</sup> 1 where the target Jacobian ∂u <sup>⋆</sup> 1 <sup>/∂xinit</sup> <sup>is</sup> <sup>contained</sup> <sup>within</sup> ∂τ1 <sup>⋆/∂xinit</sup> <sup>such</sup> <sup>that</sup> 1 <sup>⋆/∂xinit</sup> <sup>such</sup> <sup>that</sup> λ <sup>⋆</sup> t 0 t−1 0 . (21) 1 ∂xinit ∂u <sup>⋆</sup> 1 - ∂∂xxinit⋆ 1 ∂xinit ∂u <sup>⋆</sup> 1 - ∂∂xxinit⋆ Following this relation, we can derive the costate dynamics = λ <sup>⋆</sup> T <sup>⋆</sup> T −1 <sup>=CT,xx⋆</sup> T <sup>⋆</sup> T <sup>+ cT,x,</sup> (15) <sup>⋆</sup> t <sup>=F</sup> t <sup>⊤</sup> λ <sup>⋆</sup> t t <sup>⊤</sup> +1,x <sup>λt+1 + Ct+1,xx⋆</sup> t <sup>⋆</sup> t+1 <sup>+ ct+1,x,</sup> (16) If we compare the form and match the terms of (20) to the KKT factorization of the original problem in (17), then we can obtain ∂u <sup>⋆</sup> 1 <sup>/∂xinit</sup> <sup>by</sup> <sup>solving the</sup> <sup>following auxiliary</sup> LQR problem for t ∈{0, 1, . . ., T - 2}. The notation C·,x, F·,x, and c·,x stands for the first n columns of C· and F·, and first n rows of c·, respectively. The original dynamics, costate dynamics, and the initial state allow for writing the KKT factorization in (17), where K is the KKT factorization matrix. To obtain the derivatives ∂u <sup>⋆</sup> 1 <sup>/∂C</sup> t <sup>and ∂u</sup> 1 <sup>⋆/∂ct,</sup> <sup>we</sup> <sup>will</sup> take a look at the matrix differential of original dynamics, minimize τ˜1:T T t=1 t=1 1 2 <sup>τ˜</sup> t <sup>⊤C˜t ˜τt + ˜c⊤</sup> t <sup>τ˜t</sup> subject to x˜t+1 = F <sup>˜</sup> t ˜τt + f <sup>˜</sup> t, x˜1 = x˜init. (LQR-Aux)  C1 1 F1 −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> C2  λ <sup>⋆</sup> 0 τ1 <sup>⋆</sup> λ <sup>⋆</sup> 1 τ2 <sup>⋆</sup> = (17)   −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> C1 <sup>I</sup> 0 <sup>]</sup> C1 F1 <sup>⊤</sup>   <sup>I</sup> 0 <sup>]</sup> C2 ...  −xinit −c1 −f1 −c2 ... −cT −1 −fT −1 FT <sup>⊤</sup> T −1   −cT . −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> CT    2 ... λ <sup>⋆</sup> T T −1 τT <sup>⋆</sup>   <sup>I</sup> 0 <sup>]</sup> CT T :=K - �� The gradient ∂u <sup>⋆</sup> 1 <sup>/∂[xinit]j</sup> <sup>can</sup> <sup>be</sup> <sup>solved</sup> <sup>from</sup> <sup>(LQR-Aux)</sup> following the observations below. By (20), we have have <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup>  K  0 ... −(x <sup>⋆</sup> t <sup>−</sup> <sup>x¯t</sup> 0   . (22)   0 0 ...   =   .  −In 0 ∂[xinit]j ∂τ1 <sup>⋆</sup> ∂[xinit]j ... ∂λ <sup>⋆</sup> T −1 ∂[xinit]j ∂τT <sup>⋆</sup> ∂[xinit]j ∂λ <sup>⋆</sup> 0 [xinit ∂τ1 <sup>⋆</sup>   :,j 0 ... 0 0   ∗ ∂τ1 <sup>⋆/∂[Qt]i,j</sup> ∗ ∗ ∂τ1 <sup>⋆/∂[</sup> :,(i−1)n+j K  = ∂τT <sup>⋆</sup> Matching (22) with (17), ∂u <sup>⋆</sup> 1 Matching (22) with (17), ∂u <sup>⋆</sup> 1 <sup>/∂[xinit]j</sup> <sup>can</sup> <sup>be</sup> <sup>solved</sup> <sup>via</sup> (LQR-Aux) with C <sup>˜</sup> t = Ct, c˜t = 0, F <sup>˜</sup> t = Ft, f <sup>˜</sup> t = 0, and (27) In other words, the derivative ∂u <sup>⋆</sup> 1 <sup>/∂[Qt]i,j</sup> <sup>can</sup> <sup>be</sup> <sup>solved</sup> from (LQR-Aux) with C˜t = Ct, F˜t = Ft, f˜t = 0, x˜init = [In]:,j = e <sup>n</sup> j <sup>.</sup> �(x <sup>⋆</sup> t <sup≯</sup> <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup> 0 <sup>n</sup> j <sup>.</sup> x˜init = 0 for all t, and c˜t = : <sup≯</sup> :,(i−1)n+j Similarly, for the Jacobian ∂u <sup>⋆</sup> 1 <sup>/∂C</sup> t <sup>,</sup> <sup>we</sup> <sup>start</sup> <sup>with</sup> for this specific t and c˜t′ = 0 for all t <sup>′̸</sup> = t. similar derivations to obtain ∂u <sup>⋆</sup> 1 <sup>/∂[Rt]i,j</sup> x˜init = 0 for all t, and c˜t = = 0 :,(i−1)n+j - t <sup>−</sup> <sup>x¯t]je</sup> i <sup>n</sup> for this specific t and c˜t′ = 0 for all t <sup>′̸</sup> = t. 0 �[x⋆ One can apply similar derivations to obtain ∂u <sup>⋆</sup> 1 <sup>/∂[Rt]i,j</sup> One can apply similar derivations to obtain ∂u <sup>⋆</sup> 1 One can apply similar derivations to obtain ∂u <sup>⋆</sup> 1 <sup>/∂[Rt]i,j</sup> from (LQR-Aux) with x˜init = 0 for all t, and c˜t = 0 0 = and c˜t′ = (u <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup> :,(i−1)n+j [u <sup>⋆</sup> t <sup>−</sup> <sup>u¯t]je</sup> i <sup>m</sup> - - - 0 for all t <sup>′̸</sup> = t. <sup>x¯t]je</sup> i 0 t <sup>⋆</sup> <sup>= ((τ</sup> t <sup>⋆</sup> dCtτt <sup>⋆</sup> t <sup>⋆)⊤</sup> <sup>⊗</sup> <sup>In+m)dC</sup> t (23) using the vectorization (with Kronecker product) formula ABC = (C <sup>⊤</sup> ⊗ A)B. This equation will lead to the factorization 0 for all t <sup>′̸</sup> = t. <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup> <sup≯</sup> 0 [u <sup>⋆</sup> t <sup>−</sup> <sup>u¯</sup> <sup>⋆</sup> t <sup>−</sup> <sup>u¯t]je</sup> i <sup>m</sup> : :,(i−1)n+j = and c˜t′ = i t <sup>⋆)⊤</sup> <sup>⊗</sup> <sup>In+m</sup>   0 ... −(τt <sup>⋆)⊤</sup> <sup>⊗</sup> 0 0 ... . (24)     = K   ∗ ∂τ1 <sup>⋆</sup> C. Differentiation of an LQR with linear inequality constraints A slightly enhanced version of LQR includes path constraints on the state and control, e.g., with linear inequalities, which results in the formulation of (LMPC). Following the derivation in Appendix A, we can write the Lagrangian for (LMPC) as L(τ, λ, ν) 1 <sup>⋆/∂C</sup> t ∗ Matching (24) with (17), given any specific t, the derivative ∂u <sup>⋆</sup> 1 <sup>/∂[Ct]i,j</sup> <sup>can</sup> <sup>be</sup> <sup>solved</sup> <sup>from</sup> <sup>(LQR-Aux)</sup> <sup>with</sup> <sup>C˜t</sup> <sup>=</sup> ∂u <sup>⋆</sup> 1 <sup>/∂[Ct]i,j</sup> <sup>can</sup> <sup>be</sup> <sup>solved</sup> <sup>from</sup> <sup>(LQR-Aux)</sup> <sup>with</sup> <sup>C˜t</sup> <sup>=</sup> Ct, F <sup>˜</sup> t = Ft, f˜t = 0 for all t, x˜init = 0, and c˜t = [(τt <sup>⋆)⊤</sup> <sup>⊗In+m]:,(i−1)(n+m)+j</sup> <sup>= [τ</sup> t <sup>⋆]jen</sup> i <sup>+m</sup> for this specific t and c˜t′ = 0 for all t <sup>′̸</sup> = t. Likewise, if one wants to get ∂u <sup>⋆</sup> 1 <sup>/∂[ct]i,</sup> <sup>then</sup> <sup>it</sup> <sup>suffices</sup> <sup>to</sup> <sup>solve</sup> <sup>(LQR-Aux)</sup> <sup>with</sup> c˜t = e <sup>n</sup> i <sup>+m</sup> and c˜t′ = 0 for all t <sup>′̸</sup> = t. t <sup>⋆]jen</sup> i <sup>+m</sup> <sup≯</sup> t <sup>⋆)⊤</sup> <sup>⊗In+m]:,(i−1)(n+m)+j</sup> <sup>= [τ</sup> t <sup>⋆]</sup> <sup≯</sup> T −1 t=0 + <sup>n</sup> i <sup>+m</sup> and c˜t′ = 0 for all t <sup>′̸</sup> = t. = T t=1 λ <sup>⊤</sup> t <sup>(Ftτt + ft −</sup> <sup>xt+1)</sup> t=1 2 <sup>τ</sup> t <sup>⊤Ctτt + c</sup> t <sup>⊤τt</sup> - 1 2 <sup>τ</sup> t <sup>⊤Ctτt + c</sup> t <sup>⊤</sup> - 1 + νt <sup>⊤(Gtτt −</sup> <sup>lt),</sup> (28) t=1 B. Differentiation of an LQR with (Q, R)-parametrization In this case, we use the (Q, R)-parameterization with coefficients of the Qt ∈ R <sup>n×n</sup> and Rt ∈ R <sup>m×m</sup> matrices in the cost function, which are composited as Ct = diag(Qt, Rt) following the notation in (P1). Note that ct also contains the coefficients of Qt and Rt such that ct = −[ <sup>Qt</sup> Rt <sup>] ¯τt</sup> <sup>for</sup> τ¯t = [ u <sup>x¯</sup> ¯ <sup>t</sup> t <sup>].</sup> For ∂u <sup>⋆</sup> 1 <sup>/∂Q</sup> t <sup>,</sup> <sup>one</sup> <sup>can</sup> <sup>apply</sup> <sup>the</sup> <sup>same</sup> <sup>methodology</sup> <sup>as</sup> applied in the previous subsection. Note that where νt ∈ R <sup>g</sup> is the Lagrange multiplier associated with the inequality constraints. Let It,act ⊆{1, 2, . . ., g} denote the rows where the inequality Gtτt ≤ lt is active, i.e., [Gt]It,act τt = [lt]It,act . Correspondingly, It,ina = {1, 2, . . ., g} \ It,act denotes the inactive constraints, i.e., [Gt]It,inaτt < [lt]It,ina Following the complementary slackness [29], we know the following condition holds at the optimality point: T t=1 [νt]It,ina = 0, [Gt]It,inaτt − [lt]It,ina < 0, (29) [Gt]It,act τt − [lt]It,act = 0, [νt]It,act - 0. (30) The costate dynamics for (LMPC) are dCtτt <sup>⋆</sup> <sup>+ dct</sup> <sup>=</sup> dCtτt <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)</sup> dRt(u <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)</sup> �dQt(x⋆ T <sup>⋆</sup> <sup>,</sup> (31) <sup>⋆</sup> T <sup>+ cT,x + G</sup> T, <sup>⊤</sup> T, <sup>⊤</sup> x <sup>ν</sup> T <sup>⋆</sup> (x <sup>⋆</sup> t (u <sup>⋆</sup> t �� <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)</sup> (25) (32) We can write down the KKT factorization for (LMPC) as shown in (33), with the KKT factorization matrix denoted by KLI. λ <sup>⋆</sup> T <sup>⋆</sup> T −1 <sup>=CT,xx⋆</sup> T dRt - <sup>⋆</sup> t+1 <sup>+ ct+1,x</sup> <sup>+ G</sup> t, <sup>⊤</sup> t <sup>⋆.</sup> t <sup>⊤</sup> +1,x <sup>λt+1</sup> <sup>+ Ct+1,xx⋆</sup> t t, <sup>⊤</sup> x <sup>ν</sup> t <sup>⋆</sup> = (x <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup> dQt (u <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)⊤</sup> <sup>⊗</sup> <sup>Im</sup> dRt �� - then dQt(x <sup>⋆</sup> t - <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In</sup> <sup>⋆</sup> t <sup>−</sup> <sup>u¯t)⊤</sup> <sup>⊗</sup> <sup>Im</sup> (26) λ <sup>⋆</sup> t <sup>⋆</sup> t <sup>=F</sup> t <sup>⊤</sup> If Qt is symmetric, then dQt(x <sup>⋆</sup> t If Qt is symmetric, then dQt(x <sup>⋆</sup> t - x¯t) = 1 (x <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In + In ⊗</sup> <sup>(x⋆</sup> dQt (the same 2 conclusion applies to a symmetric matrix Rt). Hence, we - t <sup>−</sup> <sup>x¯t)⊤�</sup> <sup>⋆</sup> matrix t <sup>−</sup> <sup>x¯t)⊤�</sup> <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In + In ⊗</sup> <sup>(x⋆</sup> (x <sup>⋆</sup> t <sup>−</sup> <sup>x¯t)⊤</sup> <sup>⊗</sup> <sup>In + In ⊗</sup> <sup>(x⋆</sup> dQt (the same 2 conclusion applies to a symmetric matrix Rt). Hence, we - t <sup>−</sup> <sup>x¯t)⊤�</sup>  C1 1 1 II1,ina [G1]I1,act   λ <sup>⋆</sup> 0 τ1 <sup>⋆</sup> ν1 <sup>⋆</sup> λ <sup>⋆</sup> 1 τ2 <sup>⋆</sup> ν2 <sup>⋆</sup> <sup>⊤</sup> 1 F1 <sup>⊤</sup>     −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> C1 <sup>I</sup> 0 <sup>]</sup> C1 G <sup>⊤</sup> 1 F1 −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> C2 <sup>I</sup> 0 <sup>]</sup> C2 G <sup>⊤</sup> 2 C2 2 II2,ina [G2]I2,act   −xinit −c1 0I1,ina [l1]I1,act −f1 −c2 0I2,ina [l2]I1,act ... −fT −1 −cT 0IT,ina [lt]IT,act . = (33) ...   −[ I 0 ] −[ <sup>I</sup> 0 <sup>]</sup> CT 2 ... λ <sup>⋆</sup> T <sup>I</sup> 0 <sup>]</sup> CT G <sup>⊤</sup> T T −1 τT <sup>⋆</sup> t T IIT,ina [GT ]IT,act  T νt <sup>⋆</sup> :=KLI - �� For the differentials associated with (LMPC), we have the optimal solution τt <sup>⋆,</sup> <sup>i.e.,</sup> <sup>G˜t</sup> <sup>=</sup> <sup>[Gt]I</sup> t,act <sup>.</sup> <sup>The</sup> <sup>inactive</sup> constraints (at the optimal solution of (LMPC)) are not considered in (LMPC-Aux) because the optimal solution of (LMPC) is “locally unconstrained” at the inactive constraints, i.e., removing the inactive constraints will not change the optimal solution. Therefore, the inactive constraints [Gt]It,ina are not inherited from (LMPC) to (LMPC-Grad), and only active constraints show up in (LMPC-Grad) in the form of   −dxinit −dc1 − dC1τ1 <sup>⋆</sup> <sup>−</sup> <sup>dF</sup> 1 T <sup>−</sup> <sup>dG</sup> T <sup>⊤ν</sup> T <sup>⋆</sup> 0I1,ina −d[GT ]IT,act τ1 <sup>⋆</sup> <sup>+ d[lt]I</sup> T,act 1 <sup>⋆</sup> <sup>+ d[lt]I</sup> T,act   1 − dC1τ1 <sup>⋆</sup> <sup>−</sup> <sup>dF</sup> 1 <sup>⊤λ⋆</sup> 1 <sup>−</sup> <sup>dG</sup> 1 <sup>⊤ν</sup> 1 <sup>⋆</sup> 0I1,ina −d[G1]I1,act τ1 <sup>⋆</sup> <sup>+ d[l1]I</sup> 1,act are turned into an equality constraint G˜t ˜τt = l˜t in (LMPC-Aux), where the latter equality constraint only holds for those time indices t with the constraint being active at the optimal solution τt <sup>⋆,</sup> <sup>i.e.,</sup> <sup>G˜t</sup> <sup>=</sup> <sup>[Gt]I</sup> t,act <sup>.</sup> <sup>The</sup> <sup>inactive</sup> dλ <sup>⋆</sup> 0 dτ1 <sup>⋆</sup> dν1 <sup>⋆</sup> KLI  1 ... dλ <sup>⋆</sup> T =  d[G1]I1,act τ1 <sup>⋆</sup> <sup>+ d[l1]I</sup> 1,act ... −dfT −1 − dFT −1τT <sup>⋆</sup> −1 T −1 dτT <sup>⋆</sup>   −dfT −1 − dFT −1τT <sup>⋆</sup> −1 −dcT - dCT τT <sup>⋆</sup> <sup>−</sup> <sup>dG</sup> T <sup>⊤ν</sup> T <sup>⋆</sup> <sup>−</sup> <sup>dG</sup> T <sup>⊤</sup> T <sup>⊤ν</sup> T <sup>⋆</sup> T dνT <sup>⋆</sup> T (34) One observation follows from (34): when considering the Jacobians with respect to Ct and ct, then only non-zero differentials on the right-hand side of (34) will be dCt and dct, which resembles the structure of the right-hand side of (19). This observation indicates that the gradients ∂u <sup>⋆</sup> 1 <sup>/∂C</sup> t <sup>and</sup> ∂u <sup>⋆</sup> 1 <sup>/∂ct</sup> <sup>can</sup> <sup>be</sup> <sup>obtained</sup> <sup>following</sup> <sup>the</sup> <sup>approach</sup> <sup>shown</sup> before with LQR in Appendix A. Following the derivation in Appdendix A for (P1), one can solve the following auxiliary problem to obtain the gradient for (LMPC): G˜t ˜τt = 0 for G˜t = [Gt]It,act . minimize τ˜1:T T t=1 t=1 1 2 <sup>τ˜</sup> t <sup>⊤C˜tτ˜t</sup> <sup>+ ˜c</sup> t <sup>⊤τ˜t</sup> subject to x˜t+1 = F <sup>˜</sup> t ˜τt + f <sup>˜</sup> t, x˜1 = x˜init, G˜t ˜τt = ˜lt. (LMPC-Aux) D. Differential wheeled robot simulation We show the evolution of control actions through the learning episodes in Fig. 3, representing the learning conducted over trajectory 2 for the differential wheeled robot. Towards the end of the episodes, the robot learns to use control actions on the boundary of the feasible control set (see Fig. 3a) to catch up with the desired circular trajectory and then maintain a steady state with constant forward velocity us and rotational velocity uω. The time history is shown in Fig. 3b. E. Impact of control saturation on learning We use a simple linear system to illustrate the impact of control saturation on learning. Consider a 1D doubleintegrator system used in Section VI. We command the following reference trajectory for tracking x¯(t) = [1 + t − cos(t), 1+sin(t)] <sup>⊤</sup> . We consider the boxed constraint |u| ≤ ubd for control constraints and remove the state constraints. We choose ubd = 1, 2, 4 to reflect the different magnitudes of constrained control actions. A total horizon of 10 s is considered with time intervals of 0.01 s for discretization. The planning horizon of MPC is 0.2 s (T = 20). We apply DiffTune to these three cases for 20 episodes each with the loss function set to the accumulated tracking error squared and learning rate α = 0.1. The optimal closed-loop control actions in the last episode are shown in Fig. 4a, where Specifically, to obtain the gradient ∂u <sup>⋆</sup> 1 Specifically, to obtain the gradient ∂u <sup>⋆</sup> 1 <sup>/∂[Ct]i,j</sup> <sup>,</sup> <sup>one</sup> <sup>only</sup> needs to solve (LMPC-Aux) with C˜t = Ct, c˜t = (τ1 <sup>⋆)⊤</sup> <sup>⊗</sup> <sup>In+m</sup> :,(i−1)(n+m)+j = [τt <sup>⋆]jen</sup> i <sup>+m</sup>, f˜t = 0, x�˜init = 0, G˜t - = [Gt]It,act, and l <sup>˜</sup> t = 0. Likewise, to 1 <sup>⋆)⊤</sup> <sup>⊗</sup> <sup>In+m</sup> = 0, G˜t (τ1 <sup>⋆)⊤</sup> <sup>⊗</sup> <sup>In+m</sup> :,(i−1)(n+m)+j = [τt <sup>⋆]jen</sup> i <sup>+m</sup>, f˜t = 0, x�˜init = 0, G˜t - = [Gt]It,act, and l <sup>˜</sup> t = 0. Likewise, to solve for the gradient ∂u <sup>⋆</sup> 1 <sup>/∂[ct]i,</sup> <sup>we</sup> <sup>can</sup> <sup>solve</sup> <sup>the</sup> <sup>problem</sup> (LMPC-Aux) with C <sup>˜</sup> t = Ct, c˜t = e <sup>n</sup> i <sup>+m</sup>, f <sup>˜</sup> t = 0, x˜init = 0, t <sup>⋆]jen</sup> i <sup>+m</sup> :,(i−1)(n+m)+j = [τt <sup>⋆</sup> (LMPC-Aux) with C <sup>˜</sup> t = Ct, c˜t = e <sup>n</sup> i <sup>+m</sup>, f <sup>˜</sup> t = 0, x˜init = 0, G˜t = [Gt]It,act, and l˜t = 0. To solve for ∂u <sup>⋆</sup> 1 <sup>/∂[xinit]j,</sup> <sup>one</sup> G˜t = [Gt]It,act, and l˜t = 0. To solve for ∂u <sup>⋆</sup> 1 <sup>/∂[xinit]j,</sup> <sup>one</sup> can solve (LMPC-Aux) with C <sup>˜</sup> t = Ct, c˜t = 0, f <sup>˜</sup> t = 0, x˜init = e <sup>n</sup> j <sup>,</sup> <sup>G˜t</sup> <sup>=</sup> <sup>[Gt]I</sup> t,act <sup>,</sup> <sup>and</sup> <sup>l˜t</sup> <sup>=</sup> <sup>0.</sup> <sup>These</sup> <sup>results</sup> <sup>are</sup> summarized in (LMPC-Grad) and Table I in the text. x˜init = e <sup>n</sup> j Note that the inequality constraints Gtτt ≤ lt in (LMPC) 0.5 0 -0.5 -1 -1.5 -2 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 (a) Optimal closed-loop control actions through the learning episodes. The lightest and darkest blue curves stand for episodes 1 and 20, respectively. 1 0.5 0 -0.5 -1 -1.5 -2 0 1 2 3 4 5 6 7 8 9 10 time [s] (b) Time history of the control signal in the last episode. Fig. 3: Optimal closed-loop control actions with the differential wheeled robot under trajectory 2. the saturation at the beginning lasts for a shorter period for a bigger value of ubd. Correspondingly, the tracking error has a larger reduction for larger values of ubd as shown in Fig. 4b. This observation validates our earlier analysis in Section V-A, where active constraints (saturated control) cap performance improvement by learning. Tighter constraints imply more frequent control saturation, which impedes the auto-tuning to improve tracking performance because saturation is a physical limitation that is beyond what learning is for. F. Insights from (LMPC-Grad) for general controllers subject to control constraints Handling the analytical gradient of (LMPC) with constraint Gtτt ≤ lt using [Gt]It,act ˜τt = 0 in (LMPC-Grad) is instructional to the differentiation of control policies that have control constraints (e.g., saturation) in the form of Gu ≤ l. Many controllers are designed without constraints, like PID. However, when these controllers are implemented on real-world physical systems, strict adherence to state and/or input constraints is often required due to the actuator limitations or safety considerations. Mathematically, these constraint-adhering controller can be formulated as h = cG,l ◦h0, which is a composition of the constraining operator cG,l and an arbitrary constraint-free policy h0, where 4 3 2 1 0 -1 0 1 2 3 4 5 6 7 8 9 10 (a) Closed-loop optimal control in the final episode. Dashed lines indicate the boundary ubd of control actions. 1 0.8 0.6 0.4 0 2 4 6 8 10 12 14 16 18 20 (b) RMSE reduction Fig. 4: Optimal control actions and RMSE reduction subject to box constraints with different levels of tightness ubd. Tighter constraints imply more frequent control saturation, which impedes learning from improving tracking performance. ∂h/∂θ for learning, one can apply chainrule by ∂∂hθ = ∂cG,l(u) ∂h0(x,x¯,θ) ∂u ∂θ . Since (35) can be treated as an extremely simplified (LMPC) (with T reduced to 1 and x removed from τ ), one can solve for the corresponding (LMPC-Grad) for ∂cG,l(u)/∂u. For example, for upper bound constraint u ≤ umax, if u <sup>⋆</sup> = umax then ∂cG,l(u)/∂u = 0, which further indicates ∂h/∂θ = 0. This observation extends our conclusion right before this remark in the sense that control saturation can degrade learning efficiency for general controllers with constraints due to zero gradient at the time of saturation. ∂h0(x,x¯,θ) ∂u cG,l(u) = argmin uˆ ∥u − uˆ∥ subject to Guˆ ≤ l. (35) Following the notation of control policy in (2), we have h(x, ¯x, θ) = cG,l(h0(x, ¯x, θ)) for x¯ being the reference state and θ being the parameters to be learned. To obtain G. Details of the experiment setup in RotorPy We present details on the setup of the experiments in Section VII. We present the values for the initial parameters before training, the learned parameters after training, and the hand-tuned parameters in Table IV. Regarding the handtuning of the controller, we started hand-tuning by setting all controller parameters to 1 and finally achieved the handtuned parameters displayed in Table IV after 10 trials. We originally initiated the DiffTune-MPC with parameters equal to the hand-tuned parameters. However, the tracking error attained by the auto-tuned parameters is not significantly smaller than that achieved by the hand-tuned parameters. This result implied that the hand-tuned parameters are actually close to a local minimum. Next, we set the initial parameters of DiffTune-MPC as the displayed initial parameters in Table IV by perturbing the p-weights of the handtuned parameters. With the new initial parameters, DiffTuneMPC results in a set of learned parameters with much better tracking performance than achieved by the hand-tuned parameters. TABLE IV: Diagonal elements for the initial parameters, the learned parameters, and the hand-tuned parameters applied in the RotorPy experiments. Weights p v q w u Initial [5,5,5] [0.1,0.1,0.1] [0.1,0.1,0.1,0.1] [0.05,0.05,0.05] [0.1,0.1,0.1,0.1] Learned [5.01,5.03,5.00] [0.01,0.01,0.1] [0.13,0.11,0.09,0.10] [0.01,0.01,0.05] [0.01,0.01,0.01,0.01] Hand-Tuned [10,10,10] [0.1,0.1,0.1] [0.1,0.1,0.1,0.1] [0.05,0.05,0.05] [0.1,0.1,0.1,0.1] Fig. 5: Polynomial Trajectory 1 used in Table III Fig. 6: Polynomial Trajectory 2 used in Table III 5.03 0.1 5.02 The two polynomial trajectories are plotted in Figs. 5 and 6. The waypoints for generating the first polynomial trajectory are [0,0,0], [0.5,0.4,-0.2], [0.8,1,0.5], [0.3,0,1], [-0.8,-1,2], [-1.5,-2,1], [0,0,0], [1,0.6,0.4], and the average velocity of the quadrotor is set to be 1 m/s. The waypoints for generating the second polynomial trajectory are [0,0,0], [0.4,-0.2,0.1], [0.5,-0.4,0.5], [0.7,-0.1,0.2], [1.1,0.3,0.5], [1.6,1,0.8],[0.7,1.2,1.2], and the average velocity is set to be 2 m/s. In addition to the evolution of RMSE through the learning episodes presented in the main text, we plot the evolution of each parameter during training in Fig. 7. It can be observed that the weights for velocities v and angular velocities w in both x and y directions, as well as the weights for all control inputs all decrease to the minimum value 0.01, while the rest of the parameters do not change much. 5.01 5 0.14 0.12 0.1 0.08 0.1 0.08 0.06 0.04 0.02 5 10 15 Episode 5 10 15 Episode 5 10 15 Episode 5 10 15 Episode 0.05 0 0.05 0.04 0.03 0.02 0.01 2 4 6 8 10 12 14 Episode Fig. 7: Evolution of the quadrotor parameters with DiffTuneMPC used in RotorPy.

섹션 본문 106문단 · 46,548자

(제목 없음)

1Model predictive control (MPC) has been applied to many platforms in robotics and autonomous systems for its capability to predict a system's future behavior while incorporating constraints that a system may have.To enhance the performance of a system with an MPC controller, one can manually tune the MPC's cost function.However, it can be challenging due to the possibly high dimension of the parameter space as well as the potential difference between the open-loop cost function in MPC and the overall closed-loop performance metric function.This paper presents DiffTune-MPC, a novel learning method, to learn the cost function of an MPC in a closed-loop manner.The proposed framework is compatible with the scenario where the time interval for performance evaluation and MPC's planning horizon have different lengths.We show the auxiliary problem whose solution admits the analytical gradients of MPC and discuss its variations in different MPC settings, including nonlinear MPCs that are solved using sequential quadratic programming.Simulation results demonstrate the learning capability of DiffTune-MPC and the generalization capability of the learned MPC parameters.

I. INTRODUCTION

2With the advancement of computational power in embedded systems and the development of general-purpose solvers (e.g., acados, ACADO, CasADi), Model Predictive Control (MPC) is being applied to more and more platforms in robotics and autonomous systems.Effective implementation of MPC requires a well-designed cost function, a dynamical model with appropriate fidelity, realistic constraints accounting for the physical limits of the system, and sufficient computation power for real-time solutions of the MPC.

3When an MPC is applied for different tasks, one may need to design the cost function for each task to suit its individual characteristics (e.g., trajectory tracking tasks for hover or aggressive maneuvers for quadrotors).A common approach is to tune the parameters in a cost function with a given structure, e.g., with weight matrices Q and R following the convention in a linear quadratic regulator (LQR).The challenges in manually tuning the Q and R matrices come from the following two aspects:

41. Dimension: In the simplest case where Q and R are reduced to diagonal matrices, the parameter space's dimension equals the sum of the state's and control's dimensions, making manual tuning a challenging task, especially when the state is high-dimensional (≥ 12 for 3D rigid-body dynamics).2. Difference between the cost function for MPC and loss function for MPC's performance: The cost function in MPC reflects the predictive cost in an open-loop fashion: at each sample time, an optimal control problem is solved for a sequence of control actions into a future horizon, where all -except for the first control actions -are open-loop and not applied for controlling the system.On the contrary, the loss function is for evaluating the performance of the MPC as a function of the closed-loop states (and control actions), e.g., tracking error between the desired trajectory and the closed-loop trajectory.In practice, the relationship between the cost function parameters and the performance judged by the loss function is complex and nonlinear.Furthermore, the loss function can be defined over a long horizon specified by an arbitrary task, whereas the cost in MPC is defined over a shorter horizon to enable real-time solutions.

5To address the challenges in the manual tuning of MPC's cost function, we propose to learn the cost function's parameters for a given task using DiffTune [1], an auto-tuning method for closed-loop control systems.DiffTune formulates the auto-tuning problem as a parameter optimization problem that can handle different horizons in the loss function (for performance) and cost function (for control).It has been previously demonstrated on explicitly differentiable controllers whose Jacobian can be conveniently obtained by autodifferentiation.In this work, we extend DiffTune to implicitly differentiable MPC controllers.The analytical gradients of the MPC policy are obtained by the implicit differentiation proposed in [2], which is based on the implicit function theorem and Karush-Kuhn-Tucker (KKT) conditions.We first show how to differentiate a linear MPC problem (with quadratic cost, linear dynamics, and linear inequality constraints) by solving an auxiliary linear MPC.Then, we extend it to a nonlinear MPC and show how to differentiate it utilizing the auxiliary MPC.We validate our approach in simulations, where we show the efficacy of DiffTune for MPC subject to both linear and nonlinear dynamical systems.We also validated the generalization capability and applicability to real-world scenarios in a highfidelity simulator.

6We summarize our contributions as follows:

7• We extend the application of DiffTune [1] from autotuning explicitly differentiable controllers to MPC that is implicitly differentiable.

8• We propose a novel and flexible closed-loop MPC learning scheme using differentiable programming for the general scenario where the horizon for closed-loop performance evaluation is longer than an MPC's openloop planning horizon.This is a significant improvement compared to existing open-loop MPC learning schemes [2], [3] that apply differentiable programming, where the horizon of the loss function cannot be longer than the MPC's horizon because the loss function therein is defined with respect to the open-loop optimal solution.• Our differentiation of the MPC is based on a general formulation of a linear MPC that contains linear-inequality constraints of state and control, which covers a broader range of applications than the boxed-control-constraints only formulation in [2].The remainder of the paper is organized as follows: Sections II and III review related work and background, respectively.Section IV describes the problem formulation, and section V describes our method for differentiating MPC based on a general linear MPC formulation.Section VI shows the simulation results on a unicycle model with linear inequality constraints, a quadrotor model, and a 1D double integrator model.Section VII presents the results on a quadrotor model using a high-fidelity simulator to show the generalization capability.Section VIII concludes the paper.

II. RELATED WORK

9Auto-tuning, in general, can be formulated as a parameter optimization problem that looks for an optimal parameter to minimize a loss function subject to a given controller.It can be considered as a special case of learning for control, where the structure of the controller comes from physics (e.g., by Lyapunov or conventional design) rather than a generic neural network.If the system model is available, one can leverage the model information to infer optimal parameter choice, e.g., using gradient descent [1], [4]- [6].If the model is unavailable, then one needs to query the system's performance from a chosen set of parameters to determine the next choice of the parameter.For example, the authors of [7], [8] use Bayesian optimization to search for an optimal parameter by treating the mapping from parameter to loss as a Gaussian process.The parameters are iteratively chosen as the minimizer with the maximum likelihood.In [9], a policy search method is proposed to select highlevel decision variables or parameters for MPC automatically.The authors of [10] use the Metropolis-Hastings sampling to generate random parameters and selectively keep one parameter at each trial to continue the sampling procedure.A data-driven approach is proposed in [11], which consists of a system identification module, an objective specification module, and a feedback control synthesis module, where the controller is optimized end-to-end using Bayesian optimization.This method is applied to control a challenging soft robot platform [12].

10Learning for MPC has been a trending topic in recent years owing to the hardware and software advancement in deploying and training MPC-related algorithms.The concept of differentiable MPC is initially discussed in [2] and later extended to general optimal control problems [3], [13] from the perspective of Pontryagin's maximum principle and robust MPC [14].The structural similarity between optimal control and reinforcement learning (RL) has made differentiable MPC a bridge to connect the two worlds [15].In [16], a receding-horizon implementation of analytical policy gradient is proposed with a curriculum learning scheme to improve training stability for offline training of a controller.The authors of [17] propose to use the actor-critic method to learn the parameters of an MPC policy, which combines the long-term learning capability of the actor-critic RL and the short-term control capability by MPC.One particular direction for learning-based MPC uses learning to enhance the model's quality for better prediction performance (and thus control performance).Various approaches [18]- [22] have been proposed to learn the residual dynamics and show promising performance for quadrotor control.

11In contrast to previous works [2], [13] that apply differentiable programming to parameter learning of optimal control (OC) problems in an open-loop fashion, our approach tackles parameter learning of an MPC problem in a closed-loop fashion. 1 In an open-loop setting, [2], [13] formulate the loss for training as a function of the optimal states and control actions that are obtained from solving the OC problem once.This design restricts the loss function's horizon such that it cannot be longer than the MPC's horizon, limiting suitable applications (e.g., imitation learning) because the loss's horizon for an arbitrary task is usually much longer than the MPC's planning horizon.In a closed-loop setting, our loss function is defined over a closed-loop system's state and control actions, which allows for a broad set of learning applications where the loss's horizon is way longer than the MPC's horizon (which cannot be handled by openloop learning [2], [13]).The closed-loop learning of an MPC has been explored using probabilistic approaches like policy search [9] and Bayesian optimization [11].These approaches do not employ first-order information of the MPC and, therefore, are less efficient than those that do (e.g., this work).

12In addition to the different problem formulation and application scenarios discussed above, our approach has novelties in terms of differentiating the OC problem.The differentiable OC presented in previous work [2] is based on an LQR with boxed constraints for control actions only.We extend it to a more general case of LQR with linear inequality constraints, which are capable of handling state constraints for safety requirements and likely coupled control(-state) constraints.Towards the incorporation of constraints, our OC problem's formulation is similar to [3] albeit with different solution and differentiation techniques.The results in [3] augment the constraints (excluding the dynamics) to the cost function using barrier functions and thus resort to Pontryagin's differentiable programming [13] to solve the OC as well as find the analytical gradients.In this procedure, one needs to manually select the barriers' multipliers for a tradeoff between constraint tightness and cost minimization.In addition, augmenting the constraints to soft constraints makes the resulting solution only an approximate one for the original problem.In contrast, our method is based on the KKT condition of an MPC to satisfy the hard constraints.The KKT-based formulation is also general enough to use off-the-shelf MPC solvers (e.g., acados) to obtain both the MPC solution and the analytical gradient.

III. BACKGROUND

13The following notation will be used throughout the paper: For a matrix A, we use A to denote its vectorized form (by column).We use [v] j to denote the j-th element of vector v. Similarly, [A] i,: , [A] :,j and [A] i,j denote the i-th row, j-th column, and element on i-th row and j-th column of the matrix A, respectively.The matrix I n denotes an ndimensional identity matrix.The vector 1 n denotes an ndimensional vector with all elements being 1.The vector e n i denotes an n-dimensional unit vector with the i-th element being 1.

14We briefly review controller auto-tuning and DiffTune in the sequel.Consider a discrete-time dynamical system

15where x k ∈ R n and u k ∈ R m are the state and control, respectively, and the initial state x 0 is known.The control is generated by a feedback controller that tracks a desired state xk ∈ R n such that

16where θ ∈ Θ denotes the controller's parameters, and Θ represents a feasible set of parameters that can be analytically or empirically determined for a system's stability.We assume that the state x k can be measured directly or, if not, an appropriate state estimator is used.Furthermore, we assume that the dynamics (1) and controller (2) are differentiable, i.e., the Jacobians ∇ x f , ∇ u f , ∇ x h, and ∇ θ h exist, which widely applies to general systems.The controller learning/auto-tuning task adjusts θ to minimize an evaluation criterion, denoted by a loss function L(•), which is a function of the desired states, actual states, and control actions over a time interval of length N .An illustrative example is the quadratic loss of the tracking error and control-effort penalty, where L(

17with λ > 0 being the penalty coefficient.We will use the short-hand notation L(θ) for conciseness in the rest of the paper.Note that the usage of DiffTune is not limited to the above example of a tracking problem.As long as a loss function can be specified and is differentiable, we are able to apply DiffTune to learn the parameters and minimize the loss.

18To summarize, we formulate the controller learning/autotuning problem as the following parameter optimization problem minimize θ∈Θ L(θ)

19We solve the above problem by DiffTune, which uses a projected gradient descent algorithm [23] to update the parameters iteratively:

20To obtain the gradient ∇ θ L, we start with the chain rule:

21The gradients ∂L/∂x k and ∂L/∂u k can be determined once L is chosen, and we use sensitivity propagation to obtain the sensitivity states ∂x k /∂θ and ∂u k /∂θ by:

22with ∂x 0 /∂θ = 0.

23Here, the notations ∇ x k f , ∇ u k f , ∇ x k h, and ∇ θ h mean the Jacobians ∂f /∂x, ∂f /∂u, ∂h/∂x, and ∂h/∂θ is evaluated at the state x k and control u k .We allow the state x k to come from sensor measurements or state estimation, which permits data collected from a real system or simulated noisy system (beyond the data collected by rolling out the dynamics (1)).

IV. PROBLEM FORMULATION

24Model predictive control determines a sequence of control actions at every step that minimizes a cost criterion subject to system dynamics and other constraints on the states and control inputs.The problem is repeatedly solved when new state measurement becomes available, and the first optimal control action is implemented for the closed-loop control.In general, an MPC problem has the following form:

25where X t and U t stand for feasible sets of states and control actions at time t, and X T denotes the terminal set of states.The simplest case of an MPC would be an LQR, i.e., Remark 1: Note that we distinguish the time indices used in DiffTune (k and N for closed-loop systems) and in MPC (t and T for open-loop MPC solution).For the closed-loop system, at time k, the instantaneous state x k is set to x init in (MPC).Once the (MPC) is solved, its initial optimal control action u ⋆ 1 is set as u k for the closed-loop system.Furthermore, the horizon N of the performance evaluation in DiffTune is generally much larger than the horizon T in (MPC).

26The goal of this paper is to apply DiffTune to solve the problem (P) when the controller h(•) is an MPC.More specifically, we want to learn the cost functions of the MPC controller to minimize the loss function defined over the entire closed-loop trajectory.Since MPC is an implicitly differentiable controller, the Jacobians ∂h/∂θ and ∂h/∂x k cannot be calculated directly like a simple feedback control law u = Kx.Thus, we will present our method to efficiently compute the target Jacobians in the following section.

V. THE DIFFTUNE-MPC METHOD

27We learn an MPC controller following a given loss function by auto-tuning the parameter θ in the cost function (e.g., Q and R for quadratic cost) using DiffTune.Following the sensitivity propagation in (5), at each time k, we need to know the Jacobians ∂h/∂θ and ∂h/∂x k , which are the sensitivity of the controller to the parameter θ and instantaneous state x k .In the setting of using MPC as a controller h at step k, we have ∂h/∂θ = ∂u ⋆ 1 /∂θ and ∂h/∂x k = ∂u ⋆ 1 /∂x init , where u ⋆ 1 is the first optimal control action obtained by solving problem (MPC) with the initial state x init = x k .We will leverage the KKT conditions and implicit differentiation to obtain the gradients as illustrated in [2].We start by differentiating a linear MPC problem and discuss the differentiation of a nonlinear MPC at the end.Considering the page limit, we only present the main results in this section.The complete derivation and analysis can be found in the Appendix.

A. Differentiating a Linear MPC

28By linear MPC, we refer to the following problem minimize

29where

30for residuals in the dynamics; and G t ∈ R g×(n+m) and l t ∈ R g stand for g number of inequalities on the state and control actions.Although a more general form of inequality constraints for τ t of all t ∈ {1, 2, . . ., T } can be incorporated, we stick to the form displayed in (LMPC) where the linear inequality constraints are separable by t.The commonly used boxed constraint on the control action is a special case of the form used in G t τ t ≤ h t , where

31] define the bounds of each element of the control to be bounded by u max and u min , respectively.Problem (LMPC) is a quadratic program (QP) and can be solved efficiently with off-the-shelf solvers.Once we get the optimal solution {τ ⋆ t } t=1:T , we can proceed to solve the following auxiliary problem for the gradients ∂u

32Denote the first optimal control action of (LMPC-Grad) by ũ⋆ 1 .The analytical gradients for (LMPC) can be obtained by computing ũ⋆ 1 from (LMPC-Grad) with the coefficients ct and xinit listed in Table I.For details in the derivation of (LMPC-Grad) for computing the analytical gradient, see Appendix C. Once we obtain the target gradients, we can leverage ( 4) and ( 5) to obtain ∇ θ L and update the parameter using (3).

33Remark 2: The high-level idea of obtaining the analytical gradients of (LMPC) by solving (LMPC-Grad) is summarized as follows: Problem (LMPC) is solved by treating it as a QP, where the optimal solution satisfies a linear equation (33) following the KKT condition (including the dynamics, costate dynamics, boundary conditions, and constraints).One can take matrix differentials [24] of this linear equation, which governs the relation among the infinitesimal changes of the decision variables (state, control, Lagrange multipliers) and parameters (such as cost matrix C t ).Meanwhile, the resulting equation of matrix differentials (34) leads to the governing linear equation of the gradients of (LMPC), which coincides with the KKT condition of the auxiliary problem (LMPC-Grad).Hence, one can match the terms and coefficients in the original KKT condition with those in the one derived from the matrix differentials, which allows us to obtain the target gradients listed in the first column of Table I by solving (LMPC-Grad) with associated terms listed in the last two columns of Table I.TABLE I: To get target gradients of (LMPC) with (Ct, ct)parametrization in the left column, one can solve (LMPC-Grad) with coefficients ct and xinit specified in the right column.The solution ũ⋆

341 of (LMPC-Grad) equals the gradient in the left column.Note that the values in the column of ct are specifically for time step t from the target parameters Ct and ct, and for all other time steps t ′ = t, we have ct ′ = 0.

Target gradients in Coefficients in

35Remark 3: The analytical gradient obtained by solving (LMPC-Grad) is computationally cheaper than a numerical gradient obtained by finite difference.Since (LMPC-Grad) is a QP with equality constraints only (solving such a problem reduces to solving a linear system of equations [25]), (LMPC-Grad) takes less time to solve than the original problem (LMPC) which contains inequality constraints.If one wants to get the analytical gradient of u ⋆ 1 to M scalar parameters in (LMPC), then one needs to solve (LMPC-Grad) M times.However, if one wants to get the numerical gradient, then one has to solve at least M times (LMPC) with single-direction perturbations for M parameters and perform finite difference.Besides, the quality of the numerical gradient depends on the size of the perturbation, determining which can be an individual problem of study.

36In the problem setting of DiffTune, when differentiating (LMPC) through solving (LMPC-Grad), we only need τ1 (more specifically ũ1 ).Hence, if u ⋆ 1 is active at some constraints (i.e., I 1,act = ∅), then the gradient of (LMPC) ∂u ⋆ 1 /∂θ (for θ being an arbitrary scalar element of C t or c t ) satisfies

37Consider a simple example of a scalar control u 1 to be upper bounded by u max , where G t = [0 1] and l t = u max for all t.If u ⋆ 1 = u max , then ∂u ⋆ 1 /∂θ = 0, indicating the inefficacy of manipulating u ⋆ 1 by tweaking θ since u ⋆ 1 will remain u max when θ is perturbed locally.A detailed discussion on active constraints and corresponding gradients is available in Appendix F. Simulation results showing the degraded efficiency in learning when control saturation happens are included in Appendix E.

B. Linear MPC with interpretable quadratic cost function

38Although the (C t , c t )-parametrization is general enough, its physical meaning is not as clear as the conventional

39, where xt and ūt are the reference state and control to be tracked.In this case, the parameters to be learned are Q t ∈ R n×n and R t ∈ R m×m , and this cost function associates with the one in (LMPC) by

40, then one can simply plug in ct into problem (LMPC-Grad) as indicated in Table II and solve (LMPC-Grad) for the target gradients.

C. Nonlinear MPC

41The linear MPC problem discussed above does not cover practical control problems in reality, where one can have general nonlinear dynamics, costs, and constraints.For such nonlinear MPC problems, we consider the following formu-lation:

42where the nonlinear cost function C θ,t is parameterized by θ, f is the nonlinear dynamics, and g is the nonlinear inequality constraint function.

43One common way to solve this nonlinear optimization problem is to treat it as nonlinear programming and apply sequential quadratic programming (SQP) [25], which iteratively generates and optimizes the following approximate problem:

44where the nonlinear cost function and constraints are Taylorexpanded at the p-th iteration's optimal solution {τ p t } t=1:T to the second and first order, respectively.All the differentiation denoted by ∇ is with respect to τ t , with superscript 2 indicating the Hessian.The superscript p indicates that the function is evaluated at the optimal solution of (L-NMPC) obtained in the p-th iteration.The solution to the displayed (L-NMPC), denoted by {τ p+1 t } t=1:T , is used in the Taylor expansion to form the (p + 1)-th iteration of SQP.

45Problem (L-NMPC) has the identical form to that of (LMPC).When the SQP terminates at the N -th iteration, we take {τ N t } t=1:T as the optimal solution to (NMPC) and use (LMPC-Grad) to compute the gradients.Leveraging the chain rule, we can get the target gradients ∂u ⋆ 1 /∂θ and ∂u ⋆ 1 /∂x init for DiffTune.Remark 4: For the cost function of (NMPC), the quadratic cost with (Q t , R t )-parametrization in Section V-B is often used for an interpretable cost function.In this case,

46TABLE II: To get target gradients of (LMPC) with (Qt, Rt)parametrization in the left column, one can solve (LMPC-Grad) with coefficients ct and xinit specified in the right column.The solution ũ⋆

471 of (LMPC-Grad) equals the gradient of (LMPC) in the left column.

VI. SIMULATION RESULTS

48In this section, we implement DiffTune to learn MPCs on different dynamical systems and different trajectories.We compare its learning performance with a baseline learning method [9] that uses policy search (PS-MPC) to determine the parameters for MPC.We implement [9, Alg.1] with 10 samples of the MPC parameters in each episode.The samples follow a Gaussian distribution with the initial mean as the initial MPC parameters and the covariance matrix as 0.09I.We use β = 5 for the inverse temperature of the soft-max distribution.

49We use the following three dynamical systems: a 1D double-integrator system with box constraints for control actions and states, a differential wheeled robot with linear inequality constraints on the control actions, and a quadrotor with nonlinear dynamics.We use the interpretable quadratic cost function, i.e., (Q, R)-parametrization2 , within the MPC formulation in all simulation.The loss function L is set to be the accumulated tracking error squared of the closed-loop system to the reference trajectory unless specified otherwise.The (Q, R) matrices are initialized as identity matrices of appropriate dimensions.We only learn the diagonal elements of Q and R matrices and restrict them to be bounded within [0.01,1000].We solve the main MPC problem using acados [26].For solving (LMPC-Grad), we use acados if the original MPC problem has linear (time-invariant) dynamics and use MATLAB's quadprog otherwise (because acados does not support linear time-varying system of the form in (L-NMPC)).Both DiffTune and PS-MPC are set to use 20 episodes for the learning, which is sufficient for convergence with both methods (shown in Fig. 1).Double-integrator system: Consider the following double integrator with friction

50We consider the boxed constraint |u| ≤ 0.9 for control constraint and speed limit |v| ≤ 0.

51where the system state x = [p x , p y , φ] ⊤ includes the horizontal position p x , vertical position p y , and heading angle φ, and the control action u = [u s , u ω ] ⊤ includes the speed u s and angular velocity u ω .The differential wheels (radius r = 0.1 m) are placed with distance d = 0.5 m in between.The maximum angular velocity for each wheel is set to ω m = 2π rad/s.Considering the geometry and the angular velocity limit of each wheel above, the following linear inequalities on the control action u apply:

52We use the following two reference trajectories for tracking:

53The NMPC problem has a planning horizon of 0.5 s with 0.05 s sample time (T = 10).A total horizon of 10 s (N = 200) is considered for the closed-loop system.We set the learning rate to α = 0.01 for DiffTune.

54Quadrotor: Consider the following nonlinear dynamics:

55where the state

56denote the position and velocity of the quadrotor, both represented in the inertial frame, q ∈ S 3 is the quaternion representing the rotation from the inertial frame to the body frame (with ⊗ denoting the quaternion product), and ω ∈ R 3 represents the angular velocity in body frame.The gravitational acceleration is denoted by g = [0 0 − g] ⊤ (under the ENU coordinates).The vector z B is the unit vector aligning with the z-axis of the body frame, represented in the inertial frame.The control u contains four individual rotor thrusts and thus determines the total thrust f T and moment M ∈ R 3 .The quadrotor has parameters m = 0.03 kg and J = 10 −5 diag(1.43,1.43, 2.89) kgm 2 .We command two reference trajectories: one is a 3D Lissajous trajectory, and the other one is a polynomial trajectory generated using minimum-snap optimization [27].For both trajectories, the NMPC problem has a planning horizon of 0.5 s with 0.05 s sample time (T = 10).A total horizon of 10 s (N = 200) is considered for the closed-loop system.We set the learning rate to α = 0.08 for DiffTune.The results are shown in Fig. 1, where we display the root-mean-squared error as an indicator of the systems' performance through the learning episodes.Overall, DiffTune-MPC results in a better performance in fewer episodes than the PS-MPC, owing to the former's usage of the first-order information of the loss function.On the contrary, PS-MPC requires sampled parameters and queries to the loss function to update its probabilistic model, where a relatively large variation of RMSE happens in the first few episodes in all test cases.For the linear system, DiffTune-MPC takes one episode to reduce the RMSE too close to the minimum (the parameters are updated with small changes in the following episodes, resulting in a minor reduction in the RMSE towards the end).

VII. EXPERIMENTAL RESULTS WITH A HIGH-FIDELITY QUADROTOR SIMULATOR

57To show the applicability of the proposed DiffTune-MPC framework to real-world scenarios and its general-ization capability (not overfitting to any particular run or condition), we conduct experiments using the high-fidelity quadrotor simulator RotorPy [28], which provides realistic aerodynamic effects, motor dynamics, and sensor noise similar to what a quadrotor experiences in physical experiments.Using RotorPy, we apply DiffTune for batch learning an NMPC for a trajectory tracking task subject to trajectories with different speeds.In particular, the task is to track 3D Lissajous reference trajectories: p(t) = [γ sin(2πt/T ), γ sin(4πt/T ), γ sin(πt/T )] ⊤ for t ∈ [0, T ] with radius γ and total horizon T .We use a total of 11 such trajectories that share the same length (fixed γ) and shape but with various speeds.We manage different speeds by setting the total horizons T for the 11 trajectories to be evenly distributed from 5 to 15 s.In other words, each reference trajectory is designed to finish one round within the designated horizon to achieve different desired speeds.In each training episode, we control the quadrotor with an NMPC to run these reference trajectories, compute the gradient ∇ θ L for each run, and update the parameters (Q, R) using the averaged gradient from the 11 runs.The initial (Q, R) for this section can be found in Appendix G.In RotorPy, we set the simulation rate to 200 Hz and use the default vehicle and noise settings with a hummingbird profile.The NMPC problem has a planning horizon of 0.5 s with a sampling time of 0.05 s.We apply DiffTune for 15 episodes with a learning rate set to 0.01.

58The training results are shown in Fig. 2. It can be observed that the mean RMSE over the 11 reference trajectories decreases from 0.113 m to a final value of 0.082 m, indicating the learning capability of DiffTune in a batchsetting over multiple trajectories.We show the comparison of performance using learned and initial parameters in the same figure, where the improved tracking quality is clear.We evaluate the learned parameters over test trajectories that are not present in the training set to understand the generalization capability of the learned parameters.We also include the comparison to baseline hand-tuned parameters (these parameters can be found in Appendix G).The results are shown in Table III.The testing trajectories include 3D Lissajous trajectories with total horizons T outside the training set of [5,15], 3D Lissajous trajectories with different radius γ, 3D circle trajectories with different speeds, and two polynomial trajectories generated using minimum-snap optimization [27].For the two polynomial trajectories, their average speeds are set to 1 and 2 m/s.The waypoints used to define these two trajectories can be found in Appendix G.

59Overall, the results show that the learned parameters by DiffTune-MPC outperform the hand-tuned parameters in all tested trajectories.Over the 3D Lissajous trajectories with the same radius (γ = 1), it can be observed that RMSE decreases when the total horizon increases.This tendency is expected because a larger value of the total horizon indicates a lower speed, and thus, the quadrotor can track the desired trajectory better.The same tendency can be consistently observed with 3D circle trajectories, where the RMSE decreases when the radius turns bigger (subject to a fixed total horizon), III, we have validated the proposed framework's generalization capability for not overfitting to a particular task or condition and its applicability subject to realistic conditions with sensor noise and unknown factors (aerodynamics and rotor dynamics).

VIII. CONCLUSION

60In this paper, we develop a closed-loop learning scheme for MPC using DiffTune.We learn the parameters in the cost function to improve the closed-loop system's performance specified by a loss function that is different than the MPC's cost function.To use DiffTune, we need the gradient of the first optimal control action with respect to the initial state and parameters of the cost function in the MPC problem.Our derivation is centered on a linear MPC problem with linear inequality constraints (to incorporate safety requirements of physical limitations of the system).We show that the linear-MPC-based differentiation can be applied to a nonlinear MPC that is solved via SQP.We validate our approach in simulation for systems with linear and nonlinear dynamics subject to constraints on the state and control.Our approach shows more efficient learning compared with a baseline policy-search-based learning scheme due to our effective usage of first-order information.We also demonstrate the generalization capability of the proposed scheme to different trajectories and its applicability to realworld scenarios in a quadrotor high-fidelity simulator.Future work will investigate the usage of DiffTune for different evaluation criteria (e.g., robustness) and the application of DiffTune-MPC on challenging robotic tasks, e.g., designing cost functions for trajectory tracking with hybrid systems in contact-rich scenarios.

APPENDIX

61We show how the auxiliary problem (LMPC-Grad) is derived using the KKT condition in the Appendix.We start with an LQR and then cover the case with a general linear MPC (an LQR with linear inequality constraints).

A. Differentiation of an LQR

62We start the illustration of obtaining the gradient of an LQR's first optimal control action to the parameters of interest (cost coefficients and initial state).Consider the following LQR problem, where we stick to the same notation as in [2]:

63where n+m) , and f t ∈ R n stands for residuals in the dynamics.With the Lagrangian

64where λ t ∈ R n is the vector of the Lagrangian multipliers associated with the dynamics constraints, and F 0 = 0 and f 0 = x init in the Lagrangian above.The solution to the LQR problem is obtained at the fixed point of the Lagrangian, i.e., when

65Following this relation, we can derive the costate dynamics

66for t ∈ {0, 1, . . ., T − 2}.The notation C •,x , F •,x , and c •,x stands for the first n columns of C • and F • , and first n rows of c • , respectively.The original dynamics, costate dynamics, and the initial state allow for writing the KKT factorization in (17), where K is the KKT factorization matrix.

67To obtain the derivatives ∂u ⋆ 1 /∂C t and ∂u ⋆ 1 /∂c t , we will take a look at the matrix differential of original dynamics, costate dynamics, and boundary conditions:

68The KKT factorization for ( 18) can be presented as follows:

69We start with the Jacobian ∂u ⋆ 1 /∂x init because it is simpler than the other two Jacobians.We set the differentials dC t , dc t , dF t , and df t to zero for t ∈ {1, 2, . . ., T } in ( 19) and we have

70where the target Jacobian ∂u ⋆ 1 /∂x init is contained within ∂τ ⋆ 1 /∂x init such that

71If we compare the form and match the terms of (20) to the KKT factorization of the original problem in (17), then we can obtain ∂u ⋆ 1 /∂x init by solving the following auxiliary LQR problem minimize

72The gradient ∂u ⋆ 1 /∂[x init ] j can be solved from (LQR-Aux) following the observations below.By (20), we have

73Matching ( 22) with ( 17), ∂u ⋆ 1 /∂[x init ] j can be solved via (LQR-Aux) with Ct = C t , ct = 0, Ft = F t , ft = 0, and xinit = [I n ] :,j = e n j .Similarly, for the Jacobian ∂u ⋆ 1 /∂C t , we start with

74using the vectorization (with Kronecker product) formula ABC = (C ⊤ ⊗ A)B.This equation will lead to the factorization

75Matching ( 24) with ( 17), given any specific t, the derivative ∂u ⋆ 1 /∂[C t ] i,j can be solved from (LQR-Aux) with Ct = C t , Ft = F t , ft = 0 for all t, xinit = 0, and ct = [(τ ⋆ t ) ⊤ ⊗I n+m ] :,(i−1)(n+m)+j = [τ ⋆ t ] j e n+m i for this specific t and ct ′ = 0 for all t ′ = t.Likewise, if one wants to get ∂u ⋆ 1 /∂[c t ] i , then it suffices to solve (LQR-Aux) with ct = e n+m i and ct ′ = 0 for all t ′ = t.

B. Differentiation of an LQR with (Q, R)-parametrization

76In this case, we use the (Q, R)-parameterization with coefficients of the Q t ∈ R n×n and R t ∈ R m×m matrices in the cost function, which are composited as C t = diag(Q t , R t ) following the notation in (P1).Note that c t also contains the coefficients of Q t and R t such that c t = −[

77⊤ dQ t (the same conclusion applies to a symmetric matrix R t ).Hence, we have

78In other words, the derivative ∂u ⋆ 1 /∂[Q t ] i,j can be solved from (LQR-Aux) with Ct = C t , Ft = F t , ft = 0, xinit = 0 for all t, and ct = (

79for this specific t and ct ′ = 0 for all t ′ = t.

80One can apply similar derivations to obtain ∂u ⋆ 1 /∂[R t ] i,j from (LQR-Aux) with xinit = 0 for all t, and ct = 0

81and ct ′ = 0 for all t ′ = t.

C. Differentiation of an LQR with linear inequality constraints

82A slightly enhanced version of LQR includes path constraints on the state and control, e.g., with linear inequalities, which results in the formulation of (LMPC).Following the derivation in Appendix A, we can write the Lagrangian for (LMPC) as [29], we know the following condition holds at the optimality point:

83The costate dynamics for (LMPC) are

84We can write down the KKT factorization for (LMPC) as shown in (33), with the KKT factorization matrix denoted by K LI .

85For the differentials associated with (LMPC), we have

86One observation follows from (34): when considering the Jacobians with respect to C t and c t , then only non-zero differentials on the right-hand side of (34) will be dC t and dc t , which resembles the structure of the right-hand side of (19).This observation indicates that the gradients ∂u ⋆ 1 /∂C t and ∂u ⋆ 1 /∂c t can be obtained following the approach shown before with LQR in Appendix A.

87Following the derivation in Appdendix A for (P1), one can solve the following auxiliary problem to obtain the gradient for (LMPC): Note that the inequality constraints G t τ t ≤ l t in (LMPC) are turned into an equality constraint Gt τt = lt in (LMPC-Aux), where the latter equality constraint only holds for those time indices t with the constraint being active at the optimal solution τ ⋆ t , i.e., Gt = [G t ] It,act .The inactive constraints (at the optimal solution of (LMPC)) are not considered in (LMPC-Aux) because the optimal solution of (LMPC) is "locally unconstrained" at the inactive constraints, i.e., removing the inactive constraints will not change the optimal solution.Therefore, the inactive constraints [G t ] It,ina are not inherited from (LMPC) to (LMPC-Grad), and only active constraints show up in (LMPC-Grad) in the form of Gt τt = 0 for Gt = [G t ] It,act .

D. Differential wheeled robot simulation

88We show the evolution of control actions through the learning episodes in Fig. 3, representing the learning conducted over trajectory 2 for the differential wheeled robot.Towards the end of the episodes, the robot learns to use control actions on the boundary of the feasible control set (see Fig. 3a) to catch up with the desired circular trajectory and then maintain a steady state with constant forward velocity u s and rotational velocity u ω .The time history is shown in Fig. 3b.

E. Impact of control saturation on learning

89We use a simple linear system to illustrate the impact of control saturation on learning.Consider a 1D doubleintegrator system used in Section VI.We command the following reference trajectory for tracking x(t) = [1 + t − cos(t), 1 + sin(t)] ⊤ .We consider the boxed constraint |u| ≤ u bd for control constraints and remove the state constraints.We choose u bd = 1, 2, 4 to reflect the different magnitudes of constrained control actions.A total horizon of 10 s is considered with time intervals of 0.01 s for discretization.The planning horizon of MPC is 0.2 s (T = 20).We apply DiffTune to these three cases for 20 episodes each with the loss function set to the accumulated tracking error squared and learning rate α = 0.1.The optimal closed-loop control actions in the last episode are shown in Fig. 4a, where the saturation at the beginning lasts for a shorter period for a bigger value of u bd .Correspondingly, the tracking error has a larger reduction for larger values of u bd as shown in Fig. 4b.This observation validates our earlier analysis in Section V-A, where active constraints (saturated control) cap performance improvement by learning.Tighter constraints imply more frequent control saturation, which impedes the auto-tuning to improve tracking performance because saturation is a physical limitation that is beyond what learning is for.

F. Insights from (LMPC-Grad) for general controllers subject to control constraints

90Handling the analytical gradient of (LMPC) with constraint G t τ t ≤ l t using [G t ] It,act τt = 0 in (LMPC-Grad) is instructional to the differentiation of control policies that have control constraints (e.g., saturation) in the form of Gu ≤ l.Many controllers are designed without constraints, like PID.However, when these controllers are implemented on real-world physical systems, strict adherence to state and/or input constraints is often required due to the actuator limitations or safety considerations.Mathematically, these constraint-adhering controller can be formulated as h = c G,l •h 0 , which is a composition of the constraining operator c G,l and an arbitrary constraint-free policy h 0 , where c G,l (u) = argmin (35)

91Following the notation of control policy in (2), we have h(x, x, θ) = c G,l (h 0 (x, x, θ)) for x being the reference state and θ being the parameters to be learned.To obtain .Since (35) can be treated as an extremely simplified (LMPC) (with T reduced to 1 and x removed from τ ), one can solve for the corresponding (LMPC-Grad) for ∂c G,l (u)/∂u.For example, for upper bound constraint u ≤ u max , if u ⋆ = u max then ∂c G,l (u)/∂u = 0, which further indicates ∂h/∂θ = 0.This observation extends our conclusion right before this remark in the sense that control saturation can degrade learning efficiency for general controllers with constraints due to zero gradient at the time of saturation.

G. Details of the experiment setup in RotorPy

92We present details on the setup of the experiments in Section VII.We present the values for the initial parameters before training, the learned parameters after training, and the hand-tuned parameters in Table IV.Regarding the handtuning of the controller, we started hand-tuning by setting all controller parameters to 1 and finally achieved the handtuned parameters displayed in Table IV after 10 trials.We originally initiated the DiffTune-MPC with parameters equal to the hand-tuned parameters.However, the tracking error attained by the auto-tuned parameters is not significantly smaller than that achieved by the hand-tuned parameters.This result implied that the hand-tuned parameters are actually close to a local minimum.Next, we set the initial parameters of DiffTune-MPC as the displayed initial parameters in Table IV by perturbing the p-weights of the handtuned parameters.With the new initial parameters, DiffTune-MPC results in a set of learned parameters with much better tracking performance than achieved by the hand-tuned parameters., and the average velocity is set to be 2 m/s.In addition to the evolution of RMSE through the learning episodes presented in the main text, we plot the evolution of each parameter during training in Fig. 7.It can be observed that the weights for velocities v and angular velocities w in both x and y directions, as well as the weights for all control inputs all decrease to the minimum value 0.01, while the rest of the parameters do not change much.

2 Fig. 1 :

93Fig. 1: Simulation results comparing the learning progress of DiffTune-MPC (ours) and the baseline PS-MPC [9].The shaded areas show the range of RMSEs (min to max) achieved with a total of 10 sampled parameters by PS-MPC.

Fig. 2 :

94Fig. 2: Left: Evolution of the tracking performance with DiffTune-MPC in RotorPy over a total of 11 3D Lissajous training trajectories.The curve shows the mean RMSE, whereas the shaded area shows the standard deviation.Right: Performance comparison over the 3D Lissajous trajectory with a total duration of 10 s.

95τt + ft , x1 = xinit , Gt τt = lt .(LMPC-Aux)Specifically, to obtain the gradient ∂u⋆ 1 /∂[C t ] i,j , one only needs to solve (LMPC-Aux) with Ct = C t , ct = (τ ⋆ 1 ) ⊤ ⊗ I n+m :,(i−1)(n+m)+j = [τ ⋆ t ] j e n+m i , ft = 0, xinit = 0, Gt = [G t ] It,act , and lt = 0. Likewise, to solve for the gradient ∂u ⋆ 1 /∂[c t ] i , we can solve the problem (LMPC-Aux) with Ct = C t , ct = e n+m i , ft = 0, xinit = 0, Gt = [G t ] It,act , and lt = 0. To solve for ∂u ⋆ 1 /∂[x init ] j , one can solve (LMPC-Aux) with Ct = C t , ct = 0, ft = 0, xinit = e n j , Gt = [G t ] It,act , and lt = 0.These results are summarized in (LMPC-Grad) and Table I in the text.

96Optimal closed-loop control actions through the learning episodes.The lightest and darkest blue curves stand for episodes 1 and 20, respectively.Time history of the control signal in the last episode.

Fig. 3 :

97Fig. 3: Optimal closed-loop control actions with the differential wheeled robot under trajectory 2.

98û u − û subject to G û ≤ l.

Fig. 4 :

99Fig. 4: Optimal control actions and RMSE reduction subject to box constraints with different levels of tightness u bd .Tighter constraints imply more frequent control saturation, which impedes learning from improving tracking performance.∂h/∂θ for learning, one can apply chainrule by ∂h ∂θ = ∂c G,l (u) ∂u ∂h0(x, x,θ) ∂θ

Fig. 5 :

100Fig. 5: Polynomial Trajectory 1 used in TableIII

Fig. 6 :

101Fig. 6: Polynomial Trajectory 2 used in TableIII

Fig. 7 :

102Fig. 7: Evolution of the quadrotor parameters with MPC used in RotorPy.

103⊤.A total horizon of 10 s is considered with time intervals of 0.01 s for discretization.The planning horizon of MPC is 0.2 s (T = 20).The learning rate is set to α = 0.5 for DiffTune.Differential wheeled robot: Consider the following model

1048 as the state constraint.Two trajectories are used: x1 (t) = [1+t−cos(t), 1+sin(t)] ⊤ and x2 (t) = [0.8t,0.8]

TABLE III :

105Evaluation of the trained parameters and the baseline hand-tuned parameters on various trajectories in RotorPy with different radius γ and total horizon T .The reported values are the RMSEs over 5 evaluations, displayed by mean ± standard deviation.Trajectories used in the training set are signified by *.

106where ν t ∈ R g is the Lagrange multiplier associated with the inequality constraints.Let I t,act ⊆ {1, 2, ..., g} denote the rows where the inequalityG t τ t ≤ l t is active, i.e., [G t ] It,act τ t = [l t ] It,act .Correspondingly, I t,ina = {1, 2, . . ., g} \ I t,act denotes the inactive constraints, i.e., [G t ] It,ina τ t < [l t ] It,ina Following the complementary slackness

참고문헌 27건

  1. Safe controller optimization for quadrotors with Gaussian processes 2016 10.1109/icra.2016.7487170
  2. Sequential Quadratic Programming Acta Numerica · Cambridge University Press · 1995 10.1017/s0962492900002518
  3. Minimum snap trajectory generation and control for quadrotors 2011 10.1109/icra.2011.5980409
  4. Constrained Bayesian Optimization with Particle Swarms for Safe Adaptive Controller Tuning IFAC-PapersOnLine · Elsevier BV · 2017 10.1016/j.ifacol.2017.08.1991
  5. Differentiable MPC for End-to-end Planning and Control arXiv (Cornell University) · Cornell University · 2018 10.48550/arxiv.1810.13400
  6. Safe Reinforcement Learning Using Robust MPC IEEE Transactions on Automatic Control · Institute of Electrical and Electronics Engineers · 2020 10.1109/tac.2020.3024161
  7. Pontryagin Differentiable Programming: An End-to-End Learning and\n Control Framework arXiv (Cornell University) · Cornell University · 2019 10.48550/arxiv.1912.12970
  8. Data-Driven MPC for Quadrotors IEEE Robotics and Automation Letters · Institute of Electrical and Electronics Engineers · 2021 10.1109/lra.2021.3061307
  9. DiffLoop: Tuning PID Controllers by Differentiating Through the Feedback Loop 2021 10.1109/ciss50987.2021.9400299
  10. KNODE-MPC: A Knowledge-Based Data-Driven Predictive Control Framework for Aerial Robots IEEE Robotics and Automation Letters · Institute of Electrical and Electronics Engineers · 2022 10.1109/lra.2022.3144787
  11. Recursive Feasibility Guided Optimal Parameter Adaptation of Differential Convex Optimization Policies for Safety-Critical Systems 2022 International Conference on Robotics and Automation (ICRA) · 2022 10.1109/icra46639.2022.9812398
  12. Automatic Tuning for Data-driven Model Predictive Control 2021 10.1109/icra48506.2021.9562025
  13. acados—a modular open-source framework for fast embedded optimal control Mathematical Programming Computation · Springer Science+Business Media · 2021 10.1007/s12532-021-00208-8
  14. Safe Pontryagin Differentiable Programming arXiv (Cornell University) · Cornell University · 2021 10.48550/arxiv.2105.14937
  15. AutoTune: Controller Tuning for High-Speed Flight IEEE Robotics and Automation Letters · Institute of Electrical and Electronics Engineers · 2022 10.1109/lra.2022.3146897
  16. Online Dynamics Learning for Predictive Control with an Application to Aerial Robots arXiv (Cornell University) · Cornell University · 2022 10.48550/arxiv.2207.09344
  17. Physics-Inspired Temporal Learning of Quadrotor Dynamics for Accurate Model Predictive Trajectory Tracking IEEE Robotics and Automation Letters · Institute of Electrical and Electronics Engineers · 2022 10.1109/lra.2022.3192609
  18. DiffTune: Auto-Tuning through Auto-Differentiation arXiv (Cornell University) · Cornell University · 2022 10.48550/arxiv.2209.10021
  19. OptNet: Differentiable Optimization as a Layer in Neural Networks arXiv (Cornell University) · Cornell University · 2017 10.48550/arxiv.1703.00443
  20. Policy Search for Model Predictive Control with Application to Agile\n Drone Flight arXiv (Cornell University) · Cornell University · 2021 10.48550/arxiv.2112.03850
  21. DiffTune$^+$: Hyperparameter-Free Auto-Tuning using Auto-Differentiation arXiv (Cornell University) · Cornell University · 2022 10.48550/arxiv.2212.03194
  22. RotorPy: A Python-based Multirotor Simulator with Aerodynamics for Education and Research arXiv (Cornell University) · Cornell University · 2023 10.48550/arxiv.2306.04485
  23. Training Efficient Controllers via Analytic Policy Gradient 2023 10.1109/icra48891.2023.10160581
  24. Automatically-Tuned Model Predictive Control for an Underwater Soft Robot IEEE Robotics and Automation Letters · Institute of Electrical and Electronics Engineers · 2023 10.1109/lra.2023.3333662
  25. Enhancing Sample Efficiency and Uncertainty Compensation in Learning-Based Model Predictive Control for Aerial Robots 2021 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS) · 2023 10.1109/iros55552.2023.10341774
  26. Actor-Critic Model Predictive Control 2024 10.1109/icra57147.2024.10610381
  27. Differentiable Robust Model Predictive Control 2024 10.15607/rss.2024.xx.003