1 | /*! |
---|
2 | \file |
---|
3 | \brief Traffic Light Agents |
---|
4 | \author Vaclav Smidl. |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef TRAGE_H |
---|
8 | #define TRAGE_H |
---|
9 | |
---|
10 | |
---|
11 | #include <base/participants.h> |
---|
12 | |
---|
13 | using namespace bdm; |
---|
14 | |
---|
15 | //! detector of traffic variables |
---|
16 | class Detector{ |
---|
17 | public: |
---|
18 | string name; //! detector name |
---|
19 | int distance; //! distance from stop-line |
---|
20 | //! function loading info from Setting |
---|
21 | void from_setting(const Setting &set){ |
---|
22 | UI::get(name,set,"name",UI::compulsory); |
---|
23 | UI::get(distance,set,"distance",UI::compulsory); |
---|
24 | } |
---|
25 | }; |
---|
26 | |
---|
27 | //! detector fo incomming flow |
---|
28 | class DetectorIn : public Detector{ |
---|
29 | string from; // from agent |
---|
30 | Array<string> to; // to signal plans |
---|
31 | void from_setting(const Setting &set){ |
---|
32 | Detector::from_setting(set); |
---|
33 | UI::get(from,set,"from",UI::compulsory); |
---|
34 | UI::get(to, set, "to", UI::compulsory); |
---|
35 | } |
---|
36 | }; |
---|
37 | |
---|
38 | //! structure for output detectors |
---|
39 | class RequestOut { |
---|
40 | public: |
---|
41 | string to; // agent |
---|
42 | Array<string> from; //detectors |
---|
43 | void from_setting(const Setting &set){ |
---|
44 | UI::get(from,set,"from",UI::compulsory); |
---|
45 | UI::get(to, set, "to", UI::compulsory); |
---|
46 | } |
---|
47 | }; |
---|
48 | |
---|
49 | |
---|
50 | //! class with physical information about a signal group |
---|
51 | class SignalGroup { |
---|
52 | public: |
---|
53 | string name; //! names of the group |
---|
54 | Array<Detector> detectors; //! (possible) detectors |
---|
55 | |
---|
56 | //! function that loads information from Setting |
---|
57 | void from_setting(const Setting &set); |
---|
58 | }; |
---|
59 | |
---|
60 | //! class that operates on a signal group |
---|
61 | class SignalGroupHandler { |
---|
62 | protected: |
---|
63 | //! pointer to physical signal group |
---|
64 | SignalGroup *sg; |
---|
65 | |
---|
66 | public: |
---|
67 | //! actual data from the relevant signal group |
---|
68 | vec det_data; |
---|
69 | //! description of det_data |
---|
70 | RV rv_det_data; |
---|
71 | //! link from global measured data |
---|
72 | datalink ds2data; |
---|
73 | //! link from data to global out_data |
---|
74 | datalink data2out; |
---|
75 | public: |
---|
76 | SignalGroupHandler(SignalGroup *sg0, const string &agent_name):rv_det_data(){ |
---|
77 | sg=sg0; |
---|
78 | for (int i=0;i<sg->detectors.length();i++){ |
---|
79 | rv_det_data.add(RV(agent_name + sg->detectors(i).name, 1)); //TODO intensity occupancy |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?) |
---|
85 | double expected_load(){ |
---|
86 | if (det_data.length()>0){ |
---|
87 | return det_data(0); // whatever |
---|
88 | } else { |
---|
89 | return 1.0; // mean value |
---|
90 | } |
---|
91 | } |
---|
92 | }; |
---|
93 | |
---|
94 | class RequestHandler{ |
---|
95 | protected: |
---|
96 | RequestOut *rq; |
---|
97 | public: |
---|
98 | RV rv; |
---|
99 | datalink outdata2out; |
---|
100 | public: |
---|
101 | RequestHandler(RequestOut *rq0, const string &agent_name){ |
---|
102 | rq=rq0; |
---|
103 | for (int i=0;i<rq->from.length();i++){ |
---|
104 | rv.add(RV(agent_name + rq->from(i), 1)); //TODO intensity occupancy |
---|
105 | } |
---|
106 | } |
---|
107 | }; |
---|
108 | |
---|
109 | /*! |
---|
110 | \brief Basic Traffic Agent |
---|
111 | |
---|
112 | */ |
---|
113 | class BaseTrafficAgent : public Participant { |
---|
114 | protected: |
---|
115 | //! Signal Groups |
---|
116 | Array<SignalGroup> sg; |
---|
117 | |
---|
118 | Array<SignalGroupHandler> sgh; |
---|
119 | |
---|
120 | //!data from messages |
---|
121 | vec input_data; |
---|
122 | |
---|
123 | //! decription of msg_data |
---|
124 | RV input_rv; |
---|
125 | |
---|
126 | //! data to broadcast |
---|
127 | vec output_data; |
---|
128 | |
---|
129 | //! description of broadcast dataind |
---|
130 | RV output_rv; |
---|
131 | |
---|
132 | RV action_rv; |
---|
133 | |
---|
134 | datalink_part action2ds; |
---|
135 | |
---|
136 | //! output recepient |
---|
137 | Array<RequestOut> requests; |
---|
138 | Array<RequestHandler> request_handler; |
---|
139 | |
---|
140 | public: |
---|
141 | void validate(){ |
---|
142 | // write internal checks if all was loaded OK |
---|
143 | } |
---|
144 | void receive(const Setting &msg){ |
---|
145 | string what; |
---|
146 | UI::get(what, msg, "what", UI::compulsory); |
---|
147 | |
---|
148 | if (what=="data"){ // |
---|
149 | // field data |
---|
150 | //! decription of detected data |
---|
151 | shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); |
---|
152 | ivec ind=rv->dataind(input_rv); // position of rv in in_rv; |
---|
153 | if (ind.length()>0){ //data belong here |
---|
154 | vec dt; |
---|
155 | UI::get(dt, msg, "value",UI::compulsory); |
---|
156 | set_subvector(input_data, ind, dt); //check size? |
---|
157 | } |
---|
158 | } else { |
---|
159 | bdm_warning("Unknown message of type "+what); |
---|
160 | } |
---|
161 | } |
---|
162 | void broadcast(Setting& set){ |
---|
163 | // broadcast data to all neighbours |
---|
164 | for (int i=0; i<request_handler.length(); i++){ |
---|
165 | Setting &msg =set.add(Setting::TypeGroup); |
---|
166 | RequestHandler &R=request_handler(i); |
---|
167 | |
---|
168 | // copy from create message |
---|
169 | Setting &m_to=msg.add("to",Setting::TypeString); |
---|
170 | m_to=requests(i).to; |
---|
171 | Setting &m_what=msg.add("what",Setting::TypeString); |
---|
172 | m_what="data"; |
---|
173 | //Setting &m_data=msg.add("data",Setting::TypeGroup); |
---|
174 | Setting &m_rv=msg.add("rv",Setting::TypeGroup); |
---|
175 | R.rv.to_setting(m_rv); |
---|
176 | Setting &m_val=msg.add("value",Setting::TypeGroup); |
---|
177 | vec val =R.outdata2out.pushdown(output_data); |
---|
178 | UI::save(val, m_val); |
---|
179 | } |
---|
180 | } |
---|
181 | void adapt(const vec &glob_dt){ |
---|
182 | // copy data from global vector to sSGHandlers |
---|
183 | for (int i=0; i<sgh.length();i++){ |
---|
184 | sgh(i).ds2data.filldown(glob_dt, sgh(i).det_data); |
---|
185 | } |
---|
186 | // copy sg_length ... |
---|
187 | } |
---|
188 | void act(vec &glob_ut){ |
---|
189 | vec needs(sgh.length()); |
---|
190 | for (int i=0; i<sgh.length();i++){ |
---|
191 | needs(i) = sgh(i).expected_load(); |
---|
192 | } |
---|
193 | vec action = needs/sum(needs); // trivial stuff |
---|
194 | action2ds.filldown(action,glob_ut); |
---|
195 | } |
---|
196 | |
---|
197 | void ds_register(const DS &ds){ |
---|
198 | for (int i=0; i<sgh.length(); i++){ |
---|
199 | sgh(i).ds2data.set_connection(ds._drv(), sgh(i).rv_det_data); |
---|
200 | } |
---|
201 | action2ds.set_connection(action_rv, ds._urv()); |
---|
202 | } |
---|
203 | |
---|
204 | void from_setting(const Setting &set){}; |
---|
205 | }; |
---|
206 | UIREGISTER(BaseTrafficAgent); |
---|
207 | |
---|
208 | #endif //TRAGE_H |
---|