| | 3 | const double measurement_cycle_time = 90; // s |
| | 4 | const double saturated_stream = 0.5; // car/s |
| | 5 | const int sample_cycles = 5; |
| | 6 | |
| | 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 | }; |
| 11 | | |
| 12 | | void sum_lanes() { |
| 13 | | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
| 14 | | if ( inputs(2*i) > 0 ) { |
| 15 | | lane_sum(i) = lane_sum(i) + inputs(2*i); |
| 16 | | } |
| 17 | | } |
| 18 | | } |
| 19 | | |
| 20 | | void echo ( string message ) { |
| 21 | | cout << name << " hlasi: " << message << endl; |
| 22 | | } |
| 23 | | |
| 24 | | void send2neighbour( Setting &set, int i, string messageName, double messageValue ) { |
| 25 | | if ( i < neighbours.length() ) { |
| 26 | | Setting &msg =set.add(Setting::TypeGroup); |
| 27 | | UI::save( neighbours(i), msg, "to" ); |
| 28 | | UI::save (name,msg,"from"); |
| 29 | | UI::save( messageName, msg, "what" ); |
| 30 | | UI::save( messageValue, msg, "value" ); |
| 31 | | } |
| 32 | | else { |
| 33 | | //throw new Exception("soused "+((string)i)+" neexistuje"); |
| 34 | | //cout << endl << endl << "soused " << i << mimo |
| 35 | | std::stringstream out; |
| 36 | | out << "soused " << i << " neexistuje"; |
| 37 | | //throw out.str(); |
| 38 | | cout << out.str(); |
| 39 | | } |
| 40 | | } |
| | 75 | Array <SignalGroup*> signal_groups; |
| | 76 | Array <int> received_Tcs; |
| | 77 | Array <int> received_profit_sum; |
| | 78 | |
| | 79 | // POMOCNE FUNKCE |
| | 95 | void echo ( string message ) { |
| | 96 | cout << name << " hlasi: " << message << endl; |
| | 97 | } |
| | 98 | |
| | 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 | |
| | 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 | |
| | 178 | void send2allNeighbours ( Setting &set, string messageName, double messageValue ) { |
| | 179 | for ( int i = 0; i < neighbours.length(); i++ ) { |
| | 180 | send2neighbour( set, i, messageName, messageValue ); |
| | 181 | } |
| | 182 | } |
| | 183 | |
| | 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 | |
| 61 | | TrafficAgentCycleTime::measurement_cycle_time = 90; //s |
| 62 | | TrafficAgentCycleTime::saturated_stream = 0.5; |
| 63 | | TrafficAgentCycleTime::Tc = 80; |
| 64 | | } |
| 65 | | |
| 66 | | void broadcast(Setting &set){ |
| 67 | | //BaseTrafficAgent::broadcast(set); |
| 68 | | try { |
| 69 | | for ( int i = 0; i < neighbours.length(); i ++ ) { |
| 70 | | send2neighbour( set, i, testMessage, 2.3 ); |
| 71 | | } |
| 72 | | } catch ( string s ) { |
| 73 | | cout << s << endl; |
| 74 | | } |
| | 195 | //TrafficAgentCycleTime::Tc = 80; |
| | 196 | } |
| | 197 | |
| | 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; |
| | 209 | |
| | 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; |
| | 220 | } |
| | 221 | SignalGroup * sg; |
| | 222 | if ( sg_index >= 0 ) { |
| | 223 | sg = signal_groups(sg_index); |
| | 224 | } |
| | 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; |
| | 234 | } |
| | 235 | } |
| | 236 | |
| | 237 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
| | 238 | |
| | 239 | void adapt (const vec &glob_dt) { |
| | 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(); |
| | 247 | } |
| | 248 | |
| | 249 | for ( int i = 0; i < received_profit_sum.length(); i++ ) { |
| | 250 | received_profit_sum(i) = 0; |
| | 251 | } |
| | 252 | |
| | 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; |
| | 264 | } |
| | 265 | |
| | 266 | //suggestedTc = idealTc = getIdealTc(); |
| | 267 | |
| | 268 | //cout << endl << "CELKOVA FRONTA " << name << " " << getQueue() << endl; |
| | 269 | |
| | 270 | BaseTrafficAgent::adapt(glob_dt); |
| | 271 | } |
| | 272 | |
| | 273 | void broadcast(Setting &set){ |
| | 274 | |
| | 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 ++; |
| 80 | | string to; |
| 81 | | double val; |
| 82 | | try { |
| 83 | | UI::get(what, msg, "what", UI::compulsory); |
| 84 | | UI::get(from, msg, "from", UI::compulsory); |
| 85 | | UI::get(to, msg, "to", UI::compulsory); |
| 86 | | UI::get(val, msg, "value"); |
| 87 | | if ( what == testMessage ) { |
| 88 | | //cout << endl << name << ": OD: " << from << " CO: " << what << " KOLIK: " << val <<endl; |
| 89 | | } |
| 90 | | if ( what == "new_stable_state" ) { |
| 91 | | //echo("new_stable_state"); |
| 92 | | BaseTrafficAgent::receive(msg); |
| 93 | | } |
| 94 | | |
| 95 | | |
| 96 | | } catch ( UISettingException e ) { |
| 97 | | echo(e.what()); |
| 98 | | } |
| 99 | | } |
| 100 | | |
| 101 | | void validate (){ |
| 102 | | BaseTrafficAgent::validate(); |
| 103 | | lane_sum.set_size( rv_inputs.length() ); |
| 104 | | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
| 105 | | lane_sum(i) = 0; |
| 106 | | cout << endl << endl << "lane_sum initialization " << name << " " << lane_sum(i) <<endl << endl ; |
| 107 | | } |
| 108 | | echo("validate"); |
| 109 | | } |
| 110 | | |
| 111 | | void adapt (const vec &glob_dt) { |
| 112 | | sum_lanes(); |
| 113 | | //printVector( rv_queues, queues, "queues" ); |
| 114 | | printVector( rv_inputs, inputs, "inputs" ); |
| 115 | | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
| 116 | | cout << lane_sum(i) << endl; |
| 117 | | } |
| 118 | | cout << endl << endl; |
| 119 | | //printVector( green_names, green_times, "inputs" ); |
| 120 | | //cout << endl << name << "green times: " << endl; |
| 121 | | for ( int i = 0; i < green_times.length(); i ++ ) { |
| 122 | | //cout << green_names(i) << " " << green_starts(i) << " " << green_times(i) << endl; |
| 123 | | } |
| 124 | | |
| 125 | | |
| 126 | | /* |
| 127 | | for ( int i = 0; i < outputs.length(); i ++ ) { |
| 128 | | cout << name << " outputs " << outputs(i) << endl; |
| 129 | | } |
| 130 | | |
| 131 | | for ( int i = 0; i < rv_outputs.length(); i ++ ) { |
| 132 | | cout << name << " rv_outputs " << rv_outputs.name(i) << endl; |
| 133 | | } |
| 134 | | |
| 135 | | */ |
| 136 | | |
| 137 | | BaseTrafficAgent::adapt(glob_dt); |
| 138 | | //echo("adapted"); |
| | 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 | |
| 142 | | |
| | 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 ++; |