306 | | CHECK_CLOSE_EX(loss_uni, loss_rct, 0.0001); |
307 | | } |
| 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 | } |