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 | class BaseTrafficAgent; |
---|
16 | |
---|
17 | //! detector of traffic variables |
---|
18 | class Lane{ |
---|
19 | public: |
---|
20 | Array<string> inputs; |
---|
21 | Array<string> outputs; |
---|
22 | vec alpha; //size of outputs |
---|
23 | string queue; |
---|
24 | string sg; |
---|
25 | |
---|
26 | //! function loading info from Setting |
---|
27 | void from_setting(const Setting &set){ |
---|
28 | UI::get(inputs,set,"inputs",UI::compulsory); |
---|
29 | UI::get(outputs,set,"outputs",UI::compulsory); |
---|
30 | UI::get(alpha,set,"alpha",UI::compulsory); |
---|
31 | UI::get(queue,set,"queue",UI::compulsory); |
---|
32 | UI::get(sg,set,"sg",UI::compulsory); |
---|
33 | |
---|
34 | } |
---|
35 | }; |
---|
36 | |
---|
37 | //! class that operates on a signal group |
---|
38 | class LaneHandler { |
---|
39 | protected: |
---|
40 | //! pointer to physical lane |
---|
41 | const Lane &lane; |
---|
42 | //! agent pointer |
---|
43 | BaseTrafficAgent *agent; |
---|
44 | |
---|
45 | public: |
---|
46 | //! actual data from the relevant signal group |
---|
47 | vec inputs; |
---|
48 | //! |
---|
49 | double queue; |
---|
50 | //! |
---|
51 | //! description of det_data |
---|
52 | RV rv_inputs; |
---|
53 | //! description of det_data |
---|
54 | RV rv_outputs; |
---|
55 | //! description of det_data |
---|
56 | RV rv_queue; |
---|
57 | //! link from global measured data |
---|
58 | datalink agentin2input; |
---|
59 | //! |
---|
60 | datalink output2agentout; |
---|
61 | //! |
---|
62 | int queue_index; |
---|
63 | public: |
---|
64 | LaneHandler(const Lane &lane0): lane(lane0){ |
---|
65 | for (int i=0;i<lane0.inputs.length();i++){ |
---|
66 | rv_inputs.add(RV(lane.inputs(i), 2)); |
---|
67 | } |
---|
68 | for (int i=0;i<lane0.outputs.length();i++){ |
---|
69 | rv_outputs.add(RV(lane.outputs(i), 2)); |
---|
70 | } |
---|
71 | rv_queue.add(RV(lane.queue, 1)); |
---|
72 | } |
---|
73 | |
---|
74 | void connect_data(BaseTrafficAgent &agent0); |
---|
75 | |
---|
76 | //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?) |
---|
77 | double expected_output(double green_time); |
---|
78 | }; |
---|
79 | |
---|
80 | /*! |
---|
81 | \brief Basic Traffic Agent |
---|
82 | |
---|
83 | */ |
---|
84 | class BaseTrafficAgent : public Participant { |
---|
85 | LOG_LEVEL(BaseTrafficAgent,logdata); |
---|
86 | public: |
---|
87 | //! Signal Groups |
---|
88 | Array<Lane> lanes; |
---|
89 | |
---|
90 | Array<LaneHandler*> lanehs; |
---|
91 | |
---|
92 | //!data from messages |
---|
93 | vec inputs; |
---|
94 | |
---|
95 | //! decription of msg_data |
---|
96 | RV rv_inputs; |
---|
97 | |
---|
98 | //! data to broadcast |
---|
99 | vec outputs; |
---|
100 | |
---|
101 | //! description of broadcast dataind |
---|
102 | RV rv_outputs; |
---|
103 | |
---|
104 | vec queues; |
---|
105 | |
---|
106 | //! description of queues |
---|
107 | RV rv_queues; |
---|
108 | |
---|
109 | //! datalink from DS to output variables |
---|
110 | datalink ds2inputs; |
---|
111 | |
---|
112 | //! datalink from DS to output variables |
---|
113 | datalink ds2queues; |
---|
114 | |
---|
115 | //! action description |
---|
116 | RV action_rv; |
---|
117 | |
---|
118 | datalink_part action2ds; |
---|
119 | |
---|
120 | Array<string> neighbours; |
---|
121 | |
---|
122 | Array<RV> rv_neighbours_out; |
---|
123 | |
---|
124 | Array<datalink> output2neighbour; |
---|
125 | |
---|
126 | public: |
---|
127 | void validate(){ |
---|
128 | lanehs.set_length(lanes.length()); |
---|
129 | for (int l=0; l<lanes.length(); l++){ |
---|
130 | lanehs(l) = new LaneHandler(lanes(l)); |
---|
131 | |
---|
132 | rv_inputs.add(lanehs(l)->rv_inputs); |
---|
133 | rv_outputs.add(lanehs(l)->rv_outputs); |
---|
134 | rv_queues.add(lanehs(l)->rv_queue); |
---|
135 | } |
---|
136 | inputs.set_size(rv_inputs._dsize()); |
---|
137 | outputs.set_size(rv_outputs._dsize()); |
---|
138 | queues.set_size(rv_queues._dsize()); |
---|
139 | |
---|
140 | for (int l=0; l<lanes.length(); l++){ |
---|
141 | lanehs(l)->connect_data(*this); |
---|
142 | } |
---|
143 | |
---|
144 | //for -- rv_outputs -- |
---|
145 | // TODO vybrat rv pro sousedy |
---|
146 | rv_neighbours_out.set_length(neighbours.length()); |
---|
147 | output2neighbour.set_length(neighbours.length()); |
---|
148 | |
---|
149 | for (int i=0; i<neighbours.length(); i++){ |
---|
150 | for (int r=0; r<rv_outputs.length(); r++){ |
---|
151 | int str_pos = rv_outputs.name(r).compare(neighbours(i)); |
---|
152 | if (str_pos>neighbours(i).length()){ |
---|
153 | rv_neighbours_out(i).add(rv_outputs.subselect(vec_1(r))); |
---|
154 | } |
---|
155 | } |
---|
156 | // connect datasource |
---|
157 | output2neighbour(i).set_connection(rv_neighbours_out(i), rv_outputs); |
---|
158 | } |
---|
159 | |
---|
160 | // lanehs knows RVS |
---|
161 | // write internal checks if all was loaded OK |
---|
162 | |
---|
163 | } |
---|
164 | void receive(const Setting &msg){ |
---|
165 | string what; |
---|
166 | UI::get(what, msg, "what", UI::compulsory); |
---|
167 | |
---|
168 | if (what=="new_stable_state"){ // |
---|
169 | // field data |
---|
170 | // extract decription of teh received datavector |
---|
171 | shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); |
---|
172 | // find if it is needed |
---|
173 | ivec ind=rv->dataind(rv_inputs); // position of rv in in_rv; |
---|
174 | if (ind.length()>0){ //data are interesting |
---|
175 | vec dt; |
---|
176 | UI::get(dt, msg, "value",UI::compulsory); // get data |
---|
177 | set_subvector(inputs, ind, dt); //check size? |
---|
178 | } |
---|
179 | } else { |
---|
180 | Participant::receive(msg); |
---|
181 | } |
---|
182 | } |
---|
183 | void log_register(logger &L, const string &prefix){ |
---|
184 | root::log_register ( L, prefix ); |
---|
185 | if ( log_level[logdata]){ |
---|
186 | L.add_vector ( log_level, logdata, RV ( 1 ), prefix ); |
---|
187 | } |
---|
188 | } |
---|
189 | void log_write() const { |
---|
190 | if (log_level[logdata]){ |
---|
191 | log_level.store(logdata, inputs); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | void broadcast(Setting& set){ |
---|
196 | // broadcast data to all neighbours |
---|
197 | for (int i=0; i<neighbours.length(); i++){ |
---|
198 | Setting &msg =set.add(Setting::TypeGroup); |
---|
199 | |
---|
200 | // if... |
---|
201 | // copy from create message |
---|
202 | // create msg with fields {to=..., what=data, rv=..., value = ...} |
---|
203 | UI::save ( neighbours(i), msg, "to"); |
---|
204 | UI::save ( (string)"new_stable_state", msg, "what"); |
---|
205 | UI::save ( &(rv_neighbours_out(i)), msg, "rv"); |
---|
206 | UI::save( output2neighbour(i).pushdown(outputs), msg, "value"); |
---|
207 | } |
---|
208 | |
---|
209 | } |
---|
210 | void adapt(const vec &glob_dt){ |
---|
211 | // copy data from global vector to sSGHandlers |
---|
212 | ds2inputs.filldown(glob_dt, inputs); |
---|
213 | //copy data fro neighbours |
---|
214 | ds2queues.filldown(glob_dt, queues); |
---|
215 | // copy sg_length ... and others... |
---|
216 | } |
---|
217 | void act(vec &glob_ut){ |
---|
218 | vec action; // trivial stuff |
---|
219 | action2ds.filldown(action,glob_ut); |
---|
220 | } |
---|
221 | |
---|
222 | void ds_register(const DS &ds){ |
---|
223 | //register ds2output |
---|
224 | ds2inputs.set_connection(rv_inputs, ds._drv()); |
---|
225 | ds2queues.set_connection(rv_queues, ds._drv()); |
---|
226 | inputs.set_size(rv_inputs._dsize()); |
---|
227 | action2ds.set_connection( ds._urv(), action_rv); |
---|
228 | } |
---|
229 | |
---|
230 | void from_setting(const Setting &set); |
---|
231 | }; |
---|
232 | UIREGISTER(BaseTrafficAgent); |
---|
233 | |
---|
234 | #endif //TRAGE_H |
---|