| 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 | | }; |
| | 5 | const int sample_cycles = 3; |
| | 6 | |
| | 7 | |
| | 8 | |
| | 9 | |
| | 10 | |
| 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 | | } |
| | 99 | |
| | 100 | double getRelativeQueueDiff() { |
| | 101 | double dq = 0; |
| | 102 | for ( int i=0; i< lanehs.length(); i ++ ) { |
| | 103 | dq += lanehs(i)->getRelativeQueueDiff(); |
| | 104 | } |
| | 105 | return dq; |
| | 106 | } |
| | 107 | |
| | 108 | |
| 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 | | } |
| | 151 | |
| | 152 | for ( int i=0; i<lanehs.length(); i ++ ) { |
| | 153 | unsigned int index = find_index( green_names, name + "_" + lanehs(i)->getSG() ); |
| | 154 | lanehs(i)->green_time_ratio = green_times( index ); |
| | 155 | } |
| | 156 | |
| | 157 | // pomocne logovaci soubory |
| | 158 | Tcs.open("Tcs.dat"); |
| | 159 | Tcs << "cyklus\tTc\tprofit\n"; |
| | 160 | stringstream Ro_file_name; |
| | 161 | Ro_file_name << "Ro_" << name << ".dat"; |
| | 162 | if ( name == "495" ) { |
| | 163 | Ros.open("Ros_495.dat"); |
| | 164 | Qs.open("Qs_495.dat"); |
| | 165 | } |
| | 166 | else { |
| | 167 | Ros.open("Ros_601.dat"); |
| | 168 | Qs.open("Qs_601.dat"); |
| | 169 | } |
| | 170 | Ros << "Hustoty provozu ve frontach" << endl << "n"; |
| | 171 | Qs << "Prumerna delka fronty" << endl << "n"; |
| | 172 | for ( int i = 0; i < lanehs.length(); i++ ) { |
| | 173 | Ros << "\t" << lanehs(i)->getQueueName(); |
| | 174 | Qs << "\t" << lanehs(i)->getAverageQueueLength(); |
| | 175 | } |
| | 176 | Ros << endl; |
| 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++ ) { |
| | 188 | //cout << endl << name << ":" << endl << endl; |
| | 189 | // predani aktualnich dat do LaneHandler lanehs |
| | 190 | Ros << cycle_counter; |
| | 191 | Qs << cycle_counter; |
| | 192 | for ( int i=0; i<lanehs.length(); i ++ ) { |
| | 193 | lanehs(i)->Tc = Tc; |
| | 194 | lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) ); |
| | 195 | |
| | 196 | lanehs(i)->echo(); |
| | 197 | // pomocne logovaci soubory |
| | 198 | Ros << "\t" << lanehs(i)->getRo(); |
| | 199 | Qs << "\t" << lanehs(i)->getAverageQueueLength(); |
| | 200 | } |
| | 201 | Ros << endl; |
| | 202 | Qs << endl; |
| | 203 | |
| | 204 | if ( cycle_counter % sample_cycles == 0 ) { |
| | 205 | //cout << name << " queue DIFF " << getRelativeQueueDiff() << endl; |
| | 206 | |
| | 207 | for ( int i=0; i<lanehs.length(); i ++ ) { |
| | 208 | |
| | 209 | |
| | 210 | //lanehs(i)->echo(); |
| | 211 | |
| | 212 | |
| | 213 | //cout << name << " " << i << " " << lanes(i).sg << " " << lanes(i).queue << " input " << queues(i) << endl; |
| | 214 | } |
| | 215 | } |
| | 216 | |
| | 217 | for ( int i = 0; i < received_profit_sum.length(); i++ ) |
| 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; |
| | 219 | |
| 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 | | } |
| | 262 | |
| | 263 | if ( what.substr(0,2) == "tc" ) { |
| | 264 | istringstream tc_i( what.substr(3, what.length()-3) ); |
| | 265 | int index; |
| | 266 | tc_i >> index; |
| | 267 | received_Tcs( index ) = val; |
| | 268 | //cout << "tc " << val << endl; |
| | 269 | } |
| | 270 | if ( what.substr(0,6) == "profit" ) { |
| | 271 | istringstream profit_i( what.substr(7, what.length()-7) ); |
| | 272 | int index; |
| | 273 | profit_i >> index; |
| | 274 | received_profit_sum( index ) = received_profit_sum( index ) + val; |
| | 275 | //cout << "pridavam profit " << val << endl; |
| | 276 | } |
| | 277 | |