1 | function ildp
|
---|
2 |
|
---|
3 | tic
|
---|
4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
5 | %pocatecni konstanty
|
---|
6 |
|
---|
7 | Iterace = 2; %iterace
|
---|
8 | K = 20; %casy
|
---|
9 | N = 50; %vzorky
|
---|
10 |
|
---|
11 | sigma = 1;
|
---|
12 | Sigmas = [[sigma^2 0 0]; [0 0 0]; [0 0 0]];
|
---|
13 |
|
---|
14 | h = 0;
|
---|
15 | hdx = [0; 0; 0];
|
---|
16 | hdxdx = [[0 0 0]; [0 0 0]; [0 0 0]];
|
---|
17 |
|
---|
18 | Rk = ones(1, K);
|
---|
19 | x0 = [0; 0; 1];
|
---|
20 |
|
---|
21 | %velikost okoli pro lokalni metodu
|
---|
22 | rho = 5;
|
---|
23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
24 | %globalni promenne
|
---|
25 |
|
---|
26 | Kpi = ones(4, K);
|
---|
27 | Kpi(4, :) = zeros(1, K);
|
---|
28 |
|
---|
29 | Wv = zeros(10, K);
|
---|
30 |
|
---|
31 | Xkn = zeros(3, K, N);
|
---|
32 | Xstripe = zeros(3, K);
|
---|
33 |
|
---|
34 | gka = 0;
|
---|
35 | gnu = 0;
|
---|
36 |
|
---|
37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
38 | %iteracni smycka
|
---|
39 | % clf reset
|
---|
40 |
|
---|
41 | % PtMin = 0.0001;
|
---|
42 |
|
---|
43 | % UU=zeros(K,N);
|
---|
44 | for i = 1:Iterace,
|
---|
45 | %generovani stavu
|
---|
46 | % hold off
|
---|
47 | for n = 1:N,
|
---|
48 | Xkn(:, 1, n) = x0 + [sigma*randn(); 0; 0];
|
---|
49 | for k = 1:K-1,
|
---|
50 | Uk = uPi(k, Xkn(:, k, n));
|
---|
51 | % UU(k,n) = Uk;
|
---|
52 | Kk = Uk*Xkn(3, k, n)/(Uk^2*Xkn(3, k, n) + sigma^2);
|
---|
53 | Xkn(1, k+1, n) = Xkn(1, k, n) + Xkn(2, k, n)*Uk + sigma*randn();
|
---|
54 | Xkn(2, k+1, n) = Xkn(2, k, n) + Kk*(Xkn(1, k+1, n) - Xkn(1, k, n) - Xkn(2, k, n)*Uk);
|
---|
55 | Xkn(3, k+1, n) = (1 - Kk*Uk)*Xkn(3, k, n);
|
---|
56 | end
|
---|
57 | % hold all
|
---|
58 | % subplot(4,1,1);
|
---|
59 | % plot(1:K,Xkn(1,:,n))
|
---|
60 | % hold all
|
---|
61 | % subplot(4,1,2);
|
---|
62 | % plot(1:K,Xkn(2,:,n))
|
---|
63 | % hold all
|
---|
64 | % subplot(4,1,3);
|
---|
65 | % plot(1:K,Xkn(3,:,n))
|
---|
66 | % hold all
|
---|
67 | % subplot(4,1,4);
|
---|
68 | % plot(1:K,UU(:,n))
|
---|
69 |
|
---|
70 | end
|
---|
71 | Xstripe = mean(Xkn, 3);
|
---|
72 | % hold all
|
---|
73 | % subplot(4,1,1);
|
---|
74 | % plot(1:K,Xstripe(1,:),'-ro')
|
---|
75 | % hold all
|
---|
76 | % subplot(4,1,2);
|
---|
77 | % plot(1:K,Xstripe(2,:),'-ro')
|
---|
78 | % hold all
|
---|
79 | % subplot(4,1,3);
|
---|
80 | % plot(1:K,Xstripe(3,:),'-ro')
|
---|
81 |
|
---|
82 | %hold off
|
---|
83 | % Epsl = randn(3,N);
|
---|
84 | %iterace pro k = K-1..1
|
---|
85 | for k = K-1:-1:1,
|
---|
86 | gka = k;
|
---|
87 | % 1]
|
---|
88 | for n = 1:N,
|
---|
89 | %krive okoli
|
---|
90 | Xkn(1, k, n) = Xstripe(1, k) + rho*randn();
|
---|
91 | Xkn(2, k, n) = Xstripe(2, k) + rho*randn();
|
---|
92 | Xkn(3, k, n) = Xstripe(3, k)*rho*exp(randn());
|
---|
93 | % %rovne okoli
|
---|
94 | % Xkn(1, k, n) = Xstripe(1, k) + Epsl(1,n);
|
---|
95 | % Xkn(2, k, n) = Xstripe(2, k) + Epsl(2,n);
|
---|
96 | % Xkn(3, k, n) = Xstripe(3, k)*exp(Epsl(3,n));
|
---|
97 | end
|
---|
98 | % Xkn(:,k,:)
|
---|
99 |
|
---|
100 | % 2]
|
---|
101 | for n = 1:N,
|
---|
102 | gnu = n;
|
---|
103 | [Uopt(n), Hmin(n)] = fminunc(@Hamilt, uPi(k, Xkn(:, k, n)), optimset('GradObj','on','Display','notify'));
|
---|
104 | % [i, k, n]
|
---|
105 | %
|
---|
106 | % interv = -1000:1:1000;
|
---|
107 | % for ll = 1:2001,
|
---|
108 | % hodnot(ll) = Hamilt(interv(ll));
|
---|
109 | % end
|
---|
110 | % plot(interv, hodnot,'-b',Uopt(n),Hmin(n),'rs',uPi(k, Xkn(:, k, n)),Hamilt(uPi(k, Xkn(:, k, n))),'go')
|
---|
111 | % prah = 100;
|
---|
112 | % if(Uopt(n) > prah)
|
---|
113 | % Uopt(n) = prah;
|
---|
114 | % Hmin(n) = Hamilt(prah);
|
---|
115 | % disp('u > horni mez');
|
---|
116 | % elseif(Uopt(n) < -prah)
|
---|
117 | % Uopt(n) = -prah;
|
---|
118 | % Hmin(n) = Hamilt(-prah);
|
---|
119 | % disp('u < dolni mez');
|
---|
120 | % end
|
---|
121 | % if(extfl < 1)
|
---|
122 | % disp('exitflag < 1')
|
---|
123 | % end
|
---|
124 | end
|
---|
125 | % 3]
|
---|
126 | for n = 1:N, % V??? nema to byt k+1? ale asi ne
|
---|
127 | Vn(n) = Hmin(n) + Vtilde(k+1, Xkn(:, k, n));
|
---|
128 | % xxx(n) = Xkn(1,k,n);
|
---|
129 | % xxy(n) = Xkn(2,k,n);
|
---|
130 | % xxz(n) = Xkn(3,k,n);
|
---|
131 | % Vn(n) = xxx(n).*xxx(n).*xxx(n);
|
---|
132 | end
|
---|
133 | % xxx2 = sort(xxx);
|
---|
134 | % xxy2 = sort(xxy);
|
---|
135 | % xxz2 = sort(xxz);
|
---|
136 |
|
---|
137 | % 4]
|
---|
138 | Epsilon = zeros(3, N);
|
---|
139 | for n = 1:N,
|
---|
140 | Epsilon(:, n) = Xkn(:, k, n) - Xstripe(:, k);
|
---|
141 | end
|
---|
142 | mFi = matrixFi(Epsilon);
|
---|
143 | FiFiTInvFi = (mFi*mFi')\mFi;
|
---|
144 | Wv(:,k) = FiFiTInvFi * Vn';
|
---|
145 | % Wv = zeros(10,1);
|
---|
146 | % Wv(1, k) = Wvtmp(1);
|
---|
147 | % Wv(2, k) = Wvtmp(2);
|
---|
148 | % Wv(3, k) = Wvtmp(3);
|
---|
149 | % Wv(4, k) = Wvtmp(4);
|
---|
150 | % FiFiTInvFi = (mFi'*mFi)\mFi';
|
---|
151 | % Wv(:, k) = FiFiTInvFi' * Vn';
|
---|
152 | % UU(k,:) = Uopt;
|
---|
153 | for n = 1:N,
|
---|
154 | yt(n) = Xkn(1, k, n);
|
---|
155 | bt(n) = Xkn(2, k, n);
|
---|
156 | pt(n) = Xkn(3, k, n);
|
---|
157 | end
|
---|
158 | mPsi = [yt',...
|
---|
159 | bt'.*Uopt',...
|
---|
160 | pt'.*Uopt',...
|
---|
161 | Uopt'];
|
---|
162 | PsiPsiTInvPsi = (mPsi'*mPsi)\mPsi';
|
---|
163 | Kpi(:,k) = PsiPsiTInvPsi * (Rk(k)*ones(N,1));
|
---|
164 |
|
---|
165 | % for nn=1:N,
|
---|
166 | % vlv(nn) = Vtilde(k, [xxx2(nn);xxy2(nn);xxz2(nn)]);
|
---|
167 | % end
|
---|
168 | % subplot(3,1,1);
|
---|
169 | % plot(xxx,Vn,'rs',xxx2,vlv,'-b')
|
---|
170 | % subplot(3,1,2);
|
---|
171 | % plot(xxy,Vn,'rs',xxy2,vlv,'-b')
|
---|
172 | % subplot(3,1,3);
|
---|
173 | % plot(xxz,Vn,'rs',xxz2,vlv,'-b')
|
---|
174 | end
|
---|
175 | % clf reset
|
---|
176 | % for n=1:N,
|
---|
177 | %
|
---|
178 | % hold all
|
---|
179 | % subplot(4,1,1);
|
---|
180 | % plot(1:K,Xkn(1,:,n),'-b')
|
---|
181 | % hold all
|
---|
182 | % subplot(4,1,2);
|
---|
183 | % plot(1:K,Xkn(2,:,n),'-b')
|
---|
184 | % hold all
|
---|
185 | % subplot(4,1,3);
|
---|
186 | % plot(1:K,Xkn(3,:,n),'-b')
|
---|
187 | % hold all
|
---|
188 | % % subplot(4,1,4);
|
---|
189 | % % plot(1:K,UU(:,n),'-b')
|
---|
190 | % end
|
---|
191 | end
|
---|
192 | %%%%%%%%%%%
|
---|
193 | toc
|
---|
194 |
|
---|
195 | Kpi
|
---|
196 | %graficky vystup
|
---|
197 |
|
---|
198 | X = zeros(3, K);
|
---|
199 | UU = zeros(1,K);
|
---|
200 |
|
---|
201 | X(:,1) = x0 + [sigma*randn(); 0; 0];
|
---|
202 | for k = 1:K-1,
|
---|
203 | Upi = uPi(k, X);
|
---|
204 | UU(k) = Upi;
|
---|
205 | Ktmp = Upi*X(3,k)/(Upi^2*X(3,k) + sigma^2);
|
---|
206 | X(1,k+1) = X(1,k)+X(2,k)*Upi + sigma*randn();
|
---|
207 | X(2,k+1) = X(2,k) + Ktmp*(X(1,k+1) - X(1,k) - X(2,k)*Upi);
|
---|
208 | X(3,k+1) = X(3,k)*(1-Ktmp*Upi);
|
---|
209 | end
|
---|
210 | % X
|
---|
211 | % hold off
|
---|
212 | subplot(4,1,1);
|
---|
213 | plot(1:K,X(1,:),'-gs')
|
---|
214 | % hold off
|
---|
215 | subplot(4,1,2);
|
---|
216 | plot(1:K,X(2,:),'-gs')
|
---|
217 | % hold off
|
---|
218 | subplot(4,1,3);
|
---|
219 | plot(1:K,X(3,:),'-gs')
|
---|
220 | % hold off
|
---|
221 | subplot(4,1,4);
|
---|
222 | plot(1:K,UU,'-gs')
|
---|
223 | % X = zeros(1, K);
|
---|
224 | % b = 0;
|
---|
225 | % UU = zeros(1,K);
|
---|
226 | %
|
---|
227 | % X(1) = sigma*randn();
|
---|
228 | % for k = 1:K-1,
|
---|
229 | % Upi = uPi(k, [X,b,0]);
|
---|
230 | % UU(k) = Upi;
|
---|
231 | % X(k+1) = X(k) + b*Upi + sigma*randn();
|
---|
232 | % end
|
---|
233 | %
|
---|
234 | % % hold off
|
---|
235 | % subplot(2,1,1);
|
---|
236 | % plot(1:K,X,'-gs')
|
---|
237 | % % hold off
|
---|
238 | % subplot(2,1,2);
|
---|
239 | % plot(1:K,UU,'-gs')
|
---|
240 |
|
---|
241 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
242 | %pomocne funkce
|
---|
243 |
|
---|
244 | function [val_uPi] = uPi(k_uPi, x_uPi)
|
---|
245 | val_uPi = (Rk(k_uPi) - Kpi(1, k_uPi)*x_uPi(1))/(Kpi(2, k_uPi)*x_uPi(2) + Kpi(3, k_uPi)*x_uPi(3) + Kpi(4, k_uPi));
|
---|
246 | end
|
---|
247 |
|
---|
248 | function [val_ham, val_grad] = Hamilt(u_ham)
|
---|
249 | % Vtddx = Vtilde_dx(gka+1, Xkn(:, gka, gnu));
|
---|
250 | val_ham = (Xkn(1, gka, gnu) + Xkn(2, gka, gnu)*u_ham - Rk(gka+1))^2 + Xkn(3, gka, gnu)*u_ham^2 ... + sigma^2 ... %ztrata l
|
---|
251 | + [Xkn(1, gka, gnu) + Xkn(2, gka, gnu)*u_ham; ...
|
---|
252 | Xkn(2, gka, gnu) + Xkn(3, gka, gnu)*u_ham*(Xkn(1, gka+1, gnu) - Xkn(1, gka, gnu) - Xkn(2, gka, gnu)*u_ham)/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2); ...
|
---|
253 | Xkn(3, gka, gnu) - Xkn(3, gka, gnu)^2*u_ham^2/(Xkn(3, gka, gnu)*u_ham^2 + sigma^2)]' ... %fce f
|
---|
254 | *Vtilde_dx(gka+1, Xkn(:, gka, gnu)) ...
|
---|
255 | + Wv(5, gka+1)*sigma;
|
---|
256 |
|
---|
257 | val_grad = 2*(Xkn(1, gka, gnu) + Xkn(2, gka, gnu)*u_ham - Rk(gka+1))*Xkn(2, gka, gnu) + 2*Xkn(3, gka, gnu)*u_ham ... %ztrata du
|
---|
258 | + [Xkn(2, gka, gnu);...
|
---|
259 | (2*u_ham^2*Xkn(3, gka, gnu)^2*(Xkn(1, gka, gnu) - Xkn(1, gka+1, gnu) + u_ham*Xkn(2, gka, gnu)))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))^2 - (u_ham*Xkn(2, gka, gnu)*Xkn(3, gka, gnu))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu)) - (Xkn(3, gka, gnu)*(Xkn(1, gka, gnu) - Xkn(1, gka+1, gnu) + u_ham*Xkn(2, gka, gnu)))/(sigma^2 + u_ham^2*Xkn(3, gka, gnu));...
|
---|
260 | (2*u_ham^3*Xkn(3, gka, gnu)^3)/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))^2 - (2*u_ham*Xkn(3, gka, gnu)^2)/(sigma^2 + u_ham^2*Xkn(3, gka, gnu))]' ... %fce f du
|
---|
261 | * Vtilde_dx(gka+1, Xkn(:, gka, gnu)); %derivace Bellman fce
|
---|
262 | end
|
---|
263 |
|
---|
264 | function [val_Vt] = Vtilde(k_Vt, x_Vt)
|
---|
265 | if(k_Vt == K)
|
---|
266 | val_Vt = h;
|
---|
267 | else
|
---|
268 | val_Vt = vectFi(x_Vt - Xstripe(:, k_Vt))' * Wv(:,k_Vt);
|
---|
269 | end
|
---|
270 | end
|
---|
271 |
|
---|
272 | function [val_Vt] = Vtilde_dx(k_Vt, x_Vt)
|
---|
273 | if(k_Vt == K)
|
---|
274 | val_Vt = hdx;
|
---|
275 | else
|
---|
276 | val_Vt = difFi(x_Vt - Xstripe(:, k_Vt))' * Wv(:,k_Vt);
|
---|
277 | end
|
---|
278 | end
|
---|
279 |
|
---|
280 | % function [val_Vt] = Vtilde_dx_dx(k_Vt, x_Vt)
|
---|
281 | % if(k_Vt == K)
|
---|
282 | % val_Vt = hdxdx;
|
---|
283 | % else
|
---|
284 | % val_Vt = zeros(3,3);
|
---|
285 | % val_Vt(1,1) = 2*Wv(5,k_Vt);
|
---|
286 | % val_Vt(2,2) = 2*Wv(8,k_Vt);
|
---|
287 | % val_Vt(3,3) = 2*Wv(10,k_Vt);
|
---|
288 | %
|
---|
289 | % val_Vt(1,2) = Wv(6,k_Vt);
|
---|
290 | % val_Vt(1,3) = Wv(7,k_Vt);
|
---|
291 | % val_Vt(2,3) = Wv(9,k_Vt);
|
---|
292 | %
|
---|
293 | % val_Vt(2,1) = val_Vt(1,2);
|
---|
294 | % val_Vt(3,1) = val_Vt(1,3);
|
---|
295 | % val_Vt(3,2) = val_Vt(2,3);
|
---|
296 | % end
|
---|
297 | % end
|
---|
298 |
|
---|
299 | % function [val_Vt] = trSgVt(k_Vt)
|
---|
300 | % if(k_Vt == K)
|
---|
301 | % val_Vt = 0;
|
---|
302 | % else
|
---|
303 | % val_Vt = 2*Wv(5,k_Vt)*sigma;
|
---|
304 | % end
|
---|
305 | % end
|
---|
306 |
|
---|
307 | function [val_Fi] = vectFi(x_Fi)
|
---|
308 | val_Fi = [ ...
|
---|
309 | 1; ...
|
---|
310 | x_Fi(1); ...
|
---|
311 | x_Fi(2); ...
|
---|
312 | x_Fi(3); ...
|
---|
313 | x_Fi(1)^2; ...
|
---|
314 | x_Fi(1)*x_Fi(2); ...
|
---|
315 | x_Fi(1)*x_Fi(3); ...
|
---|
316 | x_Fi(2)^2; ...
|
---|
317 | x_Fi(2)*x_Fi(3); ...
|
---|
318 | x_Fi(3)^2; ...
|
---|
319 | ];
|
---|
320 | end
|
---|
321 |
|
---|
322 | function [val_Fi] = matrixFi(x_Fi)
|
---|
323 | val_Fi = [ ...
|
---|
324 | ones(1, N); ...
|
---|
325 | x_Fi(1, :); ...
|
---|
326 | x_Fi(2, :); ...
|
---|
327 | x_Fi(3, :); ...
|
---|
328 | x_Fi(1, :).^2; ...
|
---|
329 | x_Fi(1, :).*x_Fi(2, :); ...
|
---|
330 | x_Fi(1, :).*x_Fi(3, :); ...
|
---|
331 | x_Fi(2, :).^2; ...
|
---|
332 | x_Fi(2, :).*x_Fi(3, :); ...
|
---|
333 | x_Fi(3, :).^2; ...
|
---|
334 | ];
|
---|
335 | % val_Fi = [ ...
|
---|
336 | % ones(1, N); ...
|
---|
337 | % x_Fi(1, :); ...
|
---|
338 | % x_Fi(2, :); ...
|
---|
339 | % x_Fi(3, :); ...
|
---|
340 | % ];
|
---|
341 | end
|
---|
342 |
|
---|
343 | function [val_Fi] = difFi(x_Fi)
|
---|
344 | val_Fi = [ ...
|
---|
345 | 0 0 0; ...
|
---|
346 | 1 0 0; ...
|
---|
347 | 0 1 0; ...
|
---|
348 | 0 0 1; ...
|
---|
349 | 2*x_Fi(1) 0 0; ...
|
---|
350 | x_Fi(2) x_Fi(1) 0; ...
|
---|
351 | x_Fi(3) 0 x_Fi(1); ...
|
---|
352 | 0 2*x_Fi(2) 0; ...
|
---|
353 | 0 x_Fi(3) x_Fi(2); ...
|
---|
354 | 0 0 2*x_Fi(3); ...
|
---|
355 | ];
|
---|
356 | end
|
---|
357 | end |
---|