| 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 | //! Low-level class for design of control strategy |
|---|
| 19 | class Designer { |
|---|
| 20 | |
|---|
| 21 | public: |
|---|
| 22 | //! Redesign control strategy |
|---|
| 23 | virtual void redesign() { |
|---|
| 24 | bdm_error("Not implemented"); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | //! apply control strategy to obtain control input |
|---|
| 28 | virtual vec apply(const vec &cond) { |
|---|
| 29 | bdm_error("Not implemented"); |
|---|
| 30 | return vec(); |
|---|
| 31 | } |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | //! Linear Quadratic Gaussian designer for constant penalizations and constant target |
|---|
| 35 | //! Its internals are very close to Kalman estimator |
|---|
| 36 | class LQG : public Designer { |
|---|
| 37 | protected: |
|---|
| 38 | //! StateSpace model from which we read data |
|---|
| 39 | shared_ptr<StateSpace<fsqmat> > S; |
|---|
| 40 | //! required value of the output y at time t (assumed constant) |
|---|
| 41 | vec y_req; |
|---|
| 42 | //! required value of the output y at time t (assumed constant) |
|---|
| 43 | vec u_req; |
|---|
| 44 | |
|---|
| 45 | //! Control horizon, set to maxint for infinite horizons |
|---|
| 46 | int horizon; |
|---|
| 47 | //! penalization matrix Qy |
|---|
| 48 | mat Qy; |
|---|
| 49 | //! penalization matrix Qu |
|---|
| 50 | mat Qu; |
|---|
| 51 | //! time of the design step - from horizon->0 |
|---|
| 52 | int td; |
|---|
| 53 | //! controller parameters |
|---|
| 54 | mat L; |
|---|
| 55 | |
|---|
| 56 | //!@{ \name temporary storage for ricatti - use initialize |
|---|
| 57 | //! convenience parameters |
|---|
| 58 | int dimx; |
|---|
| 59 | //! convenience parameters |
|---|
| 60 | int dimy; |
|---|
| 61 | //! convenience parameters |
|---|
| 62 | int dimu; |
|---|
| 63 | |
|---|
| 64 | //! parameters |
|---|
| 65 | mat pr; |
|---|
| 66 | //! penalization |
|---|
| 67 | mat qux; |
|---|
| 68 | //! penalization |
|---|
| 69 | mat qyx; |
|---|
| 70 | //! internal quadratic form |
|---|
| 71 | mat s; |
|---|
| 72 | //! penalization |
|---|
| 73 | mat qy; |
|---|
| 74 | //! pre_qr part |
|---|
| 75 | mat hqy; |
|---|
| 76 | //! pre qr matrix |
|---|
| 77 | mat pre_qr; |
|---|
| 78 | //! post qr matrix |
|---|
| 79 | mat post_qr; |
|---|
| 80 | //!@} |
|---|
| 81 | |
|---|
| 82 | public: |
|---|
| 83 | //! set system parameters from given matrices |
|---|
| 84 | void set_system(shared_ptr<StateSpace<fsqmat> > S0); |
|---|
| 85 | //! set penalization matrices and control horizon |
|---|
| 86 | void set_control_parameters(const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0); |
|---|
| 87 | //! set system parameters from Kalman filter |
|---|
| 88 | // void set_system_parameters(const Kalman &K); |
|---|
| 89 | //! refresh temporary storage - inefficient can be improved |
|---|
| 90 | void initialize(); |
|---|
| 91 | //! validation procedure |
|---|
| 92 | void validate(); |
|---|
| 93 | //! function for future use which is called at each time td; Should call initialize()! |
|---|
| 94 | virtual void update_state(){}; |
|---|
| 95 | //! redesign one step of the |
|---|
| 96 | void ricatti_step(){ |
|---|
| 97 | pre_qr.set_submatrix(0,0,s*pr); |
|---|
| 98 | pre_qr.set_submatrix(dimx+dimu+dimy, dimu+dimx, -Qy*y_req); |
|---|
| 99 | if (!qr(pre_qr,post_qr)){ bdm_warning("QR in LQG unstable");} |
|---|
| 100 | triu(post_qr); |
|---|
| 101 | // hn(m+1:2*m+n+r,m+1:2*m+n+r); |
|---|
| 102 | s=post_qr.get(dimu, 2*dimu+dimx+dimy-1, dimu, 2*dimu+dimx+dimy-1); |
|---|
| 103 | }; |
|---|
| 104 | void redesign(){ |
|---|
| 105 | for(td=horizon; td>0; td--){ |
|---|
| 106 | update_state(); |
|---|
| 107 | ricatti_step(); |
|---|
| 108 | } |
|---|
| 109 | /* ws=hn(1:m,m+1:2*m+n+r); |
|---|
| 110 | wsd=hn(1:m,1:m); |
|---|
| 111 | Lklq=-inv(wsd)*ws;*/ |
|---|
| 112 | L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu-1, dimu, 2*dimu+dimx+dimy-1); |
|---|
| 113 | } |
|---|
| 114 | //! compute control action |
|---|
| 115 | vec apply(const vec &state, const vec &ukm){vec pom=concat(state, ones(dimy), ukm); return L*pom;} |
|---|
| 116 | } ; |
|---|
| 117 | |
|---|
| 118 | //! Base class for adaptive controllers |
|---|
| 119 | //! The base class is however non-adaptive, method \c adapt() is empty. |
|---|
| 120 | //! \note advanced Controllers will probably include estimator as their internal attribute (e.g. dual controllers) |
|---|
| 121 | class Controller : public root { |
|---|
| 122 | protected: |
|---|
| 123 | //! identifier of the system output; |
|---|
| 124 | RV yrv; |
|---|
| 125 | //! identifier of the system input; |
|---|
| 126 | RV urv; |
|---|
| 127 | //! description of data needed for \c ctrlaction , required for automatic connection to DS |
|---|
| 128 | RV drv; |
|---|
| 129 | //! vector of logger IDs |
|---|
| 130 | ivec LIDs; |
|---|
| 131 | public: |
|---|
| 132 | //! function processing new observations and adapting control strategy accordingly |
|---|
| 133 | virtual void adapt(const vec &data){}; |
|---|
| 134 | //! returns designed control action |
|---|
| 135 | virtual vec ctrlaction(const vec &data){return vec(0);} |
|---|
| 136 | |
|---|
| 137 | void from_setting(const Setting &set){ |
|---|
| 138 | UI::get(yrv,set,"yrv",UI::optional); |
|---|
| 139 | UI::get(yrv,set,"urv",UI::optional); |
|---|
| 140 | } |
|---|
| 141 | //! access function |
|---|
| 142 | const RV& _urv() {return urv;} |
|---|
| 143 | //! access function |
|---|
| 144 | const RV& _yrv() {return yrv;} |
|---|
| 145 | //! access function |
|---|
| 146 | const RV& _drv() {return drv;} |
|---|
| 147 | //! register this controller with given datasource under name "name" |
|---|
| 148 | virtual void log_add ( logger &L, const string &name = "" ) { } |
|---|
| 149 | //! write requested values into the logger |
|---|
| 150 | virtual void logit ( logger &L ) { } |
|---|
| 151 | |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | } // namespace |
|---|