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 "../base/bdmbase.h" |
---|
14 | #include "../estim/kalman.h" |
---|
15 | |
---|
16 | namespace bdm { |
---|
17 | |
---|
18 | //! Base class for adaptive controllers |
---|
19 | //! The base class is, however, non-adaptive, method \c adapt() is empty. |
---|
20 | //! \note advanced Controllers will probably include estimator as their internal attribute (e.g. dual controllers) |
---|
21 | class Controller : public root { |
---|
22 | public: |
---|
23 | //! identifier of the designed action; |
---|
24 | RV rv; |
---|
25 | protected: |
---|
26 | //! identifier of the conditioning variables - data needed ; |
---|
27 | RV rvc; |
---|
28 | public: |
---|
29 | //! function processing new observations and adapting control strategy accordingly |
---|
30 | virtual void adapt ( const vec &data ) {}; |
---|
31 | //! function redesigning the control strategy |
---|
32 | virtual void redesign() {}; |
---|
33 | //! returns designed control action |
---|
34 | virtual vec ctrlaction ( const vec &cond ) const { |
---|
35 | return vec ( 0 ); |
---|
36 | } |
---|
37 | |
---|
38 | void from_setting ( const Setting &set ) { |
---|
39 | shared_ptr<RV> rv_ptr = UI::build<RV>( set, "rv", UI::optional ); |
---|
40 | if( rv_ptr ) rv = *rv_ptr; |
---|
41 | shared_ptr<RV> rvc_ptr = UI::build<RV>( set, "rvc", UI::optional ); |
---|
42 | if( rvc_ptr ) rvc = *rvc_ptr; |
---|
43 | } |
---|
44 | //! access function |
---|
45 | const RV& _rv() { |
---|
46 | return rv; |
---|
47 | } |
---|
48 | //! access function |
---|
49 | const RV& _rvc() { |
---|
50 | return rvc; |
---|
51 | } |
---|
52 | //! register this controller with given datasource under name "name" |
---|
53 | virtual void log_register ( logger &L, const string &prefix ) { } |
---|
54 | //! write requested values into the logger |
---|
55 | virtual void log_write ( ) const { } |
---|
56 | |
---|
57 | }; |
---|
58 | |
---|
59 | //! Linear Quadratic Gaussian designer for constant penalizations and constant target |
---|
60 | //! Its internals are very close to Kalman estimator |
---|
61 | class LQG : public Controller { |
---|
62 | protected: |
---|
63 | //! StateSpace model from which we read data |
---|
64 | shared_ptr<StateSpace<chmat> > S; |
---|
65 | //! required value of the output y at time t (assumed constant) |
---|
66 | vec y_req; |
---|
67 | //! required value of the output y at time t (assumed constant) |
---|
68 | vec u_req; |
---|
69 | |
---|
70 | //! Control horizon, set to maxint for infinite horizons |
---|
71 | int horizon; |
---|
72 | //! penalization matrix Qy |
---|
73 | chmat Qy; |
---|
74 | //! penalization matrix Qu |
---|
75 | chmat Qu; |
---|
76 | //! time of the design step - from horizon->0 |
---|
77 | int td; |
---|
78 | //! controller parameters |
---|
79 | mat L; |
---|
80 | |
---|
81 | //!@{ \name temporary storage for ricatti - use initialize |
---|
82 | //! convenience parameters |
---|
83 | int dimx; |
---|
84 | //! convenience parameters |
---|
85 | int dimy; |
---|
86 | //! convenience parameters |
---|
87 | int dimu; |
---|
88 | |
---|
89 | //! parameters |
---|
90 | mat pr; |
---|
91 | //! penalization |
---|
92 | mat qux; |
---|
93 | //! penalization |
---|
94 | mat qyx; |
---|
95 | //! internal quadratic form |
---|
96 | mat s; |
---|
97 | //! penalization |
---|
98 | mat qy; |
---|
99 | //! pre_qr part |
---|
100 | mat hqy; |
---|
101 | //! pre qr matrix |
---|
102 | mat pre_qr; |
---|
103 | //! post qr matrix |
---|
104 | mat post_qr; |
---|
105 | //!@} |
---|
106 | |
---|
107 | public: |
---|
108 | //! set system parameters from given matrices |
---|
109 | void set_system ( shared_ptr<StateSpace<chmat> > S0 ); |
---|
110 | //! update internal whan system has changed |
---|
111 | void update_system(); |
---|
112 | //! set penalization matrices and control horizon |
---|
113 | void set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ); |
---|
114 | //! set penalization matrices and control horizon |
---|
115 | void set_control_Qy ( const mat &Qy0 ) { |
---|
116 | Qy = Qy0; |
---|
117 | } |
---|
118 | //! refresh temporary storage - inefficient can be improved |
---|
119 | void initial_belmann() { |
---|
120 | s = 1e-5 * eye ( dimx + dimu + dimy ); |
---|
121 | }; |
---|
122 | //! validation procedure |
---|
123 | void validate(); |
---|
124 | //! function for future use which is called at each time td; Should call initialize()! |
---|
125 | //! redesign one step of the |
---|
126 | void ricatti_step(); |
---|
127 | |
---|
128 | void redesign(); |
---|
129 | |
---|
130 | //! compute control action |
---|
131 | vec ctrlaction ( const vec &state, const vec &ukm ) const { |
---|
132 | vec pom = concat ( state, ones ( dimy ), ukm ); |
---|
133 | return L*pom; |
---|
134 | } |
---|
135 | //! access function |
---|
136 | mat _L() const { |
---|
137 | return L; |
---|
138 | } |
---|
139 | } ; |
---|
140 | |
---|
141 | |
---|
142 | } // namespace |
---|