Changeset 1064 for library/bdm/design
- Timestamp:
- 06/09/10 14:00:40 (14 years ago)
- Location:
- library/bdm/design
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/design/arx_ctrl.h
r896 r1064 22 22 class LQG_ARX : public Controller { 23 23 protected: 24 25 26 27 28 29 30 31 32 33 34 35 24 //! Internal ARX estimator 25 shared_ptr<ARX> ar; 26 //! Internal LQG designer 27 LQG lq; 28 //! Intermediate StateSpace model 29 shared_ptr<StateFromARX> Stsp; 30 //! AR predictor 31 shared_ptr<mlnorm<chmat> > pred; 32 //! datalink from rvc to ar.bayes 33 datalink rvc2ar_y; 34 //! datalink from rvc to ar.bayes 35 datalink_buffered rvc2ar_cond; 36 36 37 38 39 40 37 //! flag to use iterations spread in time (ist) 38 bool ist; 39 //! flag to use windsurfer approach 40 bool windsurfer; 41 41 public: 42 43 42 //! Default constructor 43 LQG_ARX() : ar(), lq() { } 44 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 45 //! adaptation is to store arx estimates in stsp 46 void adapt ( const vec &data ) { 47 ar->bayes ( rvc2ar_y.pushdown ( data ), rvc2ar_cond.pushdown ( data ) ); 48 rvc2ar_cond.store_data ( data ); 49 ar->ml_predictor_update ( *pred ); 50 } 51 void redesign() { 52 Stsp->update_from ( *pred ); 53 if ( windsurfer ) { 54 mat Ry = pred->_R(); 55 lq.set_control_Qy ( inv ( Ry ) ); 56 } 57 if ( !ist ) { 58 lq.initial_belmann(); 59 } 60 lq.redesign(); 61 } 62 vec ctrlaction ( const vec &cond ) const { 63 //cond is xt + ut 64 vec state = cond.left ( Stsp->_A().rows() ); 65 if ( Stsp->_have_constant() ) { 66 state ( state.length() - 1 ) = 1; 67 } 68 vec tmp=lq.ctrlaction ( state, cond.right ( Stsp->_B().cols() ) ); 69 if (!std::isfinite(sum(tmp))) { 70 cout << "infinite ctrl action"; 71 } 72 return tmp; 73 } 74 //! 75 //! LQG is defined by quadratic loss function 76 /*! \f[ L(y,u) = (y-y_{req})'Q_y (y-y_{req}) + (u-u_{req})' Q_u (u-u_{req}) \f] 77 expected input 78 \code 79 { class="LQG"; 80 arx = {class="ARX", ...} // internal arx model, see 81 Qy = ("matrix", ...); 82 Qu = ("matrix", ...); 83 yreq = []; // requested output 84 ureq = []; // requested input value 85 } 86 \endcode 87 */ 88 void from_setting ( const Setting &set ) { 89 ar = UI::build<ARX> ( set, "ARX", UI::compulsory ); 90 90 91 92 91 mat Qu; 92 mat Qy; 93 93 94 95 94 UI::get ( Qu, set, "Qu", UI::compulsory ); 95 UI::get ( Qy, set, "Qy", UI::compulsory ); 96 96 97 98 99 97 vec y_req; 98 if ( !UI::get ( y_req, set, "yreq", UI::optional ) ) 99 y_req = zeros ( ar->_yrv()._dsize() ); 100 100 101 102 103 101 int horizon; 102 UI::get ( horizon, set, "horizon", UI::compulsory ); 103 lq.set_control_parameters ( Qy, Qu, y_req, horizon ); 104 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 105 int wind; 106 if ( UI::get ( wind, set, "windsurfer", UI::optional ) ) { 107 windsurfer = wind > 0; 108 } else { 109 windsurfer = false; 110 }; 111 int ist_; 112 if ( UI::get ( ist_, set, "ist", UI::optional ) ) { 113 ist = ist_ > 0; 114 } else { 115 ist = false; 116 }; 117 validate(); 118 } 119 119 120 121 122 123 124 125 126 127 128 120 void validate() { 121 // ar is valid 122 pred = ar->ml_predictor<chmat>(); 123 Stsp = new StateFromARX; 124 RV xrv; 125 RV urvm; //old ut 126 Stsp->connect_mlnorm ( *pred, xrv, urvm ); 127 lq.set_system ( Stsp ); 128 lq.validate(); 129 129 130 rv = urvm; // rv is not shifted to t+1!! 131 rvc = concat ( xrv, urvm ); 132 rvc2ar_y.set_connection ( ar->_yrv(), rvc ); 133 rvc2ar_cond.set_connection ( ar->_rvc(), rvc ); 134 //datalink from ARX to rvc 135 } 136 void log_register ( logger &L, const string &prefix ) { 137 ar->log_register ( L, prefix ); 138 } 139 void log_write ( ) const { 140 ar->log_write(); 141 } 142 //! access function 143 const ARX& _ar() {return *ar.get();} 144 //! access function 145 mlnorm<chmat>* _pred() {return pred.get();} 146 130 rv = urvm; // rv is not shifted to t+1!! 131 rvc = concat ( xrv, urvm ); 132 rvc2ar_y.set_connection ( ar->_yrv(), rvc ); 133 rvc2ar_cond.set_connection ( ar->_rvc(), rvc ); 134 //datalink from ARX to rvc 135 } 136 void log_register ( logger &L, const string &prefix ) { 137 ar->log_register ( L, prefix ); 138 } 139 void log_write ( ) const { 140 ar->log_write(); 141 } 142 //! access function 143 const ARX& _ar() { 144 return *ar.get(); 145 } 146 //! access function 147 mlnorm<chmat>* _pred() { 148 return pred.get(); 149 } 150 147 151 148 152 }; -
library/bdm/design/ctrlbase.cpp
r744 r1064 4 4 5 5 void LQG::set_system ( shared_ptr<StateSpace<chmat> > S0 ) { 6 6 S = S0; 7 7 } 8 8 9 9 void LQG::update_system() { 10 11 10 pr.set_submatrix ( 0, 0, S->_B() ); 11 pr.set_submatrix ( 0, dimu, S->_A() ); 12 12 13 14 15 13 //penalization 14 qux.set_submatrix ( 0, 0, Qu._Ch() ); 15 qux.set_submatrix ( 0, dimx + dimu + dimy, -Qu._Ch() ); 16 16 17 18 17 qyx.set_submatrix ( 0, 0, S->_C() ); 18 qyx.set_submatrix ( 0, dimx, -eye ( dimy ) ); 19 19 20 21 20 // parts of QR 21 hqy = Qy.to_mat() * qyx * pr; 22 22 23 24 23 // pre_qr 24 pre_qr = concat_vertical ( s * pr, concat_vertical ( hqy, qux ) ); 25 25 } 26 26 27 27 void LQG::set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ) { 28 29 30 31 28 Qy = Qy0; 29 Qu = Qu0; 30 y_req = y_req0; 31 horizon = horizon0; 32 32 } 33 33 34 34 void LQG::validate() { 35 36 37 38 39 40 35 // set properties from S 36 dimx = S->_A().rows(); 37 dimy = S->_C().rows(); 38 dimu = S->_B().cols(); 39 pr = zeros ( dimx + dimu + dimy, dimu + dimx + dimu + dimy ); 40 pr.set_submatrix ( dimx, dimu + dimx, eye ( dimu + dimy ) ); 41 41 42 43 44 45 42 //penalization 43 bdm_assert ( Qy.cols() == dimy, "LQG: wrong dimensions of Qy " ); 44 bdm_assert ( Qu.cols() == dimu, "LQG: wrong dimensions of Qu " ); 45 bdm_assert ( y_req.length() == dimy, "LQG: wrong dimensions of y_req " ); 46 46 47 48 47 qux = zeros ( dimu, dimx + 2 * dimu + dimy ); 48 qyx = zeros ( dimy, dimx + dimy + dimu ); 49 49 50 51 52 53 50 // 51 initial_belmann(); 52 // parts of QR 53 post_qr = zeros ( pre_qr.rows(), pre_qr.cols() ); 54 54 55 55 update_system(); 56 56 } 57 57 … … 59 59 // pre_qr.set_submatrix ( 0, 0, s*pr ); 60 60 // pre_qr.set_submatrix ( dimx + dimu + dimy, dimu + dimx, -Qy.to_mat() *y_req ); 61 62 63 64 65 66 61 if ( !qr ( pre_qr, post_qr ) ) { 62 bdm_warning ( "QR in LQG unstable" ); 63 } 64 triu ( post_qr ); 65 // hn(m+1:2*m+n+r,m+1:2*m+n+r); 66 s = post_qr.get ( dimu, 2 * dimu + dimx + dimy - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 67 67 }; 68 68 69 69 void LQG::redesign() { 70 71 72 73 74 75 76 77 70 for ( td = horizon; td > 0; td-- ) { 71 update_system(); 72 ricatti_step(); 73 } 74 /* ws=hn(1:m,m+1:2*m+n+r); 75 wsd=hn(1:m,1:m); 76 Lklq=-inv(wsd)*ws;*/ 77 L = -inv ( post_qr.get ( 0, dimu - 1, 0, dimu - 1 ) ) * post_qr.get ( 0, dimu - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 78 78 } 79 79 -
library/bdm/design/ctrlbase.h
r907 r1064 21 21 class Controller : public root { 22 22 protected: 23 24 25 26 23 //! identifier of the designed action; 24 RV rv; 25 //! identifier of the conditioning variables - data needed ; 26 RV rvc; 27 27 public: 28 29 30 31 32 33 34 35 28 //! function processing new observations and adapting control strategy accordingly 29 virtual void adapt ( const vec &data ) {}; 30 //! function redesigning the control strategy 31 virtual void redesign() {}; 32 //! returns designed control action 33 virtual vec ctrlaction ( const vec &cond ) const { 34 return vec ( 0 ); 35 } 36 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 37 void from_setting ( const Setting &set ) { 38 shared_ptr<RV> rv_ptr = UI::build<RV>( set, "rv", UI::optional ); 39 if( rv_ptr ) rv = *rv_ptr; 40 shared_ptr<RV> rvc_ptr = UI::build<RV>( set, "rvc", UI::optional ); 41 if( rvc_ptr ) rvc = *rvc_ptr; 42 } 43 //! access function 44 const RV& _rv() { 45 return rv; 46 } 47 //! access function 48 const RV& _rvc() { 49 return rvc; 50 } 51 //! register this controller with given datasource under name "name" 52 virtual void log_register ( logger &L, const string &prefix ) { } 53 //! write requested values into the logger 54 virtual void log_write ( ) const { } 55 55 56 56 }; … … 60 60 class LQG : public Controller { 61 61 protected: 62 63 64 65 66 67 62 //! StateSpace model from which we read data 63 shared_ptr<StateSpace<chmat> > S; 64 //! required value of the output y at time t (assumed constant) 65 vec y_req; 66 //! required value of the output y at time t (assumed constant) 67 vec u_req; 68 68 69 70 71 72 73 74 75 76 77 78 69 //! Control horizon, set to maxint for infinite horizons 70 int horizon; 71 //! penalization matrix Qy 72 chmat Qy; 73 //! penalization matrix Qu 74 chmat Qu; 75 //! time of the design step - from horizon->0 76 int td; 77 //! controller parameters 78 mat L; 79 79 80 81 82 83 84 85 86 80 //!@{ \name temporary storage for ricatti - use initialize 81 //! convenience parameters 82 int dimx; 83 //! convenience parameters 84 int dimy; 85 //! convenience parameters 86 int dimu; 87 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 88 //! parameters 89 mat pr; 90 //! penalization 91 mat qux; 92 //! penalization 93 mat qyx; 94 //! internal quadratic form 95 mat s; 96 //! penalization 97 mat qy; 98 //! pre_qr part 99 mat hqy; 100 //! pre qr matrix 101 mat pre_qr; 102 //! post qr matrix 103 mat post_qr; 104 //!@} 105 105 106 106 public: 107 //! set system parameters from given matrices 108 void set_system ( shared_ptr<StateSpace<chmat> > S0 ); 109 //! update internal whan system has changed 110 void update_system(); 111 //! set penalization matrices and control horizon 112 void set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ); 113 //! set penalization matrices and control horizon 114 void set_control_Qy ( const mat &Qy0 ) { 115 Qy = Qy0; 116 } 117 //! refresh temporary storage - inefficient can be improved 118 void initial_belmann() { 119 s = 1e-5 * eye ( dimx + dimu + dimy ); 120 }; 121 //! validation procedure 122 void validate(); 123 //! function for future use which is called at each time td; Should call initialize()! 124 //! redesign one step of the 125 void ricatti_step(); 126 127 void redesign(); 107 //! set system parameters from given matrices 108 void set_system ( shared_ptr<StateSpace<chmat> > S0 ); 109 //! update internal whan system has changed 110 void update_system(); 111 //! set penalization matrices and control horizon 112 void set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ); 113 //! set penalization matrices and control horizon 114 void set_control_Qy ( const mat &Qy0 ) { 115 Qy = Qy0; 116 } 117 //! refresh temporary storage - inefficient can be improved 118 void initial_belmann() { 119 s = 1e-5 * eye ( dimx + dimu + dimy ); 120 }; 121 //! validation procedure 122 void validate(); 123 //! function for future use which is called at each time td; Should call initialize()! 124 //! redesign one step of the 125 void ricatti_step(); 128 126 129 //! compute control action 130 vec ctrlaction ( const vec &state, const vec &ukm ) const { 131 vec pom = concat ( state, ones ( dimy ), ukm ); 132 return L*pom; 133 } 134 //! access function 135 mat _L() const { 136 return L; 137 } 127 void redesign(); 128 129 //! compute control action 130 vec ctrlaction ( const vec &state, const vec &ukm ) const { 131 vec pom = concat ( state, ones ( dimy ), ukm ); 132 return L*pom; 133 } 134 //! access function 135 mat _L() const { 136 return L; 137 } 138 138 } ; 139 139