ImplementingMPCusingCVX A case illustration 1 Purpose The purpose of this document, is to illustrate how an MPC problem can be formulated in a straigh...

0 downloads 59 Views 115KB Size

No documents

1

Purpose

The purpose of this document, is to illustrate how an MPC problem can be formulated in a straight forward, intuitive and flexible way in Matlab, using a plug-in called CVX [2]. CVX is a tool that allows the user to solve general convex problems, by formulating them in an intuitive and straight forward fashion. In order to use CVX, one must obey the rules governed by disciplined convex programming, refer to e.g. [4]. We do not attempt to give a thorough introduction to CVX here, for this the reader should refer to either [2] or [3]. Instead we focus on the sole application of implementing a model predictive controller, where the constrained optimization step, is performed by use of CVX. The people targeted with this document are mainly students and perhaps researchers at the University of Aalborg, as it is the distinct impression of the author, that implementations of model predictive controllers are currently handled in a certain way, which is neither easy, intuitive or flexible. This document therefore serves to provide an alternative. Of course people outside the above mentioned targeted group are welcome to employ the tips provided by this document, however do notice and take into account any license restrictions, when you use CVX. As indicated above, this document focusses solely on Matlab and CVX. Of course implementations can be made in a morass of different programming languages, but only Matlab is treated here.

2

Motivation

As explained above, there are several ways of implementing MPC, depending on the the way any control problem is regarded. A commonly employed way of formulating an MPC problem, is described in [5], where the problem of finding the optimal control input over a horizon, is formulated as a quadratic program (QP), by iterating from the current state the control system, to the end of the control horizon. The QP can then be solved using standard Matlab functions such as quadprog() or linprog() if the problem has been reduced to a 1

linear program (LP). However this way of arranging an MPC implementation is rather cumbersome, as it requires a lot of preliminary work, for instance in order to expand the control system, to cover the entire control horizon. We will illustrate this with an example later. Also, when using the standard Matlab functions described above, the problem is required to be formulated as an LP or QP. This is, even though often applicable, not as simple as having it formulated as a general convex problem. Formulating a problem as an LP or QP is often possible, but it sometimes requires reformulating the actual optimization problem, by adding variables and constraints in order to get it into QP or LP form. This also requires a lot of cumbersome coding, which firstly is prone to errors, and secondly can take a lot of time, which would be better spend elsewhere. In the following we show how CVX can spare us from a lot of preliminary work. It should be mentioned that CVX should not be used to reduce computation time. In fact it will typically run slower than both quadprog() and linprog(). CVX should instead be used for its flexible and intutive way of formulating problems, as well as the possibility of formulating generally convex problems, rather than only LP’s and QP’s.

3

Example application

In order to illustrate the use of CVX for an MPC implementation, the following describes an example application. We will first describe the problem, where after we will illustrate the simplified model we are employing. Afterwards we will arrange the optimization problem to be solved, and illustrate how this can be handled, both in what we call the traditional way using standard Matlab functions, and also when employing CVX.

3.1

Problem description

We observe a thermal system, consisting of a home with a built-in floor-heating system. The task is to maintain a temperature within the home, close to a provided set-point. Heating the household is performed by delivering power to the heating system. The price of power varies throughout the day. The home has a significant time-constant meaning that it takes a long time to cool down. The optimization task is then how to obtain an optimal trade-off between buying power when the price is right, without the inhabitants of the home experiences any significant discomfort, either by the home being too cold or too hot. In other words, the task is to find the optimal heating profile with respect to the power delivered to the heating system at any given time. This problem is treated in [6], where from we have borrowed both the problem in its basic 2

3.2 Modeling form, as well as the model we employ in the following, however with a somewhat different style of notation.

3.2

Modeling

Below we have outlined a somewhat simplified model of the thermal system consisting of a household and its appertaining heating system. We have assumed that the home is heated by electrically controlled floor-heating, and that the time constant of our thermal model is related to the energy stored as heat in the concrete floors of the household. In the following, we formulate the system as being discrete. The floors have a thermal capacity related to both the material of which they are made, e.g. concrete, and the mass of the specific floor. We denote this thermal capacity by cM ∈ R. The heating of the room is further related to both the transmission of energy from the floors to the air in the room, as well as a loss in energy by transmission from the air in the room, through the walls and outside to the ambient air. This transportation of energy is governed by a heat transfer coefficient uA ∈ R. Finally the energy provided by the heating system to the floors are related to the coefficient of performance of the heating system, denoted γ ∈ R. With the parameters described above, we can arrange the following model T (k + 1) = aT (k) + bw(k) + eTo (k) with a = 1−h

uA , cM

b=h

γ , cM

e=h

uA cM

(1)

(2)

where T (k), To (k) and w(k) ∈ R are the room-, outside temperature and power supplied to the heating system at time k, respectively. The final parameter h ∈ R, is a scaling factor related to the time interval in which quantities of power can be bought, typically corresponding to one hour. For a more extensive treatment and arrangement of this model, consult [6].

3.3

Optimization formulation

The optimization problem of choosing when to supply the heating system with power, is both dependent of the price of electricity, as well as the price we put on discomfort for the inhabitants in the house, when the temperature deviates from the set-point Tsp ∈ R. This is constrained by a maximum deviation from the setpoint, governed by a maximum and minumum allowed room temperature denoted by Tmax ∈ R and Tmin ∈ R, respectively. The problem is further constrained by the actuator limitations dictated by input saturations in the heating system, denoted by wmax and wmin ∈ R, respectively. We would typically have wmin = 0. 3

3.4 Traditional approach The problem can now be formulated as: PN minimize i=1 (w(k + i)pw (k + i) + |T (k + i) − Tref (k + i)| pT (k + i)) subject to T (k + i) = aT (k + i − 1) + bw(k + i − 1) + eTo (k + i − 1) w(k + i) ≤ wmax , w(k + i) ≥ wmin, T (k + i) ≤ Tmax , T (k + i) ≤ Tmin ,

(3)

where [w(k + 1) . . . w(k + N)] ∈ RN is the variable, and i ∈ {1, . . . , N}, where N ∈ R is the control horizon. Above, [Tref (k + 1) . . . Tref (k + N)] ∈ RN is the temperature reference, [pw (k + 1) . . . pw (k + N)] ∈ RN and [pT (k + 1) . . . pT (k + N)] ∈ RN , are the costs of power consumption and temperature deviations respectively. It should be noted that pw (k + i), i ∈ {1, . . . , N} should be regarded as estimates of the future electricity prices, as well as To (k + i), i ∈ {1, . . . , N} should be regarded as estimates on future ambient temperatures, i.e. a whether forecast. In the following sections, we illustrate two ways of accommodating this problem, both using the standard Matlab functions, and by using CVX.

3.4

Traditional approach

In this section, we solve the problem described in Eq. 3 by first reformulating it as an LP, and then using linprog() to solve the optimization. This is a two step procedure. First we arrange matrices, governing the system dynamics throughout the control horizon. Afterwards we introduce an additional set of variables and constraints in order to formulate the cost function from Eq. 3, as an LP.

3.4.1

Expanding system dynamics

This step is described extensively in [5]. Expanding the system results in the following matrices and vectors:

T (k + 1) .. T = , . T (k + N)

Tref

Tref (k + 1) .. = , . Tref (k + N)

a a2 A = .. .

aN

4

3.4 Traditional approach

... 0 . . . 0 . . . 0 B= , .. . N −1 N −2 N −3 a b a b a b ... b b ab a2 b .. .

0 b ab .. .

0 0 b .. .

... 0 . . . 0 . . . 0 E= .. . N −1 N −2 N −3 a e a e a e ... e e ae a2 e .. .

0 e ae .. .

0 0 e .. .

We can then write an expanded system equation as T = AT (k) + BW + ETo where we have also employed the notation w(k) .. W = , . w(k + N − 1)

To =

(4)

To (k) .. .

. To (k + N − 1)

Above T (k) dictates the initial, or current conditions of the system. 3.4.2

Introducing extra variables

The cost function in Eq. 3 involves an absolute value, and is thereby not linear in its current form. We work around this by introducing two additional sets of variables, and arranging constraints on these that makes it possible to arrange a new optimization problem, equivalent to Eq. 3, but is formulated as an LP. By introducing these two sets of variables denoted X1 ∈ RN and X2 ∈ RN , along with their corresponding constraints, together with the expansions derived above, our problem can be formulated as minimize PwT W + PTT X1 + PTT X2 subject to W ≤ wmax , W ≥ wmin , T ≤ Tmax , (5) T ≤ Tmin , −X1 ≤ T − Tref X2 ≥ T − Tref , with variables W, X1 and X2 ∈ RN . In the above problem, each inequality should be interpreted element-wise. Further, we have extended our calligraphic notation by introducing Pw = [pw (k + 1), . . . , pw (k + N)]T and similarly PT = [pT (k + 1), . . . , pT (k + N)]T . Eq. 5 is now formulated as an LP, which can be solved using standard functions from Matlab. Below we have presented the actual implementation using Matlab code. 5

3.4 Traditional approach

3.4.3

Matlab code

The Matlab code for the above approach is illustrated below. Even though it is in somewhat pseudoform, we have tried to keep close to true Matlab notation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

% S t a r t i n g c o n d i t i o n s and model p a r a m e t e r s Tk = t h e r m a l M o d e l . c u r r e n t T e m p e r a t u r e ; Tref = t h e r m a l M o d e l . r e f e r e n c e T e m p e r a t u r e ; u_a = t h e r m a l M o d e l . h e a t T r a n s f e r C o e f f i c i e n t ; c_m = t h e r m a l M o d e l . h e a t C a p a c i t y ; gamma = t h e r m a l M o d e l . c o e f f i c i e n t O f P e r f o r m a n c e ; h = t h e r m a l M o d e l . s c a l i n g; N = o p t i m i z a t i o n P a r a m e t e r s . H o r i z o n; % Constraints Tmax = c o m f o r t M o d e l . m a x T e m p e r a t u r e ; Tmin = c o m f o r t M o d e l . m i n T e m p e r a t u r e ; wmax = h e a t e r M o d e l . m a x P o w e r; wmin = h e a t e r M o d e l . m i n P o w e r; % Expanding constraints Tmax = Tmax * ones (N ,1) ; Tmin = Tmin * ones (N ,1) ; wmax = wmax * ones (N ,1) ; wmin = wmin * ones (N ,1) ; % A r r a n g e model a = 1 -h * u_a / c_m ; b = h / c_m * gamma ; calA = a .^(1: N ) ’; calB = zeros (N , N ) ; calB (1 ,1) = b ; for k = 2: N calB (k ,:) = a * calB (k -1 ,:) ; calB (k , k ) = b ; end e = h * u_a / c_m ; calE = zeros (N , N ) ; calE (1 ,1) = e ; for k = 2: N calE (k ,:) = a * calE (k -1 ,:) ; calE (k , k ) = e ; end % % % %

P e r f o r m a n c e f u n k t i o n P = sum_1 ^ N ( w ( k ) * p_w ( k ) + p_T * abs ( T ( k ) - Tref ) ) is r e w r i t t e n into c ’* x = p_w ’* w + p_T ’* x1 + p_T ’* x2 x1 : - n e g a t i v e t e m p e r a t u r e d e v i a t i o n s x2 : p o s i t i v e t e m p e r a t u r d e v i a t i o n s

c = [ h * p_w ’ p_T ’ p_T ’]; G = [ calB zeros (N , N ) zeros (N , N ) ;

%T < Tmax

6

3.5 Problem implementation using CVX

48 - calB zeros (N , N ) zeros (N , N ) ; % -T < - Tmin 49 - calB - eye (N , N ) zeros (N , N ) ; % -x1 <= - Tref + T 50 calB zeros (N , N ) - eye (N , N ) ; % -x2 <= Tref - T 51 eye( N , N ) zeros (N , N ) zeros (N , N ) ]; % w < wmax 52 f = [ Tmax - calA * Tk - calE * To ; 53 - Tmin + calA * Tk + calE * To ; 54 - Tref + calA * Tk + calE * To ; 55 Tref - calA * Tk - calE * To ; 56 wmax * ones (N ,1) - whotw / gamma ]; 57 lb = zeros (3*N ,1) ; 58 X = l i n p r o g(c ,G ,f ,[] ,[] , lb ) ; 59 wopt = X (1: N ,1) ; 60 Topt = A * T0 + B * wopt + E * Tomg ; 61 Popt = c ’* X

Resulting from the above code wopt would be the optimal use of power, Topt would be the resulting temperature and Popt would be the final cost of our power consumption, with respect to the overall cost function.

3.5

Problem implementation using CVX

In Section 3.4, we made a somewhat rigorous illustration the steps connected to the MPC implementation. We did this with the intention of illuminating how much effort is actually needed, on preliminary tasks that is not related to the actual problem, and how cumbersome this preliminary work can be. In fact, from the entire code-snippet presented in Section 3.4.3, only one line is related to the actual optimization. Everything else is related to arranging the model, expanding model coefficients, and reformulating the cost function to an LP. Below is presented the way the above would be implemented in CVX. As there is no preliminary work, extra formulations or necessary rewriting of any kind, we will simply show the Matlab code. 3.5.1 1 2 3 4 5 6 7 8 9 10 11 12 13

Matlab code

% S t a r t i n g c o n d i t i o n s and model p a r a m e t e r s Tk = t h e r m a l M o d e l . c u r r e n t T e m p e r a t u r e ; Tref = t h e r m a l M o d e l . r e f e r e n c e T e m p e r a t u r e ; u_a = t h e r m a l M o d e l . h e a t T r a n s f e r C o e f f i c i e n t ; c_m = t h e r m a l M o d e l . h e a t C a p a c i t y ; gamma = t h e r m a l M o d e l . c o e f f i c i e n t O f P e r f o r m a n c e ; h = t h e r m a l M o d e l . s c a l i n g; N = o p t i m i z a t i o n P a r a m e t e r s . H o r i z o n; % Constraints Tmax = c o m f o r t M o d e l . m a x T e m p e r a t u r e ; Tmin = c o m f o r t M o d e l . m i n T e m p e r a t u r e ; wmax = h e a t e r M o d e l . m a x P o w e r;

7

3.6 Extensions

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

wmin = h e a t e r M o d e l . m i n P o w e r; % a b e

A r r a n g e model = 1 -h * u_a / c_m ; = h / cm * gamma ; = h * u_a / c_m ;

% Bounding constraints upp = wmax * ones (N ,1) - ( whotw / gamma ) ; low = zeros ( N ,1) ; cvx_begin variables w(N) T(N) m i n i m i z e( h * p_w ’* w + p_T ’* abs (T - Tref ) ) s u b j e c t to % System D y n a m i c s: T (1: end ) == a *[ Tk ; T (1: end -1) ] + b * w (1: end ) + e * To (1: end) ; % Comfort constraints: T <= Tmax ; T >= Tmin ; % A c t u a t o r limits : w <= upp ; w >= low ; cvx_end

What the reader should take from this, is how close the actual coding of the problem is to the formulation in Section 3.3. Also, the implementation illustrated above is far more flexible than the approach described in Section 3.4, if it becomes necessary to change either the cost function, or the extend of the problem. We illustrate this in the following.

3.6

Extensions

As we have mentioned earlier, CVX can solve generally formulated convex problems, when obeying the rules of disciplined convex programming. This entails that the cost function can be any convex function, provided that CVX is able to certify it as such, consult the CVX manual for elaboration on this [3]. Below we have simple example of this. 3.6.1

Using different norms

Above, our cost function consisted of two terms. The first was the cumulated cost of buying power, and the other was a scaled 1-norm of the temperature deviation of the temperature from the set-point. Different norms carries different characteristics when used in cost functions, see for instance [1]. Using the 1-norm would typically result in a sparse solution, where the tem8

3.6 Extensions perature in most instances would correspond to the set-point, but in few instances, the deviation would be relatively high. Using a 2-norm would instead minimize the average deviation, making the deviation at any point rather small, but only at rare instances would the temperature match the set-point. Similarly, if an ∞-norm where used, the maximum deviation would be small, but all deviations would have roughly the same magnitude. Obviously, other concerns could be relevant, and correspondingly, other norms or cost functions could be relevant. However, the current extend of our discussion serves a sufficient point, and we will therefore not discuss this any further here. Changing the cost function, or perhaps adding additional elements to it, in the approach presented in Section 3.4, would entail quite extensive work on reformulating the problem and system matrices involved. However the job is easily done in CVX. For instance, punishing the 2-norm of temperature deviation, instead of a 1-norm, could be achieved in the following fashion: 1 cvx_begin 2 variables w(N) T(N) 3 m i n i m i z e( h * p_w ’* w + norm ( p_T .*( T - Tref ) ,2) ) 4 s u b j e c t to 5 % System D y n a m i c s: 6 T (1: end ) == a *[ Tk ; T (1: end -1) ] + b * w (1: end ) + e * To (1: end) ; 7 8 % Comfort constraints: 9 T <= Tmax ; 10 T >= Tmin ; 11 12 % A c t u a t o r limits : 13 w <= upp ; 14 w >= low ; 15 c v x _ e n d

or perhaps an ∞-norm: 1 cvx_begin 2 variables w(N) T(N) 3 m i n i m i z e( h * p_w ’* w + norm ( p_T .*( T - Tref ) , inf ) ) 4 s u b j e c t to 5 % System D y n a m i c s: 6 T (1: end ) == a *[ Tk ; T (1: end -1) ] + b * w (1: end ) + e * To (1: end) ; 7 8 % Comfort constraints: 9 T <= Tmax ; 10 T >= Tmin ; 11 12 % A c t u a t o r limits : 13 w <= upp ;

9

3.6 Extensions

14 w >= low ; 15 c v x _ e n d

In similar fashion, a large number of different cost functions could be arranged, and also a variety of constraints can be formulated in a general convex fashion, and interpreted by CVX [3]. 3.6.2

Expanding system size

The example presented so far, have only been optimizing the power consumption for a single household, however, we can very easily extend it to involve a general number n. Here it should be mentioned, that if the problem size gets too big, CVX will run into numerical issues, and perhaps return an error message.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

n = 2; % Number of H o u s e h o l d s % a b e

Vectorized = [ a1 a2 ]; = [ b1 b2 ]; = [ e1 e2 ];

parameters % If more , expand by a d d i t i o n a l a ’ s % If more , expand by a d d i t i o n a l b ’ s % If more , expand by a d d i t i o n a l e ’ s

% Model m a t r i c e s A = diag ( a ) ; % a - c o e f f i c i e n t s for all h o u s e h o l d s B = diag ( b ) ; % b - c o e f f i c i e n t s for all h o u s e h o l d s E = diag ( e ) ; % e - c o e f f i c i e n t s for all h o u s e h o l d s % C o n s t r a i n t bounds upp = [ wmax1 * ones (1 , N ) ; wmax2 * ones (1 , N ) ; low = zeros (n , N ) ; % Cost f u n c t i o n m a t r i c e s TO = [ To1 ’; To2 ’]; TREF = [ Tref1 ’; Tref2 ’]; P_W = [ p_w1 ’; p_w2 ’]; P_T = [ p_T1 ’; p_T2 ’]; TK = [ Tk1 ; Tk2 ]; TMAX = [ Tmax1 ’; Tmax2 ]; TMIN = [ Tmin1 ’; Tmin2 ]; cvx_begin v a r i a b l e s w (n , N ) T (n , N ) m i n i m i z e( sum ( sum ( h * P_W .* w + abs (T - TREF ) .* P_T ) ) ) s u b j e c t to % System D y n a m i c s: T (: ,1: end ) == A *[ TK T (: ,1: end -1) ] + B * w (: ,1: end ) + E * TO (: ,1: end ) ;

10

34 35 36 37 38 39 40 41

% Comfort constraints: T <= TMAX ; T >= TMIN ; % A c t u a t o r limits : w <= upp ; w >= low ; cvx_end

The optimal power consumption would then be the matrix w, and the corresponding temperature would be T.

4

Closing remarks

In this document we have shown how CVX can be used for arranging simple implementations of MPC in Matlab. We have shown how the cost function can be easily changed, without having to perform any work on rearranging system matrices. Finally we have briefly illustrated how problems can be expanded.

References [1] S. P. Boyd and L. Vandenberghe. Convex optimization. Cambridge University Press, 2004. [2] M. Grant and S. P. Boyd. CVX: Matlab software for disciplined convex programming. http://cvxr.com/cvx/, 2011. [3] M. Grant and S. P. Boyd. CVX Users guide for CVX version 1.21. http://web.cvxr. com/cvx/cvx_usrguide.pdf, 2011. [4] M. Grant, S. P. Boyd, and Y. Ye. Global Optimization: From Theory to Implementation. Editors: L. Liberti and N. Maculan, Springer, 2006. Pages 155-210. [5] J. Maciejowski. Predictive control with constraints. Prentice Hall, 2000. [6] T. Pedersen, P. Andersen, K.M. Nielsen, H.L. Stærmose, and P.D. Pedersen. Using heat pump energy storages in the power grid. IEEE Conference on Control Applications, Proceedings, 2011.

11