Show
Ignore:
Timestamp:
08/12/09 16:47:33 (15 years ago)
Author:
smidl
Message:

LQG basic implementation

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/design/ctrlbase.h

    r491 r508  
    1111*/ 
    1212 
    13 #include <bdmbase.h> 
     13#include "../base/bdmbase.h" 
     14 
     15namespace bdm{ 
    1416 
    1517//! Base class of designers of control strategy 
     
    1719        public: 
    1820                //! Redesign control strategy  
    19                 virtual redesign(){it_error("Not implemented"); }; 
     21                virtual void redesign(){it_error("Not implemented"); }; 
    2022                //! apply control strategy to obtain control input 
    2123                virtual vec apply(const vec &cond){it_error("Not implemented"); return vec(0);} 
    22 } 
     24}; 
    2325 
    2426//! Linear Quadratic Gaussian designer for constant penalizations and constant target 
     
    3133                int dimu; 
    3234                //! dimension of output 
    33                 /int dimy; 
     35                int dimy; 
    3436 
    3537                //! matrix A of the linear system 
     
    3941                //! matrix C of the linear system 
    4042                mat C; 
    41                 //! expected value of x at time t 
    42                 vec xt; 
    4343                //! required value of the output y at time t (assumed constant) 
    4444                vec y_req; 
     
    7777                //! set system parameters from given matrices 
    7878                void set_system_parameters(const mat &A, const mat &B, const mat &C); 
     79                //! set penalization matrices and control horizon 
     80                void set_control_parameters(const mat &Qy0, const mat &Qu0, const vec y_req0, int horizon0); 
    7981                //! 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(){}; 
     82//              void set_system_parameters(const Kalman &K); 
     83                //! refresh temporary storage - inefficient can be improved 
     84                void prepare_qr(); 
     85                //! function for future use which is called at each time td; Should call prepare_qr()! 
     86                virtual void update_state(){}; 
    8687                //! redesign one step of the  
    8788                void ricatti_step(){ 
    8889                        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); 
     90                        pre_qr.set_submatrix(dimx+dimu+dimy, dimu+dimx, -Qy*y_req); 
     91                        if (!qr(pre_qr,post_qr)) it_warning("QR in LQG unstable"); 
    9192                        triu(post_qr); 
    9293        // 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                        s=post_qr.get(dimu, 2*dimu+dimx+dimy-1, dimu, 2*dimu+dimx+dimy-1); 
    9495                }; 
    9596                void redesign(){ 
     
    101102                        wsd=hn(1:m,1:m); 
    102103                        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                        L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu-1, dimu, 2*dimu+dimx+dimy-1); 
    104105                } 
    105                 vec apply(const vec &state, const vec &ukm){vec pom=concat_vertical(state, ones(dimy,1), ukm); return L*pom;} 
    106 } 
     106                vec apply(const vec &state, const vec &ukm){vec pom=concat(state, ones(dimy), ukm); return L*pom;} 
     107        } ; 
     108 
     109} // namespace