Changeset 1127 for applications/doprava/traffic_agent_offset.h
- Timestamp:
- 07/07/10 11:04:06 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/doprava/traffic_agent_offset.h
r1120 r1127 24 24 vec next_exps; 25 25 26 //! expectations recieved from neighbours 26 27 RV rv_recieved_changes; 27 28 vec recieved_changes; 28 29 29 //! name of agent, whose change we have accepted 30 string accepted_from; 31 32 //TODO currently useless list of neighbours asking for expectations 33 list<string> seznam; 30 //! list of agents, who request expected cars 31 list<string> requesters; 34 32 35 33 double car_leaving; //s; how long is 1 car leaving queue … … 40 38 bool send_requests; 41 39 bool final_state; 42 //bool reset_negot_offset;43 40 41 //! determines wheteher agent actively communicates 44 42 int passive; 45 43 … … 58 56 datalink exp2outputs; 59 57 vec exp; 58 string group_name; 59 double green_time; 60 int index; 60 61 61 62 for (int i=0;i<lanes.length();i++) { 62 63 for (int j=0;j<lanes(i).outputs.length();j++) { 63 64 if (lanes(i).outputs(j)!="DUMMY_DET") { 64 string group= name+"_"+lanes(i).sg; //e.g. 495_VA65 group_name = name+"_"+lanes(i).sg; //e.g. 495_VA 65 66 66 int index=group_index(group); 67 68 double green_time=green_times(index)*cycle_length; 69 70 //ivec green_index=rv_inputs.dataind(RV(group,1)); 71 //vec green_time=inputs(green_index); 72 73 //cout << "Green time "<<green_time<<endl; 67 index=group_index(group_name); 68 green_time=green_times(index)*cycle_length; 74 69 75 70 rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3); 76 77 71 exp.set_size(rv_exp._dsize()); 78 72 exp2outputs.set_connection(rv_exp,rv_outputs); … … 88 82 //TODO pushup az na konec 89 83 exp2outputs.pushup(outputs,exp); 90 91 84 } 92 85 } … … 95 88 96 89 //! counts planned rating using offset and recieved_exps 97 double count_rating(const int offset ) {90 double count_rating(const int offset, const vec recieved_exps, const RV rv_recieved_exps) { 98 91 double virtual_queue; 99 92 double t_green_begin; … … 103 96 vec t_cars_end; 104 97 bool found; 98 ivec ind; 99 string group_name; 105 100 106 101 double rating=0.0; 107 108 RV rv_vector;109 vec vector;110 102 111 103 for (int i=0;i<lanes.length();i++) { … … 113 105 //Finding, if we have some expectations 114 106 found=false; 115 116 107 for (int k=0;k<lanes(i).inputs.length();k++) { 117 108 int l=0; … … 125 116 cars_count.set_size(l+1,true); 126 117 127 128 rv_vector = RV(rv_recieved_exps.name(j),3); 129 ivec ind = rv_vector.dataind(rv_recieved_exps); 118 ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); 130 119 131 120 cars_count(l)=recieved_exps(ind(0)); … … 137 126 } 138 127 } 139 140 128 } 141 129 if (found) { 142 130 143 131 //counting rating 144 string group= name+"_"+lanes(i).sg; //e.g. 495_VA145 int index=group_index(group );132 group_name = name+"_"+lanes(i).sg; //e.g. 495_VA 133 int index=group_index(group_name); 146 134 147 135 t_green_begin=green_starts(index) + offset; 148 double green_time=green_times(index)*cycle_length; 149 t_green_end=t_green_begin+green_time; 136 t_green_end=t_green_begin + green_times(index)*cycle_length; 150 137 151 138 /************** counting with all exps ****************/ … … 235 222 int new_center; 236 223 237 rating_p=count_rating(center+interval );238 rating_c=count_rating(center );239 rating_n=count_rating(center-interval );224 rating_p=count_rating(center+interval, recieved_exps, rv_recieved_exps); 225 rating_c=count_rating(center, recieved_exps, rv_recieved_exps); 226 rating_n=count_rating(center-interval, recieved_exps, rv_recieved_exps); 240 227 241 228 new_center=center; … … 260 247 } 261 248 262 //! finds if changing neighbour's offset could have positive effect, returns found offset change 249 //! finds if changing neighbour's offset could have positive effect, returns found offset change and stores chage of rating to rating_change 263 250 int find_best_exps(const int offset_change, const string neighbour, double &rating_change) { 264 251 //! expectations recieved from neighbour … … 268 255 //! expactations after negative change of neighbour's offset 269 256 vec negative_exps; 270 //! rating if offset is r ised257 //! rating if offset is raised 271 258 double rating_p; 272 259 //! rating if offset is unchaged … … 285 272 ivec ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); 286 273 287 rating_n=count_rating(planned_offset);288 289 274 positive_exps(ind(1))+=offset_change; 290 275 positive_exps(ind(2))+=offset_change; … … 295 280 } 296 281 297 rating_c=count_rating(planned_offset); 298 299 recieved_exps=positive_exps; 300 rating_p=count_rating(planned_offset); 301 302 recieved_exps=negative_exps; 303 rating_n=count_rating(planned_offset); 304 305 recieved_exps=original_exps; 282 rating_c=count_rating(planned_offset, recieved_exps, rv_recieved_exps); 283 rating_p=count_rating(planned_offset, positive_exps, rv_recieved_exps); 284 rating_n=count_rating(planned_offset, negative_exps, rv_recieved_exps); 306 285 307 286 int max_index=max_i_of_three(rating_p,rating_c,rating_n); … … 324 303 } 325 304 326 // pravdepodobne s chybou, momentalne se nepouziva 327 void split_exps() { 328 ivec ind; 329 RV rv_exp; 330 datalink recieved2next; 331 332 rv_next_exps=RV(); 333 for (int i=0;i<rv_recieved_exps.length();i++) { 334 if (rv_recieved_exps.size(i)==3) { 335 rv_exp=RV(rv_recieved_exps.name(i),3); 336 ind = rv_exp.dataind(rv_recieved_exps); 337 //cout << "ind " << ind << endl; 338 339 int next_cycle_end=2 * cycle_length - (actual_time % cycle_length); 340 if (recieved_exps(ind(1))>next_cycle_end) { 341 rv_next_exps.add(rv_exp); 342 next_exps.set_size(rv_next_exps._dsize()); 343 344 if (actual_time==630) { 345 cout << "rv_next_exps " << rv_next_exps.to_string() << endl; 346 cout << "rv_recived_exps " << rv_recieved_exps.to_string() << endl; 347 } 348 349 recieved2next.set_connection(rv_next_exps,rv_recieved_exps); 350 recieved2next.filldown(recieved_exps,next_exps); 351 rv_recieved_exps=rv_recieved_exps.subt(rv_exp); 352 } 353 else if (recieved_exps(ind(2))>next_cycle_end) { 354 rv_next_exps.add(rv_exp); 355 next_exps.set_size(rv_next_exps._dsize()); 356 357 ivec ind2=rv_exp.dataind(rv_next_exps); 358 next_exps(ind2(0))=recieved_exps(ind(0)); //TODO to neni spravne 359 next_exps(ind2(1))=next_cycle_end; 360 next_exps(ind2(2))=recieved_exps(ind(2)); 361 recieved_exps(ind(2))=next_cycle_end; 362 } 363 } 364 } 365 } 366 367 //! returns index of signal group group 305 //! returns index of signal group "group" 368 306 int group_index(const string group) { 369 307 for (int i=0;i<green_names.length();i++) { … … 404 342 } 405 343 406 407 408 //! returns value shifted to fit interval <0;cycle_length>409 //currently not in use410 template<class T> T normalize(T time) {411 while (time<0 && time<cycle_length) {412 if (time<0) {413 time+=cycle_length;414 }415 else {416 time-=cycle_length;417 }418 }419 return time;420 }421 422 344 //! converts t to string 423 345 template <class T> inline string to_string (const T& t) … … 462 384 //! actual planned offset to set for next simulation step 463 385 int planned_offset; 464 //! planned offset for cycle after next cycle465 int planned_next_offset;466 386 //! rating of actual planned offset 467 double planned_rating; 468 //! rating of planned next offset 469 double planned_next_rating; 387 double planned_rating; 470 388 //! avarage speed of cars 471 389 int VP; 472 390 473 391 void validate() { 474 rv_action = RV(name+"_offset", 1); // <======= example392 rv_action = RV(name+"_offset", 1); 475 393 476 394 for (int i=0; i<green_names.length();i++) { … … 510 428 for (int i=0; i<neighbours.length(); i++){ 511 429 Setting &msg =set.add(Setting::TypeGroup); 512 513 430 UI::save ( neighbours(i), msg, "to"); 514 431 UI::save (name,msg,"from"); … … 519 436 520 437 // broadcast expected cars 521 if (! seznam.empty()) {438 if (!requesters.empty()) { 522 439 double a; 523 440 expected_cars(); 524 441 do { 525 442 Setting &msg =set.add(Setting::TypeGroup); 526 UI::save ( seznam.back(), msg, "to");443 UI::save ( requesters.back(), msg, "to"); 527 444 UI::save ( name, msg, "from"); 528 445 UI::save ( (string)"new_expected_cars", msg, "what"); 529 446 UI::save ( &(rv_outputs), msg, "rv"); 530 447 UI::save ( outputs, msg, "value"); 531 seznam.pop_back();448 requesters.pop_back(); 532 449 a=outputs (10); 533 534 } while (!seznam.empty()); 450 } while (!requesters.empty()); 535 451 } 536 452 … … 542 458 UI::save ( neighbours(i), msg, "to"); 543 459 UI::save ( name, msg, "from"); 544 UI::save ( (string)" new_stable_state2", msg, "what");460 UI::save ( (string)"stable_state", msg, "what"); 545 461 UI::save ( &(rv_outputs), msg, "rv"); 546 462 UI::save ( outputs, msg, "value"); … … 561 477 send_requests=false; 562 478 } 563 564 /*if (reset_negot_offset) { 565 for (int i=0;i<neighbours.length();i++) { 566 Setting &msg = set.add(Setting::TypeGroup); 567 UI::save ( neighbours(i), msg, "to"); 568 UI::save ( name, msg, "from"); 569 UI::save ( (string)"reset_negot_offset", msg, "what"); 570 } 571 }*/ 572 573 574 575 // reached final offset. Log value? 479 480 // reached final offset. 576 481 if (final_state) { 577 cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl;578 482 final_state=false; 579 483 } … … 583 487 string what; 584 488 string to; 585 586 489 string from; 587 490 vec value; … … 595 498 596 499 if (what=="expected_times_request"){ 597 seznam.push_back(from);500 requesters.push_back(from); 598 501 } 599 502 else if (what=="new_expected_cars") { 600 503 rv_recieved_exps=*rv; 601 504 recieved_exps=value; 602 //split_exps();603 604 505 605 506 if (!passive) { 606 last_offset=planned_offset;607 507 planned_offset=find_best_offset(planned_offset,8); 608 508 planned_offset=normalize_offset(planned_offset); 609 509 } 510 511 planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps); 610 512 611 612 /*if (planned_offset!=last_offset) {613 reset_negot_offset=true;614 }*/615 616 planned_rating=count_rating(planned_offset);617 513 // we have new stable state to broadcast 618 514 new_stable_state=true; 619 515 } 620 else if (what==" new_stable_state2") {516 else if (what=="stable_state") { 621 517 rv_recieved_exps=*rv; 622 518 recieved_exps=value; 623 //split_exps(); 624 planned_rating=count_rating(planned_offset );519 520 planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps); 625 521 626 522 if (!passive) { 627 628 523 for (int i=0;i<neighbours.length();i++) { 629 524 rv_change_request.add(RV(neighbours(i)+"_change",2)); 630 525 change_request.set_size(rv_change_request._dsize()); 631 526 ivec ind=RV(neighbours(i)+"_change",2).dataind(rv_change_request); 527 632 528 // offset change 633 529 change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change); … … 636 532 } 637 533 638 if (negot_offset> negot_limit) {534 if (negot_offset>=negot_limit) { 639 535 negot_offset/=2; 640 536 send_requests=true; … … 642 538 else { 643 539 final_state=true; 644 645 540 } 646 541 } … … 659 554 ivec ind=RV(rv_recieved_changes.name(i),2).dataind(rv_recieved_changes); 660 555 661 final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)) )+recieved_changes(ind(1));556 final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)), recieved_exps, rv_recieved_exps)+recieved_changes(ind(1)); 662 557 if (final_rating_diff>=0) { 663 558 planned_offset+=(int)recieved_changes(ind(0)); 664 559 planned_offset=normalize_offset(planned_offset); 665 560 planned_rating+=final_rating_diff; 666 accepted_from=from; 667 } 668 } 669 //need_exps=true;s 561 } 562 } 563 670 564 new_stable_state=true; 671 565 } 672 /*else if (what=="reset_negot_offset") {673 negot_offset=8;674 }*/675 566 else { 676 567 BaseTrafficAgent::receive(msg); … … 684 575 685 576 void from_setting(const Setting &set) { 577 RV rv_exp; 578 686 579 BaseTrafficAgent::from_setting(set); 687 688 RV rv_exp;689 580 690 581 car_leaving=2; … … 701 592 passive=0; 702 593 703 // load from file704 //UI::get(sgs, set, "sgs", UI::compulsory);705 706 //UI::get(green_starts, set, "green_starts", UI::compulsory);707 594 UI::get(last_offset, set, "offset", UI::compulsory); 708 595 UI::get(passive, set, "passive", UI::optional); … … 723 610 void act(vec &glob_ut){ 724 611 if (negot_cycle==cycle_count) { 725 726 612 vec action; 727 613 action.set_size(rv_action._dsize()); 728 614 729 ivec index = rv_action.dataind(RV(name+"_offset",1));615 ivec index = RV(name+"_offset",1).dataind(rv_action); 730 616 731 617 action(index(0))=normalize_offset(total_offset/cycle_count, false); … … 734 620 total_offset=0; 735 621 negot_cycle=1; 736 737 622 } 738 623 else { 739 624 total_offset+=planned_offset; 740 741 625 negot_cycle++; 742 626 } 743 744 627 last_offset=planned_offset; 628 } 629 630 void step() { 745 631 actual_time+=step_length; 746 632 } … … 748 634 void log_register(logger &l, const string &prefix){ 749 635 if ( log_level[logoffset]){ 750 l.add_vector ( log_level, logoffset, RV(" mujoffset",1),prefix ); //TODO636 l.add_vector ( log_level, logoffset, RV("1",2), "x"+prefix ); //TODO 751 637 } 752 638 } 753 639 void log_write() const { 754 640 if (log_level[logoffset]){ 755 vec offset_vec( 1);756 offset_vec(0)= (double)planned_offset;757 log_level.store(logoffset, offset_vec);758 //log_level.store(logoffset, planned_rating);759 } 760 } 641 vec offset_vec(2); 642 offset_vec(0)=planned_offset; 643 offset_vec(1)=planned_rating; 644 log_level.store(logoffset, offset_vec); 645 } 646 } 761 647 }; 762 648 UIREGISTER(GreenWaveTrafficAgent);