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