Changeset 508

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

LQG basic implementation

Location:
library
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/CMakeLists.txt

    r498 r508  
    22 
    33SET(bdm_base base/bdmbase.cpp base/bdmbase.h base/datasources.cpp base/datasources.h base/loggers.cpp base/loggers.h) 
    4 SET(bdm_math math/square_mat.cpp math/square_mat.h math/chmat.cpp math/chmat.h math/functions.cpp math/functions.h) 
     4SET(bdm_math math/square_mat.cpp math/square_mat.h  
     5        math/chmat.cpp math/chmat.h  
     6        math/functions.cpp math/functions.h) 
    57SET(bdm_stat stat/exp_family.cpp stat/exp_family.h stat/emix.cpp stat/emix.h stat/merger.h stat/merger.cpp) 
    68SET(bdm_estim estim/kalman.cpp estim/kalman.h estim/particles.cpp estim/particles.h estim/arx.cpp estim/arx.h estim/mixtures.cpp estim/mixtures.h) 
    7 SET(bdm_user_info base/libconfig/libconfigcpp.cc base/libconfig/grammar.c base/libconfig/libconfig.c base/libconfig/scanner.c base/user_info.cpp base/user_info.h ) 
     9SET(bdm_ctrl design/ctrlbase.cpp design/ctrlbase.h) 
     10SET(bdm_user_info base/libconfig/libconfigcpp.cc base/libconfig/grammar.c base/libconfig/libconfig.c base/libconfig/scanner.c  
     11        base/user_info.cpp base/user_info.h ) 
    812SET(bdm_mex mex/mex_datasource.h mex/mex_parser.h mex/mex_logger.h  ) 
    913 
  • library/bdm/base/bdmbase.h

    r507 r508  
    11/*! 
    22  \file 
    3   \brief Bayesian Models (bm) that use Bayes rule to learn from observations 
     3  \brief Basic structures of probability calculus: random variables, probability densities, Bayes rule 
    44  \author Vaclav Smidl. 
    55 
  • library/bdm/design/ctrlbase.cpp

    r491 r508  
    1 #include <ctrlbase.h> 
     1#include "ctrlbase.h" 
    22 
    3 LQG::set_system_parameters(const mat &A0, const mat &B0, const mat &C0){ 
     3namespace bdm{ 
     4 
     5void LQG::set_system_parameters(const mat &A0, const mat &B0, const mat &C0){ 
    46        dimx = A0.rows(); 
    57        dimy = C0.rows(); 
     
    1315        B=B0; 
    1416        C=C0; 
    15          
    16         // set temporary stuff; 
    17         pr = concat_vertical(  
    18                                         concat_horizontal(B,A, zeros(dimx, dimu+dimy)), 
    19                                         concat_horizontal(zeros(dimu+dimy,dimu+dimx), eye(dimu+dimy))); 
     17        pr=zeros(dimx+dimu+dimy,  dimu+dimx+dimu+dimy); 
     18        pr.set_submatrix(dimx, dimu+dimx, eye(dimu+dimy)); 
     19} 
     20 
     21void LQG::set_control_parameters(const mat &Qy0, const mat &Qu0, const vec y_req0, int horizon0){ 
     22        it_assert_debug ( Qy0.cols() == dimy, "LQG: wrong dimensions of Qy " ); 
     23        it_assert_debug ( Qu0.cols() == dimu, "LQG: wrong dimensions of Qu " ); 
     24        it_assert_debug ( y_req0.length() == dimy, "LQG: wrong dimensions of y_req " ); 
     25 
     26        Qy=Qy0; 
     27        Qu=Qu0; 
     28        y_req=y_req0; 
     29        horizon = horizon0; 
     30        prepare_qr(); 
     31} 
     32 
     33void LQG::prepare_qr(){ 
     34        // set parameter matrix 
     35        pr.set_submatrix(0,0,B); 
     36        pr.set_submatrix(0,dimu, A); 
    2037         
    2138        //penalization 
     
    2441        qux.set_submatrix(0,dimx+dimu+dimy,Qu); 
    2542 
    26         qyx=concat_horizontal(C,-eye(dimy),zeros(dimy,dimu)); 
     43        qyx=zeros(dimy, dimx+dimy+dimu); 
     44        qyx.set_submatrix(0,0,C); 
     45        qyx.set_submatrix(0,dimx,-eye(dimy)); 
    2746         
    2847        // 
    29         s=1e-5*eye(dimx+dimu+dimy); 
     48        s=1e-5*eye(4);//dimx+dimu+dimy); 
    3049        // parts of QR 
    3150        hqy=Qy*qyx*pr; 
    3251         
    3352        // pre_qr 
    34         pre_qr = concat_vertical(s*pr, hqy, qux); 
    35          
     53        pre_qr = concat_vertical(s*pr, concat_vertical(hqy, qux)); 
     54        post_qr = zeros(pre_qr.rows(), pre_qr.cols()); 
    3655} 
     56 
     57} 
  • 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 
  • library/bdm/itpp_ext.cpp

    r477 r508  
    371371} 
    372372 
    373 } 
     373void triu(mat &A){ 
     374        for(int i=1;i<A.rows();i++) { // row cycle 
     375                for (int j=0; j<i; j++) {A(i,j)=0;} 
     376        } 
     377} 
     378} 
  • library/bdm/itpp_ext.h

    r477 r508  
    100100//! implementation of digamma (psi) function 
    101101double psi ( double ); 
     102 
     103//! implementation of matlab triu function 
     104void triu(mat &A); 
    102105} 
    103106 
  • library/tests/CMakeLists.txt

    r506 r508  
    77link_directories (./unittest-cpp) 
    88 
    9 SET(testutil_src egiw_harness.cpp egiw_harness.h epdf_harness.cpp epdf_harness.h mat_checks.cpp mat_checks.h mpdf_harness.cpp mpdf_harness.h square_mat_point.cpp square_mat_point.h test_util.cpp test_util.h) 
     9SET(testutil_src egiw_harness.cpp egiw_harness.h epdf_harness.cpp epdf_harness.h mat_checks.cpp mat_checks.h  
     10        mpdf_harness.cpp mpdf_harness.h square_mat_point.cpp square_mat_point.h test_util.cpp test_util.h) 
    1011 
    1112add_library(testutil ${testutil_src}) 
     
    1617LINK_EXEC(square_mat_stress) 
    1718 
    18 add_executable(square_mat_prep additive_generator.cpp additive_generator.h generator.cpp generator.h size_generator.cpp size_generator.h square_mat_prep.cpp) 
     19add_executable(square_mat_prep additive_generator.cpp additive_generator.h generator.cpp generator.h  
     20        size_generator.cpp size_generator.h square_mat_prep.cpp) 
    1921target_link_libraries(square_mat_prep testutil) 
    2022LINK_EXEC(square_mat_prep) 
     
    3941# using UnitTest++ 
    4042add_executable(testsuite datalink_test.cpp egiw_test.cpp emix_test.cpp epdf_test.cpp loggers_test.cpp merger_test.cpp mpdf_test.cpp rv_test.cpp shared_ptr_test.cpp square_mat_test.cpp testsuite.cpp user_info_test.cpp) 
     43        mpdf_test.cpp rv_test.cpp shared_ptr_test.cpp square_mat_test.cpp testsuite.cpp user_info_test.cpp) 
    4144target_link_libraries(testsuite testutil unittest) 
    4245LINK_EXEC(testsuite)