Changeset 1133 for applications/doprava

Show
Ignore:
Timestamp:
07/13/10 17:33:21 (14 years ago)
Author:
ondrak
Message:

new count_rating() and related functions

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/doprava/traffic_agent_offset.h

    r1127 r1133  
    3030        //! list of agents, who request expected cars 
    3131        list<string> requesters; 
     32 
     33        //! offset set in last simulation step 
     34        int last_offset; 
     35        //! actual planned offset to set for next simulation step 
     36        int planned_offset; 
     37        //! rating of actual planned offset 
     38        double planned_rating;   
     39        //! avarage speed of cars 
     40        int VP; 
    3241         
    3342        double car_leaving; //s; how long is 1 car leaving queue 
     
    4857        //! after cycle_count cycles, we count avarege planned_offseta send it to Aimsun 
    4958        int cycle_count; 
     59        //! Finding of best own offsset starts on this offset change 
     60        int find_best_start; 
     61        //! minimal value of offsset change tested during looking for best offset 
     62        int find_best_limit; 
    5063 
    5164        //! counts all expected cars going from each lane, saves to outputs and rv_outputs 
     
    7285                                        exp2outputs.set_connection(rv_exp,rv_outputs); 
    7386 
    74                                         //Number of cars 
    75                                         exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j);         
     87                                        //cars density 
     88                                        exp(0)=lanehs(i)->expected_output(green_time)*lanes(i).alpha(j) / green_time;    
    7689 
    7790                                        start_time = green_starts(group_index(name+"_"+lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset; 
     
    8093                                        //last car arrive time 
    8194                                        exp(2)=start_time + green_time; 
    82                                         //TODO pushup az na konec 
     95                                         
    8396                                        exp2outputs.pushup(outputs,exp); 
    8497                                }                        
     
    8699                } 
    87100        }; 
     101         
     102        /*! creates periodic expansion of gb (green begin], ge (green end), using  
     103                expectations cb (cars begin), ce (cars end). Result is stored in gbv  
     104                (green begin vector) and gev (green end vector) */       
     105        void expand_greens(const double gb, const double ge, const vec cb, const vec ce, vec &gbv, vec &gev) { 
     106                gbv.set_size(1); 
     107                gev.set_size(1); 
     108                gbv(0)=gb; 
     109                gev(0)=ge; 
     110                if (ge>cycle_length) { 
     111                        gbv.ins(gbv.length(),0); 
     112                        gev.ins(gev.length(),ge-cycle_length); 
     113                } 
     114                int i=1; 
     115                while (gev(gev.length()-1)<ce(max_i(ce))) { 
     116                        gbv.ins(gbv.length(),gb+i*cycle_length); 
     117                        gev.ins(gev.length(),ge+i*cycle_length); 
     118                        i++; 
     119                } 
     120        }; 
     121         
     122        //! returns true if what is in interval <left;right> 
     123        bool in_interval(double what, const double left, const double right) { 
     124                return ((what>=left && what<=right) ? true : false); 
     125        } 
    88126 
    89127        //! counts planned rating using offset and recieved_exps 
     
    92130                double t_green_begin; 
    93131                double t_green_end; 
    94                 vec cars_count; 
     132                vec cars_density; 
    95133                vec t_cars_begin; 
    96134                vec t_cars_end; 
     
    108146                                int l=0;                 
    109147                                for (int j=0;j<rv_recieved_exps.length();j++) { 
    110                                  
    111148                                        int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-"); 
    112149                                        if (result>=0) { 
    113  
    114150                                                t_cars_begin.set_size(l+1,true); 
    115151                                                t_cars_end.set_size(l+1,true); 
    116                                                 cars_count.set_size(l+1,true); 
     152                                                cars_density.set_size(l+1,true); 
    117153 
    118154                                                ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps); 
    119155 
    120                                                 cars_count(l)=recieved_exps(ind(0)); 
     156                                                cars_density(l)=recieved_exps(ind(0)); 
    121157                                                t_cars_begin(l)=recieved_exps(ind(1)); 
    122158                                                t_cars_end(l)=recieved_exps(ind(2)); 
     
    128164                        } 
    129165                        if (found) {                     
    130  
    131166                                //counting rating 
    132167                                group_name = name+"_"+lanes(i).sg;              //e.g. 495_VA 
     
    136171                                t_green_end=t_green_begin + green_times(index)*cycle_length; 
    137172 
    138                                 /************** counting with all exps ****************/ 
    139  
    140                                 int k; 
    141                                 double t_act=t_green_begin; 
     173                                vec t_gb_vec; 
     174                                vec t_ge_vec; 
     175 
     176                                expand_greens(t_green_begin, t_green_end, t_cars_begin, t_cars_end, t_gb_vec, t_ge_vec); 
     177                                 
     178                                //! index for t_cars_begin 
     179                                int k=min_i(t_cars_begin); 
     180                                //! index for t_ggb_vec 
     181                                int l; 
     182                                //! indicator of actual position in whole interval 
     183                                double t_act=0; 
     184                                //! end of counting for actual line 
     185                                double t_limit=t_ge_vec(max_i(t_ge_vec)); 
     186                                //! end of closest future interval 
     187                                double t_end; 
     188 
    142189                                virtual_queue=lanehs(i)->queue; 
    143190 
    144191                                //cycle goes through all "stopping" points and counts queue lenght at these points 
    145                                 do { 
     192                                do {     
    146193                                        k=min_i(t_cars_begin); 
    147                                         if (k!=-1) {                                             
    148                                                 //in case there are cars comming before t_green begin 
    149                                                 if (t_cars_begin(k)<t_act) { 
    150                                                         if (t_cars_end(k)<=t_act) { 
    151                                                                 virtual_queue+=cars_count(k); 
    152  
    153                                                                 cars_count.del(k); 
    154                                                                 t_cars_begin.del(k); 
    155                                                                 t_cars_end.del(k); 
    156  
     194                                        l=min_i(t_gb_vec); 
     195                                        if (k!=-1) { 
     196                                                //cars are entering queue 
     197                                                if (in_interval(t_act,t_cars_begin(k), t_cars_end(k))) { 
     198                                                        //cars leaving and entering queue 
     199                                                        if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { 
     200                                                                t_end = min(t_cars_end(k),t_ge_vec(l)); 
     201                                                                virtual_queue+=(t_end - t_act)*(cars_density(k)-(1/car_leaving)); 
     202                                                                t_cars_begin(k)=t_end; 
     203                                                                t_gb_vec(l)=t_end; 
    157204                                                        } 
     205                                                        //cars only entering queue 
    158206                                                        else { 
    159                                                                 double frac=(t_cars_begin(k)-t_act)/(t_cars_end(k)-t_cars_begin(k)); 
    160                                                                 virtual_queue+=cars_count(k)*frac; 
    161                                                                 t_cars_begin(k)=t_act; 
     207                                                                t_end=min(t_cars_end(k),t_ge_vec(l)); 
     208                                                                virtual_queue+=(t_end-t_act)*cars_density(k); 
     209                                                                t_cars_begin(k)=t_end; 
    162210                                                        } 
    163211                                                } 
    164                                                 else if (t_cars_begin(k)==t_act) { 
    165                                                         if (t_cars_end(k)<t_green_end) { 
    166                                                                 virtual_queue+=cars_count(k)-(t_cars_end(k)-t_act)/car_leaving; 
    167                                                                 t_act=t_cars_end(k); 
     212                                                //cars are not entering queue 
     213                                                else { 
     214                                                        //cars are only leaving queue 
     215                                                        if (in_interval(t_act,t_gb_vec(l), t_ge_vec(l))) { 
     216                                                                t_end = min(t_ge_vec(l),t_cars_begin(k)); 
     217                                                                virtual_queue-=(t_end-t_act)/car_leaving; 
     218                                                                t_gb_vec(l)=t_end; 
     219                                                                 
     220                                                                //in case we emptied whole queue 
     221                                                                virtual_queue=max(virtual_queue,0.0); 
    168222                                                        } 
    169                                                         //if t_cars_end>=t_green_end 
     223                                                        //no cars entering, no cars leaving 
    170224                                                        else { 
    171                                                                 virtual_queue+=cars_count(k)-(t_green_end-t_act)/car_leaving; 
    172                                                                 t_act=t_green_end; 
     225                                                                t_end=min(t_gb_vec(l),t_cars_begin(k)); 
    173226                                                        } 
    174                                                         //more cars than cars_count(k) couldn't pass without stopping 
    175                                                         if (virtual_queue < -cars_count(k)) { 
    176                                                                 virtual_queue = -cars_count(k); 
    177                                                         } 
    178                                                         cars_count.del(k); 
     227                                                        t_act=t_endl 
     228                                                } 
     229                                                //raising rating 
     230                                                if (virtual_queue<0) { 
     231                                                        rating-=virtual_queue; 
     232                                                        virtual_queue=0; 
     233                                                } 
     234 
     235                                                //deleting used intervals 
     236                                                if (t_cars_begin(k)==t_cars_end(k)) { 
    179237                                                        t_cars_begin.del(k); 
    180238                                                        t_cars_end.del(k); 
     239                                                        cars_density.del(k); 
    181240                                                } 
    182                                                 //if t_cars_begin(k)>=t_act 
    183                                                 else { 
    184                                                         if (t_cars_begin(k)<t_green_end) { 
    185                                                                 virtual_queue-=(t_cars_begin(k)-t_act)/car_leaving; 
    186                                                                 t_act=t_cars_begin(k); 
    187                                                         } 
    188                                                         else { 
    189                                                                 virtual_queue-=(t_green_end-t_act)/car_leaving; 
    190                                                                 t_act=t_green_end; 
    191                                                         } 
    192                                                         // in case we managed to empty whole queue 
    193                                                         if (virtual_queue<0) { 
    194                                                                 virtual_queue=0; 
    195                                                         } 
     241                                                if (t_gb_vec(l)==t_ge_vec(l)) { 
     242                                                        t_gb_vec.del(l); 
     243                                                        t_ge_vec.del(l); 
    196244                                                } 
     245 
    197246                                        } 
    198247                                        //if no other expectations found 
    199248                                        else { 
    200                                                 virtual_queue-=(t_green_end-t_act)/car_leaving; 
    201                                                 t_act=t_green_end; 
     249                                                virtual_queue-=( t_ge_vec(l)-t_act)/car_leaving; 
     250                                                t_act=t_ge_vec(l); 
     251                                                t_gb_vec.del(l); 
     252                                                t_ge_vec.del(l); 
    202253                                        } 
    203                                         if (virtual_queue<0) { 
    204                                                 rating-=virtual_queue; 
    205                                                 virtual_queue=0; 
    206                                         } 
    207                                 } while (t_act<t_green_end); 
     254                                } while (t_act<t_limit); 
    208255                        } 
    209256                } 
     
    219266                //! rating if offset is lowered 
    220267                double rating_n; 
    221  
     268                //! center point for next cycle 
    222269                int new_center; 
    223270 
     
    239286                } 
    240287 
    241                 if (interval>2) { 
     288                if (interval>find_best_limit) { 
    242289                        interval/=2; 
    243290                        new_center=find_best_offset(new_center,interval); 
     
    249296        //! finds if changing neighbour's offset could have positive effect, returns found offset change and stores chage of rating to rating_change 
    250297        int find_best_exps(const int offset_change, const string neighbour, double &rating_change) { 
    251                 //! expectations recieved from neighbour 
    252                 vec original_exps; 
    253298                //! expactations after positve change of neighbour's offset 
    254299                vec positive_exps; 
     
    261306                //! rating if offset is lowered 
    262307                double rating_n;                 
    263                 original_exps.set_size(recieved_exps.length()); 
    264                  
    265                 original_exps=recieved_exps; 
     308 
    266309                positive_exps=recieved_exps; 
    267310                negative_exps=recieved_exps; 
     
    379422        } 
    380423 
     424        //! returns index of largest element in vector 
     425        int max_i(vec vector) { 
     426                if (vector.length()>0) { 
     427                        double max=vector(0); 
     428                        int index=0; 
     429                        for (int i=1;i<vector.length();i++) { 
     430                                if (vector(i)>max) { 
     431                                        max=vector(i); 
     432                                        index=i; 
     433                                } 
     434                        } 
     435                        return index; 
     436                } 
     437                return -1; 
     438        } 
     439         
     440 
    381441public: 
    382         //! offset set in last simulation step 
    383         int last_offset; 
    384         //! actual planned offset to set for next simulation step 
    385         int planned_offset; 
    386         //! rating of actual planned offset 
    387         double planned_rating;   
    388         //! avarage speed of cars 
    389         int VP; 
    390  
    391442        void validate() { 
    392443                rv_action = RV(name+"_offset", 1); 
     
    424475        void broadcast(Setting& set){ 
    425476 
    426                 //ask neighbours for exptected arrive times 
     477                //ask neighbours for expetcted arrive times 
    427478                if (need_exps) { 
    428479                        for (int i=0; i<neighbours.length(); i++){ 
     
    478529                } 
    479530         
    480                 // reached final offset. 
     531                // reached final offset 
    481532                if (final_state) { 
    482533                        final_state=false; 
     
    505556                         
    506557                        if (!passive) { 
    507                                 planned_offset=find_best_offset(planned_offset,8); 
     558                                planned_offset=find_best_offset(planned_offset,find_best_start); 
    508559                                planned_offset=normalize_offset(planned_offset); 
    509560                        } 
     
    580631 
    581632                car_leaving=2; 
    582                 VP=45; 
     633                VP=40; 
    583634                actual_time=0; 
    584635                 
    585636                negot_cycle=1; 
     637                total_offset=0; 
    586638                cycle_count=5; 
    587                 total_offset=0; 
    588639 
    589640                negot_offset=4; 
    590641                negot_limit=1; 
     642 
     643                find_best_start=8; 
     644                find_best_limit=2; 
    591645 
    592646                passive=0;