1 | #define BDMLIB |
---|
2 | #include "../mat_checks.h" |
---|
3 | #include "design/lq_ctrl.h" |
---|
4 | |
---|
5 | using namespace bdm; |
---|
6 | |
---|
7 | TEST ( LQG_test ) { |
---|
8 | LQG reg; |
---|
9 | shared_ptr<StateSpace<chmat> > stsp = new StateSpace<chmat>; |
---|
10 | // 2 x 1 x 1 |
---|
11 | stsp-> set_parameters ( eye ( 2 ), ones ( 2, 1 ), ones ( 1, 2 ), ones ( 1, 1 ), /* Q,R */ eye ( 2 ), eye ( 1 ) ); |
---|
12 | reg.set_system ( stsp ); // A, B, C |
---|
13 | reg.set_control_parameters ( eye ( 1 ), eye ( 1 ), vec_1 ( 1.0 ), 6 ); //Qy, Qu, horizon |
---|
14 | reg.validate(); |
---|
15 | |
---|
16 | reg.redesign(); |
---|
17 | double reg_apply = reg.ctrlaction ( "0.5, 1.1", "0.0" ) ( 0 ); /*convert vec to double*/ |
---|
18 | CHECK_CLOSE ( reg_apply, -0.248528137234392, 0.0001 ); |
---|
19 | } |
---|
20 | |
---|
21 | TEST ( to_state_test ) { |
---|
22 | mlnorm<fsqmat> ml; |
---|
23 | mat A = "1.1, 2.3"; |
---|
24 | ml.set_parameters ( A, vec_1 ( 1.3 ), eye ( 1 ) ); |
---|
25 | RV yr = RV ( "y", 1 ); |
---|
26 | RV ur = RV ( "u", 1 ); |
---|
27 | ml.set_rv ( yr ); |
---|
28 | yr.t_plus ( -1 ); |
---|
29 | ml.set_rvc ( concat ( yr, ur ) ); |
---|
30 | |
---|
31 | shared_ptr<StateCanonical > Stsp = new StateCanonical; |
---|
32 | Stsp->connect_mlnorm ( ml ); |
---|
33 | |
---|
34 | /* results from |
---|
35 | [A,B,C,D]=tf2ss([2.3 0],[1 -1.1]) |
---|
36 | */ |
---|
37 | CHECK_CLOSE_EX ( Stsp->_A().get_row ( 0 ), vec ( "1.1" ), 0.0001 ); |
---|
38 | CHECK_CLOSE_EX ( Stsp->_C().get_row ( 0 ), vec ( "2.53" ), 0.0001 ); |
---|
39 | CHECK_CLOSE_EX ( Stsp->_D().get_row ( 0 ), vec ( "2.30" ), 0.0001 ); |
---|
40 | } |
---|
41 | |
---|
42 | TEST ( to_state_arx_test ) { |
---|
43 | mlnorm<chmat> ml; |
---|
44 | mat A = "1.1, 2.3, 3.4"; |
---|
45 | ml.set_parameters ( A, vec_1 ( 1.3 ), eye ( 1 ) ); |
---|
46 | RV yr = RV ( "y", 1 ); |
---|
47 | RV ur = RV ( "u", 1 ); |
---|
48 | ml.set_rv ( yr ); |
---|
49 | ml.set_rvc ( concat ( yr.copy_t ( -1 ), concat ( ur, ur.copy_t ( -1 ) ) ) ); |
---|
50 | |
---|
51 | shared_ptr<StateFromARX> Stsp = new StateFromARX; |
---|
52 | RV xrv; |
---|
53 | RV urv; |
---|
54 | Stsp->connect_mlnorm ( ml, xrv, urv ); |
---|
55 | |
---|
56 | /* results from |
---|
57 | [A,B,C,D]=tf2ss([2.3 0],[1 -1.1]) |
---|
58 | */ |
---|
59 | CHECK_CLOSE_EX ( Stsp->_A().get_row ( 0 ), vec ( "1.1, 3.4, 1.3" ), 0.0001 ); |
---|
60 | CHECK_CLOSE_EX ( Stsp->_B().get_row ( 0 ), vec ( "2.3" ), 0.0001 ); |
---|
61 | CHECK_CLOSE_EX ( Stsp->_C().get_row ( 0 ), vec ( "1, 0, 0" ), 0.0001 ); |
---|
62 | } |
---|
63 | |
---|
64 | TEST ( arx_LQG_test ) { |
---|
65 | mlnorm<chmat> ml; |
---|
66 | mat A = "1.81, -.81, .00468, .00438"; |
---|
67 | ml.set_parameters ( A, vec_1 ( 0.0 ), 0.00001*eye ( 1 ) ); |
---|
68 | RV yr = RV ( "y", 1 ); |
---|
69 | RV ur = RV ( "u", 1 ); |
---|
70 | RV rgr = yr.copy_t ( -1 ); |
---|
71 | rgr.add ( yr.copy_t ( -2 ) ); |
---|
72 | rgr.add ( yr.copy_t ( -2 ) ); |
---|
73 | rgr.add ( ur.copy_t ( -1 ) ); |
---|
74 | rgr.add ( ur ); |
---|
75 | |
---|
76 | ml.set_rv ( yr ); |
---|
77 | ml.set_rvc ( rgr ); |
---|
78 | ml.validate(); |
---|
79 | |
---|
80 | shared_ptr<StateFromARX> Stsp = new StateFromARX; |
---|
81 | RV xrv; |
---|
82 | RV urv; |
---|
83 | Stsp->connect_mlnorm ( ml, xrv, urv ); |
---|
84 | |
---|
85 | LQG L; |
---|
86 | L.set_system ( Stsp ); |
---|
87 | L.set_control_parameters ( eye ( 1 ), sqrt ( 1.0 / 1000 ) *eye ( 1 ), vec_1 ( 0.0 ), 100 ); |
---|
88 | L.validate(); |
---|
89 | |
---|
90 | L.redesign(); |
---|
91 | cout << L.to_string() << endl; |
---|
92 | } |
---|
93 | |
---|
94 | TEST (quadratic_test){ |
---|
95 | /*quadraticfn qf; |
---|
96 | qf.Q = chmat(2); |
---|
97 | qf.Q._Ch() = mat("1 -1 0; 0 0 0; 0 0 0"); |
---|
98 | CHECK_CLOSE_EX(qf.eval(vec("1 2")), vec_1(1.0), 0.0000000000000001); |
---|
99 | |
---|
100 | LQG_universal lq; |
---|
101 | lq.Losses = Array<quadraticfn>(1); |
---|
102 | lq.Losses(0) = quadraticfn(); |
---|
103 | lq.Losses(0).Q._Ch() = mat("1 -1 0; 0 0 0; 0 0 0"); |
---|
104 | lq.Losses(0).rv = RV("{u up }"); |
---|
105 | |
---|
106 | lq.Models = Array<linfnEx>(1); |
---|
107 | lq.Models(0) = linfnEx(mat("1"),vec("1")); |
---|
108 | lq.Models(0).rv = RV("{x }"); |
---|
109 | |
---|
110 | lq.rv = RV("u",1); |
---|
111 | |
---|
112 | lq.redesign(); |
---|
113 | CHECK_CLOSE_EX(lq.ctrlaction(vec("1,0")), vec("1.24, -5.6"), 0.0000001);*/ |
---|
114 | } |
---|
115 | |
---|
116 | TEST (LQGU_static_vs_Riccati_test){ |
---|
117 | //test of universal LQG controller |
---|
118 | LQG_universal lq; |
---|
119 | lq.rv = RV("u", 2, 0); |
---|
120 | lq.set_rvc(RV("x", 2, 0)); |
---|
121 | lq.horizon = 10; |
---|
122 | |
---|
123 | /* |
---|
124 | model: x = Ax + Bu time: 0..horizon |
---|
125 | loss: l = x'Q'Qx + u'R'Ru time: 0..horizon-1 |
---|
126 | final loss: l = x'S'Sx time: horizon |
---|
127 | |
---|
128 | dim: x: 2 |
---|
129 | u: 2 |
---|
130 | |
---|
131 | A = [ 2 -1 ] |
---|
132 | [ 0 0.5 ] |
---|
133 | |
---|
134 | B = [ 1 -0.1 ] |
---|
135 | [ -0.2 2 ] |
---|
136 | |
---|
137 | Q = [ 5 0 ] |
---|
138 | [ 0 1 ] |
---|
139 | |
---|
140 | R = [ 0.01 0 ] |
---|
141 | [ 0 0.1 ] |
---|
142 | |
---|
143 | S = Q |
---|
144 | */ |
---|
145 | |
---|
146 | mat mA("2 -1;0 0.5"); |
---|
147 | mat mB("1 -0.1;-0.2 2"); |
---|
148 | mat mQ("5 0;0 1"); |
---|
149 | mat mR("0.01 0;0 0.1"); |
---|
150 | mat mS = mQ; |
---|
151 | |
---|
152 | //starting configuration |
---|
153 | vec x0("6 3"); |
---|
154 | |
---|
155 | /*cout << "configuration:" << endl |
---|
156 | << "mA:" << endl << mA << endl |
---|
157 | << "mB:" << endl << mB << endl |
---|
158 | << "mQ:" << endl << mQ << endl |
---|
159 | << "mR:" << endl << mR << endl |
---|
160 | << "mS:" << endl << mS << endl |
---|
161 | << "x0:" << endl << x0 << endl;*/ |
---|
162 | |
---|
163 | //model |
---|
164 | Array<linfnEx> model(2); |
---|
165 | //model Ax part |
---|
166 | model(0).A = mA; |
---|
167 | model(0).B = vec("0 0"); |
---|
168 | model(0).rv = RV("x", 2, 0); |
---|
169 | model(0).rv_ret = RV("x", 2, 1); |
---|
170 | //model Bu part |
---|
171 | model(1).A = mB; |
---|
172 | model(1).B = vec("0 0"); |
---|
173 | model(1).rv = RV("u", 2, 0); |
---|
174 | model(1).rv_ret = RV("x", 2, 1); |
---|
175 | //setup |
---|
176 | lq.Models = model; |
---|
177 | |
---|
178 | //loss |
---|
179 | Array<quadraticfn> loss(2); |
---|
180 | //loss x'Qx part |
---|
181 | loss(0).Q.setCh(mQ); |
---|
182 | loss(0).rv = RV("x", 2, 0); |
---|
183 | //loss u'Ru part |
---|
184 | loss(1).Q.setCh(mR); |
---|
185 | loss(1).rv = RV("u", 2, 0); |
---|
186 | //setup |
---|
187 | lq.Losses = loss; |
---|
188 | |
---|
189 | //finalloss setup |
---|
190 | lq.finalLoss.Q.setCh(mS); |
---|
191 | lq.finalLoss.rv = RV("x", 2, 1); |
---|
192 | |
---|
193 | lq.validate(); |
---|
194 | |
---|
195 | //default L |
---|
196 | //cout << "default L matrix:" << endl << lq.getL() << endl; |
---|
197 | |
---|
198 | //produce last control matrix L |
---|
199 | lq.redesign(); |
---|
200 | |
---|
201 | //verification via Riccati LQG version |
---|
202 | mat mK = mS; |
---|
203 | mat mL = - inv(mR + mB.transpose() * mK * mB) * mB.transpose() * mK * mA; |
---|
204 | |
---|
205 | //cout << "L matrix LQG_universal:" << endl << lq.getL() << endl << |
---|
206 | //"L matrix LQG Riccati:" << endl << mL << endl; |
---|
207 | |
---|
208 | //checking L matrix (universal vs Riccati), tolerance is high, but L is not main criterion |
---|
209 | //more important is reached loss compared in the next part |
---|
210 | double tolerr = 1;//0.01; //0.01 OK x 0.001 NO OK |
---|
211 | |
---|
212 | //check last time L matrix |
---|
213 | CHECK_CLOSE_EX(lq.getL().get_cols(0,1), mL, tolerr); |
---|
214 | |
---|
215 | mat oldK; |
---|
216 | int i; |
---|
217 | |
---|
218 | //produce next control matrix L |
---|
219 | for(i = 0; i < lq.horizon - 1; i++) { |
---|
220 | lq.redesign(); |
---|
221 | oldK = mK; |
---|
222 | mK = mA.transpose() * (oldK - oldK * mB * inv(mR + mB.transpose() * oldK * mB) * mB.transpose() * oldK) * mA + mQ; |
---|
223 | mL = - inv(mR + mB.transpose() * mK * mB) * mB.transpose() * mK * mA; |
---|
224 | |
---|
225 | //cout << "L matrix LQG_universal:" << endl << lq.getL() << endl << |
---|
226 | //"L matrix LQG Riccati:" << endl << mL << endl; |
---|
227 | |
---|
228 | //check other times L matrix |
---|
229 | CHECK_CLOSE_EX(lq.getL().get_cols(0,1), mL, tolerr); |
---|
230 | } |
---|
231 | |
---|
232 | //check losses of LQG control - compare LQG_universal and Riccati version, no noise |
---|
233 | |
---|
234 | //loss of LQG_universal |
---|
235 | /*double*/vec loss_uni("0"); |
---|
236 | |
---|
237 | //setup |
---|
238 | vec x = x0; |
---|
239 | vec xold = x; |
---|
240 | vec u; |
---|
241 | //vec tmploss; |
---|
242 | |
---|
243 | //iteration |
---|
244 | for(i = 0; i < lq.horizon - 1; i++){ |
---|
245 | u = lq.getL().get_cols(0,1) * xold; |
---|
246 | x = mA * xold + mB * u; |
---|
247 | /*tmploss*/loss_uni = x.transpose() * mQ * x + u.transpose() * mR * u; |
---|
248 | //loss_uni += tmploss.get(0); |
---|
249 | xold = x; |
---|
250 | } |
---|
251 | /*tmploss*/ loss_uni = x.transpose() * mS * x; |
---|
252 | //loss_uni += tmploss.get(0); |
---|
253 | |
---|
254 | //loss of LQG Riccati version |
---|
255 | /*double*/ vec loss_rct("0"); |
---|
256 | |
---|
257 | //setup |
---|
258 | x = x0; |
---|
259 | xold = x; |
---|
260 | |
---|
261 | //iteration |
---|
262 | for(i = 0; i < lq.horizon - 1; i++){ |
---|
263 | u = mL * xold; |
---|
264 | x = mA * xold + mB * u; |
---|
265 | /*tmploss*/loss_rct = x.transpose() * mQ * x + u.transpose() * mR * u; |
---|
266 | //loss_rct += tmploss.get(0); |
---|
267 | xold = x; |
---|
268 | } |
---|
269 | /*tmploss*/loss_rct = x.transpose() * mS * x; |
---|
270 | //loss_rct += tmploss.get(0); |
---|
271 | |
---|
272 | //cout << "Loss LQG_universal: " << loss_uni << " vs Loss LQG Riccati: " << loss_rct << endl; |
---|
273 | CHECK_CLOSE_EX(loss_uni, loss_rct, 0.0001); |
---|
274 | } |
---|
275 | |
---|
276 | TEST (LQGU_random_vs_Riccati_test){ |
---|
277 | |
---|
278 | //uniform random generator |
---|
279 | Uniform_RNG urng; |
---|
280 | double maxmult = 10.0; |
---|
281 | int maxxsize = 5; |
---|
282 | int maxusize = 5; |
---|
283 | |
---|
284 | urng.setup(2.0, maxxsize); |
---|
285 | int matxsize = round(urng()); |
---|
286 | urng.setup(2.0, maxusize); |
---|
287 | int matusize = round(urng()); |
---|
288 | |
---|
289 | urng.setup(-maxmult, maxmult); |
---|
290 | |
---|
291 | mat tmpmat; |
---|
292 | |
---|
293 | mat mA; |
---|
294 | mA = urng(matxsize, matxsize); |
---|
295 | mat mB; |
---|
296 | mB = urng(matxsize, matusize); |
---|
297 | mat mQ; |
---|
298 | tmpmat = urng(matxsize, matxsize); |
---|
299 | mQ = tmpmat*tmpmat.transpose(); |
---|
300 | mat mR; |
---|
301 | tmpmat = urng(matusize, matusize); |
---|
302 | mR = tmpmat*tmpmat.transpose(); |
---|
303 | mat mS; |
---|
304 | tmpmat = urng(matxsize, matxsize); |
---|
305 | mS = tmpmat*tmpmat.transpose(); |
---|
306 | |
---|
307 | //starting configuration |
---|
308 | vec x0; |
---|
309 | x0 = urng(matxsize); |
---|
310 | |
---|
311 | /*cout << "configuration:" << endl |
---|
312 | << "x size " << matxsize << " u size " << matusize << endl |
---|
313 | << "mA:" << endl << mA << endl |
---|
314 | << "mB:" << endl << mB << endl |
---|
315 | << "mQ:" << endl << mQ << endl |
---|
316 | << "mR:" << endl << mR << endl |
---|
317 | << "mS:" << endl << mS << endl |
---|
318 | << "x0:" << endl << x0 << endl;*/ |
---|
319 | |
---|
320 | vec zerovecx(matxsize); |
---|
321 | zerovecx.zeros(); |
---|
322 | vec zerovecu(matusize); |
---|
323 | zerovecu.zeros(); |
---|
324 | |
---|
325 | //test of universal LQG controller |
---|
326 | LQG_universal lq; |
---|
327 | lq.rv = RV("u", matusize, 0); |
---|
328 | lq.set_rvc(RV("x", matxsize, 0)); |
---|
329 | lq.horizon = 10; |
---|
330 | |
---|
331 | //model |
---|
332 | Array<linfnEx> model(2); |
---|
333 | //model Ax part |
---|
334 | model(0).A = mA; |
---|
335 | model(0).B = zerovecx; |
---|
336 | model(0).rv = RV("x", matxsize, 0); |
---|
337 | model(0).rv_ret = RV("x", matxsize, 1); |
---|
338 | //model Bu part |
---|
339 | model(1).A = mB; |
---|
340 | model(1).B = zerovecu; |
---|
341 | model(1).rv = RV("u", matusize, 0); |
---|
342 | model(1).rv_ret = RV("x", matxsize, 1); |
---|
343 | //setup |
---|
344 | lq.Models = model; |
---|
345 | |
---|
346 | //loss |
---|
347 | Array<quadraticfn> loss(2); |
---|
348 | //loss x'Qx part |
---|
349 | loss(0).Q.setCh(mQ); |
---|
350 | loss(0).rv = RV("x", matxsize, 0); |
---|
351 | //loss u'Ru part |
---|
352 | loss(1).Q.setCh(mR); |
---|
353 | loss(1).rv = RV("u", matusize, 0); |
---|
354 | //setup |
---|
355 | lq.Losses = loss; |
---|
356 | |
---|
357 | //finalloss setup |
---|
358 | lq.finalLoss.Q.setCh(mS); |
---|
359 | lq.finalLoss.rv = RV("x", matxsize, 1); |
---|
360 | |
---|
361 | lq.validate(); |
---|
362 | |
---|
363 | //produce last control matrix L |
---|
364 | lq.redesign(); |
---|
365 | |
---|
366 | //verification via Riccati LQG version |
---|
367 | mat mK = mS; |
---|
368 | mat mL = - inv(mR + mB.transpose() * mK * mB) * mB.transpose() * mK * mA; |
---|
369 | |
---|
370 | //checking L matrix (universal vs Riccati), tolerance is high, but L is not main criterion |
---|
371 | //more important is reached loss compared in the next part |
---|
372 | double tolerr = 2;//1;//0.01; //0.01 OK x 0.001 NO OK |
---|
373 | |
---|
374 | //check last time L matrix |
---|
375 | CHECK_CLOSE_EX(lq.getL().get_cols(0,(matxsize-1)), mL, tolerr); |
---|
376 | |
---|
377 | mat oldK; |
---|
378 | int i; |
---|
379 | |
---|
380 | //produce next control matrix L |
---|
381 | for(i = 0; i < lq.horizon - 1; i++) { |
---|
382 | lq.redesign(); |
---|
383 | oldK = mK; |
---|
384 | mK = mA.transpose() * (oldK - oldK * mB * inv(mR + mB.transpose() * oldK * mB) * mB.transpose() * oldK) * mA + mQ; |
---|
385 | mL = - inv(mR + mB.transpose() * mK * mB) * mB.transpose() * mK * mA; |
---|
386 | |
---|
387 | //check other times L matrix |
---|
388 | CHECK_CLOSE_EX(lq.getL().get_cols(0,(matxsize-1)), mL, tolerr); |
---|
389 | } |
---|
390 | |
---|
391 | //check losses of LQG control - compare LQG_universal and Riccati version, no noise |
---|
392 | |
---|
393 | //loss of LQG_universal |
---|
394 | vec loss_uni("0"); |
---|
395 | |
---|
396 | //setup |
---|
397 | vec x = x0; |
---|
398 | vec xold = x; |
---|
399 | vec u; |
---|
400 | //vec tmploss; |
---|
401 | |
---|
402 | //iteration |
---|
403 | for(i = 0; i < lq.horizon - 1; i++){ |
---|
404 | u = lq.getL().get_cols(0,(matxsize-1)) * xold; |
---|
405 | x = mA * xold + mB * u; |
---|
406 | loss_uni = x.transpose() * mQ * x + u.transpose() * mR * u; |
---|
407 | xold = x; |
---|
408 | } |
---|
409 | |
---|
410 | loss_uni = x.transpose() * mS * x; |
---|
411 | |
---|
412 | //loss of LQG Riccati version |
---|
413 | vec loss_rct("0"); |
---|
414 | |
---|
415 | //setup |
---|
416 | x = x0; |
---|
417 | xold = x; |
---|
418 | |
---|
419 | //iteration |
---|
420 | for(i = 0; i < lq.horizon - 1; i++){ |
---|
421 | u = mL * xold; |
---|
422 | x = mA * xold + mB * u; |
---|
423 | loss_rct = x.transpose() * mQ * x + u.transpose() * mR * u; |
---|
424 | xold = x; |
---|
425 | } |
---|
426 | loss_rct = x.transpose() * mS * x; |
---|
427 | |
---|
428 | CHECK_CLOSE_EX(loss_uni, loss_rct, 0.0001); |
---|
429 | } |
---|
430 | |
---|
431 | TEST (LQGU_SiSy_test){ |
---|
432 | int K = 5; |
---|
433 | int N = 100; |
---|
434 | double sigma = 0.1; |
---|
435 | double yr = 1; |
---|
436 | double y0 = 0; |
---|
437 | double x0 = y0 - yr; |
---|
438 | double Eb = 1; |
---|
439 | double P; |
---|
440 | //double P = 0.01; //loss ~ 1.0??? e-2 |
---|
441 | //double P = 0.1; //loss ~ 1.??? e-1 |
---|
442 | //double P = 1; //loss ~ ???.??? e+2 |
---|
443 | //double P = 10; //loss ~ ?e+6 |
---|
444 | |
---|
445 | mat A("1"); |
---|
446 | mat B(1,1); |
---|
447 | B(0,0) = Eb; |
---|
448 | mat Q("1"); |
---|
449 | mat R("0"); |
---|
450 | |
---|
451 | vec u(K); |
---|
452 | u.zeros(); |
---|
453 | mat xn(K, N); |
---|
454 | xn.zeros(); |
---|
455 | vec S(K); |
---|
456 | S.zeros(); |
---|
457 | vec L(K); |
---|
458 | L.zeros(); |
---|
459 | |
---|
460 | vec loss(N); |
---|
461 | loss.zeros(); |
---|
462 | |
---|
463 | LQG_universal lq; |
---|
464 | lq.rv = RV("u", 1, 0); |
---|
465 | lq.set_rvc(RV("x", 1, 0)); |
---|
466 | lq.horizon = K; |
---|
467 | |
---|
468 | Array<linfnEx> model(2); |
---|
469 | model(0).A = A; |
---|
470 | model(0).B = vec("0 0"); |
---|
471 | model(0).rv = RV("x", 1, 0); |
---|
472 | model(0).rv_ret = RV("x", 1, 1); |
---|
473 | model(1).A = B; |
---|
474 | model(1).B = vec("0 0"); |
---|
475 | model(1).rv = RV("u", 1, 0); |
---|
476 | model(1).rv_ret = RV("x", 1, 1); |
---|
477 | lq.Models = model; |
---|
478 | |
---|
479 | Array<quadraticfn> qloss(2); |
---|
480 | qloss(0).Q.setCh(Q); |
---|
481 | qloss(0).rv = RV("x", 1, 0); |
---|
482 | qloss(1).Q.setCh(R); |
---|
483 | qloss(1).rv = RV("u", 1, 0); |
---|
484 | lq.Losses = qloss; |
---|
485 | |
---|
486 | lq.finalLoss.Q.setCh(Q); |
---|
487 | lq.finalLoss.rv = RV("x", 1, 1); |
---|
488 | |
---|
489 | lq.validate(); |
---|
490 | |
---|
491 | int n, k; |
---|
492 | double Br; |
---|
493 | //double x00; |
---|
494 | |
---|
495 | for(k = K-1; k >= 0;k--){ |
---|
496 | lq.redesign(); |
---|
497 | L(k) = (lq.getL())(0,0); |
---|
498 | } |
---|
499 | |
---|
500 | for(P = 0.01; P <= 10; (P*=10)){ |
---|
501 | lq.resetTime(); |
---|
502 | |
---|
503 | //cout << "Ls " << L << endl; |
---|
504 | for(n = 0; n < N; n++){ |
---|
505 | Br = Eb + sqrt(P)*randn(); |
---|
506 | |
---|
507 | xn(0, n) = x0 + sigma*randn(); |
---|
508 | for(k = 0; k < K-1; k++){ |
---|
509 | u(k) = L(k)*xn(k, n); |
---|
510 | xn(k+1, n) = (A*xn(k, n))(0,0) + Br*u(k) + sigma*randn(); |
---|
511 | } |
---|
512 | |
---|
513 | loss(n) = 0; |
---|
514 | for(k=0;k<K;k++) loss(n) += xn(k, n)*xn(k, n); |
---|
515 | |
---|
516 | } |
---|
517 | |
---|
518 | /*vec mtraj(K); |
---|
519 | for(k=0;k<K;k++){ |
---|
520 | mtraj(k) = 0; |
---|
521 | for(n = 0; n < N; n++) mtraj(k) += xn(k, n); |
---|
522 | mtraj(k) /= N; |
---|
523 | mtraj(k) += yr; |
---|
524 | } |
---|
525 | cout << "prum trajek " << mtraj << endl;*/ |
---|
526 | //cout << "prum ztrata " << sum(loss)/N << endl; |
---|
527 | //rfloss = mean(loss); |
---|
528 | //rftraj = (mean((xn(1:K, :)), 2) + yr*ones(K, 1))'; |
---|
529 | |
---|
530 | double tolerr; |
---|
531 | //double P = 0.01; //loss ~ 1.0??? e-2 |
---|
532 | //double P = 0.1; //loss ~ 1.??? e-1 |
---|
533 | //double P = 1; //loss ~ ???.??? e+2 |
---|
534 | //double P = 10; //loss ~ ?e+6 |
---|
535 | if(P == 0.01) tolerr = 0.2; |
---|
536 | else if(P == 0.1) tolerr = 2; |
---|
537 | else if(P == 1) tolerr = 2000; |
---|
538 | else if(P == 10) tolerr = 2e+7; |
---|
539 | else tolerr = 0; |
---|
540 | |
---|
541 | CHECK_CLOSE_EX(1.0, sum(loss)/N, tolerr); |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | TEST (LQGU_SiSy_SuSt_test){ |
---|
546 | int K = 5; |
---|
547 | int N = 100; |
---|
548 | double sigma = 0.1; |
---|
549 | double yr = 1; |
---|
550 | double y0 = 0; |
---|
551 | double Eb = 1; |
---|
552 | double inP; |
---|
553 | //double inP = 0.01; //loss ~ 1.0??? e-2 |
---|
554 | //double inP = 0.1; //loss ~ 1.0??? e-2 |
---|
555 | //double inP = 1; //loss ~ 1.0??? e-2 |
---|
556 | //double inP = 10; //loss ~ 1.0??? e-2 |
---|
557 | |
---|
558 | vec x0(3); |
---|
559 | x0(0) = y0 - yr; |
---|
560 | x0(1) = Eb; |
---|
561 | //x0(2) = inP; |
---|
562 | |
---|
563 | mat A("1 0 0; 0 1 0; 0 0 1"); |
---|
564 | mat B("0; 0; 0"); |
---|
565 | mat X("1 0 0; 0 0 0; 0 0 0"); |
---|
566 | mat Y("0.00001"); |
---|
567 | mat Q(3,3); |
---|
568 | Q.zeros(); |
---|
569 | Q(1,1) = sigma*sigma; |
---|
570 | //mat R("0"); |
---|
571 | //mat P(3,3); |
---|
572 | // P.zeros(); |
---|
573 | |
---|
574 | vec u(K); |
---|
575 | u.zeros(); |
---|
576 | vec Kk(K); |
---|
577 | Kk.zeros(); |
---|
578 | mat xn0(K+1, N); |
---|
579 | xn0.zeros(); |
---|
580 | mat xn1(K+1, N); |
---|
581 | xn1.zeros(); |
---|
582 | mat xn2(K+1, N); |
---|
583 | xn2.zeros(); |
---|
584 | //vec S(K); |
---|
585 | // S.zeros(); |
---|
586 | //vec L(K); |
---|
587 | // L.zeros(); |
---|
588 | mat L(1, 3); |
---|
589 | L.zeros(); |
---|
590 | vec loss(N); |
---|
591 | loss.zeros(); |
---|
592 | |
---|
593 | LQG_universal lq; |
---|
594 | lq.rv = RV("u", 1, 0); |
---|
595 | lq.set_rvc(RV("x", 3, 0)); |
---|
596 | lq.horizon = K; |
---|
597 | |
---|
598 | Array<linfnEx> model(2); |
---|
599 | model(0).A = A; |
---|
600 | model(0).B = vec("0 0"); |
---|
601 | model(0).rv = RV("x", 3, 0); |
---|
602 | model(0).rv_ret = RV("x", 3, 1); |
---|
603 | model(1).A = B; |
---|
604 | model(1).B = vec("0 0"); |
---|
605 | model(1).rv = RV("u", 1, 0); |
---|
606 | model(1).rv_ret = RV("x", 3, 1); |
---|
607 | lq.Models = model; |
---|
608 | |
---|
609 | Array<quadraticfn> qloss(2); |
---|
610 | qloss(0).Q.setCh(X); |
---|
611 | qloss(0).rv = RV("x", 3, 0); |
---|
612 | qloss(1).Q.setCh(Y); |
---|
613 | qloss(1).rv = RV("u", 1, 0); |
---|
614 | lq.Losses = qloss; |
---|
615 | |
---|
616 | lq.finalLoss.Q.setCh(X); |
---|
617 | lq.finalLoss.rv = RV("x", 3, 1); |
---|
618 | |
---|
619 | lq.validate(); |
---|
620 | |
---|
621 | int n, k; |
---|
622 | //double Br; |
---|
623 | //double x00; |
---|
624 | |
---|
625 | /*for(k = K-1; k >= 0;k--){ |
---|
626 | lq.redesign(); |
---|
627 | L(k) = (lq.getL())(0,0); |
---|
628 | }*/ |
---|
629 | //cout << "Ls " << L << endl; |
---|
630 | for(inP = 0.01; inP <= 10; (inP*=10)){ |
---|
631 | lq.resetTime(); |
---|
632 | x0(2) = inP; |
---|
633 | for(n = 0; n < N; n++){ |
---|
634 | L.zeros(); |
---|
635 | xn0(0, n) = x0(0); |
---|
636 | xn1(0, n) = x0(1) + sqrt(inP) * randn(); |
---|
637 | xn2(0, n) = x0(2); |
---|
638 | //^neznalost, sum zatim ne |
---|
639 | |
---|
640 | for(k = 0; k < K-1; k++){ |
---|
641 | u(k) = L(0)*xn0(k, n) + L(1)*xn1(k, n) + L(2)*xn2(k, n); |
---|
642 | Kk(k) = u(k)*xn2(k, n)/(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
643 | xn0(k+1, n) = xn0(k, n) + xn1(k, n)*u(k) + sigma*randn(); |
---|
644 | xn1(k+1, n) = xn1(k, n) + Kk(k)*(xn0(k+1, n) - xn0(k, n) - xn1(k, n)*u(k)); |
---|
645 | xn2(k+1, n) = (1 - Kk(k)*u(k))*xn2(k, n); |
---|
646 | } |
---|
647 | |
---|
648 | lq.resetTime(); |
---|
649 | lq.redesign(); |
---|
650 | for(k = K-1; k > 0; k--){ |
---|
651 | A(0, 1) = u(k); |
---|
652 | A(1, 0) = -Kk(k); |
---|
653 | A(1, 1) = 1-Kk(k)*u(k); |
---|
654 | A(1, 2) = u(k)*sigma*sigma*(xn0(k+1, n) - xn0(k, n) - xn1(k, n)*u(k)) / sqr(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
655 | A(2, 2) = 1 - u(k)*u(k)*xn2(k, n)*(u(k)*u(k)*xn2(k, n) + 2*sigma*sigma) / sqr(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
656 | B(0) = xn1(k, n); |
---|
657 | B(1) = ((xn2(k, n)*sigma*sigma-u(k)*u(k)*xn2(k, n)*xn2(k, n))*(xn0(k+1, n)-xn0(k, n)) - 2*xn1(k, n)*u(k)) / sqr(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
658 | B(2) = -2*u(k)*xn2(k, n)*xn2(k, n)*sigma*sigma / sqr(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
659 | lq.Models(0).A = A; |
---|
660 | lq.Models(1).A = B; |
---|
661 | lq.redesign(); |
---|
662 | } |
---|
663 | L = lq.getL(); |
---|
664 | |
---|
665 | //simulation |
---|
666 | xn0(0, n) += sigma*randn(); |
---|
667 | for(k = 0; k < K-1; k++){ |
---|
668 | u(k) = L(0)*xn0(k, n) + L(1)*xn1(k, n) + L(2)*xn2(k, n); |
---|
669 | Kk(k) = u(k)*xn2(k, n)/(u(k)*u(k)*xn2(k, n) + sigma*sigma); |
---|
670 | xn0(k+1, n) = xn0(k, n) + xn1(k, n)*u(k) + sigma*randn(); |
---|
671 | xn1(k+1, n) = xn1(k, n) + Kk(k)*(xn0(k+1, n) - xn0(k, n) - xn1(k, n)*u(k)); |
---|
672 | xn2(k+1, n) = (1 - Kk(k)*u(k))*xn2(k, n); |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | loss(n) = 0; |
---|
677 | for(k=0;k<K;k++) loss(n) += xn0(k, n)*xn0(k, n); |
---|
678 | |
---|
679 | } |
---|
680 | |
---|
681 | /*vec mtraj(K); |
---|
682 | for(k=0;k<K;k++){ |
---|
683 | mtraj(k) = 0; |
---|
684 | for(n = 0; n < N; n++) mtraj(k) += xn0(k, n); |
---|
685 | mtraj(k) /= N; |
---|
686 | mtraj(k) += yr; |
---|
687 | } |
---|
688 | cout << "prum trajek " << mtraj << endl;*/ |
---|
689 | //cout << "prum ztrata " << sum(loss)/N << endl; |
---|
690 | |
---|
691 | double tolerr = 0.2; |
---|
692 | /*if(inP == 0.01) tolerr = 0.2; |
---|
693 | else if(inP == 0.1) tolerr = 2; |
---|
694 | else if(inP == 1) tolerr = 2000; |
---|
695 | else if(inP == 10) tolerr = 2e+7; |
---|
696 | else tolerr = 0;*/ |
---|
697 | |
---|
698 | CHECK_CLOSE_EX(1.0, sum(loss)/N, tolerr); |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | TEST (LQGU_PMSM_test){ |
---|
703 | bdm_assert(0, "Test not implemented."); |
---|
704 | } |
---|
705 | |
---|
706 | TEST (LQGU_large_test){ |
---|
707 | bdm_assert(0, "Test not implemented."); |
---|
708 | } |
---|
709 | |
---|
710 | TEST (validation_test){ |
---|
711 | RV rv1("x", 1); |
---|
712 | RV rv2("y", 2); |
---|
713 | RV rv3("z", 3); |
---|
714 | RV rv4("u", 2); |
---|
715 | RV rv5("v", 1); |
---|
716 | RV all; |
---|
717 | all = rv1; |
---|
718 | all.add(rv2); |
---|
719 | all.add(rv3); |
---|
720 | all.add(rv4); |
---|
721 | all.add(rv5); |
---|
722 | all.add(RV("1",1,0)); |
---|
723 | cout << "all rv: " << all << endl; |
---|
724 | |
---|
725 | ivec fy = rv2.findself(all); |
---|
726 | cout << "finding y: " << fy << endl; |
---|
727 | ivec dy = rv2.dataind(all); |
---|
728 | cout << "dataind y: " << dy << endl; |
---|
729 | |
---|
730 | RV rvzyu; |
---|
731 | rvzyu = rv3; |
---|
732 | rvzyu.add(rv2); |
---|
733 | rvzyu.add(rv4); |
---|
734 | fy = rvzyu.findself(all); |
---|
735 | cout << "finding zyu: " << fy << endl; |
---|
736 | dy = rvzyu.dataind(all); |
---|
737 | cout << "dataind zyu: " << dy << endl; |
---|
738 | |
---|
739 | rvzyu.add(RV("1",1,0)); |
---|
740 | fy = rvzyu.findself(all); |
---|
741 | cout << "finding zyu1: " << fy << endl; |
---|
742 | dy = rvzyu.dataind(all); |
---|
743 | cout << "dataind zyu1: " << dy << endl; |
---|
744 | |
---|
745 | rvzyu.add(RV("k",1,0)); |
---|
746 | fy = rvzyu.findself(all); |
---|
747 | cout << "finding zyu1 !k!: " << fy << endl; |
---|
748 | dy = rvzyu.dataind(all); |
---|
749 | cout << "dataind zyu1 !k!: " << dy << endl; |
---|
750 | |
---|
751 | RV emptyrv; |
---|
752 | fy = emptyrv.findself(all); |
---|
753 | cout << "finding empty: " << fy << " size " << fy.size() << endl; |
---|
754 | dy = emptyrv.dataind(all); |
---|
755 | cout << "dataind empty: " << dy << " size " << dy.size() << endl; |
---|
756 | |
---|
757 | LQG_universal lq; |
---|
758 | lq.validate(); |
---|
759 | } |
---|