| 1 | #include "traffic_agent.cpp" |
|---|
| 2 | const string testMessage = "test_message"; |
|---|
| 3 | const double measurement_cycle_time = 90; // s |
|---|
| 4 | const double saturated_stream = 0.5; // car/s |
|---|
| 5 | const int sample_cycles = 3; |
|---|
| 6 | const double min_profit = 30.0; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class TrafficAgentCycleTime : public BaseTrafficAgent { |
|---|
| 14 | public: |
|---|
| 15 | int Tc; |
|---|
| 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 |
|---|
| 20 | int idealTc; |
|---|
| 21 | int cycle_counter; |
|---|
| 22 | double max_profit; |
|---|
| 23 | unsigned int n_of_broadcast; |
|---|
| 24 | Array <int> lane_sum; |
|---|
| 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; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 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 ++; |
|---|
| 45 | } |
|---|
| 46 | cout << endl; |
|---|
| 47 | } |
|---|
| 48 | cout << endl; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void add2array( Array <double> &arr, double item ) { |
|---|
| 52 | arr.set_length( arr.length()+1, true ); |
|---|
| 53 | arr(arr.length()-1) = item; |
|---|
| 54 | //cout << endl << "addin to array value " << item << endl; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void echo ( string message ) { |
|---|
| 58 | cout << name << " hlasi: " << message << endl; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | unsigned int find_index ( RV rv_vector, string index_string ) { |
|---|
| 62 | for ( unsigned int i = 0; i < rv_vector.length(); i ++) { |
|---|
| 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 ) { |
|---|
| 70 | for ( unsigned int i = 0; i < arr.length(); i ++) { |
|---|
| 71 | if ( arr(i) == index_string ) |
|---|
| 72 | return i; |
|---|
| 73 | } |
|---|
| 74 | return arr.length(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // VYPOCETNI FUNKCE |
|---|
| 78 | |
|---|
| 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 | } |
|---|
| 85 | return sum; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | double getProfit ( double tc ) { |
|---|
| 89 | if ( tc != Tc ) |
|---|
| 90 | return (getWT(Tc) - getWT(tc))*( 1/ (Tc-tc)*(Tc-tc) ); |
|---|
| 91 | else |
|---|
| 92 | return 0; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | double getQueue () { |
|---|
| 96 | double queue = 0; |
|---|
| 97 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
|---|
| 98 | queue += lanehs(i)->getAverageQueueLength(); |
|---|
| 99 | } |
|---|
| 100 | return queue; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | double getAverageRo () { |
|---|
| 104 | double Ro_sum = 0; |
|---|
| 105 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
|---|
| 106 | Ro_sum += lanehs(i)->getRo(); |
|---|
| 107 | } |
|---|
| 108 | return Ro_sum/lanehs.length(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 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 | |
|---|
| 132 | void send2allNeighbours ( Setting &set, string messageName, double messageValue ) { |
|---|
| 133 | for ( int i = 0; i < neighbours.length(); i++ ) { |
|---|
| 134 | send2neighbour( set, i, messageName, messageValue ); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | // FUNKCE VOLANE V main_loop NA ZACATKU |
|---|
| 142 | |
|---|
| 143 | void validate (){ |
|---|
| 144 | rv_action = RV("Tc",1); |
|---|
| 145 | rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) ); |
|---|
| 146 | Tc = 80; |
|---|
| 147 | idealTc = Tc; |
|---|
| 148 | max_profit = 0; |
|---|
| 149 | // prijate Tc a profity |
|---|
| 150 | received_profit_sum.set_length( (maxTc - minTc)/stepTc +1 ); |
|---|
| 151 | received_Tcs.set_length( (maxTc - minTc)/stepTc +1 ); |
|---|
| 152 | cycle_counter = 0; |
|---|
| 153 | |
|---|
| 154 | BaseTrafficAgent::validate(); |
|---|
| 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 ); |
|---|
| 159 | } |
|---|
| 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; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
|---|
| 184 | |
|---|
| 185 | void adapt (const vec &glob_dt) { |
|---|
| 186 | // vynulovani pole prijatych rozdilu fronty |
|---|
| 187 | received_queue_diffs.set_length(0); |
|---|
| 188 | |
|---|
| 189 | // inicializes vars of cycle of broadcast |
|---|
| 190 | n_of_broadcast = 0; |
|---|
| 191 | max_profit = 0; |
|---|
| 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 ++ ) { |
|---|
| 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 ); |
|---|
| 199 | lanehs(i)->Tc = Tc; |
|---|
| 200 | lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) ); |
|---|
| 201 | |
|---|
| 202 | //lanehs(i)->echo(); |
|---|
| 203 | // pomocne logovaci soubory |
|---|
| 204 | Ros << ";" << lanehs(i)->getRo(); |
|---|
| 205 | Qs << ";" << queues( find_index( rv_queues, lanehs(i)->getQueueName() ) )<<";"<< lanehs(i)->getAverageQueueLength()<< ";"; |
|---|
| 206 | } |
|---|
| 207 | Ros <<";;"<<getAverageRo() << endl; |
|---|
| 208 | Qs <<";;"<<getQueue() << endl; |
|---|
| 209 | |
|---|
| 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 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | for ( int i = 0; i < received_profit_sum.length(); i++ ) |
|---|
| 224 | received_profit_sum(i) = 0; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | BaseTrafficAgent::adapt(glob_dt); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | void broadcast(Setting &set){ |
|---|
| 231 | |
|---|
| 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 |
|---|
| 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; |
|---|
| 240 | double profit = getProfit(time_cycle); |
|---|
| 241 | //cout << "\t" << time_cycle << " " <<profit; |
|---|
| 242 | // send tc |
|---|
| 243 | stringstream time_cycle_stream; |
|---|
| 244 | time_cycle_stream << "tc_" << (i); |
|---|
| 245 | send2allNeighbours( set, time_cycle_stream.str(), time_cycle); |
|---|
| 246 | |
|---|
| 247 | // send profit |
|---|
| 248 | stringstream profit_stream; |
|---|
| 249 | profit_stream << "profit_" << (i); |
|---|
| 250 | send2allNeighbours( set, profit_stream.str(), profit ); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | //cout << endl; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | n_of_broadcast ++; |
|---|
| 257 | } |
|---|
| 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"); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | //cout << name << " receiving from " << from << " " << what << " : " << val<<endl; |
|---|
| 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; |
|---|
| 276 | } |
|---|
| 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 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 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){ |
|---|
| 294 | |
|---|
| 295 | if ( cycle_counter >= 10 && (cycle_counter %sample_cycles) == 0 ) { |
|---|
| 296 | // choose tc with max profit |
|---|
| 297 | //cout << name << " soucet front " << getQueue()<< endl; |
|---|
| 298 | //cout << endl << name << " received + my profits "<<endl;; |
|---|
| 299 | idealTc = Tc; |
|---|
| 300 | max_profit = 0; |
|---|
| 301 | for ( int i = 0; i < received_profit_sum.length(); i ++ ) { |
|---|
| 302 | int time_cycle = received_Tcs(i); |
|---|
| 303 | double profit = received_profit_sum(i) + getProfit(time_cycle); |
|---|
| 304 | //cout << time_cycle << " " << profit << endl; |
|---|
| 305 | if ( profit > max_profit && profit > min_profit ) { |
|---|
| 306 | max_profit = profit; |
|---|
| 307 | idealTc = time_cycle; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 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; |
|---|
| 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() ) { |
|---|
| 336 | st = round(((double)(stage_times(i)*Tc))/80.0); |
|---|
| 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 | } |
|---|
| 345 | Tcs << cycle_counter << ";" << Tc<< ";" << idealTc << ";" << max_profit << endl; |
|---|
| 346 | |
|---|
| 347 | cycle_counter ++; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | // KONEC |
|---|
| 351 | void finalize() { |
|---|
| 352 | Tcs.close(); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | }; |
|---|
| 358 | UIREGISTER(TrafficAgentCycleTime); |
|---|