1 | #ifndef ARX_CTRL_H |
---|
2 | #define ARX_CTRL_H |
---|
3 | |
---|
4 | #include <design/ctrlbase.h> |
---|
5 | using namespace bdm; |
---|
6 | |
---|
7 | /*! exact controller for system \f$ y_{t+1} = y_{t} + b u_{t} + \sigma e_t \f$ |
---|
8 | |
---|
9 | From \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 | |
---|
14 | class 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)const{ |
---|
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 | }; |
---|
40 | UIREGISTER(exact_ctrl); |
---|
41 | |
---|
42 | /*! adaptive CE controller for \f$ y_{t+1} = y_{t} + b u_{t} + \sigma e_t \f$ |
---|
43 | |
---|
44 | From \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 | |
---|
47 | Using certainty equivalent approach, i.e. \f$ b= E(b|y_t, u_t, \ldots )\f$, which is |
---|
48 | estimated using ARX model. This class is not truly appropriate since it estimates also \f$ \sigma \f$ |
---|
49 | |
---|
50 | |
---|
51 | */ |
---|
52 | |
---|
53 | class 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 &dt){ |
---|
66 | //prepare data for ARX |
---|
67 | // do estimation |
---|
68 | est.bayes(vec_1(dt(0)-ytm),vec_1(dt(1))); |
---|
69 | ytm = dt(0); |
---|
70 | // save estimate b = E(b); |
---|
71 | b = est.posterior().mean()(0); // first |
---|
72 | } |
---|
73 | //! same as exact control, but stores ut as utm |
---|
74 | vec ctrlaction(const vec &dt)const{ |
---|
75 | vec ut(1); |
---|
76 | mat P = est.posterior().variance(); |
---|
77 | ut(0) =(yr - dt(0))/(b + (b<0 ? P(0,0) : -P(0,0)));; |
---|
78 | // remember last ut for estimation |
---|
79 | return ut; |
---|
80 | } |
---|
81 | void from_setting(const Setting &set){ |
---|
82 | Controller::from_setting(set); |
---|
83 | UI::get(yr,set,"yr",UI::compulsory); |
---|
84 | double b0; |
---|
85 | UI::get(b0,set,"b0",UI::compulsory); |
---|
86 | double P0; |
---|
87 | UI::get(P0,set,"P0",UI::compulsory); |
---|
88 | |
---|
89 | mat V0(2,2); |
---|
90 | V0(0,0) = 0.1; |
---|
91 | V0(1,0) = b0; |
---|
92 | V0(0,1) = b0; |
---|
93 | V0(1,1) = P0/6; |
---|
94 | |
---|
95 | est.set_statistics(1,V0,6); |
---|
96 | est.set_constant(false); |
---|
97 | est.validate(); |
---|
98 | validate(); |
---|
99 | } |
---|
100 | void validate(){ |
---|
101 | Psi = zeros(2); |
---|
102 | } |
---|
103 | void log_register( logger& L, const string& prefix ){ |
---|
104 | est.set_log_level(1); |
---|
105 | est.log_register(L,""); |
---|
106 | }; |
---|
107 | void log_write() const{ |
---|
108 | est.log_write(); |
---|
109 | } |
---|
110 | }; |
---|
111 | UIREGISTER(ce_ctrl); |
---|
112 | |
---|
113 | #endif //ARX_CTRL |
---|