-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddp.m
312 lines (230 loc) · 5.41 KB
/
ddp.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function [dus, V, Vn, dV, a] = ddp(x0, us, S)
% Second-order numerical optimal control. The code computes
% the optimal control adjustment for a given dynamical system
% Changelog:
% * Added logic to check whether an updated u in the forward
% pass exceeds bounds set by the system S
% * In the same logic, updated how du was computed and thus
% changed what dus was returned by ddp().
%
% params:
% x0 - initial state
% us - m-N matrix with discrete controls
% S - problem data:
% S.L : handle to the cost function
% S.f : handle to the discrete dynamics functions
% S.mu : regularizing constant (default is 0)
% S.a : initial step size (default is 1)
% S.diff : difference function (default is minus, i.e. vector space)
%
% return:
% dus: m-N matrix containing computed optimal change in control
% V: current value function
% Vn: new value function
% dV: predicted change in value function
% a: computed step-size along control search direction
%
%
% Note: this implementation is most closely related to second-order
% metehods known as stage-wise Newton (SN) - Bertsekas, 2003
% and differential-dynamic-programming (DDP), Mayne, 1966.
% In this implementation second-order terms in the dynamics
% are ignored which corresponds to the linear-quadratic-subproblem
% (LQS) approach (see also iterative-LQR (Todorov et al)).
%
% Disclaimer: the code is for education purposes only
%
% Author: Marin Kobilarov marin(at)jhu.edu
if ~isfield(S, 'diff')
S.diff = @diff_def;
end
if ~isfield(S, 'mu')
S.mu = 0;
end
if ~isfield(S, 'mu0')
S.mu0 = 1e-3;
end
if ~isfield(S, 'dmu0')
S.dmu0 = 2;
end
if ~isfield(S, 'mumax')
S.mumax = 1e6;
end
if ~isfield(S, 'a')
S.a = 1;
end
if ~isfield(S, 'amin')
S.amin = 1e-32;
end
if ~isfield(S, 'n')
S.n = length(x0);
end
if ~isfield(S, 'info')
S.info = 0;
end
n = S.n;
m = size(us, 1);
N = size(us, 2);
Ps = zeros(n,n,N+1);
vs = zeros(n,N+1);
cs = zeros(m,N);
Ds = zeros(m,n,N);
dus = zeros(size(us));
% integrate trajectory and get terminal cost
xs = ddp_traj(x0, us, S);
[L, Lx, Lxx, Lu, Luu] = S.L(N+1, xs(:,end), [], S);
% initialize
V = L;
v = Lx;
P = Lxx;
dV = [0; 0];
Ps(:,:,N+1) = P;
vs(:,N+1) = v;
for k=N:-1:1,
x = xs(:,k);
u = us(:,k);
[xn, A, B] = S.f(k, x, u, S);
if isempty(A) || isempty(B)
[A, B] = fd(S.f, k, x, u, S, 1e-6);
end
[L, Lx, Lxx, Lu, Luu] = S.L(k, x, u, S);
V = V + L;
Qx = Lx + A'*v;
Qu = Lu + B'*v;
Qxx = Lxx + A'*P*A;
Quu = Luu + B'*P*B;
Qux = B'*P*A;
mu = S.mu;
dmu = 1;
while 1
Quum = Quu + mu*eye(m);
[F, d] = chol(Quum);
if d == 0
% this is the standard quadratic rule specified by Tassa and Todorov
dmu = min(1/S.dmu0, dmu/S.dmu0);
if (mu*dmu > S.mu0)
mu = mu*dmu;
else
mu = S.mu0;
end
if S.info
disp(['[I] Ddp::Backward: reduced mu=' num2str(mu) ' at k=' num2str(k)]);
end
break;
end
dmu = max(S.dmu0, dmu*S.dmu0);
mu = max(S.mu0, mu*dmu);
if S.info
disp(['[I] Ddp::Backward: increased mu=' num2str(mu) ' at k=' num2str(k)]);
end
if (mu > S.mumax)
disp(['[W] Ddp::Backward: mu= ' num2str(mu) 'exceeded maximum ']);
break;
end
end
if (mu > S.mumax)
break;
end
% control law is du = c + D*dx
cD = -F\(F'\[Qu, Qux]);
c = cD(:, 1);
D = cD(:, 2:end);
v = Qx + D'*Qu;
P = Qxx + D'*Qux;
dV = dV + [c'*Qu; c'*Quu*c/2];
vs(:, k) = v;
Ps(:, :, k) = P;
cs(:, k) = c;
Ds(:, :, k) = D;
end
s1 = .1;
s2 = .5;
b1 = .25;
b2 = 2;
a = S.a;
% measured change in V
dVm = eps;
while dVm > 0
% variation
dx = zeros(n, 1);
% varied x
xn = x0;
% new measured cost
Vn = 0;
for k=1:N,
u = us(:,k);
c = cs(:,k);
D = Ds(:,:,k);
du = a*c + D*dx;
un = u + du;
tmp = size(un, 1);
if isfield(S, 'umin')
if ~isempty(S.umin)
for i = 1:tmp
if un(i) < S.umin(i)
un(i) = S.umin(i);
du(i) = un(i) - u(i);
end
end
end
end
if isfield(S, 'umax')
if ~isempty(S.umax)
for i = 1:tmp
if un(i) > S.umax(i)
un(i) = S.umax(i);
du(i) = un(i) - u(i);
end
end
end
end
[Ln, Lx, Lxx, Lu, Luu] = S.L(k, xn, un, S);
[xn, A, B] = S.f(k, xn, un, S);
dx = S.diff(xs(:,k+1), xn);
Vn = Vn + Ln;
dus(:,k) = du;
end
[L, Lx, Lxx, Lu, Luu] = S.L(N+1, xn, [], S);
Vn = Vn + L;
dVm = Vn - V;
if dVm > 0
a = b1*a;
if S.info
disp(['[I] Ddp: decreasing a=' num2str(a)])
end
if a < S.amin
break
end
continue
end
dVp = [a; a*a]'*dV;
r = dVm/dVp;
if r < s1
a = b1*a;
else
if r >= s2
a = b2*a;
end
end
if S.info
disp(['[I] ddp: decreasing a=' num2str(a)])
end
end
function dx = diff_def(x, xn)
% default state difference
dx = xn - x;
function [A, B] = fd(func, k, x, u, S, e)
% compute numerically the jacobians A=fx, B=fu of a given function f(k,x,u,S)
f = func(k, x, u, S);
n = length(x);
m = length(u);
En = eye(n);
Em = eye(m);
A = zeros(n, n);
B = zeros(n, m);
for j=1:n,
A(:,j) = (func(k, x + e*En(:,j), u, S) - f)/e;
end
for j=1:m,
B(:,j) = (func(k, x, u + e*Em(:,j), S) - f)/e;
end