Changeset 1071 for applications/doprava
- Timestamp:
- 06/10/10 00:41:54 (15 years ago)
- Location:
- applications/doprava
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/doprava
-
Property
svn:ignore set
to
ALL_BUILD.vcproj
ALL_BUILD.vcproj.GUYBRUSH.Kamil.user
CMakeCache.txt
CMakeFiles
Debug
Release
ZERO_CHECK.dir
ZERO_CHECK.vcproj
ZERO_CHECK.vcproj.GUYBRUSH.Kamil.user
bin
cmake_install.cmake
doprava.ncb
doprava.sln
doprava.suo
experiments
graphs
main_loop.dir
main_loop.vcproj
main_loop.vcproj.GUYBRUSH.Kamil.user
run_cmake.bat
traffic_agents.dir
traffic_agents.vcproj
traffic_agents.vcproj.GUYBRUSH.Kamil.user
xxx
-
Property
svn:ignore set
to
-
applications/doprava/Zlicingw.cfg
r1067 r1071 13 13 neighbours = ("601"); 14 14 offset = 60; //s 15 sgs=("VA","VB","VC","VD","VE","VF"); 16 green_starts=[72,4,5,35,36,71]; 17 moje_special=14; 18 }, 15 green_names = ("495_VA","495_VB","495_VC","495_VD","495_VE","495_VF"); 16 green_starts=[72,4,5,35,36,71]; 17 green_times = [0.2,0.2,0.1,0.1,0.2,0.2];// ratios of green of 80s 18 stage_names = ("495_F1","495_F2","495_F3"); 19 stage_times = [30,30,20];}, 19 20 { //agent 2 20 21 class = "GreenWaveTrafficAgent"; … … 32 33 neighbours = ("495"); 33 34 offset = 40; 34 sgs=("VA","VB","VC","VD","VE","SE"); 35 green_starts=[54,9,26,25,9,9]; 35 green_names = ("601_VA","601_VB","601_VC","601_VD","601_VE","601_SE"); 36 green_starts=[54,9,26,25,9,9]; 37 green_times = [0.2,0.2,0.1,0.1,0.2,0.2];// ratios of green of 80s 38 stage_names = ("601_F1","601_F2","601_F3"); 39 stage_times = [30,30,20]; 36 40 } 37 38 41 ); 39 42 40 43 logger = {class="memlog"; filename = "mujzlicin"; maxlen = 1000;}; 41 42 43 44 44 45 45 system = { … … 54 54 // simulation in our case starts always at 00:00:00. Default simulation 55 55 // length is 24 hours (24:00:00). 56 stop_time = "0 1:00:00";56 stop_time = "00:05:00"; 57 57 58 58 }; -
applications/doprava/main_loop.cpp
r1026 r1071 59 59 // INITIALISATION OF UT 60 60 glob_ut[ 0] = 80; // cycle time 61 glob_ut[ 1] = 40; // offset 49561 glob_ut[ 1] = 60; // offset 495 62 62 glob_ut[ 2] = 30; // ut[2]+ut[3]+ut[4] has to sum up to ut[0] 63 63 glob_ut[ 3] = 30; 64 64 glob_ut[ 4] = 20; 65 glob_ut[ 8] = 20; // offset 60165 glob_ut[ 8] = 40; // offset 601 66 66 glob_ut[ 9] = 30; // ut[9]+ut[10]+ut[11] has to sum up to ut[0] 67 67 glob_ut[10] = 30; -
applications/doprava/traffic_agent_offset.h
r1026 r1071 1 1 #include "traffic_agent.h" 2 2 #include <list> 3 #include <fstream> 3 4 4 5 class GreenWaveTrafficAgent : public BaseTrafficAgent { … … 7 8 int negot_offset; 8 9 10 int actual_time; 11 12 RV rv_outputs; 13 vec outputs; 14 9 15 RV rv_change_request; 10 16 vec change_request; … … 12 18 RV rv_recieved_exps; 13 19 vec recieved_exps; 20 21 RV rv_next_exps; 22 vec next_exps; 14 23 15 24 RV rv_recieved_changes; … … 30 39 31 40 //! counts all expected cars going from each lane, saves to outputs and rv_outputs 32 void expected_cars( /*const string dest*/) {41 void expected_cars() { 33 42 double start_time; 43 ivec ind; 44 RV rv_exp; 45 datalink exp2outputs; 46 vec exp; 34 47 35 48 for (int i=0;i<lanes.length();i++) { 36 49 for (int j=0;j<lanes(i).outputs.length();j++) { 37 string group = name+"_"+lanes(i).sg; //e.g. 495_VA 38 ivec index=rv_inputs.dataind(RV(group,1)); 39 vec green_time=inputs(index); 40 41 rv_outputs.add(RV(lanes(i).outputs(j)+"-"+group,3)); 42 outputs.set_size(rv_outputs._dsize()); 43 44 ivec ind = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3)); 45 46 //Number of cars 47 outputs(ind(0))=lanehs(i)->expected_output(green_time(0)); 48 49 start_time = green_starts(sg_index(lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset; 50 //first car arrive time 51 outputs(ind(1))=start_time; 52 //last car arrive time 53 outputs(ind(2))= start_time + green_time(0); 50 if (lanes(i).outputs(j)!="DUMMY_DET") { 51 string group = name+"_"+lanes(i).sg; //e.g. 495_VA 52 53 int index=group_index(group); 54 55 double green_time=green_times(index)*cycle_length; 56 57 //ivec green_index=rv_inputs.dataind(RV(group,1)); 58 //vec green_time=inputs(green_index); 59 60 //cout << "Green time "<<green_time<<endl; 61 62 rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3); 63 64 exp.set_size(rv_exp._dsize()); 65 exp2outputs.set_connection(rv_exp,rv_outputs); 66 67 //Number of cars 68 exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j); 69 70 start_time = green_starts(group_index(name+"_"+lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset; 71 //first car arrive time 72 exp(1)=start_time; 73 //last car arrive time 74 exp(2)=start_time + green_time; 75 //TODO pushup az na konec 76 exp2outputs.pushup(outputs,exp); 77 78 } 54 79 } 55 80 } … … 59 84 double count_rating(const int offset) { 60 85 double virtual_queue; 61 double t_emptiyng;62 86 double t_green_begin; 63 87 double t_green_end; 64 double t_cars_begin; 65 double t_cars_end; 66 // vec t_cars_begin; 67 // vec t_cars_end; 68 double t_mixing; 88 vec cars_count; 89 vec t_cars_begin; 90 vec t_cars_end; 69 91 bool found; 70 92 71 93 double rating=0.0; 94 95 RV rv_vector; 96 vec vector; 97 datalink exps2vector; 72 98 73 99 for (int i=0;i<lanes.length();i++) { … … 75 101 //Finding, if we have some expectations 76 102 found=false; 103 77 104 for (int k=0;k<lanes(i).inputs.length();k++) { 78 // t_cars_begin.set_size(lanes(i).inputs.length()); 79 // t_cars_end.set_size(lanes(i).inputs.length()); 80 105 int l=0; 81 106 for (int j=0;j<rv_recieved_exps.length();j++) { 107 t_cars_begin.set_size(l+1,true); 108 t_cars_end.set_size(l+1,true); 109 cars_count.set_size(l+1,true); 110 82 111 int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-"); 83 112 if (result>=0) { 84 ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3)); 85 86 t_cars_begin=recieved_exps(ind(1)); //TODO now using only last found exp. 87 t_cars_end=recieved_exps(ind(2)); 113 114 rv_vector = RV(rv_recieved_exps.name(j),3); 115 ivec ind = rv_vector.dataind(rv_recieved_exps); 116 117 cars_count(l)=recieved_exps(ind(0)); 118 t_cars_begin(l)=recieved_exps(ind(1)); 119 t_cars_end(l)=recieved_exps(ind(2)); 120 l++; 121 88 122 found=true; 89 123 } 90 124 } 91 } 92 93 if (!found) { 94 t_cars_begin=0; 95 t_cars_end=cycle_length; 96 } 97 //counting rating 98 t_green_begin=green_starts(sg_index(lanes(i).sg)) + offset; 99 ivec ind = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1)); 100 t_green_end=t_green_begin+inputs(ind(0)); 101 102 //TODO use real queues lenghts 103 if (t_green_begin<t_cars_begin) { 104 //virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving; 105 //virtual_queue=(rand()%8)-(t_cars_begin-t_green_begin)/car_leaving; 106 virtual_queue=5-(t_cars_begin-t_green_begin)/car_leaving; 107 } 108 else if (t_green_begin>t_cars_begin) { 109 //virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 110 //virtual_queue=(rand()%9)+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 111 virtual_queue=4+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 112 } 113 114 t_emptiyng=virtual_queue/(1/car_leaving - lanehs(i)->expected_density()); 115 t_mixing=min(t_green_end,t_cars_end)-max(t_green_begin,t_cars_begin); 116 117 rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0); 125 if (found) { 126 //counting rating 127 string group = name+"_"+lanes(i).sg; //e.g. 495_VA 128 int index=group_index(group); 129 130 t_green_begin=green_starts(index) + offset; 131 double green_time=green_times(index)*cycle_length; 132 t_green_end=t_green_begin+green_time; 133 134 /************** counting with all exps ****************/ 135 136 int k; 137 double t_act=t_green_begin; 138 virtual_queue=lanehs(i)->queue; 139 140 //cycle goes through all stopping points and counts queue lenght at these points 141 do { 142 k=min_i(t_cars_begin); 143 144 if (k!=-1) { 145 double a=cars_count(k); 146 double b=t_cars_begin(k); 147 double c=t_cars_end(k); 148 149 //in case there are cars comming before t_green begin 150 if (t_cars_begin(k)<t_act) { 151 if (t_cars_end(k)<=t_act) { 152 virtual_queue+=cars_count(k); 153 //jen prodlouzime, t_act zustava, odstranime k-ty predpoklad (mozna hned, mozna az na konci 154 cars_count.del(k); 155 t_cars_begin.del(k); 156 t_cars_end.del(k); 157 158 } 159 else { 160 //prodlouzujeme do casu t_green_begin, 161 //pak bud green_begin posuneme do t_act 162 double pomer=(t_cars_begin(k)-t_act)/(t_cars_end(k)-t_cars_begin(k)); 163 virtual_queue+=cars_count(k)*pomer; 164 t_cars_begin(k)=t_act; 165 } 166 } 167 else if (t_cars_begin(k)==t_act) { 168 //mixujeme az do t_cars_end nebo t_green_end 169 //posuneme t_act na jeden z tech casu 170 if (t_cars_end(k)<t_green_end) { 171 virtual_queue+=cars_count(k)-(t_cars_end(k)-t_act)/car_leaving; 172 t_act=t_cars_end(k); 173 174 cars_count.del(k); 175 t_cars_begin.del(k); 176 t_cars_end.del(k); 177 } 178 //t_cars_end>=t_green_end 179 else { 180 virtual_queue+=cars_count(k)-(t_green_end-t_act)/car_leaving; 181 t_act=t_green_end; 182 183 cars_count.del(k); 184 t_cars_begin.del(k); 185 t_cars_end.del(k); 186 } 187 188 189 } 190 //t_cars_begin(k)>=t_act 191 else { 192 //zkracujeme frontu az do t_cars_end(k) nebo t_green_end a posuneme t_act 193 if (t_cars_end(k)<t_green_end) { 194 virtual_queue-=(t_cars_end(k)-t_act)/car_leaving; 195 t_act=t_cars_end(k); 196 } 197 else { 198 virtual_queue-=(t_green_end-t_act)/car_leaving; 199 t_act=t_green_end; 200 } 201 } 202 } 203 else { 204 virtual_queue-=(t_green_end-t_act)/car_leaving; 205 t_act=t_green_end; 206 } 207 if (virtual_queue<0) { 208 rating-=virtual_queue; 209 virtual_queue=0; 210 } 211 212 } while (t_act<t_green_end); 213 } 214 } 118 215 } 119 216 return rating; … … 179 276 int res = rv_recieved_exps.name(j).find("-"+neighbour); 180 277 if (res>0) { 181 ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3)); 278 ivec ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); 279 182 280 rating_n=count_rating(planned_offset); 183 281 … … 219 317 } 220 318 221 //! returns index of signal group sg 222 int sg_index(const string sg) { 223 for (int i=0;i<sgs.length();i++) { 224 if (sgs(i)==sg) { 319 // pravdepodobne s chybou, momentalne se nepouziva 320 void split_exps() { 321 ivec ind; 322 RV rv_exp; 323 datalink recieved2next; 324 325 rv_next_exps=RV(); 326 for (int i=0;i<rv_recieved_exps.length();i++) { 327 if (rv_recieved_exps.size(i)==3) { 328 rv_exp=RV(rv_recieved_exps.name(i),3); 329 ind = rv_exp.dataind(rv_recieved_exps); 330 //cout << "ind " << ind << endl; 331 332 int next_cycle_end=2 * cycle_length - (actual_time % cycle_length); 333 if (recieved_exps(ind(1))>next_cycle_end) { 334 rv_next_exps.add(rv_exp); 335 next_exps.set_size(rv_next_exps._dsize()); 336 337 if (actual_time==630) { 338 cout << "rv_next_exps " << rv_next_exps.to_string() << endl; 339 cout << "rv_recived_exps " << rv_recieved_exps.to_string() << endl; 340 } 341 342 recieved2next.set_connection(rv_next_exps,rv_recieved_exps); 343 recieved2next.filldown(recieved_exps,next_exps); 344 rv_recieved_exps=rv_recieved_exps.subt(rv_exp); 345 } 346 else if (recieved_exps(ind(2))>next_cycle_end) { 347 rv_next_exps.add(rv_exp); 348 next_exps.set_size(rv_next_exps._dsize()); 349 350 ivec ind2=rv_exp.dataind(rv_next_exps); 351 next_exps(ind2(0))=recieved_exps(ind(0)); //TODO to neni spravne 352 next_exps(ind2(1))=next_cycle_end; 353 next_exps(ind2(2))=recieved_exps(ind(2)); 354 recieved_exps(ind(2))=next_cycle_end; 355 } 356 } 357 } 358 } 359 360 //! returns index of signal group group 361 int group_index(const string group) { 362 for (int i=0;i<green_names.length();i++) { 363 if (green_names(i)==group) { 225 364 return i; 226 365 } … … 231 370 //! returns offset value shifted to fit interval <0;cycle_length> 232 371 int normalize_offset(int offset) { 233 while (offset<0 && offset<cycle_length) {372 while (offset<0 || offset>cycle_length) { 234 373 if (offset<0) { 235 374 offset+=cycle_length; … … 241 380 return offset; 242 381 } 243 382 383 //! returns value shifted to fit interval <0;cycle_length> 384 template<class T> T normalize(T time) { 385 while (time<0 && time<cycle_length) { 386 if (time<0) { 387 time+=cycle_length; 388 } 389 else { 390 time-=cycle_length; 391 } 392 } 393 return time; 394 } 395 396 template <class T> inline string to_string (const T& t) 397 { 398 std::stringstream ss; 399 ss << t; 400 return ss.str(); 401 } 244 402 245 403 //! returns index of maximum of entered values … … 256 414 } 257 415 416 //! returns index of smallest element in vectorsx 417 int min_i(vec vector) { 418 if (vector.length()>0) { 419 double min=vector(0); 420 int index=0; 421 for (int i=1;i<vector.length();i++) { 422 if (vector(i)<min) { 423 min=vector(i); 424 index=i; 425 } 426 } 427 return index; 428 } 429 return -1; 430 } 431 258 432 public: 259 433 //! offset set in last simulation step … … 261 435 //! actual planned offset to set for next simulation step 262 436 int planned_offset; 437 //! planned offset for cycle after next cycle 438 int planned_next_offset; 263 439 //! rating of actual planned offset 264 440 double planned_rating; 441 //! rating of planned next offset 442 double planned_next_rating; 265 443 266 //! array of existing signal groups267 Array<string> sgs;268 //! relative starts of green for signal groups269 ivec green_starts;270 271 444 //! avarage speed of cars 272 445 int VP; … … 275 448 rv_action = RV(name+"_offset", 1); // <======= example 276 449 277 for (int i=0; i< sgs.length();i++) {278 rv_inputs.add(RV( name+"_"+sgs(i),1));450 for (int i=0; i<green_names.length();i++) { 451 rv_inputs.add(RV(green_names(i),1)); 279 452 } 280 453 inputs.set_size(rv_inputs._dsize()); … … 283 456 284 457 for (int i=0;i<lanehs.length();i++) { 285 ivec index = rv_queues.dataind(RV(lanes(i).queue,1));458 ivec index = RV(lanes(i).queue,1).dataind(rv_queues); 286 459 lanehs(i)->queue_index=index(0); 287 460 } 288 289 290 461 } 291 462 … … 323 494 // broadcast expected cars 324 495 if (!seznam.empty()) { 496 double a; 325 497 expected_cars(); 326 498 do { … … 332 504 UI::save ( outputs, msg, "value"); 333 505 seznam.pop_back(); 506 a=outputs (10); 507 334 508 } while (!seznam.empty()); 335 509 } … … 375 549 // reached final offset. Log value? 376 550 if (final_state) { 377 378 //cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl; 551 cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl; 379 552 final_state=false; 380 553 } … … 401 574 rv_recieved_exps=*rv; 402 575 recieved_exps=value; 576 //split_exps(); 403 577 404 578 last_offset=planned_offset; … … 411 585 reset_negot_offset=true; 412 586 }*/ 587 413 588 planned_rating=count_rating(planned_offset); 414 589 // we have new stable state to broadcast … … 418 593 rv_recieved_exps=*rv; 419 594 recieved_exps=value; 595 //split_exps(); 420 596 planned_rating=count_rating(planned_offset); 421 597 … … 423 599 rv_change_request.add(RV(neighbours(i)+"_change",2)); 424 600 change_request.set_size(rv_change_request._dsize()); 425 ivec ind= rv_change_request.dataind(RV(neighbours(i)+"_change",2));601 ivec ind=RV(neighbours(i)+"_change",2).dataind(rv_change_request); 426 602 // offset change 427 603 change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change); … … 446 622 for (int i=0;i<rv_recieved_changes.length();i++) { 447 623 448 ivec ind= rv_recieved_changes.dataind(RV(rv_recieved_changes.name(i),2));449 450 final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0))) -recieved_changes(ind(0));451 if (final_rating_diff> 0) {624 ivec ind=RV(rv_recieved_changes.name(i),2).dataind(rv_recieved_changes); 625 626 final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)))+recieved_changes(ind(1)); 627 if (final_rating_diff>=0) { 452 628 planned_offset+=(int)recieved_changes(ind(0)); 629 planned_offset=normalize_offset(planned_offset); 453 630 planned_rating+=final_rating_diff; 454 631 accepted_from=from; … … 473 650 void from_setting(const Setting &set) { 474 651 BaseTrafficAgent::from_setting(set); 475 476 srand(time(NULL)); 652 653 RV rv_exp; 654 655 std::ofstream ofile; 656 ofile.open("greentimes.txt"); 657 ofile.close(); 477 658 478 659 car_leaving=2; 479 660 VP=45; 661 actual_time=0; 480 662 481 663 negot_offset=8; 482 664 483 665 // load from file 484 UI::get(sgs, set, "sgs", UI::compulsory); 485 UI::get(green_starts, set, "green_starts", UI::compulsory); 666 //UI::get(sgs, set, "sgs", UI::compulsory); 667 668 //UI::get(green_starts, set, "green_starts", UI::compulsory); 486 669 UI::get(last_offset, set, "offset", UI::compulsory); 487 488 } 670 671 for (int i=0;i<lanes.length();i++) { 672 for (int j=0;j<lanes(i).outputs.length();j++) { 673 if (lanes(i).outputs(j)!="DUMMY_DET") { 674 rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3); 675 rv_outputs.add(rv_exp); 676 } 677 } 678 } 679 outputs.set_size(rv_outputs._dsize()); 680 } 681 489 682 void act(vec &glob_ut){ 490 683 vec action; 491 684 action.set_size(rv_action._dsize()); 492 685 493 686 ivec index = rv_action.dataind(RV(name+"_offset",1)); 687 494 688 action(index(0))=planned_offset; 495 689 last_offset=planned_offset; 496 497 690 action2ds.filldown(action,glob_ut); 691 692 actual_time+=step_length; 498 693 } 499 694