root/applications/dual/src/arx1_ctrl.h @ 650

Revision 650, 2.6 kB (checked in by smidl, 15 years ago)

example of control for a trivial ARX model

Line 
1#ifndef ARX_CTRL_H
2#define ARX_CTRL_H
3
4#include <design/ctrlbase.h>
5using namespace bdm;
6
7/*! exact controller for system \f$ y_{t+1} = y_{t} + b u_{t} + \sigma e_t \f$
8
9From \f$ E\{y_{ref}\} = E\{ y_{t} + b u_{t} \} \f$, the optimal control action is:
10\f[ u_t = (y_{ref} - y_{t})/b \f]
11
12*/ 
13
14class exact_ctrl: public Controller {
15        protected:
16                //! target value of y
17                double yr;
18                //! parameter b (or its estimate)
19                double b;
20        public:
21                void set_b (const double b0){b=b0;}
22                //! data in ctrlaction are only past outputs yt
23                vec ctrlaction(const vec &yt){
24                        vec ut(1);
25                        if (abs(b)<1e-2) {
26                                ut= 0.1*randn();
27                        } else {
28                                ut = (yr-yt)/b;
29                        }
30                        return ut;
31                }
32                void from_setting(const Setting &set){
33                        Controller::from_setting(set);
34                        UI::get(yr,set,"yr",UI::compulsory);
35                        UI::get(b,set,"b",UI::compulsory);
36                        validate();
37                }
38                void validate() {       }
39};
40UIREGISTER(exact_ctrl);
41
42/*! adaptive CE controller for \f$ y_{t+1} = y_{t} + b u_{t} + \sigma e_t \f$
43
44From \f$ E\{y_{ref}\} = E\{ y_{t} + b u_{t} \} \f$, the optimal control action is:
45\f[ u_t = (y_{ref} - y_{t})/b \f]
46
47Using certainty equivalent approach, i.e. \f$ b= E(b|y_t, u_t, \ldots )\f$, which is
48estimated using ARX model. This class is not truly appropriate since it estimates also \f$ \sigma \f$
49
50
51*/
52
53class ce_ctrl: public exact_ctrl {
54        protected:
55                //! estimator
56                ARX est;
57                //! vector of "dyad" for estimation
58                vec Psi;
59                //! remember last action - for estimation
60                double utm;
61                //! remember last input - for estimation
62                double ytm;
63        public:
64                //! expected input is yt
65                virtual void adapt(const vec &yt){
66                        //prepare data for ARX
67                        Psi(0) = yt(0)-ytm;
68                        Psi(1) = utm;
69                        // do estimation
70                        est.bayes(Psi);
71                        // save estimate b = E(b);
72                        b = est.posterior().mean()(0); // first
73                }
74                //! same as exact control, but stores ut as utm
75                vec ctrlaction(const vec &yt){
76                        vec ut=exact_ctrl::ctrlaction(yt);
77                        // remember last ut for estimation
78                        utm=ut(0);
79                        ytm=yt(0);
80                        return ut;
81                }
82                void from_setting(const Setting &set){
83                        Controller::from_setting(set);
84                        UI::get(yr,set,"yr",UI::compulsory);
85                        double b0;
86                        UI::get(b0,set,"b0",UI::compulsory);
87                        double P0;
88                        UI::get(P0,set,"P0",UI::compulsory);
89                       
90                        mat V0(2,2);
91                        V0(0,0) = 0.1;
92                        V0(1,0) = b0;
93                        V0(0,1) = b0;
94                        V0(1,1) = P0;
95                       
96                        est.set_statistics(1,V0);
97                        est.set_constant(false);
98                        validate();
99                }
100                void validate(){
101                        Psi = zeros(2);
102                        LIDs = zeros_i(1);
103                }
104                virtual void log_add ( logger &L, const string &name = "" ) {
105                        LIDs ( 0 ) = L.add ( RV("bhat",1) );
106                }
107                virtual void logit ( logger &L ) {
108                        L.logit ( LIDs ( 0 ), b );
109                }
110               
111};
112UIREGISTER(ce_ctrl);
113
114#endif //ARX_CTRL
Note: See TracBrowser for help on using the browser.