00001
00013 #include "../base/bdmbase.h"
00014 #include "../estim/kalman.h"
00015
00016 namespace bdm{
00017
00019 class Designer {
00020
00021 public:
00023 virtual void redesign() {
00024 bdm_error("Not implemented");
00025 }
00026
00028 virtual vec apply(const vec &cond) {
00029 bdm_error("Not implemented");
00030 return vec();
00031 }
00032 };
00033
00036 class LQG : public Designer {
00037 protected:
00039 shared_ptr<StateSpace<fsqmat> > S;
00041 vec y_req;
00043 vec u_req;
00044
00046 int horizon;
00048 mat Qy;
00050 mat Qu;
00052 int td;
00054 mat L;
00055
00058 int dimx;
00060 int dimy;
00062 int dimu;
00063
00065 mat pr;
00067 mat qux;
00069 mat qyx;
00071 mat s;
00073 mat qy;
00075 mat hqy;
00077 mat pre_qr;
00079 mat post_qr;
00081
00082 public:
00084 void set_system(shared_ptr<StateSpace<fsqmat> > S0);
00086 void set_control_parameters(const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0);
00088
00090 void initialize();
00092 void validate();
00094 virtual void update_state(){};
00096 void ricatti_step(){
00097 pre_qr.set_submatrix(0,0,s*pr);
00098 pre_qr.set_submatrix(dimx+dimu+dimy, dimu+dimx, -Qy*y_req);
00099 if (!qr(pre_qr,post_qr)){ bdm_warning("QR in LQG unstable");}
00100 triu(post_qr);
00101
00102 s=post_qr.get(dimu, 2*dimu+dimx+dimy-1, dimu, 2*dimu+dimx+dimy-1);
00103 };
00104 void redesign(){
00105 for(td=horizon; td>0; td--){
00106 update_state();
00107 ricatti_step();
00108 }
00109
00110
00111
00112 L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu-1, dimu, 2*dimu+dimx+dimy-1);
00113 }
00115 vec apply(const vec &state, const vec &ukm){vec pom=concat(state, ones(dimy), ukm); return L*pom;}
00116 } ;
00117
00121 class Controller : public root {
00122 protected:
00124 RV yrv;
00126 RV urv;
00128 RV drv;
00130 ivec LIDs;
00131 public:
00133 virtual void adapt(const vec &data){};
00135 virtual vec ctrlaction(const vec &data){return vec(0);}
00136
00137 void from_setting(const Setting &set){
00138 UI::get(yrv,set,"yrv",UI::optional);
00139 UI::get(yrv,set,"urv",UI::optional);
00140 }
00142 const RV& _urv() {return urv;}
00144 const RV& _yrv() {return yrv;}
00146 const RV& _drv() {return drv;}
00148 virtual void log_add ( logger &L, const string &name = "" ) { }
00150 virtual void logit ( logger &L ) { }
00151
00152 };
00153
00154 }