Changeset 1133 for applications
- Timestamp:
- 07/13/10 17:33:21 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/doprava/traffic_agent_offset.h
r1127 r1133 30 30 //! list of agents, who request expected cars 31 31 list<string> requesters; 32 33 //! offset set in last simulation step 34 int last_offset; 35 //! actual planned offset to set for next simulation step 36 int planned_offset; 37 //! rating of actual planned offset 38 double planned_rating; 39 //! avarage speed of cars 40 int VP; 32 41 33 42 double car_leaving; //s; how long is 1 car leaving queue … … 48 57 //! after cycle_count cycles, we count avarege planned_offseta send it to Aimsun 49 58 int cycle_count; 59 //! Finding of best own offsset starts on this offset change 60 int find_best_start; 61 //! minimal value of offsset change tested during looking for best offset 62 int find_best_limit; 50 63 51 64 //! counts all expected cars going from each lane, saves to outputs and rv_outputs … … 72 85 exp2outputs.set_connection(rv_exp,rv_outputs); 73 86 74 // Number of cars75 exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j) ;87 //cars density 88 exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j) / green_time; 76 89 77 90 start_time = green_starts(group_index(name+"_"+lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset; … … 80 93 //last car arrive time 81 94 exp(2)=start_time + green_time; 82 //TODO pushup az na konec95 83 96 exp2outputs.pushup(outputs,exp); 84 97 } … … 86 99 } 87 100 }; 101 102 /*! creates periodic expansion of gb (green begin], ge (green end), using 103 expectations cb (cars begin), ce (cars end). Result is stored in gbv 104 (green begin vector) and gev (green end vector) */ 105 void expand_greens(const double gb, const double ge, const vec cb, const vec ce, vec &gbv, vec &gev) { 106 gbv.set_size(1); 107 gev.set_size(1); 108 gbv(0)=gb; 109 gev(0)=ge; 110 if (ge>cycle_length) { 111 gbv.ins(gbv.length(),0); 112 gev.ins(gev.length(),ge-cycle_length); 113 } 114 int i=1; 115 while (gev(gev.length()-1)<ce(max_i(ce))) { 116 gbv.ins(gbv.length(),gb+i*cycle_length); 117 gev.ins(gev.length(),ge+i*cycle_length); 118 i++; 119 } 120 }; 121 122 //! returns true if what is in interval <left;right> 123 bool in_interval(double what, const double left, const double right) { 124 return ((what>=left && what<=right) ? true : false); 125 } 88 126 89 127 //! counts planned rating using offset and recieved_exps … … 92 130 double t_green_begin; 93 131 double t_green_end; 94 vec cars_ count;132 vec cars_density; 95 133 vec t_cars_begin; 96 134 vec t_cars_end; … … 108 146 int l=0; 109 147 for (int j=0;j<rv_recieved_exps.length();j++) { 110 111 148 int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-"); 112 149 if (result>=0) { 113 114 150 t_cars_begin.set_size(l+1,true); 115 151 t_cars_end.set_size(l+1,true); 116 cars_ count.set_size(l+1,true);152 cars_density.set_size(l+1,true); 117 153 118 154 ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); 119 155 120 cars_ count(l)=recieved_exps(ind(0));156 cars_density(l)=recieved_exps(ind(0)); 121 157 t_cars_begin(l)=recieved_exps(ind(1)); 122 158 t_cars_end(l)=recieved_exps(ind(2)); … … 128 164 } 129 165 if (found) { 130 131 166 //counting rating 132 167 group_name = name+"_"+lanes(i).sg; //e.g. 495_VA … … 136 171 t_green_end=t_green_begin + green_times(index)*cycle_length; 137 172 138 /************** counting with all exps ****************/ 139 140 int k; 141 double t_act=t_green_begin; 173 vec t_gb_vec; 174 vec t_ge_vec; 175 176 expand_greens(t_green_begin, t_green_end, t_cars_begin, t_cars_end, t_gb_vec, t_ge_vec); 177 178 //! index for t_cars_begin 179 int k=min_i(t_cars_begin); 180 //! index for t_ggb_vec 181 int l; 182 //! indicator of actual position in whole interval 183 double t_act=0; 184 //! end of counting for actual line 185 double t_limit=t_ge_vec(max_i(t_ge_vec)); 186 //! end of closest future interval 187 double t_end; 188 142 189 virtual_queue=lanehs(i)->queue; 143 190 144 191 //cycle goes through all "stopping" points and counts queue lenght at these points 145 do { 192 do { 146 193 k=min_i(t_cars_begin); 147 if (k!=-1) {148 //in case there are cars comming before t_green begin149 if (t_cars_begin(k)<t_act) {150 if (t_cars_end(k)<=t_act) {151 virtual_queue+=cars_count(k);152 153 cars_count.del(k);154 t_cars_begin.del(k);155 t_cars_ end.del(k);156 194 l=min_i(t_gb_vec); 195 if (k!=-1) { 196 //cars are entering queue 197 if (in_interval(t_act,t_cars_begin(k), t_cars_end(k))) { 198 //cars leaving and entering queue 199 if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { 200 t_end = min(t_cars_end(k),t_ge_vec(l)); 201 virtual_queue+=(t_end - t_act)*(cars_density(k)-(1/car_leaving)); 202 t_cars_begin(k)=t_end; 203 t_gb_vec(l)=t_end; 157 204 } 205 //cars only entering queue 158 206 else { 159 double frac=(t_cars_begin(k)-t_act)/(t_cars_end(k)-t_cars_begin(k));160 virtual_queue+= cars_count(k)*frac;161 t_cars_begin(k)=t_ act;207 t_end=min(t_cars_end(k),t_ge_vec(l)); 208 virtual_queue+=(t_end-t_act)*cars_density(k); 209 t_cars_begin(k)=t_end; 162 210 } 163 211 } 164 else if (t_cars_begin(k)==t_act) { 165 if (t_cars_end(k)<t_green_end) { 166 virtual_queue+=cars_count(k)-(t_cars_end(k)-t_act)/car_leaving; 167 t_act=t_cars_end(k); 212 //cars are not entering queue 213 else { 214 //cars are only leaving queue 215 if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { 216 t_end = min(t_ge_vec(l),t_cars_begin(k)); 217 virtual_queue-=(t_end-t_act)/car_leaving; 218 t_gb_vec(l)=t_end; 219 220 //in case we emptied whole queue 221 virtual_queue=max(virtual_queue,0.0); 168 222 } 169 // if t_cars_end>=t_green_end223 //no cars entering, no cars leaving 170 224 else { 171 virtual_queue+=cars_count(k)-(t_green_end-t_act)/car_leaving; 172 t_act=t_green_end; 225 t_end=min(t_gb_vec(l),t_cars_begin(k)); 173 226 } 174 //more cars than cars_count(k) couldn't pass without stopping 175 if (virtual_queue < -cars_count(k)) { 176 virtual_queue = -cars_count(k); 177 } 178 cars_count.del(k); 227 t_act=t_endl 228 } 229 //raising rating 230 if (virtual_queue<0) { 231 rating-=virtual_queue; 232 virtual_queue=0; 233 } 234 235 //deleting used intervals 236 if (t_cars_begin(k)==t_cars_end(k)) { 179 237 t_cars_begin.del(k); 180 238 t_cars_end.del(k); 239 cars_density.del(k); 181 240 } 182 //if t_cars_begin(k)>=t_act 183 else { 184 if (t_cars_begin(k)<t_green_end) { 185 virtual_queue-=(t_cars_begin(k)-t_act)/car_leaving; 186 t_act=t_cars_begin(k); 187 } 188 else { 189 virtual_queue-=(t_green_end-t_act)/car_leaving; 190 t_act=t_green_end; 191 } 192 // in case we managed to empty whole queue 193 if (virtual_queue<0) { 194 virtual_queue=0; 195 } 241 if (t_gb_vec(l)==t_ge_vec(l)) { 242 t_gb_vec.del(l); 243 t_ge_vec.del(l); 196 244 } 245 197 246 } 198 247 //if no other expectations found 199 248 else { 200 virtual_queue-=(t_green_end-t_act)/car_leaving; 201 t_act=t_green_end; 249 virtual_queue-=( t_ge_vec(l)-t_act)/car_leaving; 250 t_act=t_ge_vec(l); 251 t_gb_vec.del(l); 252 t_ge_vec.del(l); 202 253 } 203 if (virtual_queue<0) { 204 rating-=virtual_queue; 205 virtual_queue=0; 206 } 207 } while (t_act<t_green_end); 254 } while (t_act<t_limit); 208 255 } 209 256 } … … 219 266 //! rating if offset is lowered 220 267 double rating_n; 221 268 //! center point for next cycle 222 269 int new_center; 223 270 … … 239 286 } 240 287 241 if (interval> 2) {288 if (interval>find_best_limit) { 242 289 interval/=2; 243 290 new_center=find_best_offset(new_center,interval); … … 249 296 //! finds if changing neighbour's offset could have positive effect, returns found offset change and stores chage of rating to rating_change 250 297 int find_best_exps(const int offset_change, const string neighbour, double &rating_change) { 251 //! expectations recieved from neighbour252 vec original_exps;253 298 //! expactations after positve change of neighbour's offset 254 299 vec positive_exps; … … 261 306 //! rating if offset is lowered 262 307 double rating_n; 263 original_exps.set_size(recieved_exps.length()); 264 265 original_exps=recieved_exps; 308 266 309 positive_exps=recieved_exps; 267 310 negative_exps=recieved_exps; … … 379 422 } 380 423 424 //! returns index of largest element in vector 425 int max_i(vec vector) { 426 if (vector.length()>0) { 427 double max=vector(0); 428 int index=0; 429 for (int i=1;i<vector.length();i++) { 430 if (vector(i)>max) { 431 max=vector(i); 432 index=i; 433 } 434 } 435 return index; 436 } 437 return -1; 438 } 439 440 381 441 public: 382 //! offset set in last simulation step383 int last_offset;384 //! actual planned offset to set for next simulation step385 int planned_offset;386 //! rating of actual planned offset387 double planned_rating;388 //! avarage speed of cars389 int VP;390 391 442 void validate() { 392 443 rv_action = RV(name+"_offset", 1); … … 424 475 void broadcast(Setting& set){ 425 476 426 //ask neighbours for exp tected arrive times477 //ask neighbours for expetcted arrive times 427 478 if (need_exps) { 428 479 for (int i=0; i<neighbours.length(); i++){ … … 478 529 } 479 530 480 // reached final offset .531 // reached final offset 481 532 if (final_state) { 482 533 final_state=false; … … 505 556 506 557 if (!passive) { 507 planned_offset=find_best_offset(planned_offset, 8);558 planned_offset=find_best_offset(planned_offset,find_best_start); 508 559 planned_offset=normalize_offset(planned_offset); 509 560 } … … 580 631 581 632 car_leaving=2; 582 VP=4 5;633 VP=40; 583 634 actual_time=0; 584 635 585 636 negot_cycle=1; 637 total_offset=0; 586 638 cycle_count=5; 587 total_offset=0;588 639 589 640 negot_offset=4; 590 641 negot_limit=1; 642 643 find_best_start=8; 644 find_best_limit=2; 591 645 592 646 passive=0;