-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparseMPCprob.m
213 lines (177 loc) · 7.06 KB
/
sparseMPCprob.m
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
classdef sparseMPCprob < handle
% sparseMPCprob container for the sparse MPC QP formulation.
% H; The problem Hessian
% Aeq; The LHS equality constraint enforcing dynamcs.
% beq; the RHS equality equality constraint enforcing dynamcs.
% Ainq; Inequality LHS for control constraint.
% binq; Inequality RHS for control constraint.
% N_mpc; Control horizon
% kappa; condition number of H
% ns; number of states.
% nu; Number of controls.
% qpopts; Options that get passed to quadrog. From optimset.
%
% Construction: condensedMPCprob(sys,N, Q,Qp, R)
% condensedMPCprob(sys,N, Q,Qp, R, S)
%
% This sets H, M, N_mpc, and kappa fields. We leave it to you to fill in
% Ainq and binq.
properties
H; % The problem Hessian
Aeq; % The LHS equality constraint enforcing dynamcs.
beq; % the RHS equality equality constraint enforcing dynamcs.
Ainq; % Inequality LHS for control constraint.
binq; % Inequality RHS for control constraint.
N_mpc; % Control horizon
kappa; % condition number of H
ns; % number of states.
nu; % Number of controls.
qpopts; % Options that get passed to quadrog. From optimset.
end
methods
function obj = sparseMPCprob(sys,N, Q,Qp, R, S)
% obj = sparseMPCprob(sys,N, Q,Qp, R, S)
% S can be [], in which case, it is set to the zero matrix.
if ~exist('S', 'var')
ns = size(sys.b,1);
nu = size(sys.b, 2);
S = zeros(ns, nu);
end
[H, Aeq, beq] = clqrProblem_local(sys,N, Q, R, Qp, S);
obj.H = sparse(H);
obj.Aeq = sparse(Aeq);
obj.beq = beq;
obj.Ainq = [];
obj.binq = [];
obj.N_mpc = N;
obj.kappa = cond(H);
obj.ns = size(sys.b,1);
obj.nu = size(sys.b,2);
obj.qpopts = optimset('Display', 'off');
end
function [U, X] = solve(self, xk_1, varargin)
% [U, X] = solve(self, xk_1)
% Solves the MPC problem defined by the instance, given
% an initial condition xk_1.
%
% Inputs
% ------
% xk_1 : the initial condition at which to solve the MPC
% problem
%
% solve(..., 'getX', {0|1}) If 1, also return the state
% sequence. Since we are in the condensed version, this is
% found via lsim(...)
%
% solve(...,'warm_start_data', not used currently.
%
% Outputs
% ------
% U : the sequence of optimal controls from k=1...self.N_mpc
% X : the sequence of optimal states from k= 1...self.N_mpc.
% Empty if 'getX' is 0 (default), otherwise, a matrix
% of dimensions (Ns x N_mpc).
% Parse varagin.
defaultGetX = true;
defaultWarm_start_data = [];
p = inputParser;
p.addParameter('getX', defaultGetX);
p.addParameter('warm_start_data', defaultWarm_start_data);
parse(p, varargin{:});
getX = p.Results.getX;
beq_xk = self.beq;
beq_xk(1:self.ns) = xk_1;
UX = mpcSolve_local(self.H, self.Ainq, self.binq,...
self.Aeq, beq_xk, self.qpopts);
% Split the solution into controls and states.
U = UX(1:self.nu*self.N_mpc)';
if getX
X = UX(self.nu*self.N_mpc+1:end);
X = reshape(X, self.ns, []);
else
X = [];
end
end
function self = add_U_constraint(self, type, bnds)
% self = add_U_constraint(self, type, bnds)
%
% Add an input constraint to the MpcPRoblem instance.
% type: 'box' forms a box constraint on u between bnds(1) and
% bnds(2)
% type: symmetric 'slew' slew rate constraint. bnds is scalar
if strcmp('box', type) & length(bnds)==1
bnds = [-bnds, bnds];
fprintf('Type box constraint indicated but only one bound found\n')
fprintf('Assuming symmentric bounds of [%.3f, %.3f]\n', bnds(1), bnds(2));
end
if strcmp(type, 'box')
Zro = zeros(self.N_mpc*self.nu*2, (self.N_mpc+1)*self.ns);
I = eye(self.N_mpc*self.nu);
Ainq = [[I;-I], Zro];
binq = [ones(self.N_mpc,1)*bnds(2);
ones(self.N_mpc,1)*(-bnds(1))];
elseif strcmp(type, 'slew')
Zro = zeros((self.N_mpc*self.nu-1)*2, (self.N_mpc+1)*self.ns);
S = derMat(self.N_mpc);
Ainq = [[S; -S], Zro];
binq = [zeros(2*self.N_mpc-2, 1)+bnds(1)];
end
self.Ainq = [self.Ainq; Ainq];
self.binq = [self.binq; binq];
end
end % METHODS
end % CLASDEF
function UX = mpcSolve_local(H, Ainq,binq, Aeq, beq, qpopts)
f = zeros(size(H,2),1);
UX = quadprog(H, f, Ainq, binq, Aeq, beq, [], [], [], qpopts);
end
function [H, Aeq, beq] = clqrProblem_local(sys, N, Q, R, Qp, S)
if sys.Ts == 0
error('system "sys" should be discrete time dynamical system')
end
ns = size(sys.b, 1);
nu = size(sys.b, 2);
[a, b] = ssdata(sys);
% -------------------------------------------------------------------------
% Sparse cLQR problem
% Build the cost function. THis should look like:
% [R |S 0 0 ][u0]
% [ R |0 S 0 ][u1]
% H = [-----|------][--]
% [S' 0 |Q ][x0]
% [0 S'| Q ][x1]
% [0 0 | Qp][xN]
% First, build the part without the cross term. Use eye(N) because
% 0...N-1 has N elements.
RRR = kron(eye(N), R);
QQQp = blkdiag(kron(eye(N), Q), Qp);
Sx = [kron(eye(N), S);
zeros(ns, N*nu)]; % The lower left.
Su = Sx'; % The upper right.
% The main event:
H = [RRR, Su;
Sx, QQQp];
% Now build the equality constraint. This should look like:
% [0 0 0| |0 0 ][u0] [x0]
% [0 0 0| I |0 0 ][u1] [0]
% [0 0 0| |0 0 ][uN] [0]
% [-----------------][--] = [--]
% [B 0 0 |I -A 0 ][x0] [0 ]
% [0 B 0 |0 I -A][x1] [0 ]
% [x_N]
I_b = eye(N, N);
Aeq_b = kron(I_b, b);
Aeq_a = [kron(eye(N), a), zeros(N*ns, ns)]...
+ [zeros(N*ns, ns), kron(eye(N), -eye(ns))];
Aeq = [[zeros(ns, nu*N), eye(ns), zeros(ns, N*ns)];
[Aeq_b, Aeq_a]];
beq = zeros(ns*(N+1), 1);
end
function S = derMat(N)
% S = derMat(N)
% Computes and N-1 x N derivitave matrix.
S = -eye(N);
I2 = eye(N-1);
S(1:N-1, 2:N) = I2+S(1:N-1, 2:N);
S = S(1:N-1, :);
end