1 | /*! |
---|
2 | \file |
---|
3 | \brief Base classes for designers of control strategy |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #include <bdmbase.h> |
---|
14 | |
---|
15 | //! Base class of designers of control strategy |
---|
16 | class Designer : public root { |
---|
17 | public: |
---|
18 | //! Redesign control strategy |
---|
19 | virtual redesign(){it_error("Not implemented"); }; |
---|
20 | //! apply control strategy to obtain control input |
---|
21 | virtual vec apply(const vec &cond){it_error("Not implemented"); return vec(0);} |
---|
22 | } |
---|
23 | |
---|
24 | //! Linear Quadratic Gaussian designer for constant penalizations and constant target |
---|
25 | //! Its internals are very close to Kalman estimator |
---|
26 | class LQG : public Designer { |
---|
27 | protected: |
---|
28 | //! dimension of state |
---|
29 | int dimx; |
---|
30 | //! dimension of inputs |
---|
31 | int dimu; |
---|
32 | //! dimension of output |
---|
33 | /int dimy; |
---|
34 | |
---|
35 | //! matrix A of the linear system |
---|
36 | mat A; |
---|
37 | //! matrix B of the linear system |
---|
38 | mat B; |
---|
39 | //! matrix C of the linear system |
---|
40 | mat C; |
---|
41 | //! expected value of x at time t |
---|
42 | vec xt; |
---|
43 | //! required value of the output y at time t (assumed constant) |
---|
44 | vec y_req; |
---|
45 | |
---|
46 | //! Control horizon, set to maxint for infinite horizons |
---|
47 | int horizon; |
---|
48 | //! penalization matrix Qy |
---|
49 | mat Qy; |
---|
50 | //! penalization matrix Qu |
---|
51 | mat Qu; |
---|
52 | //! time of the design step - from horizon->0 |
---|
53 | int td; |
---|
54 | //! controller parameters |
---|
55 | mat L; |
---|
56 | |
---|
57 | //!@{ \name temporary storage for ricatti |
---|
58 | //! parameters |
---|
59 | mat pr; |
---|
60 | //! penalization |
---|
61 | mat qux; |
---|
62 | //! penalization |
---|
63 | mat qyx; |
---|
64 | //! internal quadratic form |
---|
65 | mat s; |
---|
66 | //! penalization |
---|
67 | mat qy; |
---|
68 | //! pre_qr part |
---|
69 | mat hqy; |
---|
70 | //! pre qr matrix |
---|
71 | mat pre_qr; |
---|
72 | //! post qr matrix |
---|
73 | mat post_qr; |
---|
74 | //!@} |
---|
75 | |
---|
76 | public: |
---|
77 | //! set system parameters from given matrices |
---|
78 | void set_system_parameters(const mat &A, const mat &B, const mat &C); |
---|
79 | //! set system parameters from Kalman filter |
---|
80 | void set_system_parameters(const Kalman &K); |
---|
81 | //! set current state |
---|
82 | void set_state(const vec &xt0){xt=xt0;}; |
---|
83 | //! refresh temporary storage |
---|
84 | //! function for future use which is called at each time td; |
---|
85 | virtual update_state(){}; |
---|
86 | //! redesign one step of the |
---|
87 | void ricatti_step(){ |
---|
88 | pre_qr.set_submatrix(0,0,s*pr); |
---|
89 | pre_qr.set_submatrix(dimx, dimu+dimx, -Qy*y_req); |
---|
90 | post_qr=qr(pre_qr); |
---|
91 | triu(post_qr); |
---|
92 | // hn(m+1:2*m+n+r,m+1:2*m+n+r); |
---|
93 | s=post_qr.get(dimu, 2*dimu+dimx+dimy, dimu, 2*dimu+dimx+dimy); |
---|
94 | }; |
---|
95 | void redesign(){ |
---|
96 | for(td=horizon; td>0; td--){ |
---|
97 | update_state(); |
---|
98 | ricatti_step(); |
---|
99 | } |
---|
100 | /* ws=hn(1:m,m+1:2*m+n+r); |
---|
101 | wsd=hn(1:m,1:m); |
---|
102 | Lklq=-inv(wsd)*ws;*/ |
---|
103 | L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu, dimu, 2*dimu+dimx+dimy); |
---|
104 | } |
---|
105 | vec apply(const vec &state, const vec &ukm){vec pom=concat_vertical(state, ones(dimy,1), ukm); return L*pom;} |
---|
106 | } |
---|