1 | /*! |
---|
2 | \file |
---|
3 | \brief ARX Agents for MPDM |
---|
4 | \author Vaclav Smidl. |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef ARXAG_H |
---|
8 | #define ARXAG_H |
---|
9 | |
---|
10 | |
---|
11 | #include <design/arx_ctrl.h> |
---|
12 | #include <stat/merger.h> |
---|
13 | #include <base/participants.h> |
---|
14 | |
---|
15 | |
---|
16 | namespace bdm { |
---|
17 | |
---|
18 | /*! |
---|
19 | \brief ARX agent |
---|
20 | |
---|
21 | */ |
---|
22 | class ARXAgent : public Participant { |
---|
23 | protected: |
---|
24 | //! Pointers to neighbours |
---|
25 | Array<string > neighbours; |
---|
26 | |
---|
27 | //! Internal ARX Controller |
---|
28 | shared_ptr<LQG_ARX> lqg_arx; |
---|
29 | //! Merger for predictors from neighbours |
---|
30 | shared_ptr<merger_mix > merger; |
---|
31 | //! All available predictors |
---|
32 | Array<shared_ptr<pdf> > preds; |
---|
33 | //! My own predictor |
---|
34 | mlnorm_chmat_ptr my_pred; |
---|
35 | |
---|
36 | //! data link of data |
---|
37 | datalink_part dlU; |
---|
38 | //! data link of |
---|
39 | datalink_buffered dlDt; |
---|
40 | |
---|
41 | //! internal data vector for controller |
---|
42 | vec dt; |
---|
43 | //! internal data vector for controller output |
---|
44 | vec ut; |
---|
45 | public: |
---|
46 | void validate() { |
---|
47 | lqg_arx->validate(); |
---|
48 | my_pred=lqg_arx->_ar().ml_predictor<chmat>(); |
---|
49 | } |
---|
50 | void receive(const Setting &msg) { |
---|
51 | string what; |
---|
52 | UI::get(what, msg, "what", UI::compulsory); |
---|
53 | |
---|
54 | if (what=="predictor") { // add predictor |
---|
55 | preds.set_length(preds.length()+1, true); |
---|
56 | preds(preds.length()-1) = UI::build<pdf>(msg["data"]); |
---|
57 | } else { |
---|
58 | bdm_warning("Unknown message of type "+what); |
---|
59 | } |
---|
60 | } |
---|
61 | void broadcast(Setting& set) { |
---|
62 | // broadcast predictor (created in adapt) to all neighbours |
---|
63 | for (int i=0; i<neighbours.length(); i++) { |
---|
64 | Setting &msg =set.add(Setting::TypeGroup); |
---|
65 | create_message(msg, neighbours(i), "predictor", *preds(0).get()); |
---|
66 | } |
---|
67 | } |
---|
68 | void adapt(const vec &glob_dt) { |
---|
69 | dlDt.filldown(glob_dt, dt); |
---|
70 | dlDt.store_data(glob_dt); // do not ever use dlDt again! |
---|
71 | |
---|
72 | lqg_arx->adapt(dt); |
---|
73 | // remove old predictors |
---|
74 | preds.set_length(1, false); |
---|
75 | lqg_arx->_ar().ml_predictor_update<chmat>(*my_pred) ; |
---|
76 | preds(0)=my_pred; |
---|
77 | |
---|
78 | // we arer finished with datasource |
---|
79 | } |
---|
80 | void act(vec &glob_ut) { |
---|
81 | if (preds.length()>1) { |
---|
82 | merger->set_sources(preds); |
---|
83 | if (preds.length()>1) { |
---|
84 | cout<<preds(1)->to_string()<<endl; |
---|
85 | } |
---|
86 | merger->merge(); |
---|
87 | enorm<chmat> joint_pred; |
---|
88 | mat Cov=merger->merger().covariance(); |
---|
89 | if (sumsum(Cov)==0.0) { |
---|
90 | bdm_error("merging failed"); |
---|
91 | } |
---|
92 | joint_pred.set_parameters(merger->merger().mean(), Cov); |
---|
93 | joint_pred.set_rv(merger->merger()._rv()); |
---|
94 | |
---|
95 | enorm<chmat> marg_pred; // remove rvs taht should not be there |
---|
96 | joint_pred.marginal(concat(preds(0)->_rv(), preds(0)->_rvc()), marg_pred); |
---|
97 | |
---|
98 | // assign |
---|
99 | mlnorm<chmat> *merg_pred = lqg_arx->_pred(); |
---|
100 | marg_pred.condition(preds(0)->_rv(),*merg_pred); |
---|
101 | } else { |
---|
102 | lqg_arx->_ar().ml_predictor_update<chmat>(*lqg_arx->_pred()) ; |
---|
103 | } |
---|
104 | // |
---|
105 | lqg_arx->redesign(); |
---|
106 | ut=lqg_arx->ctrlaction(dt); |
---|
107 | dlU.filldown(ut,glob_ut); |
---|
108 | } |
---|
109 | virtual void ds_register(const DS &ds) { |
---|
110 | dlDt.set_connection(lqg_arx->_rvc(), ds._drv() ); |
---|
111 | dlU.set_connection(ds._urv() , lqg_arx->_rv() ); |
---|
112 | dt=zeros(lqg_arx->_rvc()._dsize()); |
---|
113 | ut=zeros(lqg_arx->_rv()._dsize()); |
---|
114 | } |
---|
115 | void from_setting(const Setting &set) { |
---|
116 | Participant::from_setting(set); |
---|
117 | lqg_arx = UI::build<LQG_ARX>(set, "lqg_arx",UI::compulsory); |
---|
118 | merger = UI::build<merger_mix>(set, "merger", UI::compulsory); |
---|
119 | UI::get(neighbours, set, "neighbours",UI::compulsory); |
---|
120 | validate(); |
---|
121 | } |
---|
122 | }; |
---|
123 | //UIREGISTER(LQG_ARX); |
---|
124 | UIREGISTER(ARXAgent); |
---|
125 | |
---|
126 | }//namespace |
---|
127 | #endif //ARXAG_H |
---|