AAAA ProjectProjectProjectProject ReportReportReportReport
OnOnOnOn
Recent AdvancesRecent AdvancesRecent AdvancesRecent Advances inininin Model Predictive Control Model Predictive Control Model Predictive Control Model Predictive Control
based on Convex Programming based on Convex Programming based on Convex Programming based on Convex Programming
BYBYBYBY
Kamal Reza Varhoushi
Supervisor Supervisor Supervisor Supervisor
Dr. Farzad Towhidkhah
DEPARTMENT OF ELECTERICAL ENGINEERING
January - 2012
1
Contents
1 Introduction ------------------------------------------------------------------------------------- 2
1.1 The philosophy behind MPC ------------------------------------------------------------------- 2
1.2 MPC formulation ----------------------------------------------------------------------------------- 4
1.3 Robust MPC ------------------------------------------------------------------------------------------ 6
1.4 Real time MPC -------------------------------------------------------------------------------------- 8
References ------------------------------------------------------------------------------------------- 9
2 Robust MPC ----------------------------------------------------------------------------------- 10
2.1 Standard convex optimization problems ----------------------------------------------- 10
2.2 Modeling and solving uncertain optimization problems --------------------------- 11
2.3 Designing robust MPC for four-tank level controller -------------------------------- 16
References ----------------------------------------------------------------------------------------- 26
3 Real time MPC ------------------------------------------------------------------------------- 27
3.1 CVXGEN example -------------------------------------------------------------------------------- 27
3.2 CVXGEN implementation --------------------------------------------------------------------- 30
3.3 Code Generation for MPC -------------------------------------------------------------------- 31
References ----------------------------------------------------------------------------------------- 40
2
1 1 1 1 IntroductionIntroductionIntroductionIntroduction
Model predictive control (MPC) refers to a class of computer control algorithms
that utilize a process model to predict the future response of a plant. During the
past thirty years, MPC has become the most widely implemented process control
technology. The main reasons that make MPC successful in industry are:[1,2]
• It handles multivariable control problems naturally
• It can take account of physical and operational constraints, which are often
associated with the industry cost such as actuator limitations.
• It can carry out Necessary computation on-line with the speed of hardware
increasing and optimization algorithms improvement.
• It is an easy to tune method.
• It can handle non-minimal phase and unstable processes.
• It handles structural changes.
• It allows operation closer to constraints, hence increased profit.
However, a practical disadvantage, needed to be solved:
1. The main disadvantage of the MPC is that it cannot be able of explicitly
dealing with plant model uncertainties. For confronting such problems,
several Robust Model Predictive Control (RMPC) techniques have been
developed in recent decades.
2. Another practical disadvantage is the computational cost, which tends to limit
MPC applications to linear processes with relatively slow dynamics.
Next sections illustrate some recent works to develop new algorithms in Robust
MPC and Fast MPC. Before that, it is necessary to discuss about philosophy behind
MPC.
1.1 1.1 1.1 1.1 The philosophy behind MPCThe philosophy behind MPCThe philosophy behind MPCThe philosophy behind MPC
The conceptual structure of MPC is depicted in Fig. 1. The name MPC stems from
the idea of employing an explicit model of the plant to be controlled which is used
to predict the future output behavior. This prediction capability allows solving
optimal control problems on line, where tracking error, namely the difference
between the predicted output and the desired reference (as shown in Fig.2), is
minimized over a future horizon, possibly subject to constraints on the manipulated
inputs and outputs. When the model is linear, then the optimization problem is
quadratic if the performance index is expressed through the L2-norm, or linear if
expressed through the L1/L∞-norm. The result of the optimization is applied
according to a receding horizon philosophy [1].
3
Fig.1: Basic structure of Model Predictive Control
Fig.2: Receding horizon strategy: only the first one
of the computed moves u(t) is implemented
Output horizon Np represent two kinds of MPC, for finite Np, we have Finite
Horizon Model Predictive Control (FH-MPC), and for infinite Np, we use IH-MPC.
Figure.3 illustrates the flow chart of a representative MPC calculation at each
control execution. The first step is to read the current values of inputs (manipulated
variables, MVs) and outputs (controlled variables, CVs), i.e. yk, from the plant. The
outputs yk then go into the second step, state estimation. This second step is to
compensate for the model-plant mismatch and disturbance. An internal model is
used to predict the behavior of the plant over the prediction horizon during the
optimal computation. It is often the case that the internal model is not same as the
plant, which will result in incorrect outputs. Therefore the internal model is adjusted
to be accurate before it is used for calculations in the next step. The third step,
dynamic optimization, is the critical step of the predictive control, and will be
discussed heavily in this report. At this step, the estimated state, x, together with
the current input, uk−1, and the reference trajectory, r, are used to compute a set
of MVs and states. Since only the first element of MVs, u0, is implemented in the
4
plant, u0 goes to the last step. The first element of states returns to the second
step for the next state estimation. At last step, the optimal input, u0 is sent to the
plant[1,3].
Fig.3: Flow chart of MPC calculation
To simplify the problem, we make a few assumptions listed below. These
assumptions are not valid in industrial practice, but for the development and
comparison of numerical methods, they are both reasonable and useful. The
assumptions are: [1]
• The internal model is an ideal model, meaning that the model is the same as
the plant.
• The environment is noise free. There is no input and output disturbances and
measurement noise.
Since the internal model and the plant are matched, and no disturbances and
measurement noise exist, state estimation (the second step in Figure.3) can be
omitted from MPC computations when simulating.
• The system is time-invariant.
1.21.21.21.2 MPC formulation MPC formulation MPC formulation MPC formulation
MPC is formulated almost always in the state space. Let the model ∑ of the plant
to be controlled be described by the linear discrete-time difference equations:
5
Where denote the state, control input, and output
respectively. Let or, in short, denote the prediction obtained by
iterating model k times from the current state .
A receding horizon implementation is typically based on the solution of the
following open-loop optimization problem:[3]
Subject to:
linear nominal system
polytopic constraints
And
“stability constraints”
The main goals of MPC formulation are:
1. Guaranty stability of system.
2. Constraint satisfaction.
3. Performance guarantee
Below, we review some of the popular techniques used to “enforce” stability (for
more details reference [3], can be useful):
End (Terminal) Constraint Infinite Output Prediction Horizon Terminal Weighting Matrix Invariant terminal set Contraction Constraint
The policy of this report is in principle of “End (Terminal) Constraint”.
6
Now with this assumption, MPC law is described by the following algorithm:
1. Get the new state x(t)
2. Solve the optimization problem
3. Apply only u(t) = u(t + 0/t)
4. t→ t+1. Go to 1.
End (Terminal) Constraint :
The stability constraint is:
x(t + Np/t) = 0
Value attained for the minimize
Of optimization problem at each time t as a Lyapunov function. This renders the
sequence feasible at time t + 1, and therefore V (t +
1) ≤ J(U1; x(t +1);Np;Nm) ≤ J(U*; x(t);Np;Nm) = V (t) is a Lyapunov function of
the system.
The main drawback of using terminal constraints is that the control effort required
to steer the state to the origin can be large, especially for short Np, and therefore
feasibility is more critical because of polytopic constraints. The domain of attraction
of the closed-loop (MPC+plant) is limited to the set of initial states x0 that can be
steered to 0 in Np steps while satisfying polytopic, constraints which can be
considerably smaller then the set of initial states steerable to the origin in an
arbitrary number of steps. Also, performance can be negatively affected because of
the artificial terminal constraint. A variation of the terminal constraint idea has been
proposed where only the unstable modes are forced to zero at the end of the
horizon. This mitigates some of the mentioned problems.
1.3 Robust MPC1.3 Robust MPC1.3 Robust MPC1.3 Robust MPC
The basic MPC algorithm described in the previous section assumes that the plant
∑0 to be controlled and the model ∑ used for prediction and optimization are the
same, and no unmeasured disturbance is acting on the system. In order to talk
about robustness issues, we have to relax these hypotheses and assume that (i)
the true plant ∑0 Є S, where S is a given family of LTI systems, and/or (ii) an
unmeasured noise w(k) enters the system, namely:
Where w(t) Є W and W is a given set (usually a polytope).
Different uncertainty sets S, W have been proposed in the literature in the context
of MPC, and are mostly based on time-domain representations. Frequency-domain
descriptions of uncertainty are not suitable for the formulation of robust MPC
because MPC is primarily a time-domain technique.
7
The uncertainty represent by following descriptions:
• Impulse/Step-Response
• Structured Feedback Uncertainty
• Multi-Plant
• Polytopic Uncertainty
• Bounded Input Disturbances
In this report, I focus on polytopic uncertainty and structured feedback uncertainty
[3,4].
Polytopic Uncertainty: The set of models S is described as
and Ω = Co[A1 B1],…, [AM BM], the convex hull of the “extreme” models [Ai Bi]
is a polytope. As remarked by Kothare (1996), polytopic uncertainty is a
conservative approach to model a nonlinear system x(t+1) = f(x(k); u(k); k) when
the Jacobian is known to lie in the polytope Ω.
Structured Feedback UncertaintyStructured Feedback UncertaintyStructured Feedback UncertaintyStructured Feedback Uncertainty:::: A common paradigm for robust control
consists of a linear time-invariant system with uncertainties in the feedback loop, as
depicted in Fig.4(B) (Kothare . 1996).
Fig.4: (A) graphical representation of polytopic uncertainty (B) Structured Feedback
Uncertainty.
8
Several Robust Model Predictive Control (RMPC) techniques have been developed
in recent decades. This report introduces a Robust Model Predictive Control
technique by using Linear Matrix Inequalities (LMIs) and in the convex form, which
is mainly presented in Kothare, 1996. And especially focus on minmax optimization
problems. The first step to do that, needs a complete knowledge of modeling and
solving uncertain optimization problems. (my project is based on Johan Lofberg
works on developing YALMIP ).
1.4 Real time MPC1.4 Real time MPC1.4 Real time MPC1.4 Real time MPC
For each MPC problem, we need to solve an optimization problem for each
sampling. So MPC limited to applications with slow dynamics (sampling time in
seconds or minutes). A well-known technique for implementing fast MPC is to
compute the entire control law offline (explicit MPC), in which case the online
controller can be implemented as a lookup table. The control action is then
implemented online in the form of a lookup table. The major drawback here is that
the number of entries in the table can grow exponentially with the horizon, state,
and input dimensions, so that “explicit MPC” can only be applied reliably to small
problems (where the state dimension is no more than around five). So this method
is not applicable for more complex problems.
The new methods for fast MPC, develops by professor Boyd research group in
Stanford university. These methods are based on combination of MPC with
automatic code generation. Below figure shows a brief description [5,6].
Fig.5: (a) shows a general-purpose parser-solver, which takes a single problem instance and outputs the optimal solution. (b) shows a code generator, which takes a problem family description, and produces C code that can be compiled into a customized solver for the problem family. That solver can then be used to solve problem instances.
9
ReferencesReferencesReferencesReferences:::: [1] Jing Yang, “Numerical Methods for Model Predictive Control”, Technical University of Denmark [2] Yousof Koohmaskan, “Convex Optimization in Model Predictive Control”, August 25, 2010, Amirkabir university of technology [3] Alberto Bemporad and Manfred Morari, “Robust Model Predictive Control: A Survey” , Automatic Control Laboratory, Swiss Federal Institute of Technology (ETH) [4] Mayuresh V.Kotare, “ Robust Constraint Model Predictive Control using Linear Matrix Inequality”, Automatica , March 20 1995 [5] JACOB MATTINGLEY,YANG WANG, and STEPHEN BOYD, “Receding Horizon Control Automatic Generation of High Speed Solvers”, IEEE CONTROL SYSTEMS MAGAZINE » JUNE 2011 [6] Yang Wang and Stephen Boyd, “Fast Model Predictive Control Using Online Optimization”, IEEE TRANSACTIONS ON CONTROL SYSTEMS TECHNOLOGY, VOL. 18, NO. 2, MARCH 2010
10
2 2 2 2 Robust MPCRobust MPCRobust MPCRobust MPC
Controlling a system with control and state constraints is one of the most
important problems in control theory, but also one of the most challenging. Another
important but just as demanding topic is robustness against uncertainties in a
controlled system. One of the most successful approaches, both in theory and
practice, to control constrained systems is model predictive control (MPC). The
basic idea in MPC is to repeatedly solve optimization problems on-line to find an
optimal input to the controlled system. In recent years, much effort has been spent
to incorporate the robustness problem into this framework.
The main part of the project revolves around minimax formulations of MPC for
uncertain constrained systems. A minimax strategy in MPC means that worst-case
performance with respect to uncertainties is optimized. But the basic problem is
converting an uncertain problem to a robust counterpart without uncertainty. A considerable part of the material in this report is based on convex optimization.
The idea in almost all parts is to formulate an optimization problem, and then show
that the problem can be cast as some standard convex optimization problem.
2.1 Standard convex optimization problems 2.1 Standard convex optimization problems 2.1 Standard convex optimization problems 2.1 Standard convex optimization problems
Let us begin with the definition of a general convex optimization problem:
The objective function and the constraint functions are assumed convex in the
decision variable x.
Some commonly known problem in convex optimization:
1. Linear program, LPLinear program, LPLinear program, LPLinear program, LP
2. Quadratic program, QPQuadratic program, QPQuadratic program, QPQuadratic program, QP
11
3. Second order cone program, SOCPSecond order cone program, SOCPSecond order cone program, SOCPSecond order cone program, SOCP
4. SemidefiniteSemidefiniteSemidefiniteSemidefinite program, SDPprogram, SDPprogram, SDPprogram, SDP
5. Determinant maximization, MAXDETDeterminant maximization, MAXDETDeterminant maximization, MAXDETDeterminant maximization, MAXDET
2.2.2.2.2 2 2 2 Modeling and solving uncertain optimization problemsModeling and solving uncertain optimization problemsModeling and solving uncertain optimization problemsModeling and solving uncertain optimization problems
In a general setting, robust optimization deals with optimization problems with
two sets of variables, decision variables (here denoted x) and uncertain variables
(w). The goal in deterministic worst-case robust optimization is to find a solution on
the decision variables such that the worst-case cost is minimized and the
constraints are robustly feasible, when the uncertainty is allowed to take arbitrary
values in a defined uncertainty set.
This paradigm, commonly referred to as a worst-case approach. The problem
considerably changed from the original problem, indicated by the new objective,
constraints and additional variables, introduced in order to eliminate the
uncertainty.
12
The process of removing the uncertainty and deriving a robust counterpart is
called the filtering step, and there are currently five filters implemented, as outlined
in the next section. The filters developed in YALMIP are:
1. Duality filter
2. Enumeration filter
3. Explicit filter
4. Polya filter
5. Elimination filter
The different cases are called scenarios in the [1], and they are converted to a
robust counterpart using so called filters. There are three major scenarios with
corresponding filters, all discussed in the paper referenced.
1. For elementwise constraints affinely (for fixed x) parameterized in the uncertainty,
polytopic and general conic uncertainty sets are supported. The uncertainty is
eliminated using either duality theory or enumeration.
2. For elementwise constraints affinely parameterized in the uncertainty and the
uncertainty constrained to a norm-ball (p=1,2, ∞), the uncertainty is removed by
explicitly maximizing the expression w.r.t the uncertainty typically leading to a very
efficient representation of the worst-case.
3. For conic constraints affinely parameterized in the uncertainty, polytopic uncertainty
sets are supported. The uncertainty is removed using enumeration.
Robust lRobust lRobust lRobust linear programming: inear programming: inear programming: inear programming:
Min
Subject to:
,
1. We use explicit maximization approach.
The constraints equivalent with:
for worst case approach (max w).
means elementwise absolute value (the size of the problem remains the same)
Tc x
( )A x w b+ ≤ 1w ≤
1Ax A b+ ≤
A
13
2. The enumeration approach: it is based on the vertices of uncertain constraint
and would lead to the constraint where denote the vertices
of the unit cube. Hence the original set of constraints are replicated times,
thus leading to a quickly growing problem size.
YALMIP solve the problem by using the first approach.
For very easy example:
>> A=2;
>> b=6;
>> c=1;
>> x=sdpvar(1,1);
>> w=sdpvar(1,1);
>> C= [A*(x+w) <=b,
abs(w)<=1,
uncertain(w)];
>> C=[C,x>=1];
>> D=c' * x;
>> solvesdp(C,D)
~ 0: obj = 1.000000000e+000 infeas = 0.000e+000
ans =
yalmiptime: 0.2070
solvertime: 1.0000e-003
info: 'No problems detected (GLPK-GLPKMEX-CC)'
problem: 0
dimacs: [0 0 0 0 0 0]
b=3.9;
ans =
yalmiptime: 0.1010
solvertime: 0
info: 'Infeasible problem (GLPK-GLPKMEX-CC)'
problem: 1
dimacs: [0.5000 0 0 0.3448 0 0]
( )iA x v b+ ≤ iv 2n
2n
14
LLLLinear parameterinear parameterinear parameterinear parameter----varying (LPV) system stabilizationvarying (LPV) system stabilizationvarying (LPV) system stabilizationvarying (LPV) system stabilization::::
Our goal is to compute a controller u=Kx for an uncertain linear system. Where A
is linearly parameterized in the variable which is constrained to a simplex.
, .
The performance measure is given by the standard infinite horizon quadratic cost
A controller which minimizes the worst case cost is given by solving the following
semidefinite programming problem.
This is a nonconvex problem, but a standard trick in control (Boyd 1994) is to
perform a congruence transformation with Y=P-1, introduce L=KY, and apply a
Schur complement. Instead of solving the nonconvex problem, the following
problem is solved instead.
To elimination the uncertainty, the approach can be obtained by using Polya filter
Polya filter: consider the constraints as below
The sufficient condition obtained (for arbitrary N):
α
1iα =∑ 0α ≥
15
>> Anominal = [0 1 0;0 0 1;0 0 0];
>> B = [0;0;1];
>> Q = eye(3);
>> R = 1;
>> A1 = Anominal;A1(1,3) = -0.1;
>> A2 = Anominal;A2(1,3) = 0.1;
The uncertainty we will address is a perturbation in the (1,3) element. We create two matrices to denote the two extreme values of the uncertain matrix.
>> sdpvar t1 t2
>> A = A1*t1 + A2*t2;
>> Y=sdpvar(3,3);
>> L=sdpvar(1,3);
>> F = [Y >=0];
>> F = [F, [-A*Y-B*L + (-A*Y-B*L)' Y L';Y inv(Q) zeros(3,1);L zeros(1,3) inv(R)] > 0];
>> F = [F, 0 <= [t1 t2] <= 1, t1+t2 == 1, uncertain([t1 t2])];
>> solvesdp(F,-trace(Y));
>> K = double(L)*inv(double(Y));
>> K
K =
1.0e+003 *
-0.7261 -2.2733 -0.2539
Uncertain sumUncertain sumUncertain sumUncertain sum----ofofofof----squares:squares:squares:squares:
A nonlinear system is described by the following differential equation.
The model is not known exactly, due to the uncertain parameter
Our goal is to show that the nonlinear system is stable for any w Є W, and our
approach to do this is to construct a polynomial Lyapunov function, and prove
robust (asymptotic) stability using sum-of-squares techniques.
16
A polynomial Lyapunov function:
For stability, we requires
A sum-of-squares (SOS) approach tries to find symmetric matrices Q1 and Q2 such
that and , given a polynomial basis h(x).
the semidefinite problem will include two constraints, .
Remark: In its most basic formulation, a sum-of-squares (SOS) problem takes a
polynomial p(x) and tries to find a real-valued polynomial vector function h(x) such
that p(x)=hT(x)h(x) (or equivalently p(x)=vT(x)Qv(x), where Q is positive
semidefinite and v(x) is a vector of monomials), hence proving non-negativity of
the polynomial p(x). To see if the decomposition was successful, we simply
calculate p(x)-hT(x)h(x) which should be 0.
>> sdpvar X1 X2 w; >> f=[-1.5*X1^3-0.5*X1^2-X2; 6*X1-w*X2]; >> X=[X1,X2]; >> [V,c]=polynomial(X,4); >> dvdt=jacobian(V,X)*f; >> r=X'*X; >> C=[uncertain(w),3<=w<=5]; >> C=[C,sos(V-r),sos(-dvdt-r)]; >> solvesdp(C,[],[],c) -> Solver reported infeasible primal problem. -> Your SOS problem is probably infeasible.
The sum-of-squares constraints and the uncertainty model are defined and the
problem is solved. If feasible, robust asymptotic stability is proven.
2.3 DDDDesigning robust MPC for fouresigning robust MPC for fouresigning robust MPC for fouresigning robust MPC for four----tank tank tank tank level controllerlevel controllerlevel controllerlevel controller
The four tanks plant is a multivariable laboratory plant of interconnected tanks
with nonlinear dynamics and subject to state and input constraints. A scheme of
this plant is presented in Fig.6.
A schematic plot of the process is shown in the figure 6. It can be seen the four
tanks (T1, T2, T3 and T4) which can be filled by several flows from a storage tank
sited in the bottom of the plant. The tanks at top (T3 and T4) discharge in the tanks
at bottom (T1 and T2 respectively). The aim is to control the water levels in the
tanks with the two pumps. The inputs of the process to control are the input
17
voltages of the pumps v1 and v2. The outputs are the corresponding water levels in
the tanks. The pump and valve symbols used in Fig.7.
Fig.6: The four-tank process
The three-ways valve of the original quadruple-tank process has been emulated:
according to the taken parameter γi and flow qi, the reference for each flow control
loop is suitably calculated. Thus, the controllers are responsible to ensure the taken
ratio and flow of each inlet.
Fig.7: (a). Symbol of a pump with input voltage vj generating ow qpump;j
(b). Symbol of valve splitting up pump generated flow
The variables of the system are:
1. Tank cross sectional area
A1 = 15.5179; A2 = 15.5179; A3 = 15.5179; A4 = 15.5179
2. Outlet cross sectional area
a1 = 0.1781; a2 = 0.1781; a3 = 0.1781; a4 = 0.1781
3. Pump coefficients
k1 = 4.35 ,k2 = 4.39
18
4. Ratio of allocated pump capacity between lower and upper tank
γ1 = 0.36, γ2 = 0.36
The nonlinear model shown in fig.6 and the linearization model of the systems
implemented in the Matlab (using jMPC toolbox).
,
Steady state values: x0 = [15.0751 15.0036 6.2151 6.1003] , u0 = [7 7]
Due to the non-square nature of the system (2 inputs and 4 outputs) we will
control two outputs, namely the bottom two tank levels (y1, y2) to a specified
setpoint. This will be achieved by controlling the pump voltages via the inputs (u1,
u2). The top two tank levels are measured and are also constrained within safe
operating limits, but are not directly controlled.
System constraints are:
Both Pump Voltages are constrained as:
The Bottom Two Tank Levels are constrained as:
And The Top Two Tank Levels are constrained as:
The unmeasured disturbances are the following:
Inlet flows:Inlet flows:Inlet flows:Inlet flows: the plant has a couple of manual valves downstream the regulation
valves in the inlet of the upper tanks. These can be partially opened to add an
unexpected inlet flow in the tanks.
19
IntIntIntInterconnection between tanks 1 and 2:erconnection between tanks 1 and 2:erconnection between tanks 1 and 2:erconnection between tanks 1 and 2: a valve located in the connection pipe can be
manually open to allow a flow between both tanks. This disturbance is state-
dependent.
Water transfer:Water transfer:Water transfer:Water transfer: an additional disturbance can be easily added by means of a
submersible pump transferring water from a tank to another.
Section:Section:Section:Section: Adding an object inside of one tank yields to a variation of the section of
the tank that may depend on the level.
Global diGlobal diGlobal diGlobal disturbances:sturbances:sturbances:sturbances: the uncertainty that the plant exhibits can be modelled as a
global disturbance that should be taken into account in the control design to be
rejected.
Constructing model:Constructing model:Constructing model:Constructing model:
% Parameters
%Tank cross sectional area
A1 = 15.5179; A2 = 15.5179; A3 = 15.5179; A4 = 15.5179;
%Outlet cross sectional area
a1 = 0.1781; a2 = 0.1781; a3 = 0.1781; a4 = 0.1781;
%Gravity
g = 981;
%Pump coefficients
k1 = 4.35;
k2 = 4.39;
%Ratio of allocated pump capacity between lower and upper tank
g1 = 0.36;
g2 = 0.36;
%Steady state values
x0 = [15.0751 15.0036 6.2151 6.1003];
u0 = [7 7];
%Constants
T1 = A1/a1*sqrt((2*x0(1)/g));
T2 = A2/a2*sqrt((2*x0(2)/g));
T3 = A3/a3*sqrt((2*x0(3)/g));
T4 = A4/a4*sqrt((2*x0(4)/g));
%Plant
A = [-1/T1 0 A3/(A1*T3) 0;
0 -1/T2 0 A4/(A2*T4);
0 0 -1/T3 0;
0 0 0 -1/T4];
B = [(g1*k1)/A1 0;
20
0 (g2*k2)/A2;
0 ((1-g2)*k2)/A3;
((1-g1)*k1)/A4 0];
C = eye(4);
D = 0;
>> A
A =
-0.0655 0 0.1020 0
0 -0.0656 0 0.1029
0 0 -0.1020 0
0 0 0 -0.1029
>> B
B =
0.1009 0
0 0.1018
0 0.1811
0.1794 0
>> C
C =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> D
D =
0
%Create jSS Object
Plant = jSS(A,B,C,D);
>> Plant
Plant =
-- jMPC State Space Object --
States: 4
Inputs: 2
Outputs: 4
Continuous Model
%Discontinuous Plant
Ts = 3;
Plant = c2d(Plant,Ts);
>> Plant
Plant =
-- jMPC State Space Object --
21
States: 4
Inputs: 2
Outputs: 4
Sampling Time: 3 sec
Designing MPC controller:Designing MPC controller:Designing MPC controller:Designing MPC controller:
%MPC Model
Model = Plant; %no model/plant mismatch
%Horizons & Time
Np = 10; prediction horizon
Nc = 3; control horizon
%Constraints
con.u = [0 12 6;
0 12 6];
con.y = [0 25;
0 25;
0 10;
0 10];
%Weighting
uwt = [8 8]'; R matrix in quadratic programming
ywt = [10 10 0 0]'; Q matrix
%Estimator Gain
Kest = dlqe(Model); state estimator or observer gain
%Simulation Length
T = 250;
%Setpoints (Bottom Left, Bottom Right)
setp = 15*ones(T,2);
setp(100:end,1:2) = 20;
setp(200:end,2) = 15;
%Initial values
Plant.x0 = [0 0 0 0]';
Model.x0 = [0 0 0 0]';
%-- Build MPC & Simulation --%
MPC1 = jMPC(Model,Np,Nc,uwt,ywt,con,Kest);
simopts = jSIM(MPC1,Plant,T,setp);
22
%-- Simulate & Plot Result --%
simresult = sim(MPC1,simopts,'Matlab');
plot(MPC1,simresult,'summary');
------------------------------------------------
CONSTRAINED MODEL PREDICTIVE CONTROLLER
------------------------------------------------
Model Properties:
States: 4
Inputs: 2
Outputs: 4
Observable: YES
Controllable: YES
------------------------------------------------
Controller Specifications:
Sampling Period: 3.00 sec
Prediction Horizon: 10
Control Horizon: 3
QP Solver: jMPC Wrights Method
Soft Y Constraints: NO
State Estimation: YES
Setpoint Look Ahead: NO
Input Weights:
IN1 = 8.00
IN2 = 8.00
Output Weights:
OUT1 = 10.00
OUT2 = 10.00
------------------------------------------------
Constraints:
0.00 <= IN1 <= 12.00, -6.00 <= IN1/rate <= 6.00
0.00 <= IN2 <= 12.00, -6.00 <= IN2/rate <= 6.00
0.00 <= OUT1 <= 25.00
0.00 <= OUT2 <= 25.00
0.00 <= OUT3 <= 10.00
0.00 <= OUT4 <= 10.00
------------------------------------------------
------------------------------------------------
23
MPC SIMULATION RESULTS
------------------------------------------------
Simulation Specifications:
Simulation Mode: Matlab
Plant Type: Linear State Space
Length Of Simulation: 750.00 sec
Sampling Time: 3.00 sec
Output Disturbances: NO
Input Disturbances: NO
------------------------------------------------
Sampling Statistics:
Average: 0.00187 (s)
Maximum: 0.10042 (s)
Theoretical Maximum Sampling Frequency: 9.95865 (Hz)
In this case we have chosen a prediction horizon of 10 samples (30 seconds at Ts
= 3s) and a control horizon of 3 samples (6 seconds).
DesigningDesigningDesigningDesigning robustrobustrobustrobust MPC controller:MPC controller:MPC controller:MPC controller:
This next example will show how to add an unmeasured input (load) disturbance as
well as measurement noise to the system.
The variable umdist now contains a 2 column matrix of input disturbance values,
which in this case u1 has a 1V bias subtracted from it for samples 80 to 100. The
0 50 100 150 200 2500
5
10
15
20
25
Outputs: yp(k)
Am
plitu
de
0 50 100 150 200 2500
5
10
15
20
25
States: xp(k)
Am
plitu
de
0 50 100 150 200 2500
5
10
15Input: u(k)
Sample
Am
plitu
de
0 50 100 150 200 250-4
-2
0
2
4
6Change in Input: ∆ u(k)
Sample
Am
plitu
de
24
variable noise now contains a 4 column matrix of normal distribution noise, with a
mean of 0 and a low power.
%Disturbances
umdist = zeros(T,2);
umdist(80:end,1) = -1;
noise = randn(T,4)/30;
%Rebuild Simulation
simopts = jSIM(MPC1,Plant,T,setp,umdist,[],noise);
%Re Simulate & Plot
simresult = sim(MPC1,simopts,'Matlab');
plot(MPC1,simresult,'summary');
------------------------------------------------
CONSTRAINED MODEL PREDICTIVE CONTROLLER
------------------------------------------------
Model Properties:
States: 4
Inputs: 2
Outputs: 4
Observable: YES
Controllable: YES
------------------------------------------------
Controller Specifications:
Sampling Period: 3.00 sec
Prediction Horizon: 10
Control Horizon: 3
QP Solver: jMPC Wrights Method
Soft Y Constraints: NO
State Estimation: YES
Setpoint Look Ahead: NO
Input Weights:
IN1 = 8.00
IN2 = 8.00
Output Weights:
OUT1 = 10.00
OUT2 = 10.00
------------------------------------------------
Constraints:
25
0.00 <= IN1 <= 12.00, -6.00 <= IN1/rate <= 6.00
0.00 <= IN2 <= 12.00, -6.00 <= IN2/rate <= 6.00
0.00 <= OUT1 <= 25.00
0.00 <= OUT2 <= 25.00
0.00 <= OUT3 <= 10.00
0.00 <= OUT4 <= 10.00
------------------------------------------------
------------------------------------------------
MPC SIMULATION RESULTS
------------------------------------------------
Simulation Specifications:
Simulation Mode: Matlab
Plant Type: Linear State Space
Length Of Simulation: 750.00 sec
Sampling Time: 3.00 sec
Output Disturbances: YES
Input Disturbances: NO
------------------------------------------------
Sampling Statistics:
Average: 0.00150 (s)
Maximum: 0.00425 (s)
Theoretical Maximum Sampling Frequency: 235.128 (Hz)
0 50 100 150 200 2500
5
10
15
20
25
Outputs: yp(k)
Am
plitu
de
0 50 100 150 200 2500
5
10
15
20
25
States: xp(k)
Am
plitu
de
0 50 100 150 200 2500
2
4
6
8
10
12Input: u(k)
Sample
Am
plitu
de
0 50 100 150 200 250-6
-4
-2
0
2
4
6Change in Input: ∆ u(k)
Sample
Am
plitu
de
0 50 100 150 200 250-0.2
-0.15
-0.1
-0.05
0
0.05
0.1
0.15Measurement Noise
Am
plitu
de
0 50 100 150 200 250-1
-0.8
-0.6
-0.4
-0.2
0Unmeasured Input Disturbance
Sample
Am
plitu
de
26
References:References:References:References:
[1] Johan Lofberg, “Modeling and solving uncertain optimization problems in YALMIP”
Proceedings of the 17th World Congress The International Federation of Automatic Control
Seoul, Korea, July 6-11, 2008.
[2] Johan Lofberg, “Minimax approaches to robust model predictive control” Dissertations,
Department of Electrical Engineering Linkoping University, Sweden 2003.
[3] Jonathan Currie, “jMPC Toolbox v3.11 User's Guide” AUT University.
[4] M. Kvasnica, P. Grieder, M. Baoti´c and F.J. Christophersen, “ Multi-Parametric Toolbox ”
ETH - Swiss Federal Institute of Technology.
[5] I. Alvarado, D. Limon, “A comparative analysis of distributed MPC techniques applied to
the HD-MPC four-tank benchmark” Journal of Process Control 21 (2011) 800–815.
27
3 Real time MPC3 Real time MPC3 Real time MPC3 Real time MPC
The applications of convex programming in wide areas of science make it
important in areas included control, economic, signal processing and the whole
fields related to optimization problems. Many solvers developed for solving convex
optimization problem simple, like CVX and YALMIP, but the resulting solve times are
not suitable in application with fast dynamics or sampling time (real time
applications). This report describe the capabilities and implementation of CVXGEN
software package (new solver that developed by Boyd research group in Stanford
university).
CVXGEN takes a high level description of a convex optimization family, and
automatically generate flat, library-free C code that can be compiled into high
speed custom solver for the problem family.
In this report we propose an automatic code generation system for real-time
embedded convex optimization, By ‘embedded’ we mean that the optimization
algorithm is part of a larger, fully automated system, that executes automatically
with newly arriving data or changing conditions, and without any human
intervention or action. By ‘real-time’ we mean that the optimization algorithm
executes much faster than a typical or generic method with a human in the loop, in
times measured in milliseconds or microseconds for small and medium size
problems. This impose special requirements on the solver. First, the solver must be
robust. Second, the solver must also be fast and finally, the solver should have a
simple footprint. These requirement are important for embedded applications.
The CVXGEN can handling poor quality data, so it’s robust and also it’s fast
enough rather than other solvers and it use simple code with minimal or no library
dependencies. The main disadvantages of CVXGEN are:
1. It reduced to quadratic programming (QPs).
2. It is suitable for small and medium size problems.
next generations of this solver provide code generation for many convex problem
family.
An early example of this kind of embedded optimization, is model predictive
control (MPC). A numerical example [3], illustrate the advantage of this method.
The remainder of this section discusses the use and implementation of CVXGEN.
First part show how CVXGEN looks to the user. Second part describes CVXGEN
implementation and finally we explain code generation for MPC example using the
software package CVXGEN.
3.1 CVXGEN example 3.1 CVXGEN example 3.1 CVXGEN example 3.1 CVXGEN example In this part, simple example of multi-period trading that uses CVXGEN is
implemented. We don’t attention to the details associated to modeling the problem
28
(for more details refer to the reference [1]), finally the trading policy will be based
on solving the optimization problem:
With variables and parameters (problem data)
Our trading policy works as follows. In period t, we obtain the problem parameters.
Some of these are known (such as the current portfolio xt); others are specified or
chosen (such as eta); and others are estimated by an auxiliary algorithm (qt, Qt,bt,
st). We then solve the optimization problem, which is feasible provided xt ≥ 0. (If
this is not the case, we declare ruin and quit.) We then choose the trade ut as a
solution of problem. By construction, this trade will be self-financing, and will
respect the post-trade leverage constraint. (The solution can be ut = 0, meaning
that no trades should be executed in period t). The CVXGEN specification of this
problem family is (for the case with n=20 assest):
29
Here we describe CVXGEN’s problem specification language.
Symbols:Symbols:Symbols:Symbols:
Dimensions: The first part of the problem specification shows the numeric
dimensions of each of the problem’s parameters and variables.
Parameters: Parameters are placeholders for problem data, which are not
specified numerically until solve time. Parameters are used to describe problem
families; the actual parameter values, specified when the solver is called, define
each problem instance. Parameter values are specified with a name and
dimensions, and include optional attributes, which are used for DCP convexity
verification. Available attributes are nonnegative, nonpositive, psd, nsd and
symmetric and diagonal.
Variables: The third block shows optimization variables, which are to be found
during the solve phase, i.e., when the solver is called. Variables are also specified
with a name and dimension, and optional attributes.
Functions and expressions:Functions and expressions:Functions and expressions:Functions and expressions:
Expressions are created from parameters and variables using addition,
subtraction, multiplication, division and several additional functions. These
expressions can then be used in the objective and constraints.
CVXGEN performs syntax, dimension and convexity checks on each problem
description. Once the problem description has been finalized, CVXGEN converts the
description into a custom C solver. Next part shows this implementation.
Fig.8: Operation of a parser-solver, and a code generator for embedded solvers.
30
3.2 3.2 3.2 3.2 CVXGEN implementationCVXGEN implementationCVXGEN implementationCVXGEN implementation
This part of the report describes techniques used to create CVXGEN solvers and
make them fast and robust. CVXGEN handles only problems that transform to QPs.
As shown in fig.8 code generation product C source files, before code generation
the specification problem parsed and converted to an internal representation. All
convexity and dimension checking is performed in this internal layer. In particular,
the output is C code that takes a problem instance and transforms it for use as the
Q, q, G, h, A and b in the canonical form. This step also produces code for taking
the optimal point x from the canonical form, and transforming it back to the
variables in the original CVXGEN problem specification. The canonical form is:
Where
are the problem data.
Once the problem is in canonical form, we use a standard primal dual interior
point method to find the solution.
First we introduce slack variables to solve equivalent problem:
With the dual variable y and z that associated with equality and inequality
constraints, respectively, the KKT conditions for this problem are:
In the second step we find analytically the solution of the pair of primal and dual
problems:
And
We don’t engage in theoretical subjects (more details are in reference [1]). Each
of the primal and dual algorithms require two solves with the so-called KKT matrix
to find the solution (L) for the system KL = R.
31
Code generation produces five primary C source files. The bulk of the algorithm is
contained in solver.c, which has the main solve function and core routines. KKT
matrix factorization and solution is carried out by functions in ldl.c, while
matrix_support.c contains code for filling vectors and matrices, and performing
certain matrix-vector products. All data structures and function prototypes are
defined in solver.h, and testsolver.c contains simple driver code for exercising the
solver.
3.3 3.3 3.3 3.3 Code GeneratiCode GeneratiCode GeneratiCode Generation for MPon for MPon for MPon for MPCCCC One drawback of MPC is that an optimization problem must be solved at each time
step. Using conventional numerical optimization techniques, the time taken to solve
this problem is often much longer. some techniques developed to solve this
optimization problem fast enough for real time applications, such as explicit MPC,
but as mentioned before these methods have their own problems that prevented us
to use them in more complicated applications And this report is based on CVXGEN,
the newest method of real time convex programming, this section show CVXGEN’s
use in numerical MPC example (energy storage system).
We do not claim that RHC always outperforms traditional control methods. In
many cases, a skilled designer can achieve similar performance by carefully tuning
a conventional linear controller such as a PID controller, suitably modified to handle
constraints (for example by saturating control inputs that are outside their limits).
ProbProbProbProblem statement:lem statement:lem statement:lem statement: We consider an energy storage system that can be charged or
discharged from a source with varying energy price. A simple example is a battery
connected to a power grid. The goal is to alternate between charging and
discharging in order to maximize the average revenue.
Let first introduce problem variables and parameters:
: denote the charge in the energy store at time period t. The energy
store has capacity C.
: denote the amount of energy taken from the source in period t to
charge the energy store.
0 tq C≤ ≤
max0 c
tu C≤ ≤
: denote the amount of energy discharged into the source from our
energy store.
The system dynamic is:
Where the shows the leakage coefficient, charge efficiency and discharge
efficiency, respectively that belongs to the interval [0
: is the energy price at time
Our cost function that wants to be minimized is:
Where denote discourage excessive charging and discharging
Now the main challenging that need to be solve
one approach is estimating future energy prices using N previous prices.
project, my strategy is using random function to generate energy price
The receding horizon policy for energy storage process:
This is a convex optimization problem.
We look at a particular numerical instance with problem data’s:
For MPC we use time horizon T=50.
The simulation results follow by several steps
1. The easiest way to use this interface
online interface, Go to the website “
project, then impose your project data’s to t
window.
max0 d
tu D≤ ≤
, ,c dk kη
tp
0γ ≥
denote the amount of energy discharged into the source from our
shows the leakage coefficient, charge efficiency and discharge
that belongs to the interval [0-1].
is the energy price at time t.
Our cost function that wants to be minimized is:
denote discourage excessive charging and discharging.
Now the main challenging that need to be solved is modeling energy price p[t],
ng future energy prices using N previous prices.
strategy is using random function to generate energy price
The receding horizon policy for energy storage process:
convex optimization problem.
We look at a particular numerical instance with problem data’s:
For MPC we use time horizon T=50.
The simulation results follow by several steps using cvxgen software package
The easiest way to use this interface is via the ‘Matlab’ screen in CVXGEN's
Go to the website “www.cvxgen.com” and create new
project, then impose your project data’s to the blank spaces shown in
32
denote the amount of energy discharged into the source from our
shows the leakage coefficient, charge efficiency and discharge
is modeling energy price p[t],
ng future energy prices using N previous prices. In this
strategy is using random function to generate energy prices.
using cvxgen software package:
is via the ‘Matlab’ screen in CVXGEN's
” and create new
he blank spaces shown in
33
2. The second step is creating custom C codes for using in Matlab, shown in
below fig.
34
3. Download and extract the ‘cvxgen.zip’ archive for your problem. This will
create a subdirectory called CVXGEN.
4. Inside the cvxgen/ folder in Matlab, call make_csolve. This will use the mex
command to compile and build your custom solver and creates a csolve.mex* file.
Now we are ready to use this solver, as mentioned before, we use random
function to obtain energy price for our project.
params.eta=.98;
params.kappac=.98;
params.kappad=.98;
params.Cmax=10;
params.Dmax=10;
params.C=50;
params.gamma=.02;
params.q_0=0;
params.p_0=1;
for i=1:50, params.pi=rand(1)*10
end
[vars, status] = csolve(params);
iter objv gap |Ax-b| |Gx+s-h| step
1 -2.641e+002 7.27e+003 3.38e-008 8.05e-001 0.9915
2 -8.725e+002 1.12e+003 4.08e-009 9.72e-002 0.8792
3 -1.056e+003 3.47e+002 1.15e-009 2.73e-002 0.7195
4 -1.102e+003 1.12e+002 2.54e-010 6.04e-003 0.7785
5 -1.118e+003 5.74e+001 1.06e-010 2.52e-003 0.5832
6 -1.128e+003 1.74e+001 7.98e-012 1.89e-004 0.9248
7 -1.130e+003 8.02e+000 2.70e-012 5.57e-005 0.7057
8 -1.132e+003 1.50e+000 7.64e-013 8.39e-006 0.8494
9 -1.133e+003 4.44e-002 4.23e-013 1.17e-007 0.9860
10 -1.133e+003 4.46e-004 3.12e-014 1.18e-009 0.9899
11 -1.133e+003 4.46e-006 9.53e-015 1.18e-011 0.9900
After 11 iterations, we find optimal solution for our cost function and the variables
of the problem are:
>> vars
vars =
q_1: 9.8000
35
q_2: 19.4040 q_3: 8.8118 q_4: 18.4356 q_5: 27.8669 q_6: 36.4007 q_7: 45.4727 q_8: 34.3592 q_9: 23.4679 q_10: 12.7945 q_11: 22.3386 q_12: 11.6877 q_13: 21.2540 q_14: 10.6248 q_15: 10.4123 q_16: 7.6011e-009 q_17: 0.6248 q_18: 10.4123 q_19: 1.2517e-008 q_20: 9.8000 q_21: 19.4040 q_22: 28.8159 q_23: 23.9740 q_24: 13.2904 q_25: 2.8205 q_26: 12.5641 q_27: 22.1128 q_28: 11.4665 q_29: 21.0372 q_30: 10.4123 q_31: 3.2724e-008 q_32: 9.8000 q_33: 0.6248 q_34: 10.4123 q_35: 1.7515e-008 q_36: 9.8000 q_37: 19.4040 q_38: 15.9945 q_39: 25.4746 q_40: 14.7610 q_41: 24.2658 q_42: 13.5764 q_43: 3.1008 q_44: 12.8388 q_45: 2.3779 q_46: 12.1303 q_47: 21.6877 q_48: 11.0499 q_49: 0.6248 q_50: 10.4123 q_51: 3.0567e-009 uc_0: 10.0000 uc_1: 10.0000 uc_2: 7.6158e-009 uc_3: 10.0000 uc_4: 10.0000 uc_5: 9.2767 uc_6: 10.0000 uc_7: 5.3053e-009 uc_8: 5.5717e-009 uc_9: 9.3884e-009 uc_10: 10.0000 uc_11: 2.1184e-008 uc_12: 10.0000 uc_13: 4.0029e-009 uc_14: 6.4831e-008 uc_15: 8.8862e-009
36
uc_16: 0.6376 uc_17: 10.0000 uc_18: 4.0215e-009 uc_19: 10.0000 uc_20: 10.0000 uc_21: 10.0000 uc_22: 1.0351e-007 uc_23: 3.1983e-009 uc_24: 3.5030e-009 uc_25: 10.0000 uc_26: 10.0000 uc_27: 1.2778e-008 uc_28: 10.0000 uc_29: 6.4378e-009 uc_30: 7.6649e-009 uc_31: 10.0000 uc_32: 6.0607e-008 uc_33: 10.0000 uc_34: 2.5104e-009 uc_35: 10.0000 uc_36: 10.0000 uc_37: 7.9212e-008 uc_38: 10.0000 uc_39: 1.2094e-008 uc_40: 10.0000 uc_41: 2.5293e-009 uc_42: 2.7328e-009 uc_43: 10.0000 uc_44: 4.4275e-009 uc_45: 10.0000 uc_46: 10.0000 uc_47: 1.3073e-008 uc_48: 2.8971e-009 uc_49: 10.0000 uc_50: 2.7678e-009 ud_0: 3.5892e-009 ud_1: 3.2340e-009 ud_2: 10.0000 ud_3: 2.9702e-009 ud_4: 3.0852e-009 ud_5: 5.2090e-008 ud_6: 3.0805e-009 ud_7: 10.0000 ud_8: 10.0000 ud_9: 10.0000 ud_10: 3.0975e-009 ud_11: 10.0000 ud_12: 1.2580e-008 ud_13: 10.0000 ud_14: 1.8089e-007 ud_15: 10.0000 ud_16: 5.8697e-008 ud_17: 2.6438e-008 ud_18: 10.0000 ud_19: 4.9420e-009 ud_20: 5.8192e-009 ud_21: 6.7055e-009 ud_22: 4.1803 ud_23: 10.0000 ud_24: 10.0000 ud_25: 3.9753e-009 ud_26: 5.4340e-008 ud_27: 10.0000 ud_28: 5.4716e-008 ud_29: 10.0000 ud_30: 10.0000
37
ud_31: 1.1085e-008 ud_32: 8.7996 ud_33: 3.4403e-009 ud_34: 10.0000 ud_35: 7.4073e-009 ud_36: 5.5124e-009 ud_37: 2.9610 ud_38: 7.7707e-009 ud_39: 10.0000 ud_40: 2.3894e-008 ud_41: 10.0000 ud_42: 10.0000 ud_43: 3.8214e-009 ud_44: 10.0000 ud_45: 8.2154e-009 ud_46: 4.8712e-008 ud_47: 10.0000 ud_48: 10.0000 ud_49: 2.3268e-008 ud_50: 10.0000
p: 1x50 cell: energy price
q: 51x1 cell: the charging at energy store
0 10 20 30 40 500
1
2
3
4
5
6
7
8
9
10
0 10 20 30 40 50 600
5
10
15
20
25
30
35
40
45
38
uc: 50x1 cell: amount of energy taken from the source to charge
ud: 50x1 cell: amount of energy discharged into the source
As shown in figures, the variation of uc and ud between max and min values,
determine this agreement that process has been done in optimal way.
If we want to modify solver behavior, we can change the settings, this allow us to
trade off between solve-time and solution accuracy to make our solving process
more efficiency.
The code information (involving problem size, KKT matrix information and code
generation information) are:
0 10 20 30 40 500
1
2
3
4
5
6
7
8
9
10
0 10 20 30 40 500
1
2
3
4
5
6
7
8
9
10
39
40
References:References:References:References:
[1] Jacob Mattingley and Stephen Boyd, “CVXGEN: A Code Generator for Embedded Convex
Optimization” , November 14, 2010
[2] JACOB MATTINGLEY,YANG WANG, and STEPHEN BOYD, “Receding Horizon Control Automatic Generation of High Speed Solvers”, IEEE CONTROL SYSTEMS MAGAZINE » JUNE 2011 [3] www.cvxgen.com
Top Related