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