[986] | 1 | #include "traffic_agent.h" |
---|
| 2 | #include <list> |
---|
[1071] | 3 | #include <fstream> |
---|
[941] | 4 | |
---|
[921] | 5 | class GreenWaveTrafficAgent : public BaseTrafficAgent { |
---|
[1111] | 6 | LOG_LEVEL(GreenWaveTrafficAgent,logoffset); |
---|
[927] | 7 | protected: |
---|
[1026] | 8 | double rating_change; |
---|
[1135] | 9 | int negot_start; |
---|
[1146] | 10 | int negot_offset; |
---|
[1111] | 11 | int negot_limit; |
---|
[986] | 12 | |
---|
[1071] | 13 | int actual_time; |
---|
| 14 | |
---|
| 15 | RV rv_outputs; |
---|
| 16 | vec outputs; |
---|
| 17 | |
---|
[1026] | 18 | RV rv_change_request; |
---|
| 19 | vec change_request; |
---|
| 20 | |
---|
[986] | 21 | RV rv_recieved_exps; |
---|
| 22 | vec recieved_exps; |
---|
[1026] | 23 | |
---|
[1071] | 24 | RV rv_next_exps; |
---|
| 25 | vec next_exps; |
---|
| 26 | |
---|
[1127] | 27 | //! expectations recieved from neighbours |
---|
[1026] | 28 | RV rv_recieved_changes; |
---|
| 29 | vec recieved_changes; |
---|
| 30 | |
---|
[1127] | 31 | //! list of agents, who request expected cars |
---|
| 32 | list<string> requesters; |
---|
[1133] | 33 | |
---|
| 34 | //! offset set in last simulation step |
---|
| 35 | int last_offset; |
---|
| 36 | //! actual planned offset to set for next simulation step |
---|
| 37 | int planned_offset; |
---|
| 38 | //! rating of actual planned offset |
---|
| 39 | double planned_rating; |
---|
| 40 | //! avarage speed of cars |
---|
| 41 | int VP; |
---|
[986] | 42 | |
---|
[1135] | 43 | double car_leaving_time; //s; how long is 1 car leaving queue |
---|
[941] | 44 | |
---|
[1081] | 45 | // some state variables |
---|
[1026] | 46 | bool need_exps; |
---|
| 47 | bool new_stable_state; |
---|
| 48 | bool send_requests; |
---|
| 49 | bool final_state; |
---|
[1127] | 50 | |
---|
| 51 | //! determines wheteher agent actively communicates |
---|
[1111] | 52 | int passive; |
---|
| 53 | |
---|
[1081] | 54 | //! sum of final planned_offset values since last reach of cycle_count |
---|
| 55 | int total_offset; |
---|
| 56 | //! number of finished cycles since last reach of cycle_count |
---|
| 57 | int negot_cycle; |
---|
| 58 | //! after cycle_count cycles, we count avarege planned_offseta send it to Aimsun |
---|
| 59 | int cycle_count; |
---|
[1133] | 60 | //! Finding of best own offsset starts on this offset change |
---|
| 61 | int find_best_start; |
---|
| 62 | //! minimal value of offsset change tested during looking for best offset |
---|
| 63 | int find_best_limit; |
---|
[1081] | 64 | |
---|
[1026] | 65 | //! counts all expected cars going from each lane, saves to outputs and rv_outputs |
---|
[1071] | 66 | void expected_cars() { |
---|
[986] | 67 | double start_time; |
---|
[1071] | 68 | ivec ind; |
---|
| 69 | RV rv_exp; |
---|
| 70 | datalink exp2outputs; |
---|
| 71 | vec exp; |
---|
[1127] | 72 | string group_name; |
---|
| 73 | double green_time; |
---|
| 74 | int index; |
---|
[986] | 75 | |
---|
[941] | 76 | for (int i=0;i<lanes.length();i++) { |
---|
| 77 | for (int j=0;j<lanes(i).outputs.length();j++) { |
---|
[1071] | 78 | if (lanes(i).outputs(j)!="DUMMY_DET") { |
---|
[1127] | 79 | group_name = name+"_"+lanes(i).sg; //e.g. 495_VA |
---|
[1071] | 80 | |
---|
[1127] | 81 | index=group_index(group_name); |
---|
| 82 | green_time=green_times(index)*cycle_length; |
---|
[941] | 83 | |
---|
[1071] | 84 | rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3); |
---|
| 85 | exp.set_size(rv_exp._dsize()); |
---|
| 86 | exp2outputs.set_connection(rv_exp,rv_outputs); |
---|
| 87 | |
---|
[1133] | 88 | //cars density |
---|
| 89 | exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j) / green_time; |
---|
[1071] | 90 | |
---|
[1146] | 91 | start_time = green_starts(group_index(name+"_"+lanes(i).sg)) + (3.6*lanes(i).output_distances(j))/(VP) + planned_offset; |
---|
[1071] | 92 | //first car arrive time |
---|
| 93 | exp(1)=start_time; |
---|
| 94 | //last car arrive time |
---|
| 95 | exp(2)=start_time + green_time; |
---|
[1133] | 96 | |
---|
[1071] | 97 | exp2outputs.pushup(outputs,exp); |
---|
| 98 | } |
---|
[941] | 99 | } |
---|
| 100 | } |
---|
[1026] | 101 | }; |
---|
[1133] | 102 | |
---|
| 103 | /*! creates periodic expansion of gb (green begin], ge (green end), using |
---|
| 104 | expectations cb (cars begin), ce (cars end). Result is stored in gbv |
---|
| 105 | (green begin vector) and gev (green end vector) */ |
---|
| 106 | void expand_greens(const double gb, const double ge, const vec cb, const vec ce, vec &gbv, vec &gev) { |
---|
| 107 | gbv.set_size(1); |
---|
| 108 | gev.set_size(1); |
---|
| 109 | gbv(0)=gb; |
---|
| 110 | gev(0)=ge; |
---|
| 111 | if (ge>cycle_length) { |
---|
| 112 | gbv.ins(gbv.length(),0); |
---|
| 113 | gev.ins(gev.length(),ge-cycle_length); |
---|
| 114 | } |
---|
| 115 | int i=1; |
---|
| 116 | while (gev(gev.length()-1)<ce(max_i(ce))) { |
---|
| 117 | gbv.ins(gbv.length(),gb+i*cycle_length); |
---|
| 118 | gev.ins(gev.length(),ge+i*cycle_length); |
---|
| 119 | i++; |
---|
| 120 | } |
---|
| 121 | }; |
---|
| 122 | |
---|
| 123 | //! returns true if what is in interval <left;right> |
---|
| 124 | bool in_interval(double what, const double left, const double right) { |
---|
| 125 | return ((what>=left && what<=right) ? true : false); |
---|
| 126 | } |
---|
[941] | 127 | |
---|
[1026] | 128 | //! counts planned rating using offset and recieved_exps |
---|
[1127] | 129 | double count_rating(const int offset, const vec recieved_exps, const RV rv_recieved_exps) { |
---|
[986] | 130 | double virtual_queue; |
---|
| 131 | double t_green_begin; |
---|
| 132 | double t_green_end; |
---|
[1133] | 133 | vec cars_density; |
---|
[1071] | 134 | vec t_cars_begin; |
---|
| 135 | vec t_cars_end; |
---|
[986] | 136 | bool found; |
---|
[1127] | 137 | ivec ind; |
---|
| 138 | string group_name; |
---|
[941] | 139 | |
---|
[1026] | 140 | double rating=0.0; |
---|
[986] | 141 | |
---|
[1026] | 142 | for (int i=0;i<lanes.length();i++) { |
---|
[986] | 143 | |
---|
| 144 | //Finding, if we have some expectations |
---|
| 145 | found=false; |
---|
[1026] | 146 | for (int k=0;k<lanes(i).inputs.length();k++) { |
---|
[1071] | 147 | int l=0; |
---|
[1026] | 148 | for (int j=0;j<rv_recieved_exps.length();j++) { |
---|
[986] | 149 | int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-"); |
---|
| 150 | if (result>=0) { |
---|
[1081] | 151 | t_cars_begin.set_size(l+1,true); |
---|
| 152 | t_cars_end.set_size(l+1,true); |
---|
[1133] | 153 | cars_density.set_size(l+1,true); |
---|
[1081] | 154 | |
---|
[1127] | 155 | ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); |
---|
[1081] | 156 | |
---|
[1133] | 157 | cars_density(l)=recieved_exps(ind(0)); |
---|
[1071] | 158 | t_cars_begin(l)=recieved_exps(ind(1)); |
---|
| 159 | t_cars_end(l)=recieved_exps(ind(2)); |
---|
| 160 | l++; |
---|
| 161 | |
---|
[986] | 162 | found=true; |
---|
| 163 | } |
---|
| 164 | } |
---|
[1118] | 165 | } |
---|
| 166 | if (found) { |
---|
| 167 | //counting rating |
---|
[1127] | 168 | group_name = name+"_"+lanes(i).sg; //e.g. 495_VA |
---|
| 169 | int index=group_index(group_name); |
---|
[986] | 170 | |
---|
[1118] | 171 | t_green_begin=green_starts(index) + offset; |
---|
[1127] | 172 | t_green_end=t_green_begin + green_times(index)*cycle_length; |
---|
[986] | 173 | |
---|
[1133] | 174 | vec t_gb_vec; |
---|
| 175 | vec t_ge_vec; |
---|
[986] | 176 | |
---|
[1133] | 177 | expand_greens(t_green_begin, t_green_end, t_cars_begin, t_cars_end, t_gb_vec, t_ge_vec); |
---|
| 178 | |
---|
| 179 | //! index for t_cars_begin |
---|
| 180 | int k=min_i(t_cars_begin); |
---|
| 181 | //! index for t_ggb_vec |
---|
| 182 | int l; |
---|
| 183 | //! indicator of actual position in whole interval |
---|
| 184 | double t_act=0; |
---|
| 185 | //! end of counting for actual line |
---|
| 186 | double t_limit=t_ge_vec(max_i(t_ge_vec)); |
---|
| 187 | //! end of closest future interval |
---|
| 188 | double t_end; |
---|
| 189 | |
---|
[1118] | 190 | virtual_queue=lanehs(i)->queue; |
---|
[1071] | 191 | |
---|
[1118] | 192 | //cycle goes through all "stopping" points and counts queue lenght at these points |
---|
[1133] | 193 | do { |
---|
[1118] | 194 | k=min_i(t_cars_begin); |
---|
[1133] | 195 | l=min_i(t_gb_vec); |
---|
| 196 | if (k!=-1) { |
---|
| 197 | //cars are entering queue |
---|
| 198 | if (in_interval(t_act,t_cars_begin(k), t_cars_end(k))) { |
---|
| 199 | //cars leaving and entering queue |
---|
| 200 | if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { |
---|
| 201 | t_end = min(t_cars_end(k),t_ge_vec(l)); |
---|
[1135] | 202 | virtual_queue+=(t_end - t_act)*(cars_density(k)-(1/car_leaving_time)); |
---|
[1133] | 203 | t_cars_begin(k)=t_end; |
---|
| 204 | t_gb_vec(l)=t_end; |
---|
[1118] | 205 | } |
---|
[1133] | 206 | //cars only entering queue |
---|
[1118] | 207 | else { |
---|
[1133] | 208 | t_end=min(t_cars_end(k),t_ge_vec(l)); |
---|
| 209 | virtual_queue+=(t_end-t_act)*cars_density(k); |
---|
| 210 | t_cars_begin(k)=t_end; |
---|
[1118] | 211 | } |
---|
| 212 | } |
---|
[1133] | 213 | //cars are not entering queue |
---|
| 214 | else { |
---|
| 215 | //cars are only leaving queue |
---|
| 216 | if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { |
---|
| 217 | t_end = min(t_ge_vec(l),t_cars_begin(k)); |
---|
[1135] | 218 | virtual_queue-=(t_end-t_act)/car_leaving_time; |
---|
[1133] | 219 | t_gb_vec(l)=t_end; |
---|
| 220 | |
---|
| 221 | //in case we emptied whole queue |
---|
| 222 | virtual_queue=max(virtual_queue,0.0); |
---|
[1071] | 223 | } |
---|
[1133] | 224 | //no cars entering, no cars leaving |
---|
[1071] | 225 | else { |
---|
[1133] | 226 | t_end=min(t_gb_vec(l),t_cars_begin(k)); |
---|
[1071] | 227 | } |
---|
[1133] | 228 | } |
---|
[1136] | 229 | t_act=t_end; |
---|
| 230 | |
---|
[1133] | 231 | //raising rating |
---|
| 232 | if (virtual_queue<0) { |
---|
| 233 | rating-=virtual_queue; |
---|
| 234 | virtual_queue=0; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | //deleting used intervals |
---|
| 238 | if (t_cars_begin(k)==t_cars_end(k)) { |
---|
[1120] | 239 | t_cars_begin.del(k); |
---|
| 240 | t_cars_end.del(k); |
---|
[1133] | 241 | cars_density.del(k); |
---|
[1071] | 242 | } |
---|
[1133] | 243 | if (t_gb_vec(l)==t_ge_vec(l)) { |
---|
| 244 | t_gb_vec.del(l); |
---|
| 245 | t_ge_vec.del(l); |
---|
[1071] | 246 | } |
---|
[1118] | 247 | } |
---|
| 248 | //if no other expectations found |
---|
| 249 | else { |
---|
[1135] | 250 | virtual_queue-=( t_ge_vec(l)-t_act)/car_leaving_time; |
---|
[1133] | 251 | t_act=t_ge_vec(l); |
---|
| 252 | t_gb_vec.del(l); |
---|
| 253 | t_ge_vec.del(l); |
---|
[1118] | 254 | } |
---|
[1133] | 255 | } while (t_act<t_limit); |
---|
[1071] | 256 | } |
---|
[986] | 257 | } |
---|
[1026] | 258 | return rating; |
---|
[986] | 259 | } |
---|
| 260 | |
---|
[1026] | 261 | //! finds best offset using recieved_exps. Returns found offset |
---|
| 262 | int find_best_offset(const int center, int interval) { |
---|
[1081] | 263 | //! rating if offset is rised |
---|
[1026] | 264 | double rating_p; |
---|
| 265 | //! rating if offset is unchaged (=center) |
---|
| 266 | double rating_c; |
---|
| 267 | //! rating if offset is lowered |
---|
| 268 | double rating_n; |
---|
[1133] | 269 | //! center point for next cycle |
---|
[1026] | 270 | int new_center; |
---|
| 271 | |
---|
[1127] | 272 | rating_p=count_rating(center+interval, recieved_exps, rv_recieved_exps); |
---|
| 273 | rating_c=count_rating(center, recieved_exps, rv_recieved_exps); |
---|
| 274 | rating_n=count_rating(center-interval, recieved_exps, rv_recieved_exps); |
---|
[1026] | 275 | |
---|
| 276 | new_center=center; |
---|
[1118] | 277 | int max_index=max_i_of_three(rating_p,rating_c,rating_n); |
---|
[1026] | 278 | switch (max_index) { |
---|
| 279 | case 0: |
---|
| 280 | new_center+=interval; |
---|
| 281 | break; |
---|
| 282 | case 1: |
---|
| 283 | break; |
---|
| 284 | case 2: |
---|
| 285 | new_center-=interval; |
---|
| 286 | break; |
---|
| 287 | } |
---|
| 288 | |
---|
[1133] | 289 | if (interval>find_best_limit) { |
---|
[1026] | 290 | interval/=2; |
---|
| 291 | new_center=find_best_offset(new_center,interval); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | return new_center; |
---|
| 295 | } |
---|
| 296 | |
---|
[1127] | 297 | //! finds if changing neighbour's offset could have positive effect, returns found offset change and stores chage of rating to rating_change |
---|
[1026] | 298 | int find_best_exps(const int offset_change, const string neighbour, double &rating_change) { |
---|
| 299 | //! expactations after positve change of neighbour's offset |
---|
| 300 | vec positive_exps; |
---|
| 301 | //! expactations after negative change of neighbour's offset |
---|
| 302 | vec negative_exps; |
---|
[1127] | 303 | //! rating if offset is raised |
---|
[1026] | 304 | double rating_p; |
---|
| 305 | //! rating if offset is unchaged |
---|
| 306 | double rating_c; |
---|
| 307 | //! rating if offset is lowered |
---|
| 308 | double rating_n; |
---|
[1133] | 309 | |
---|
[1026] | 310 | positive_exps=recieved_exps; |
---|
| 311 | negative_exps=recieved_exps; |
---|
| 312 | |
---|
| 313 | for (int j=0;j<rv_recieved_exps.length();j++) { |
---|
| 314 | int res = rv_recieved_exps.name(j).find("-"+neighbour); |
---|
| 315 | if (res>0) { |
---|
[1071] | 316 | ivec ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); |
---|
| 317 | |
---|
[1026] | 318 | positive_exps(ind(1))+=offset_change; |
---|
| 319 | positive_exps(ind(2))+=offset_change; |
---|
| 320 | |
---|
| 321 | negative_exps(ind(1))-=offset_change; |
---|
| 322 | negative_exps(ind(2))-=offset_change; |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
[1127] | 326 | rating_c=count_rating(planned_offset, recieved_exps, rv_recieved_exps); |
---|
[1146] | 327 | rating_p=count_rating(planned_offset, positive_exps, rv_recieved_exps); |
---|
| 328 | rating_n=count_rating(planned_offset, negative_exps, rv_recieved_exps); |
---|
| 329 | |
---|
[1118] | 330 | int max_index=max_i_of_three(rating_p,rating_c,rating_n); |
---|
[1026] | 331 | switch (max_index) { |
---|
| 332 | case 0: |
---|
| 333 | rating_change=rating_p-rating_c; |
---|
| 334 | return offset_change; |
---|
| 335 | break; |
---|
| 336 | case 1: |
---|
| 337 | rating_change=0; |
---|
| 338 | return 0; |
---|
| 339 | break; |
---|
| 340 | case 2: |
---|
| 341 | rating_change=rating_n-rating_c; |
---|
| 342 | return -offset_change; |
---|
| 343 | break; |
---|
| 344 | } |
---|
| 345 | rating_change=NULL; |
---|
| 346 | return NULL; |
---|
| 347 | } |
---|
| 348 | |
---|
[1127] | 349 | //! returns index of signal group "group" |
---|
[1071] | 350 | int group_index(const string group) { |
---|
| 351 | for (int i=0;i<green_names.length();i++) { |
---|
| 352 | if (green_names(i)==group) { |
---|
[1026] | 353 | return i; |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | return -1; |
---|
| 357 | } |
---|
| 358 | |
---|
[1081] | 359 | /*! |
---|
| 360 | returns offset value shifted to fit interval <-cycle_length/2;cycle_length/2> |
---|
| 361 | or (when second parameter is false)) <0;cycle_length> |
---|
| 362 | */ |
---|
| 363 | int normalize_offset(int offset, bool zero=true) { |
---|
| 364 | if (zero) { |
---|
| 365 | while ((offset<(-cycle_length/2)) || (offset>(cycle_length/2))) { |
---|
| 366 | if (offset<0) { |
---|
| 367 | offset+=cycle_length; |
---|
| 368 | } |
---|
| 369 | else { |
---|
| 370 | offset-=cycle_length; |
---|
| 371 | } |
---|
[1026] | 372 | } |
---|
[1081] | 373 | return offset; |
---|
| 374 | } |
---|
| 375 | else { |
---|
| 376 | while (offset<0 || offset>cycle_length) { |
---|
| 377 | if (offset<0) { |
---|
| 378 | offset+=cycle_length; |
---|
| 379 | } |
---|
| 380 | else { |
---|
| 381 | offset-=cycle_length; |
---|
| 382 | } |
---|
[1026] | 383 | } |
---|
[1081] | 384 | return offset; |
---|
[1026] | 385 | } |
---|
| 386 | } |
---|
[1081] | 387 | |
---|
| 388 | //! converts t to string |
---|
[1071] | 389 | template <class T> inline string to_string (const T& t) |
---|
| 390 | { |
---|
| 391 | std::stringstream ss; |
---|
| 392 | ss << t; |
---|
| 393 | return ss.str(); |
---|
| 394 | } |
---|
[1026] | 395 | |
---|
| 396 | //! returns index of maximum of entered values |
---|
[1118] | 397 | int max_i_of_three(const double a, const double b, const double c) { |
---|
[1026] | 398 | int index = a > b ? 0 : 1; |
---|
| 399 | |
---|
| 400 | if (index == 0) { |
---|
| 401 | index = a > c ? 0 : 2; |
---|
| 402 | } |
---|
| 403 | else { |
---|
| 404 | index = b > c ? 1 : 2; |
---|
| 405 | } |
---|
| 406 | return index; |
---|
| 407 | } |
---|
| 408 | |
---|
[1081] | 409 | //! returns index of smallest element in vector |
---|
[1071] | 410 | int min_i(vec vector) { |
---|
| 411 | if (vector.length()>0) { |
---|
| 412 | double min=vector(0); |
---|
| 413 | int index=0; |
---|
| 414 | for (int i=1;i<vector.length();i++) { |
---|
| 415 | if (vector(i)<min) { |
---|
| 416 | min=vector(i); |
---|
| 417 | index=i; |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | return index; |
---|
| 421 | } |
---|
| 422 | return -1; |
---|
| 423 | } |
---|
| 424 | |
---|
[1133] | 425 | //! returns index of largest element in vector |
---|
| 426 | int max_i(vec vector) { |
---|
| 427 | if (vector.length()>0) { |
---|
| 428 | double max=vector(0); |
---|
| 429 | int index=0; |
---|
| 430 | for (int i=1;i<vector.length();i++) { |
---|
| 431 | if (vector(i)>max) { |
---|
| 432 | max=vector(i); |
---|
| 433 | index=i; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | return index; |
---|
| 437 | } |
---|
| 438 | return -1; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | |
---|
[1026] | 442 | public: |
---|
| 443 | void validate() { |
---|
[1127] | 444 | rv_action = RV(name+"_offset", 1); |
---|
[1026] | 445 | |
---|
[1071] | 446 | for (int i=0; i<green_names.length();i++) { |
---|
| 447 | rv_inputs.add(RV(green_names(i),1)); |
---|
[1026] | 448 | } |
---|
| 449 | inputs.set_size(rv_inputs._dsize()); |
---|
| 450 | |
---|
| 451 | BaseTrafficAgent::validate(); |
---|
| 452 | |
---|
| 453 | for (int i=0;i<lanehs.length();i++) { |
---|
[1071] | 454 | ivec index = RV(lanes(i).queue,1).dataind(rv_queues); |
---|
[1026] | 455 | lanehs(i)->queue_index=index(0); |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | void adapt(const vec &glob_dt) { |
---|
| 460 | BaseTrafficAgent::adapt(glob_dt); |
---|
| 461 | |
---|
| 462 | for (int i=0;i<lanehs.length();i++) { |
---|
| 463 | lanehs(i)->queue=queues(lanehs(i)->queue_index); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | planned_offset=last_offset; |
---|
[1146] | 467 | planned_rating=0; |
---|
[1026] | 468 | |
---|
| 469 | //set state variables to default values |
---|
| 470 | final_state=false; |
---|
| 471 | new_stable_state=false; |
---|
| 472 | send_requests=false; |
---|
| 473 | need_exps=true; |
---|
[1146] | 474 | negot_offset=negot_start; |
---|
| 475 | |
---|
| 476 | if (actual_time/cycle_length<2) { |
---|
| 477 | need_exps=false; |
---|
| 478 | final_state=true; |
---|
| 479 | } |
---|
[1026] | 480 | } |
---|
| 481 | |
---|
[941] | 482 | void broadcast(Setting& set){ |
---|
[1026] | 483 | |
---|
[1133] | 484 | //ask neighbours for expetcted arrive times |
---|
[1026] | 485 | if (need_exps) { |
---|
[941] | 486 | for (int i=0; i<neighbours.length(); i++){ |
---|
| 487 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 488 | UI::save ( neighbours(i), msg, "to"); |
---|
| 489 | UI::save (name,msg,"from"); |
---|
| 490 | UI::save ( (string)"expected_times_request", msg, "what"); |
---|
| 491 | } |
---|
[1026] | 492 | need_exps=false; |
---|
[941] | 493 | } |
---|
| 494 | |
---|
| 495 | // broadcast expected cars |
---|
[1127] | 496 | if (!requesters.empty()) { |
---|
[1026] | 497 | expected_cars(); |
---|
[986] | 498 | do { |
---|
| 499 | Setting &msg =set.add(Setting::TypeGroup); |
---|
[1127] | 500 | UI::save ( requesters.back(), msg, "to"); |
---|
[986] | 501 | UI::save ( name, msg, "from"); |
---|
| 502 | UI::save ( (string)"new_expected_cars", msg, "what"); |
---|
| 503 | UI::save ( &(rv_outputs), msg, "rv"); |
---|
| 504 | UI::save ( outputs, msg, "value"); |
---|
[1127] | 505 | requesters.pop_back(); |
---|
| 506 | } while (!requesters.empty()); |
---|
[1026] | 507 | } |
---|
[941] | 508 | |
---|
[1026] | 509 | // broadcast new stable state (new stable expectations) |
---|
| 510 | if (new_stable_state) { |
---|
| 511 | expected_cars(); |
---|
| 512 | for (int i=0;i<neighbours.length();i++) { |
---|
| 513 | Setting &msg = set.add(Setting::TypeGroup); |
---|
| 514 | UI::save ( neighbours(i), msg, "to"); |
---|
| 515 | UI::save ( name, msg, "from"); |
---|
[1127] | 516 | UI::save ( (string)"stable_state", msg, "what"); |
---|
[1026] | 517 | UI::save ( &(rv_outputs), msg, "rv"); |
---|
| 518 | UI::save ( outputs, msg, "value"); |
---|
| 519 | } |
---|
| 520 | new_stable_state=false; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | // broadcast requests to change offset(s) |
---|
| 524 | if (send_requests) { |
---|
| 525 | for (int i=0;i<neighbours.length();i++) { |
---|
| 526 | Setting &msg = set.add(Setting::TypeGroup); |
---|
| 527 | UI::save ( neighbours(i), msg, "to"); |
---|
| 528 | UI::save ( name, msg, "from"); |
---|
| 529 | UI::save ( (string)"offset_change_request", msg, "what"); |
---|
| 530 | UI::save ( &(rv_change_request), msg, "rv"); |
---|
| 531 | UI::save ( change_request, msg, "value"); |
---|
| 532 | } |
---|
| 533 | send_requests=false; |
---|
| 534 | } |
---|
[1127] | 535 | |
---|
[1133] | 536 | // reached final offset |
---|
[1026] | 537 | if (final_state) { |
---|
| 538 | final_state=false; |
---|
[941] | 539 | } |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | void receive(const Setting &msg){ |
---|
| 543 | string what; |
---|
| 544 | string to; |
---|
| 545 | string from; |
---|
[986] | 546 | vec value; |
---|
| 547 | RV *rv; |
---|
[941] | 548 | |
---|
| 549 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 550 | UI::get(to, msg, "to", UI::compulsory); |
---|
| 551 | UI::get(from, msg, "from"); |
---|
[986] | 552 | UI::get(rv, msg, "rv"); |
---|
[941] | 553 | UI::get(value, msg, "value"); |
---|
[1026] | 554 | |
---|
[986] | 555 | if (what=="expected_times_request"){ |
---|
[1127] | 556 | requesters.push_back(from); |
---|
[941] | 557 | } |
---|
| 558 | else if (what=="new_expected_cars") { |
---|
[986] | 559 | rv_recieved_exps=*rv; |
---|
| 560 | recieved_exps=value; |
---|
[1026] | 561 | |
---|
[1118] | 562 | if (!passive) { |
---|
[1133] | 563 | planned_offset=find_best_offset(planned_offset,find_best_start); |
---|
[1118] | 564 | planned_offset=normalize_offset(planned_offset); |
---|
| 565 | } |
---|
[1026] | 566 | |
---|
[1127] | 567 | planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps); |
---|
| 568 | |
---|
[1026] | 569 | // we have new stable state to broadcast |
---|
| 570 | new_stable_state=true; |
---|
[941] | 571 | } |
---|
[1127] | 572 | else if (what=="stable_state") { |
---|
[1026] | 573 | rv_recieved_exps=*rv; |
---|
| 574 | recieved_exps=value; |
---|
| 575 | |
---|
[1127] | 576 | planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps); |
---|
[1026] | 577 | |
---|
[1127] | 578 | if (!passive) { |
---|
[1111] | 579 | for (int i=0;i<neighbours.length();i++) { |
---|
| 580 | rv_change_request.add(RV(neighbours(i)+"_change",2)); |
---|
| 581 | change_request.set_size(rv_change_request._dsize()); |
---|
| 582 | ivec ind=RV(neighbours(i)+"_change",2).dataind(rv_change_request); |
---|
[1127] | 583 | |
---|
[1111] | 584 | // offset change |
---|
[1146] | 585 | change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change); |
---|
[1111] | 586 | // rating change |
---|
| 587 | change_request(ind(1))=rating_change; |
---|
| 588 | } |
---|
| 589 | |
---|
[1146] | 590 | if (negot_offset>negot_limit) { |
---|
| 591 | negot_offset/=2; |
---|
[1026] | 592 | send_requests=true; |
---|
[1111] | 593 | } |
---|
| 594 | else { |
---|
| 595 | final_state=true; |
---|
| 596 | } |
---|
[1026] | 597 | } |
---|
| 598 | else { |
---|
| 599 | final_state=true; |
---|
| 600 | } |
---|
| 601 | } |
---|
| 602 | else if (what=="offset_change_request") { |
---|
| 603 | double final_rating_diff; |
---|
[1146] | 604 | |
---|
| 605 | planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps); |
---|
[1026] | 606 | |
---|
| 607 | rv_recieved_changes=*rv; |
---|
| 608 | recieved_changes=value; |
---|
| 609 | |
---|
| 610 | for (int i=0;i<rv_recieved_changes.length();i++) { |
---|
[1071] | 611 | ivec ind=RV(rv_recieved_changes.name(i),2).dataind(rv_recieved_changes); |
---|
[1146] | 612 | final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)), recieved_exps, rv_recieved_exps)+recieved_changes(ind(1)); |
---|
[1026] | 613 | |
---|
[1071] | 614 | if (final_rating_diff>=0) { |
---|
[1026] | 615 | planned_offset+=(int)recieved_changes(ind(0)); |
---|
[1071] | 616 | planned_offset=normalize_offset(planned_offset); |
---|
[1026] | 617 | planned_rating+=final_rating_diff; |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | new_stable_state=true; |
---|
| 621 | } |
---|
[941] | 622 | else { |
---|
| 623 | BaseTrafficAgent::receive(msg); |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | void ds_register(const DS &ds) { |
---|
| 628 | BaseTrafficAgent::ds_register(ds); |
---|
[986] | 629 | action2ds.set_connection( ds._urv(), rv_action); |
---|
[941] | 630 | } |
---|
| 631 | |
---|
| 632 | void from_setting(const Setting &set) { |
---|
[1071] | 633 | RV rv_exp; |
---|
| 634 | |
---|
[1127] | 635 | BaseTrafficAgent::from_setting(set); |
---|
| 636 | |
---|
[1135] | 637 | total_offset=0; |
---|
[1071] | 638 | actual_time=0; |
---|
[1081] | 639 | negot_cycle=1; |
---|
[1135] | 640 | |
---|
| 641 | car_leaving_time=2; |
---|
| 642 | VP=40; |
---|
| 643 | |
---|
[1081] | 644 | cycle_count=5; |
---|
[1026] | 645 | |
---|
[1135] | 646 | negot_start=4; |
---|
[1111] | 647 | negot_limit=1; |
---|
| 648 | |
---|
[1133] | 649 | find_best_start=8; |
---|
| 650 | find_best_limit=2; |
---|
| 651 | |
---|
[1111] | 652 | passive=0; |
---|
[986] | 653 | |
---|
| 654 | UI::get(last_offset, set, "offset", UI::compulsory); |
---|
[1146] | 655 | |
---|
| 656 | //optional reading parameters from config |
---|
[1111] | 657 | UI::get(passive, set, "passive", UI::optional); |
---|
[1146] | 658 | UI::get(car_leaving_time, set, "car_leaving_time", UI::optional); |
---|
[1135] | 659 | UI::get(VP, set, "VP", UI::optional); |
---|
| 660 | UI::get(cycle_count, set, "cycle_count", UI::optional); |
---|
| 661 | UI::get(negot_start, set, "negot_start", UI::optional); |
---|
| 662 | UI::get(negot_limit, set, "negot_limit", UI::optional); |
---|
| 663 | UI::get(find_best_start, set, "find_best_start", UI::optional); |
---|
| 664 | UI::get(find_best_limit, set, "find_best_limit", UI::optional); |
---|
[1071] | 665 | |
---|
[1135] | 666 | |
---|
[1071] | 667 | for (int i=0;i<lanes.length();i++) { |
---|
| 668 | for (int j=0;j<lanes(i).outputs.length();j++) { |
---|
| 669 | if (lanes(i).outputs(j)!="DUMMY_DET") { |
---|
| 670 | rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3); |
---|
| 671 | rv_outputs.add(rv_exp); |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | outputs.set_size(rv_outputs._dsize()); |
---|
[1111] | 676 | |
---|
| 677 | log_level[logoffset]=true; |
---|
[941] | 678 | } |
---|
[1071] | 679 | |
---|
[986] | 680 | void act(vec &glob_ut){ |
---|
[1081] | 681 | if (negot_cycle==cycle_count) { |
---|
| 682 | vec action; |
---|
| 683 | action.set_size(rv_action._dsize()); |
---|
| 684 | |
---|
[1127] | 685 | ivec index = RV(name+"_offset",1).dataind(rv_action); |
---|
[1026] | 686 | |
---|
[1081] | 687 | action(index(0))=normalize_offset(total_offset/cycle_count, false); |
---|
| 688 | action2ds.filldown(action,glob_ut); |
---|
| 689 | |
---|
| 690 | total_offset=0; |
---|
| 691 | negot_cycle=1; |
---|
| 692 | } |
---|
| 693 | else { |
---|
| 694 | total_offset+=planned_offset; |
---|
| 695 | negot_cycle++; |
---|
| 696 | } |
---|
[1026] | 697 | last_offset=planned_offset; |
---|
[1127] | 698 | } |
---|
| 699 | |
---|
| 700 | void step() { |
---|
[1071] | 701 | actual_time+=step_length; |
---|
[986] | 702 | } |
---|
| 703 | |
---|
[1118] | 704 | void log_register(logger &l, const string &prefix){ |
---|
| 705 | if ( log_level[logoffset]){ |
---|
[1146] | 706 | l.add_vector ( log_level, logoffset, RV("a",2), "x"+prefix ); |
---|
[1118] | 707 | } |
---|
| 708 | } |
---|
| 709 | void log_write() const { |
---|
| 710 | if (log_level[logoffset]){ |
---|
[1127] | 711 | vec offset_vec(2); |
---|
| 712 | offset_vec(0)=planned_offset; |
---|
| 713 | offset_vec(1)=planned_rating; |
---|
| 714 | log_level.store(logoffset, offset_vec); |
---|
[1118] | 715 | } |
---|
[1127] | 716 | } |
---|
[921] | 717 | }; |
---|
| 718 | UIREGISTER(GreenWaveTrafficAgent); |
---|