[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 |
---|
[1139] | 5 | const int sample_cycles = 3; |
---|
[1141] | 6 | const double min_profit = 30.0; |
---|
[1076] | 7 | |
---|
[1129] | 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | |
---|
[1076] | 13 | class TrafficAgentCycleTime : public BaseTrafficAgent { |
---|
[1129] | 14 | public: |
---|
| 15 | int Tc; |
---|
[1141] | 16 | static const int minTc = 40; // nepouziva se |
---|
| 17 | static const int maxTc = 200; // nepouziva se |
---|
| 18 | static const int stepTc = 2; |
---|
| 19 | static const int d_Tc = 3; // pocet navrhovanych delek cyklu |
---|
[1129] | 20 | int idealTc; |
---|
[1076] | 21 | int cycle_counter; |
---|
[1139] | 22 | double max_profit; |
---|
[1129] | 23 | unsigned int n_of_broadcast; |
---|
[1076] | 24 | Array <int> lane_sum; |
---|
[1139] | 25 | //Array <SignalGroup*> signal_groups; |
---|
| 26 | Array <double> received_Tcs; |
---|
| 27 | Array <double> received_profit_sum; |
---|
| 28 | Array <double> received_queue_diffs; |
---|
| 29 | // pomocne logovaci soubory |
---|
| 30 | ofstream Tcs; |
---|
| 31 | ofstream Ros; |
---|
| 32 | ofstream Qs; |
---|
[1076] | 33 | |
---|
[1139] | 34 | |
---|
[1129] | 35 | // POMOCNE FUNKCE |
---|
| 36 | |
---|
| 37 | void printVector ( RV rv_vector, vec vector, string description ) { |
---|
| 38 | cout << endl << description << " " << name << endl; |
---|
| 39 | int k = 0; |
---|
| 40 | for ( int i = 0; i < rv_vector.length(); i ++ ) { |
---|
| 41 | cout << rv_vector.name(i) << " : "; |
---|
| 42 | for ( int j = 0; j < rv_vector.size(i); j ++ ) { |
---|
| 43 | cout << vector(k) << " "; |
---|
| 44 | k ++; |
---|
[1076] | 45 | } |
---|
[1129] | 46 | cout << endl; |
---|
[1076] | 47 | } |
---|
[1129] | 48 | cout << endl; |
---|
[1076] | 49 | } |
---|
| 50 | |
---|
[1139] | 51 | void add2array( Array <double> &arr, double item ) { |
---|
| 52 | arr.set_length( arr.length()+1, true ); |
---|
| 53 | arr(arr.length()-1) = item; |
---|
[1141] | 54 | //cout << endl << "addin to array value " << item << endl; |
---|
[1139] | 55 | } |
---|
| 56 | |
---|
[1076] | 57 | void echo ( string message ) { |
---|
| 58 | cout << name << " hlasi: " << message << endl; |
---|
| 59 | } |
---|
| 60 | |
---|
[1129] | 61 | unsigned int find_index ( RV rv_vector, string index_string ) { |
---|
[1139] | 62 | for ( unsigned int i = 0; i < rv_vector.length(); i ++) { |
---|
[1129] | 63 | if ( rv_vector.name(i) == index_string ) |
---|
| 64 | return i; |
---|
| 65 | } |
---|
| 66 | return rv_vector.length(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | unsigned int find_index ( Array <string> arr, string index_string ) { |
---|
[1139] | 70 | for ( unsigned int i = 0; i < arr.length(); i ++) { |
---|
[1129] | 71 | if ( arr(i) == index_string ) |
---|
| 72 | return i; |
---|
| 73 | } |
---|
| 74 | return arr.length(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // VYPOCETNI FUNKCE |
---|
| 78 | |
---|
[1139] | 79 | // soucet cekacich autocasu |
---|
| 80 | double getWT ( double time_cycle ) { |
---|
| 81 | double sum = 0; |
---|
| 82 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
---|
| 83 | sum += lanehs(i)->getWT( time_cycle ); |
---|
| 84 | } |
---|
[1129] | 85 | return sum; |
---|
| 86 | } |
---|
| 87 | |
---|
[1139] | 88 | double getProfit ( double tc ) { |
---|
[1141] | 89 | if ( tc != Tc ) |
---|
| 90 | return (getWT(Tc) - getWT(tc))*( 1/ (Tc-tc)*(Tc-tc) ); |
---|
| 91 | else |
---|
| 92 | return 0; |
---|
[1139] | 93 | } |
---|
| 94 | |
---|
| 95 | double getQueue () { |
---|
| 96 | double queue = 0; |
---|
| 97 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
---|
| 98 | queue += lanehs(i)->getAverageQueueLength(); |
---|
[1129] | 99 | } |
---|
| 100 | return queue; |
---|
| 101 | } |
---|
| 102 | |
---|
[1141] | 103 | double getAverageRo () { |
---|
| 104 | double Ro_sum = 0; |
---|
| 105 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
---|
| 106 | Ro_sum += lanehs(i)->getRo(); |
---|
[1139] | 107 | } |
---|
[1141] | 108 | return Ro_sum/lanehs.length(); |
---|
[1129] | 109 | } |
---|
| 110 | |
---|
[1139] | 111 | |
---|
[1141] | 112 | |
---|
[1129] | 113 | |
---|
[1076] | 114 | void send2neighbour( Setting &set, int i, string messageName, double messageValue ) { |
---|
| 115 | if ( i < neighbours.length() ) { |
---|
| 116 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 117 | UI::save( neighbours(i), msg, "to" ); |
---|
| 118 | UI::save (name,msg,"from"); |
---|
| 119 | UI::save( messageName, msg, "what" ); |
---|
| 120 | UI::save( messageValue, msg, "value" ); |
---|
| 121 | } |
---|
| 122 | else { |
---|
| 123 | //throw new Exception("soused "+((string)i)+" neexistuje"); |
---|
| 124 | //cout << endl << endl << "soused " << i << mimo |
---|
| 125 | std::stringstream out; |
---|
| 126 | out << "soused " << i << " neexistuje"; |
---|
| 127 | //throw out.str(); |
---|
| 128 | cout << out.str(); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
[1129] | 132 | void send2allNeighbours ( Setting &set, string messageName, double messageValue ) { |
---|
| 133 | for ( int i = 0; i < neighbours.length(); i++ ) { |
---|
| 134 | send2neighbour( set, i, messageName, messageValue ); |
---|
[1076] | 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[1139] | 138 | |
---|
[1129] | 139 | |
---|
[1076] | 140 | |
---|
[1129] | 141 | // FUNKCE VOLANE V main_loop NA ZACATKU |
---|
[1139] | 142 | |
---|
[1129] | 143 | void validate (){ |
---|
| 144 | rv_action = RV("Tc",1); |
---|
| 145 | rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) ); |
---|
[1139] | 146 | Tc = 80; |
---|
| 147 | idealTc = Tc; |
---|
[1129] | 148 | max_profit = 0; |
---|
[1141] | 149 | // prijate Tc a profity |
---|
| 150 | received_profit_sum.set_length( (maxTc - minTc)/stepTc +1 ); |
---|
| 151 | received_Tcs.set_length( (maxTc - minTc)/stepTc +1 ); |
---|
[1129] | 152 | cycle_counter = 0; |
---|
[1076] | 153 | |
---|
[1129] | 154 | BaseTrafficAgent::validate(); |
---|
[1139] | 155 | |
---|
| 156 | for ( int i=0; i<lanehs.length(); i ++ ) { |
---|
| 157 | unsigned int index = find_index( green_names, name + "_" + lanehs(i)->getSG() ); |
---|
| 158 | lanehs(i)->green_time_ratio = green_times( index ); |
---|
[1076] | 159 | } |
---|
[1139] | 160 | |
---|
| 161 | // pomocne logovaci soubory |
---|
| 162 | Tcs.open("Tcs.dat"); |
---|
| 163 | Tcs << "cyklus\tTc\tprofit\n"; |
---|
| 164 | stringstream Ro_file_name; |
---|
| 165 | Ro_file_name << "Ro_" << name << ".dat"; |
---|
| 166 | if ( name == "495" ) { |
---|
| 167 | Ros.open("Ros_495.dat"); |
---|
| 168 | Qs.open("Qs_495.dat"); |
---|
| 169 | } |
---|
| 170 | else { |
---|
| 171 | Ros.open("Ros_601.dat"); |
---|
| 172 | Qs.open("Qs_601.dat"); |
---|
| 173 | } |
---|
| 174 | Ros << "Hustoty provozu ve frontach" << endl << "n"; |
---|
| 175 | Qs << "Prumerna delka fronty" << endl << "n"; |
---|
| 176 | for ( int i = 0; i < lanehs.length(); i++ ) { |
---|
| 177 | Ros << "\t" << lanehs(i)->getQueueName(); |
---|
| 178 | Qs << "\t" << lanehs(i)->getAverageQueueLength(); |
---|
| 179 | } |
---|
| 180 | Ros << endl; |
---|
[1076] | 181 | } |
---|
| 182 | |
---|
[1129] | 183 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
---|
[1076] | 184 | |
---|
| 185 | void adapt (const vec &glob_dt) { |
---|
[1139] | 186 | // vynulovani pole prijatych rozdilu fronty |
---|
| 187 | received_queue_diffs.set_length(0); |
---|
| 188 | |
---|
[1129] | 189 | // inicializes vars of cycle of broadcast |
---|
| 190 | n_of_broadcast = 0; |
---|
| 191 | max_profit = 0; |
---|
[1139] | 192 | //cout << endl << name << ":" << endl << endl; |
---|
| 193 | // predani aktualnich dat do LaneHandler lanehs |
---|
| 194 | Ros << cycle_counter; |
---|
| 195 | Qs << cycle_counter; |
---|
| 196 | for ( int i=0; i<lanehs.length(); i ++ ) { |
---|
[1141] | 197 | lanehs(i)->inputs( 0 ) = inputs( 2*find_index(rv_inputs, lanehs(i)->rv_inputs.name(0) ) ); |
---|
| 198 | lanehs(i)->inputs( 1 ) = inputs( 2*find_index(rv_inputs, lanehs(i)->rv_inputs.name(0) ) + 1 ); |
---|
[1139] | 199 | lanehs(i)->Tc = Tc; |
---|
| 200 | lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) ); |
---|
| 201 | |
---|
[1141] | 202 | //lanehs(i)->echo(); |
---|
[1139] | 203 | // pomocne logovaci soubory |
---|
[1141] | 204 | Ros << ";" << lanehs(i)->getRo(); |
---|
| 205 | Qs << ";" << queues( find_index( rv_queues, lanehs(i)->getQueueName() ) )<<";"<< lanehs(i)->getAverageQueueLength()<< ";"; |
---|
[1076] | 206 | } |
---|
[1141] | 207 | Ros <<";;"<<getAverageRo() << endl; |
---|
| 208 | Qs <<";;"<<getQueue() << endl; |
---|
[1076] | 209 | |
---|
[1139] | 210 | if ( cycle_counter % sample_cycles == 0 ) { |
---|
| 211 | //cout << name << " queue DIFF " << getRelativeQueueDiff() << endl; |
---|
| 212 | |
---|
| 213 | for ( int i=0; i<lanehs.length(); i ++ ) { |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | //lanehs(i)->echo(); |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | //cout << name << " " << i << " " << lanes(i).sg << " " << lanes(i).queue << " input " << queues(i) << endl; |
---|
| 220 | } |
---|
[1076] | 221 | } |
---|
| 222 | |
---|
[1139] | 223 | for ( int i = 0; i < received_profit_sum.length(); i++ ) |
---|
| 224 | received_profit_sum(i) = 0; |
---|
[1076] | 225 | |
---|
| 226 | |
---|
| 227 | BaseTrafficAgent::adapt(glob_dt); |
---|
| 228 | } |
---|
| 229 | |
---|
[1129] | 230 | void broadcast(Setting &set){ |
---|
[1076] | 231 | |
---|
[1129] | 232 | // 1. cycle of communication |
---|
| 233 | // sends all tcs in range +- d_tc and expected profits according to tc |
---|
| 234 | // format: tc_index, profit_index |
---|
[1141] | 235 | if ( n_of_broadcast == 0 ) { |
---|
| 236 | //cout << endl << name+" PROFITS:"; |
---|
| 237 | int broadcast_width = (maxTc - minTc)/stepTc + 1; |
---|
| 238 | for ( int i = 0; i < broadcast_width; i++ ) { |
---|
| 239 | int time_cycle = minTc + i*stepTc; |
---|
[1139] | 240 | double profit = getProfit(time_cycle); |
---|
[1141] | 241 | //cout << "\t" << time_cycle << " " <<profit; |
---|
[1129] | 242 | // send tc |
---|
| 243 | stringstream time_cycle_stream; |
---|
[1141] | 244 | time_cycle_stream << "tc_" << (i); |
---|
[1129] | 245 | send2allNeighbours( set, time_cycle_stream.str(), time_cycle); |
---|
| 246 | |
---|
| 247 | // send profit |
---|
| 248 | stringstream profit_stream; |
---|
[1141] | 249 | profit_stream << "profit_" << (i); |
---|
[1139] | 250 | send2allNeighbours( set, profit_stream.str(), profit ); |
---|
[1129] | 251 | } |
---|
[1139] | 252 | |
---|
[1141] | 253 | //cout << endl; |
---|
[1129] | 254 | } |
---|
| 255 | |
---|
| 256 | n_of_broadcast ++; |
---|
[1076] | 257 | } |
---|
[1129] | 258 | |
---|
| 259 | void receive(const Setting& msg){ |
---|
| 260 | string what; |
---|
| 261 | string from; |
---|
| 262 | double val; |
---|
| 263 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 264 | UI::get(from, msg, "from", UI::compulsory); |
---|
| 265 | UI::get(val, msg, "value"); |
---|
[1139] | 266 | |
---|
| 267 | |
---|
[1129] | 268 | //cout << name << " receiving from " << from << " " << what << " : " << val<<endl; |
---|
[1139] | 269 | |
---|
| 270 | if ( what.substr(0,2) == "tc" ) { |
---|
| 271 | istringstream tc_i( what.substr(3, what.length()-3) ); |
---|
| 272 | int index; |
---|
| 273 | tc_i >> index; |
---|
| 274 | received_Tcs( index ) = val; |
---|
| 275 | //cout << "tc " << val << endl; |
---|
[1129] | 276 | } |
---|
[1139] | 277 | if ( what.substr(0,6) == "profit" ) { |
---|
| 278 | istringstream profit_i( what.substr(7, what.length()-7) ); |
---|
| 279 | int index; |
---|
| 280 | profit_i >> index; |
---|
| 281 | received_profit_sum( index ) = received_profit_sum( index ) + val; |
---|
| 282 | //cout << "pridavam profit " << val << endl; |
---|
| 283 | } |
---|
[1129] | 284 | |
---|
[1139] | 285 | |
---|
[1129] | 286 | if ( what == "new_stable_state" ) { |
---|
| 287 | //echo("new_stable_state"); |
---|
| 288 | BaseTrafficAgent::receive(msg); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | void act (vec &glob_ut){ |
---|
[1139] | 294 | |
---|
[1141] | 295 | if ( cycle_counter >= 10 && (cycle_counter %sample_cycles) == 0 ) { |
---|
[1129] | 296 | // choose tc with max profit |
---|
[1141] | 297 | //cout << name << " soucet front " << getQueue()<< endl; |
---|
| 298 | //cout << endl << name << " received + my profits "<<endl;; |
---|
| 299 | idealTc = Tc; |
---|
| 300 | max_profit = 0; |
---|
[1129] | 301 | for ( int i = 0; i < received_profit_sum.length(); i ++ ) { |
---|
| 302 | int time_cycle = received_Tcs(i); |
---|
[1139] | 303 | double profit = received_profit_sum(i) + getProfit(time_cycle); |
---|
[1141] | 304 | //cout << time_cycle << " " << profit << endl; |
---|
| 305 | if ( profit > max_profit && profit > min_profit ) { |
---|
[1129] | 306 | max_profit = profit; |
---|
| 307 | idealTc = time_cycle; |
---|
| 308 | } |
---|
| 309 | } |
---|
[1141] | 310 | |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | // NASTAVENI Tc |
---|
| 314 | //Tc = idealTc; |
---|
| 315 | //Tc = 40 + (cycle_counter%4)*80; |
---|
| 316 | int dTc = 0; |
---|
| 317 | if ( idealTc - Tc >= 2 ) |
---|
| 318 | dTc = 2; |
---|
| 319 | if ( Tc - idealTc >=2 ) |
---|
| 320 | dTc = -2; |
---|
| 321 | if ( idealTc - Tc >= 4 ) |
---|
| 322 | dTc = 4; |
---|
| 323 | if ( Tc - idealTc >=4 ) |
---|
| 324 | dTc = -4; |
---|
| 325 | Tc += dTc; |
---|
| 326 | //Tc = 84; |
---|
| 327 | cout << endl << name << "Tc: " << Tc << " idalni Tc " << idealTc << " se ziskem " << max_profit << endl; |
---|
[1129] | 328 | // nastaveni delky cyklu na Tc |
---|
| 329 | vec action; |
---|
| 330 | action.set_size(rv_action._dsize()); |
---|
| 331 | int st; |
---|
| 332 | int stage_time_sum = 0; // soucet delky fazi |
---|
| 333 | action(find_index(rv_action, "Tc")) = Tc; |
---|
| 334 | for ( int i =0; i < stage_names.length(); i ++) { |
---|
| 335 | if ( (i+1) < stage_names.length() ) { |
---|
[1141] | 336 | st = round(((double)(stage_times(i)*Tc))/80.0); |
---|
[1129] | 337 | stage_time_sum += st; |
---|
| 338 | action(find_index(rv_action, stage_names(i))) = st; |
---|
| 339 | } |
---|
| 340 | else // dopocitani posledni faze - oprava zaokrouhlovaci chyby |
---|
| 341 | action(find_index(rv_action, stage_names(i))) = Tc - stage_time_sum; |
---|
| 342 | } |
---|
| 343 | action2ds.filldown(action,glob_ut); |
---|
| 344 | } |
---|
[1141] | 345 | Tcs << cycle_counter << ";" << Tc<< ";" << idealTc << ";" << max_profit << endl; |
---|
| 346 | |
---|
| 347 | cycle_counter ++; |
---|
[1129] | 348 | } |
---|
[1076] | 349 | |
---|
[1139] | 350 | // KONEC |
---|
| 351 | void finalize() { |
---|
| 352 | Tcs.close(); |
---|
| 353 | } |
---|
[1076] | 354 | |
---|
[1139] | 355 | |
---|
| 356 | |
---|
[1076] | 357 | }; |
---|
| 358 | UIREGISTER(TrafficAgentCycleTime); |
---|