[1076] | 1 | #include "traffic_agent.cpp" |
---|
| 2 | const string testMessage = "test_message"; |
---|
[1129] | 3 | const double measurement_cycle_time = 90; // s |
---|
| 4 | const double saturated_stream = 0.5; // car/s |
---|
| 5 | const int sample_cycles = 5; |
---|
[1076] | 6 | |
---|
[1129] | 7 | |
---|
| 8 | class SignalGroup { |
---|
| 9 | public: |
---|
| 10 | string name; |
---|
| 11 | Array <Lane*> lanes; |
---|
| 12 | double green_time_ratio; |
---|
| 13 | int average_queue_length; |
---|
| 14 | string queue_name; |
---|
| 15 | int cycle_counter; |
---|
| 16 | |
---|
| 17 | SignalGroup () { |
---|
| 18 | average_queue_length = 0; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | void adapt() { |
---|
| 22 | if ( (cycle_counter +1)% sample_cycles == 0 ) |
---|
| 23 | average_queue_length = 0; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | int getSumCarsWaitingTime ( double time_cycle ) { |
---|
| 28 | int q = average_queue_length/sample_cycles; // pomocna delka fronty - UPRAVIT - pocitat s vetsi nez prumernou? |
---|
| 29 | int cars_per_green = time_cycle * green_time_ratio * lanes.length() * saturated_stream; // pocet aut ktere projedou na zelenou |
---|
| 30 | int sum_cars_waiting_time = 0; // suma aut * cekacich casu |
---|
| 31 | int i = 0; // iterator |
---|
| 32 | while ( q > cars_per_green ) { |
---|
| 33 | q -= cars_per_green; |
---|
| 34 | sum_cars_waiting_time += q*time_cycle; |
---|
| 35 | i ++; |
---|
| 36 | } |
---|
| 37 | // + zbytek fronty + prvni nezapocitana vlna |
---|
| 38 | if ( i > 0 ) { |
---|
| 39 | sum_cars_waiting_time += q*time_cycle + cars_per_green*(1.0-green_time_ratio)*time_cycle*0,5; |
---|
| 40 | //cout << "delsi fronta " << q << " "; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // pocet aut * stredni hodnota cekaci doby pri volne krizovatces |
---|
| 44 | else { |
---|
| 45 | sum_cars_waiting_time += (0.5*(1.0-green_time_ratio))*(time_cycle*q); |
---|
| 46 | //cout << "kratsi fronta " << q << " "; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | //cout << "signal group " << name << " tc " << time_cycle << " cekani " << sum_cars_waiting_time << " fronta " << average_queue_length << endl; |
---|
| 50 | |
---|
| 51 | return sum_cars_waiting_time; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void addQueue( int q ) { |
---|
| 55 | |
---|
| 56 | if ( q > 0 ) |
---|
| 57 | average_queue_length += q; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | }; |
---|
| 62 | |
---|
[1076] | 63 | class TrafficAgentCycleTime : public BaseTrafficAgent { |
---|
[1129] | 64 | public: |
---|
| 65 | int Tc; |
---|
| 66 | int minTc; |
---|
| 67 | int maxTc; |
---|
| 68 | int stepTc; |
---|
| 69 | int idealTc; |
---|
| 70 | static const int d_Tc = 3; // pocet navrhovanych delek cyklu |
---|
[1076] | 71 | int cycle_counter; |
---|
[1129] | 72 | int max_profit; |
---|
| 73 | unsigned int n_of_broadcast; |
---|
[1076] | 74 | Array <int> lane_sum; |
---|
[1129] | 75 | Array <SignalGroup*> signal_groups; |
---|
| 76 | Array <int> received_Tcs; |
---|
| 77 | Array <int> received_profit_sum; |
---|
[1076] | 78 | |
---|
[1129] | 79 | // POMOCNE FUNKCE |
---|
| 80 | |
---|
| 81 | void printVector ( RV rv_vector, vec vector, string description ) { |
---|
| 82 | cout << endl << description << " " << name << endl; |
---|
| 83 | int k = 0; |
---|
| 84 | for ( int i = 0; i < rv_vector.length(); i ++ ) { |
---|
| 85 | cout << rv_vector.name(i) << " : "; |
---|
| 86 | for ( int j = 0; j < rv_vector.size(i); j ++ ) { |
---|
| 87 | cout << vector(k) << " "; |
---|
| 88 | k ++; |
---|
[1076] | 89 | } |
---|
[1129] | 90 | cout << endl; |
---|
[1076] | 91 | } |
---|
[1129] | 92 | cout << endl; |
---|
[1076] | 93 | } |
---|
| 94 | |
---|
| 95 | void echo ( string message ) { |
---|
| 96 | cout << name << " hlasi: " << message << endl; |
---|
| 97 | } |
---|
| 98 | |
---|
[1129] | 99 | unsigned int find_index ( RV rv_vector, string index_string ) { |
---|
| 100 | for ( unsigned int i = 0; i < (int) rv_vector.length(); i ++) { |
---|
| 101 | if ( rv_vector.name(i) == index_string ) |
---|
| 102 | return i; |
---|
| 103 | } |
---|
| 104 | return rv_vector.length(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | unsigned int find_index ( Array <string> arr, string index_string ) { |
---|
| 108 | for ( unsigned int i = 0; i < (int) arr.length(); i ++) { |
---|
| 109 | if ( arr(i) == index_string ) |
---|
| 110 | return i; |
---|
| 111 | } |
---|
| 112 | return arr.length(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | // VYPOCETNI FUNKCE |
---|
| 116 | |
---|
| 117 | int getSumCarsWaitingTime ( double time_cycle ) { |
---|
| 118 | int sum = 0; |
---|
| 119 | SignalGroup * sg; |
---|
| 120 | for ( int i = 0; i < signal_groups.length(); i ++ ) { |
---|
| 121 | sg = signal_groups(i); |
---|
| 122 | sum += sg->getSumCarsWaitingTime( time_cycle ); |
---|
| 123 | } |
---|
| 124 | return sum; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | int getQueue () { |
---|
| 128 | int queue = 0; |
---|
| 129 | SignalGroup *sg; |
---|
| 130 | for ( int i = 0; i < signal_groups.length(); i ++ ) { |
---|
| 131 | sg = signal_groups(i); |
---|
| 132 | queue += sg->average_queue_length; |
---|
| 133 | } |
---|
| 134 | return queue; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // NEPOUZIVAT? |
---|
| 138 | //int getIdealTc() { |
---|
| 139 | // int min_waiting_time = getSumCarsWaitingTime(Tc); |
---|
| 140 | // int waiting_time; |
---|
| 141 | // int idealTc = Tc; |
---|
| 142 | // cout << name << " Tc wt" << endl; |
---|
| 143 | // for ( int t_c = Tc - 2*stepTc; t_c <= Tc + 2*stepTc; t_c += stepTc ) { |
---|
| 144 | // waiting_time = getSumCarsWaitingTime(t_c); |
---|
| 145 | // cout << t_c << " " << waiting_time << endl; |
---|
| 146 | // if ( waiting_time < min_waiting_time ) { |
---|
| 147 | // min_waiting_time = waiting_time; |
---|
| 148 | // idealTc = t_c; |
---|
| 149 | // } |
---|
| 150 | // } |
---|
| 151 | // cout << "IDEAL " << idealTc << endl; |
---|
| 152 | // return idealTc; |
---|
| 153 | //} |
---|
| 154 | |
---|
| 155 | int getIdealTc() { |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
[1076] | 160 | void send2neighbour( Setting &set, int i, string messageName, double messageValue ) { |
---|
| 161 | if ( i < neighbours.length() ) { |
---|
| 162 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 163 | UI::save( neighbours(i), msg, "to" ); |
---|
| 164 | UI::save (name,msg,"from"); |
---|
| 165 | UI::save( messageName, msg, "what" ); |
---|
| 166 | UI::save( messageValue, msg, "value" ); |
---|
| 167 | } |
---|
| 168 | else { |
---|
| 169 | //throw new Exception("soused "+((string)i)+" neexistuje"); |
---|
| 170 | //cout << endl << endl << "soused " << i << mimo |
---|
| 171 | std::stringstream out; |
---|
| 172 | out << "soused " << i << " neexistuje"; |
---|
| 173 | //throw out.str(); |
---|
| 174 | cout << out.str(); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[1129] | 178 | void send2allNeighbours ( Setting &set, string messageName, double messageValue ) { |
---|
| 179 | for ( int i = 0; i < neighbours.length(); i++ ) { |
---|
| 180 | send2neighbour( set, i, messageName, messageValue ); |
---|
[1076] | 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[1129] | 184 | int getProfit( int time_cycle ) { |
---|
| 185 | if ( Tc != time_cycle ) |
---|
| 186 | return getSumCarsWaitingTime( Tc ) - getSumCarsWaitingTime ( time_cycle ); |
---|
| 187 | else |
---|
| 188 | return 0; |
---|
| 189 | } |
---|
| 190 | |
---|
[1076] | 191 | |
---|
[1129] | 192 | // FUNKCE VOLANE V main_loop NA ZACATKU |
---|
[1076] | 193 | void from_setting( Setting &set ) { |
---|
| 194 | BaseTrafficAgent::from_setting(set); |
---|
[1129] | 195 | //TrafficAgentCycleTime::Tc = 80; |
---|
[1076] | 196 | } |
---|
| 197 | |
---|
[1129] | 198 | void validate (){ |
---|
| 199 | rv_action = RV("Tc",1); |
---|
| 200 | rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) ); |
---|
| 201 | Tc = 80; |
---|
| 202 | minTc = 60; |
---|
| 203 | maxTc = 120; |
---|
| 204 | stepTc = 5; |
---|
| 205 | max_profit = 0; |
---|
| 206 | received_profit_sum.set_length( 2*d_Tc +1 ); |
---|
| 207 | received_Tcs.set_length( 2*d_Tc +1 ); |
---|
| 208 | cycle_counter = 0; |
---|
[1076] | 209 | |
---|
[1129] | 210 | BaseTrafficAgent::validate(); |
---|
| 211 | // inicializace signalnich skupin |
---|
| 212 | for ( int i = 0; i < lanes.length(); i ++ ) { |
---|
| 213 | Lane * l = & lanes(i); |
---|
| 214 | int sg_index = -1; |
---|
| 215 | for ( int j = 0; j < signal_groups.length(); j ++ ) { |
---|
| 216 | SignalGroup * sg; |
---|
| 217 | sg = signal_groups(j); |
---|
| 218 | if ( sg->name == l->sg ) |
---|
| 219 | sg_index = j; |
---|
[1076] | 220 | } |
---|
[1129] | 221 | SignalGroup * sg; |
---|
| 222 | if ( sg_index >= 0 ) { |
---|
| 223 | sg = signal_groups(sg_index); |
---|
[1076] | 224 | } |
---|
[1129] | 225 | else { |
---|
| 226 | sg = new SignalGroup(); |
---|
| 227 | sg->name = l->sg; |
---|
| 228 | sg->average_queue_length = 0; |
---|
| 229 | signal_groups.set_size( signal_groups.length()+1, true ); |
---|
| 230 | signal_groups(signal_groups.length()-1) = sg; |
---|
| 231 | } |
---|
| 232 | sg->lanes.set_size(sg->lanes.length()+1, true); |
---|
| 233 | sg->lanes(sg->lanes.length()-1) = l; |
---|
[1076] | 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[1129] | 237 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
---|
[1076] | 238 | |
---|
| 239 | void adapt (const vec &glob_dt) { |
---|
[1129] | 240 | // inicializes vars of cycle of broadcast |
---|
| 241 | n_of_broadcast = 0; |
---|
| 242 | max_profit = 0; |
---|
| 243 | idealTc = Tc; |
---|
| 244 | |
---|
| 245 | for ( int i = 0; i < signal_groups.length(); i ++ ) { |
---|
| 246 | signal_groups(i)->adapt(); |
---|
[1076] | 247 | } |
---|
| 248 | |
---|
[1129] | 249 | for ( int i = 0; i < received_profit_sum.length(); i++ ) { |
---|
| 250 | received_profit_sum(i) = 0; |
---|
[1076] | 251 | } |
---|
| 252 | |
---|
[1129] | 253 | // nacteni dat do signalnich skupin |
---|
| 254 | cout << endl << cycle_counter << " SG " << (cycle_counter%sample_cycles +1) << endl; |
---|
| 255 | for ( int i = 0; i < rv_queues.length(); i ++ ) { |
---|
| 256 | SignalGroup * sg; |
---|
| 257 | sg = signal_groups(i); |
---|
| 258 | sg->queue_name = rv_queues.name(i); |
---|
| 259 | sg->addQueue(queues(i)); |
---|
| 260 | sg->green_time_ratio = green_times(i); |
---|
| 261 | sg->cycle_counter = cycle_counter; |
---|
| 262 | //cout << sg->name << " " << sg->getSumCarsWaitingTime(Tc) << " " << sg->average_queue_length << " " << sg->green_time_ratio*Tc << " " << sg->getTime2zeroQue() << endl; |
---|
| 263 | cout << sg->name << " " << queues(i) << " " << sg->average_queue_length << " " << sg->average_queue_length / (cycle_counter%sample_cycles +1) << endl; |
---|
[1076] | 264 | } |
---|
| 265 | |
---|
[1129] | 266 | //suggestedTc = idealTc = getIdealTc(); |
---|
| 267 | |
---|
| 268 | //cout << endl << "CELKOVA FRONTA " << name << " " << getQueue() << endl; |
---|
[1076] | 269 | |
---|
| 270 | BaseTrafficAgent::adapt(glob_dt); |
---|
| 271 | } |
---|
| 272 | |
---|
[1129] | 273 | void broadcast(Setting &set){ |
---|
[1076] | 274 | |
---|
[1129] | 275 | // 1. cycle of communication |
---|
| 276 | // sends all tcs in range +- d_tc and expected profits according to tc |
---|
| 277 | // format: tc_index, profit_index |
---|
| 278 | if ( n_of_broadcast == 0 && ( cycle_counter % sample_cycles == 0 ) ) { |
---|
| 279 | for ( int i = -d_Tc; i <= d_Tc; i++ ) { |
---|
| 280 | int time_cycle = Tc + i*stepTc; |
---|
| 281 | int profit = getProfit(time_cycle); |
---|
| 282 | |
---|
| 283 | // send tc |
---|
| 284 | stringstream time_cycle_stream; |
---|
| 285 | time_cycle_stream << "tc_" << (i+d_Tc); |
---|
| 286 | send2allNeighbours( set, time_cycle_stream.str(), time_cycle); |
---|
| 287 | |
---|
| 288 | // send profit |
---|
| 289 | stringstream profit_stream; |
---|
| 290 | profit_stream << "profit_" << (i+d_Tc); |
---|
| 291 | send2allNeighbours( set, profit_stream.str(), profit ); |
---|
| 292 | |
---|
| 293 | //cout << name << " tc " << time_cycle << " profit " << profit << " cekani " << getSumCarsWaitingTime(time_cycle) << endl; |
---|
| 294 | } |
---|
| 295 | //cout << endl; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | n_of_broadcast ++; |
---|
[1076] | 299 | } |
---|
[1129] | 300 | |
---|
| 301 | void receive(const Setting& msg){ |
---|
| 302 | string what; |
---|
| 303 | string from; |
---|
| 304 | double val; |
---|
| 305 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 306 | UI::get(from, msg, "from", UI::compulsory); |
---|
| 307 | UI::get(val, msg, "value"); |
---|
| 308 | |
---|
| 309 | //cout << name << " receiving from " << from << " " << what << " : " << val<<endl; |
---|
| 310 | if ( n_of_broadcast == 1 ) { |
---|
| 311 | if ( what.substr(0,2) == "tc" ) { |
---|
| 312 | istringstream tc_i( what.substr(3, what.length()-3) ); |
---|
| 313 | int index; |
---|
| 314 | tc_i >> index; |
---|
| 315 | received_Tcs( index ) = val; |
---|
| 316 | //cout << "tc " << val << endl; |
---|
| 317 | } |
---|
| 318 | if ( what.substr(0,6) == "profit" ) { |
---|
| 319 | istringstream profiti( what.substr(7, what.length()-7) ); |
---|
| 320 | int index; |
---|
| 321 | profiti >> index; |
---|
| 322 | received_profit_sum( index ) = received_profit_sum( index ) + val; |
---|
| 323 | //cout << "profit " << val << endl; |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | if ( what == "new_stable_state" ) { |
---|
| 328 | //echo("new_stable_state"); |
---|
| 329 | BaseTrafficAgent::receive(msg); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | void act (vec &glob_ut){ |
---|
| 335 | if ( cycle_counter % sample_cycles == 0 ) { |
---|
| 336 | // choose tc with max profit |
---|
| 337 | for ( int i = 0; i < received_profit_sum.length(); i ++ ) { |
---|
| 338 | int time_cycle = received_Tcs(i); |
---|
| 339 | int profit = received_profit_sum(i) + getProfit(time_cycle); |
---|
| 340 | if ( profit > max_profit ) { |
---|
| 341 | max_profit = profit; |
---|
| 342 | idealTc = time_cycle; |
---|
| 343 | //cout << name << " idealni Tc " << time_cycle << " s celkovym ziskem " << profit << endl; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | //cout << endl << name << " nastevuje TC na " << idealTc << endl; |
---|
| 347 | Tc = idealTc; |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | // nastaveni delky cyklu na Tc |
---|
| 351 | vec action; |
---|
| 352 | action.set_size(rv_action._dsize()); |
---|
| 353 | int st; |
---|
| 354 | int stage_time_sum = 0; // soucet delky fazi |
---|
| 355 | action(find_index(rv_action, "Tc")) = Tc; |
---|
| 356 | for ( int i =0; i < stage_names.length(); i ++) { |
---|
| 357 | if ( (i+1) < stage_names.length() ) { |
---|
| 358 | st = (stage_times(i)/80)*Tc; |
---|
| 359 | stage_time_sum += st; |
---|
| 360 | action(find_index(rv_action, stage_names(i))) = st; |
---|
| 361 | } |
---|
| 362 | else // dopocitani posledni faze - oprava zaokrouhlovaci chyby |
---|
| 363 | action(find_index(rv_action, stage_names(i))) = Tc - stage_time_sum; |
---|
| 364 | } |
---|
| 365 | action2ds.filldown(action,glob_ut); |
---|
| 366 | } |
---|
| 367 | cycle_counter ++; |
---|
| 368 | } |
---|
[1076] | 369 | |
---|
| 370 | |
---|
| 371 | }; |
---|
| 372 | UIREGISTER(TrafficAgentCycleTime); |
---|