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