[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; |
---|
[1076] | 6 | |
---|
[1129] | 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
[1076] | 12 | class TrafficAgentCycleTime : public BaseTrafficAgent { |
---|
[1129] | 13 | public: |
---|
| 14 | int Tc; |
---|
[1139] | 15 | static const int minTc = 60; // nepouziva se |
---|
| 16 | static const int maxTc = 300; // nepouziva se |
---|
| 17 | static const int stepTc = 8; |
---|
| 18 | static const int d_Tc = 2; // pocet navrhovanych delek cyklu |
---|
[1129] | 19 | int idealTc; |
---|
[1076] | 20 | int cycle_counter; |
---|
[1139] | 21 | double max_profit; |
---|
[1129] | 22 | unsigned int n_of_broadcast; |
---|
[1076] | 23 | Array <int> lane_sum; |
---|
[1139] | 24 | //Array <SignalGroup*> signal_groups; |
---|
| 25 | Array <double> received_Tcs; |
---|
| 26 | Array <double> received_profit_sum; |
---|
| 27 | Array <double> received_queue_diffs; |
---|
| 28 | // pomocne logovaci soubory |
---|
| 29 | ofstream Tcs; |
---|
| 30 | ofstream Ros; |
---|
| 31 | ofstream Qs; |
---|
[1076] | 32 | |
---|
[1139] | 33 | |
---|
[1129] | 34 | // POMOCNE FUNKCE |
---|
| 35 | |
---|
| 36 | void printVector ( RV rv_vector, vec vector, string description ) { |
---|
| 37 | cout << endl << description << " " << name << endl; |
---|
| 38 | int k = 0; |
---|
| 39 | for ( int i = 0; i < rv_vector.length(); i ++ ) { |
---|
| 40 | cout << rv_vector.name(i) << " : "; |
---|
| 41 | for ( int j = 0; j < rv_vector.size(i); j ++ ) { |
---|
| 42 | cout << vector(k) << " "; |
---|
| 43 | k ++; |
---|
[1076] | 44 | } |
---|
[1129] | 45 | cout << endl; |
---|
[1076] | 46 | } |
---|
[1129] | 47 | cout << endl; |
---|
[1076] | 48 | } |
---|
| 49 | |
---|
[1139] | 50 | void add2array( Array <double> &arr, double item ) { |
---|
| 51 | arr.set_length( arr.length()+1, true ); |
---|
| 52 | arr(arr.length()-1) = item; |
---|
| 53 | cout << endl << "addin to array value " << item << endl; |
---|
| 54 | } |
---|
| 55 | |
---|
[1076] | 56 | void echo ( string message ) { |
---|
| 57 | cout << name << " hlasi: " << message << endl; |
---|
| 58 | } |
---|
| 59 | |
---|
[1129] | 60 | unsigned int find_index ( RV rv_vector, string index_string ) { |
---|
[1139] | 61 | for ( unsigned int i = 0; i < rv_vector.length(); i ++) { |
---|
[1129] | 62 | if ( rv_vector.name(i) == index_string ) |
---|
| 63 | return i; |
---|
| 64 | } |
---|
| 65 | return rv_vector.length(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | unsigned int find_index ( Array <string> arr, string index_string ) { |
---|
[1139] | 69 | for ( unsigned int i = 0; i < arr.length(); i ++) { |
---|
[1129] | 70 | if ( arr(i) == index_string ) |
---|
| 71 | return i; |
---|
| 72 | } |
---|
| 73 | return arr.length(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // VYPOCETNI FUNKCE |
---|
| 77 | |
---|
[1139] | 78 | // soucet cekacich autocasu |
---|
| 79 | double getWT ( double time_cycle ) { |
---|
| 80 | double sum = 0; |
---|
| 81 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
---|
| 82 | sum += lanehs(i)->getWT( time_cycle ); |
---|
| 83 | } |
---|
[1129] | 84 | return sum; |
---|
| 85 | } |
---|
| 86 | |
---|
[1139] | 87 | double getProfit ( double tc ) { |
---|
| 88 | return getWT(Tc) - getWT(tc); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | double getQueue () { |
---|
| 92 | double queue = 0; |
---|
| 93 | for ( int i = 0; i <lanehs.length(); i ++ ) { |
---|
| 94 | queue += lanehs(i)->getAverageQueueLength(); |
---|
[1129] | 95 | } |
---|
| 96 | return queue; |
---|
| 97 | } |
---|
| 98 | |
---|
[1139] | 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; |
---|
[1129] | 106 | } |
---|
| 107 | |
---|
[1139] | 108 | |
---|
[1129] | 109 | |
---|
[1139] | 110 | |
---|
[1076] | 111 | void send2neighbour( Setting &set, int i, string messageName, double messageValue ) { |
---|
| 112 | if ( i < neighbours.length() ) { |
---|
| 113 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 114 | UI::save( neighbours(i), msg, "to" ); |
---|
| 115 | UI::save (name,msg,"from"); |
---|
| 116 | UI::save( messageName, msg, "what" ); |
---|
| 117 | UI::save( messageValue, msg, "value" ); |
---|
| 118 | } |
---|
| 119 | else { |
---|
| 120 | //throw new Exception("soused "+((string)i)+" neexistuje"); |
---|
| 121 | //cout << endl << endl << "soused " << i << mimo |
---|
| 122 | std::stringstream out; |
---|
| 123 | out << "soused " << i << " neexistuje"; |
---|
| 124 | //throw out.str(); |
---|
| 125 | cout << out.str(); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[1129] | 129 | void send2allNeighbours ( Setting &set, string messageName, double messageValue ) { |
---|
| 130 | for ( int i = 0; i < neighbours.length(); i++ ) { |
---|
| 131 | send2neighbour( set, i, messageName, messageValue ); |
---|
[1076] | 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
[1139] | 135 | |
---|
[1129] | 136 | |
---|
[1076] | 137 | |
---|
[1129] | 138 | // FUNKCE VOLANE V main_loop NA ZACATKU |
---|
[1139] | 139 | |
---|
[1129] | 140 | void validate (){ |
---|
| 141 | rv_action = RV("Tc",1); |
---|
| 142 | rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) ); |
---|
[1139] | 143 | Tc = 80; |
---|
| 144 | idealTc = Tc; |
---|
[1129] | 145 | max_profit = 0; |
---|
| 146 | received_profit_sum.set_length( 2*d_Tc +1 ); |
---|
| 147 | received_Tcs.set_length( 2*d_Tc +1 ); |
---|
| 148 | cycle_counter = 0; |
---|
[1076] | 149 | |
---|
[1129] | 150 | BaseTrafficAgent::validate(); |
---|
[1139] | 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 ); |
---|
[1076] | 155 | } |
---|
[1139] | 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; |
---|
[1076] | 177 | } |
---|
| 178 | |
---|
[1129] | 179 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
---|
[1076] | 180 | |
---|
| 181 | void adapt (const vec &glob_dt) { |
---|
[1139] | 182 | // vynulovani pole prijatych rozdilu fronty |
---|
| 183 | received_queue_diffs.set_length(0); |
---|
| 184 | |
---|
[1129] | 185 | // inicializes vars of cycle of broadcast |
---|
| 186 | n_of_broadcast = 0; |
---|
| 187 | max_profit = 0; |
---|
[1139] | 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(); |
---|
[1076] | 200 | } |
---|
[1139] | 201 | Ros << endl; |
---|
| 202 | Qs << endl; |
---|
[1076] | 203 | |
---|
[1139] | 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 | } |
---|
[1076] | 215 | } |
---|
| 216 | |
---|
[1139] | 217 | for ( int i = 0; i < received_profit_sum.length(); i++ ) |
---|
| 218 | received_profit_sum(i) = 0; |
---|
[1076] | 219 | |
---|
| 220 | |
---|
| 221 | BaseTrafficAgent::adapt(glob_dt); |
---|
| 222 | } |
---|
| 223 | |
---|
[1129] | 224 | void broadcast(Setting &set){ |
---|
[1076] | 225 | |
---|
[1129] | 226 | // 1. cycle of communication |
---|
| 227 | // sends all tcs in range +- d_tc and expected profits according to tc |
---|
| 228 | // format: tc_index, profit_index |
---|
| 229 | if ( n_of_broadcast == 0 && ( cycle_counter % sample_cycles == 0 ) ) { |
---|
[1139] | 230 | cout << endl << name+" PROFITS:"; |
---|
[1129] | 231 | for ( int i = -d_Tc; i <= d_Tc; i++ ) { |
---|
| 232 | int time_cycle = Tc + i*stepTc; |
---|
[1139] | 233 | double profit = getProfit(time_cycle); |
---|
| 234 | cout << "\t" << time_cycle << " " <<profit; |
---|
[1129] | 235 | // send tc |
---|
| 236 | stringstream time_cycle_stream; |
---|
| 237 | time_cycle_stream << "tc_" << (i+d_Tc); |
---|
| 238 | send2allNeighbours( set, time_cycle_stream.str(), time_cycle); |
---|
| 239 | |
---|
| 240 | // send profit |
---|
| 241 | stringstream profit_stream; |
---|
| 242 | profit_stream << "profit_" << (i+d_Tc); |
---|
[1139] | 243 | send2allNeighbours( set, profit_stream.str(), profit ); |
---|
[1129] | 244 | } |
---|
[1139] | 245 | |
---|
| 246 | cout << endl; |
---|
[1129] | 247 | } |
---|
| 248 | |
---|
| 249 | n_of_broadcast ++; |
---|
[1076] | 250 | } |
---|
[1129] | 251 | |
---|
| 252 | void receive(const Setting& msg){ |
---|
| 253 | string what; |
---|
| 254 | string from; |
---|
| 255 | double val; |
---|
| 256 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 257 | UI::get(from, msg, "from", UI::compulsory); |
---|
| 258 | UI::get(val, msg, "value"); |
---|
[1139] | 259 | |
---|
| 260 | |
---|
[1129] | 261 | //cout << name << " receiving from " << from << " " << what << " : " << val<<endl; |
---|
[1139] | 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; |
---|
[1129] | 269 | } |
---|
[1139] | 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 | } |
---|
[1129] | 277 | |
---|
[1139] | 278 | |
---|
[1129] | 279 | if ( what == "new_stable_state" ) { |
---|
| 280 | //echo("new_stable_state"); |
---|
| 281 | BaseTrafficAgent::receive(msg); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | void act (vec &glob_ut){ |
---|
[1139] | 287 | |
---|
[1129] | 288 | if ( cycle_counter % sample_cycles == 0 ) { |
---|
| 289 | // choose tc with max profit |
---|
[1139] | 290 | cout << endl << name << " received + my profits "<<endl;; |
---|
[1129] | 291 | for ( int i = 0; i < received_profit_sum.length(); i ++ ) { |
---|
| 292 | int time_cycle = received_Tcs(i); |
---|
[1139] | 293 | double profit = received_profit_sum(i) + getProfit(time_cycle); |
---|
| 294 | cout << time_cycle << " " << profit << endl; |
---|
[1129] | 295 | if ( profit > max_profit ) { |
---|
| 296 | max_profit = profit; |
---|
| 297 | idealTc = time_cycle; |
---|
| 298 | } |
---|
| 299 | } |
---|
[1139] | 300 | cout << endl << name << "Tc: " << idealTc << " s celkovym ziskem " << max_profit << endl; |
---|
| 301 | // reseting lanehs |
---|
| 302 | for ( int i = 0; i < lanehs.length(); i ++ ) { |
---|
| 303 | lanehs(i)->resetQueue(); |
---|
| 304 | } |
---|
| 305 | |
---|
[1129] | 306 | Tc = idealTc; |
---|
[1139] | 307 | Tcs << cycle_counter << "\t" << Tc << "\t" << max_profit << endl; |
---|
[1129] | 308 | |
---|
| 309 | |
---|
| 310 | // nastaveni delky cyklu na Tc |
---|
| 311 | vec action; |
---|
| 312 | action.set_size(rv_action._dsize()); |
---|
| 313 | int st; |
---|
| 314 | int stage_time_sum = 0; // soucet delky fazi |
---|
| 315 | action(find_index(rv_action, "Tc")) = Tc; |
---|
| 316 | for ( int i =0; i < stage_names.length(); i ++) { |
---|
| 317 | if ( (i+1) < stage_names.length() ) { |
---|
| 318 | st = (stage_times(i)/80)*Tc; |
---|
| 319 | stage_time_sum += st; |
---|
| 320 | action(find_index(rv_action, stage_names(i))) = st; |
---|
| 321 | } |
---|
| 322 | else // dopocitani posledni faze - oprava zaokrouhlovaci chyby |
---|
| 323 | action(find_index(rv_action, stage_names(i))) = Tc - stage_time_sum; |
---|
| 324 | } |
---|
| 325 | action2ds.filldown(action,glob_ut); |
---|
| 326 | } |
---|
[1139] | 327 | cycle_counter ++; |
---|
[1129] | 328 | } |
---|
[1076] | 329 | |
---|
[1139] | 330 | // KONEC |
---|
| 331 | void finalize() { |
---|
| 332 | Tcs.close(); |
---|
| 333 | } |
---|
[1076] | 334 | |
---|
[1139] | 335 | |
---|
| 336 | |
---|
[1076] | 337 | }; |
---|
| 338 | UIREGISTER(TrafficAgentCycleTime); |
---|