Changeset 1139

Show
Ignore:
Timestamp:
07/14/10 17:16:09 (14 years ago)
Author:
jabu
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/doprava/traffic_agent_cycle_time.h

    r1129 r1139  
    33const double measurement_cycle_time = 90; // s 
    44const 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 }; 
     5const int sample_cycles = 3; 
     6 
     7 
     8 
     9 
     10 
    6211 
    6312class TrafficAgentCycleTime : public BaseTrafficAgent { 
    6413public: 
    6514        int Tc; 
    66         int minTc; 
    67         int maxTc; 
    68         int stepTc; 
     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     
    6919        int idealTc; 
    70         static const int d_Tc = 3; // pocet navrhovanych delek cyklu     
    7120        int cycle_counter; 
    72         int max_profit; 
     21        double max_profit; 
    7322        unsigned int n_of_broadcast; 
    7423        Array <int> lane_sum; 
    75         Array <SignalGroup*> signal_groups; 
    76         Array <int> received_Tcs; 
    77         Array <int> received_profit_sum; 
     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; 
     32 
    7833 
    7934// POMOCNE FUNKCE 
     
    9348        } 
    9449 
     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 
    9556        void echo ( string message ) { 
    9657                cout << name << " hlasi: " << message << endl;           
     
    9859 
    9960        unsigned int find_index ( RV rv_vector, string index_string ) { 
    100                 for ( unsigned int i = 0; i < (int) rv_vector.length(); i ++) { 
     61                for ( unsigned int i = 0; i < rv_vector.length(); i ++) { 
    10162                        if ( rv_vector.name(i) == index_string ) 
    10263                                return i; 
     
    10667 
    10768        unsigned int find_index ( Array <string> arr, string index_string ) { 
    108                 for ( unsigned int i = 0; i < (int) arr.length(); i ++) { 
     69                for ( unsigned int i = 0; i < arr.length(); i ++) { 
    10970                        if ( arr(i) == index_string ) 
    11071                                return i; 
     
    11576// VYPOCETNI FUNKCE 
    11677 
    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                 } 
     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                }                
    12484                return sum; 
    12585        } 
    12686 
    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; 
     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(); 
    13395                } 
    13496                return queue; 
    13597        } 
    13698 
    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         
    158109 
    159110 
     
    182133        } 
    183134 
    184         int getProfit( int time_cycle ) { 
    185                 if ( Tc != time_cycle ) 
    186                         return getSumCarsWaitingTime( Tc ) - getSumCarsWaitingTime ( time_cycle ); 
    187                 else 
    188                         return 0; 
    189         } 
     135         
    190136 
    191137         
    192138// FUNKCE VOLANE V main_loop NA ZACATKU 
    193         void from_setting( Setting &set ) { 
    194                 BaseTrafficAgent::from_setting(set); 
    195                 //TrafficAgentCycleTime::Tc = 80; 
    196         } 
    197  
     139         
    198140        void validate (){ 
    199141                rv_action = RV("Tc",1); 
    200142                rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) ); 
    201                 Tc = 80; 
    202                 minTc = 60; 
    203                 maxTc = 120; 
    204                 stepTc = 5; 
     143                Tc = 80;                 
     144                idealTc = Tc; 
    205145                max_profit = 0; 
    206146                received_profit_sum.set_length( 2*d_Tc +1 ); 
     
    209149 
    210150                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                 } 
     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; 
    235177        } 
    236178 
     
    238180 
    239181        void adapt (const vec &glob_dt) { 
     182                // vynulovani pole prijatych rozdilu fronty 
     183                received_queue_diffs.set_length(0); 
     184 
    240185                // inicializes vars of cycle of broadcast 
    241186                n_of_broadcast = 0; 
    242187                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++ ) { 
     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++ ) 
    250218                        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; 
     219 
    269220 
    270221                BaseTrafficAgent::adapt(glob_dt); 
     
    277228                // format: tc_index, profit_index 
    278229                if ( n_of_broadcast == 0 && ( cycle_counter % sample_cycles == 0 ) ) { 
     230                        cout << endl << name+" PROFITS:"; 
    279231                        for ( int i = -d_Tc; i <= d_Tc; i++ ) {                          
    280232                                int time_cycle = Tc + i*stepTc; 
    281                                 int profit = getProfit(time_cycle); 
    282  
     233                                double profit = getProfit(time_cycle); 
     234                                cout << "\t" << time_cycle << " " <<profit; 
    283235                                // send tc 
    284236                                stringstream time_cycle_stream; 
     
    289241                                stringstream profit_stream; 
    290242                                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; 
     243                                send2allNeighbours( set, profit_stream.str(), profit );                          
     244                        } 
     245                         
     246                        cout << endl; 
    296247                } 
    297248                 
     
    306257                UI::get(from, msg, "from", UI::compulsory); 
    307258                UI::get(val, msg, "value"); 
    308                  
     259 
     260                                 
    309261                //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                 } 
     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                 
    326278                 
    327279                if ( what == "new_stable_state" ) { 
     
    333285 
    334286        void act (vec &glob_ut){ 
     287                 
    335288                if ( cycle_counter % sample_cycles == 0 ) { 
    336289                        // choose tc with max profit             
     290                        cout << endl << name << " received + my profits "<<endl;; 
    337291                        for ( int i = 0; i < received_profit_sum.length(); i ++ ) { 
    338292                                int time_cycle = received_Tcs(i); 
    339                                 int profit = received_profit_sum(i) + getProfit(time_cycle); 
     293                                double profit = received_profit_sum(i) + getProfit(time_cycle); 
     294                                cout << time_cycle << " " << profit << endl; 
    340295                                if ( profit > max_profit ) { 
    341296                                        max_profit = profit; 
    342297                                        idealTc = time_cycle; 
    343                                         //cout << name << " idealni Tc " << time_cycle << " s celkovym ziskem " << profit << endl; 
    344298                                } 
    345299                        } 
    346                         //cout << endl << name << " nastevuje TC na " << idealTc << endl; 
     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 
    347306                        Tc = idealTc; 
     307                        Tcs << cycle_counter << "\t" << Tc << "\t" << max_profit <<  endl; 
    348308 
    349309 
     
    365325                        action2ds.filldown(action,glob_ut); 
    366326                } 
    367                 cycle_counter ++; 
    368         } 
    369          
     327                        cycle_counter ++; 
     328        } 
     329         
     330        // KONEC 
     331        void finalize() { 
     332                Tcs.close(); 
     333        } 
     334 
     335 
    370336 
    371337};