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