Changeset 1026

Show
Ignore:
Timestamp:
06/02/10 00:13:50 (14 years ago)
Author:
ondrak
Message:

zlicin.cfg now compatible with BaseTrafficAgent?.
main_loop shows warning, if reaches max number of negotiation cycles
step_time and cycle_time renamed to step_length and cycle_length
GreenWaveTrafficAgent? works, but queues are not from Aimsun yet.

Location:
applications/doprava
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • applications/doprava/Zlicin.cfg

    r903 r1026  
    22        { // agent 1 
    33        class = "BaseTrafficAgent"; 
    4           name = "495"; 
    5           sg = ( 
    6             { name = "VA"; 
    7               detectors = ( 
    8                         { name = "DVA1";  distance = 10;},  
    9               {name = "DVA";  distance = 20; } 
    10               ); 
    11             },  
    12             { name = "VB"; 
    13               detectors = (); 
    14               input = {}; 
    15               output = {}; 
    16             }); 
    17   input = ( // remote detectors  
    18     { 
    19       name = "601_DVAa"; 
    20                 distance = 100; 
    21       to_sg = ("VC","VD"); 
    22     }, 
    23     { 
    24       name = "601_DVA"; 
    25                 distance = 100; 
    26       to_sg = ("VC","VD"); 
    27     } 
    28   ); 
    29   output = ( //  
    30      { 
    31                 detectors = ("495_DVC"); 
    32       to = "601"; 
    33       } 
    34   ); 
    35  
    36   offset = 40; //s 
    37   moje_special=14; 
     4        name = "495"; 
     5        lanes = ( 
     6                        { sg="VA"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";},  
     7                        { sg="VB"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";} 
     8        ); 
     9        neighbours = ("601"); 
     10        offset = 10; //s 
     11        moje_special=14; 
    3812}, 
    3913{ //agent 2 
    4014        class = "BaseTrafficAgent"; 
    41   name = "601"; 
    42   sg = ( 
    43     { name = "VA"; 
    44       detectors = ( 
    45                 { name = "DVA"; distance = 10; },  
    46                  {name = "DVAa"; distance = 20; } 
    47       ); 
    48     },  
    49     { name = "VB"; 
    50       detectors = (); 
    51     } 
    52   ); 
    53    
    54   offset = 60; //s 
    55         input = (); 
    56         output = ({to="495"; detectors=("601_DVAa","601_DVA");}); 
     15        name = "601"; 
     16        lanes = ( 
     17                        { sg="VA"; inputs = ("601_DVA","601_DVB");  outputs= ("495_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";},  
     18                        { sg="VB"; inputs = ("601_DVC","601_DVD");  outputs= ("495_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";} 
     19        );   
     20        neighbours = ("495"); 
     21        offset = 40; 
     22 
    5723} 
     24 
    5825); 
    5926 
    60 logger = {class="memlog"; filename = "zlicin"; maxlen = 1000;}; 
     27logger = {class="memlog"; filename = "mujlog"; maxlen = 1000;}; 
     28 
     29 
     30 
    6131 
    6232system = { 
     
    7242        // length is 24 hours (24:00:00). 
    7343        stop_time = "01:00:00"; 
     44         
    7445}; 
     46 
     47//Number of recieve/broadcast calls before end of negotiation 
     48negotiation_cycles = 5; 
  • applications/doprava/Zlicingw.cfg

    r986 r1026  
    44        name = "495"; 
    55        lanes = ( 
    6                         { sg="VA"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";},  
    7                         { sg="VB"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";} 
     6                        { sg="VA"; inputs = ("495_DVA1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";},  
     7                        { sg="VB"; inputs = ("495_DVB1");  outputs= ("601_DVA","601_DVAa"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="495_Q2";}, 
     8                        { sg="VC"; inputs = ("495_DVC");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";},  
     9                        { sg="VD"; inputs = ("495_DVD");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";}, 
     10                        { sg="VE"; inputs = ("495_DVE");  outputs= ("601_DVA","601_DVAa"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="495_Q1";},  
     11                        { sg="VF"; inputs = ("495_DVF1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";} 
    812        ); 
    913        neighbours = ("601"); 
     
    1721        name = "601"; 
    1822        lanes = ( 
    19                         { sg="VA"; inputs = ("601_DVA","601_DVB");  outputs= ("495_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";},  
    20                         { sg="VB"; inputs = ("601_DVC","601_DVD");  outputs= ("495_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";} 
     23                        { sg="VA"; inputs = ("601_DVA");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";},  
     24                        { sg="VA"; inputs = ("601_DVAa");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";}, 
     25                        { sg="VB"; inputs = ("601_DVB1");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="601_Q1";},  
     26                        { sg="VB"; inputs = ("601_DVB1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";}, 
     27                        { sg="VC"; inputs = ("601_DVC");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [1.0 ]; queue="601_Q1";},  
     28                        { sg="VD"; inputs = ("601_DVD1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";}, 
     29                        { sg="SE"; inputs = ("601_DSE1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";},  
     30                        { sg="VE"; inputs = ("601_DVE1");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="601_Q2";} 
    2131        );   
    2232        neighbours = ("495"); 
     
    2838); 
    2939 
    30 logger = {class="memlog"; filename = "zlicin"; maxlen = 1000;}; 
     40logger = {class="memlog"; filename = "mujzlicin"; maxlen = 1000;}; 
    3141 
    3242 
     
    4454        // simulation in our case starts always at 00:00:00. Default simulation 
    4555        // length is 24 hours (24:00:00). 
    46         stop_time = "00:05:00"; 
     56        stop_time = "00:30:00"; 
    4757         
    4858}; 
    4959 
    5060//Number of recieve/broadcast calls before end of negotiation 
    51 negotiation_cycles = 5; 
     61negotiation_cycles = 15; 
  • applications/doprava/main_loop.cpp

    r932 r1026  
    3636        // LOGGER 
    3737        shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" ); 
    38         if (!L) {L=new stdlog();} // DEFAULT LOGGER <== poor choice, use better ones 
     38        if (!L) { 
     39                L=new stdlog(); // DEFAULT LOGGER <== poor choice, use better ones 
     40        }  
    3941 
    4042 
     
    105107                }  
    106108                while ((Queue.getLength()>0) && (cycle<max_cycles)); 
     109                if (cycle==max_cycles) { 
     110                        bdm_warning("Reached maximum number of cycles"); 
     111                } 
    107112 
    108113                for ( int i=0; i<Ags.length(); i++ ) { 
  • applications/doprava/traffic_agent.cpp

    r986 r1026  
    3131        } 
    3232 
    33         exp_output=last_inputs*(green_time/agent->step_time); 
     33        exp_output=last_inputs*(green_time/agent->step_length); 
    3434 
    3535        //////// 
     
    4343         
    4444        for (int i=0;i<inputs.length();i+=2) { 
    45                 density+=inputs(i)/agent->step_time; 
     45                density+=inputs(i)/agent->step_length; 
    4646        } 
    4747 
     
    5151void BaseTrafficAgent::from_setting(const Setting& set) 
    5252{ 
    53         step_time=90; 
    54         cycle_time=80; 
     53        step_length=90; 
     54        cycle_length=80; 
    5555 
    5656        bdm::Participant::from_setting(set); 
  • applications/doprava/traffic_agent.h

    r986 r1026  
    135135 
    136136                //! simulator's step length in seconds 
    137                 int step_time; 
     137                int step_length; 
    138138 
    139139                //! lenght of cycle in seconds 
    140                 int cycle_time; 
     140                int cycle_length; 
    141141                 
    142142        public: 
  • applications/doprava/traffic_agent_offset.h

    r986 r1026  
    44class GreenWaveTrafficAgent : public BaseTrafficAgent { 
    55protected: 
    6         double stable_state_loss; 
    7         double best_offset; 
     6        double rating_change; 
     7        int negot_offset; 
     8 
     9        RV rv_change_request; 
     10        vec change_request; 
    811 
    912        RV rv_recieved_exps; 
    1013        vec recieved_exps; 
    11          
     14 
     15        RV rv_recieved_changes; 
     16        vec recieved_changes; 
     17 
     18        //! name of agent, whose change we have accepted 
     19        string accepted_from; 
     20 
    1221        list<string> seznam; 
    1322         
    14  
    1523        double car_leaving; //s; how long is 1 car leaving queue 
    1624 
    17         bool recieved_exp_request; 
    18                          
    19         //counts all expected cars going from each lane 
     25        bool need_exps; 
     26        bool new_stable_state; 
     27        bool send_requests; 
     28        bool final_state; 
     29        //bool reset_negot_offset; 
     30 
     31        //! counts all expected cars going from each lane, saves to outputs and rv_outputs 
    2032        void expected_cars(/*const string dest*/) { 
    2133                double start_time; 
     
    2335                for (int i=0;i<lanes.length();i++) { 
    2436                        for (int j=0;j<lanes(i).outputs.length();j++) { 
    25                                 //              if (lanes(i).outputs(j).substr(0,3)==dest) { 
    26                                 // 
    2737                                string group = name+"_"+lanes(i).sg;            //e.g. 495_VA 
    2838                                ivec index=rv_inputs.dataind(RV(group,1)); 
    2939                                vec green_time=inputs(index); 
    3040 
    31  
    3241                                rv_outputs.add(RV(lanes(i).outputs(j)+"-"+group,3)); 
    3342                                outputs.set_size(rv_outputs._dsize()); 
    3443 
    35                                 ivec indexes = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3)); 
     44                                ivec ind = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3)); 
    3645 
    3746                                //Number of cars 
    38                                 outputs(indexes(0))=lanehs(i)->expected_output(green_time(0));   
     47                                outputs(ind(0))=lanehs(i)->expected_output(green_time(0));       
    3948 
    4049                                start_time = green_starts(sg_index(lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset; 
    4150                                //first car arrive time 
    42                                 outputs(indexes(1))=start_time;                                                                  
     51                                outputs(ind(1))=start_time;                                                                      
    4352                                //last car arrive time 
    44                                 outputs(indexes(2))= start_time + green_time(0); 
    45  
    46                                 //} 
    47                         } 
    48                 } 
    49         } 
    50  
    51 public: 
    52         int last_offset; 
    53         int planned_offset; 
    54         double actual_rating; 
    55          
    56         //! array of existing signal groups 
    57         Array<string> sgs; 
    58         //! relative starts of green for signal groups 
    59         ivec green_starts; 
    60  
    61         //! avarage speed of cars 
    62         int VP; 
    63  
    64         void validate() { 
    65                 rv_action = RV(name+"_offset", 1); // <======= example 
    66  
    67                 for (int i=0; i<sgs.length();i++) { 
    68                         rv_inputs.add(RV(name+"_"+sgs(i),1)); 
    69                          
    70                         //place for expected inputs 
    71                         //rv_expected_traffic.add(RV(name+"_"+sgs(i)+"_exp",3)); 
    72                 } 
    73  
    74                 inputs.set_size(rv_inputs._dsize()); 
    75                 //expected_traffic.set_size(rv_expected_traffic._dsize()); 
    76                  
    77                 BaseTrafficAgent::validate(); 
    78         } 
    79  
    80         //! returns index of signal group sg 
    81         int sg_index(string sg) { 
    82                 for (int i=0;i<sgs.length();i++) { 
    83                         if (sgs(i)==sg) { 
    84                                 return i; 
    85                         } 
    86                 } 
    87                 return -1; 
    88         } 
    89  
    90         void adapt(const vec &glob_dt) { 
    91                 BaseTrafficAgent::adapt(glob_dt); 
    92  
    93                 planned_offset=last_offset; 
    94         } 
    95         //! counts actual rating using planned_offset and recieved_exps 
    96         void count_rating() { 
     53                                outputs(ind(2))= start_time + green_time(0); 
     54                        } 
     55                } 
     56        }; 
     57 
     58        //! counts planned rating using offset and recieved_exps 
     59        double count_rating(const int offset) { 
    9760                double virtual_queue; 
    9861                double t_emptiyng; 
     
    10164                double t_cars_begin; 
    10265                double t_cars_end; 
     66//              vec t_cars_begin; 
     67//              vec t_cars_end; 
    10368                double t_mixing; 
    10469                bool found; 
    10570 
    106                 actual_rating=0.0; 
    107  
    108                 for (int i=0;i<lanehs.length();i++) { 
     71                double rating=0.0; 
     72 
     73                for (int i=0;i<lanes.length();i++) { 
    10974 
    11075                        //Finding, if we have some expectations 
    11176                        found=false; 
    112                         for (int j=0;j<rv_recieved_exps.length();j++) { 
    113                                 for (int k=0;k<lanes(i).inputs.length();k++) { 
     77                        for (int k=0;k<lanes(i).inputs.length();k++) {  
     78//                              t_cars_begin.set_size(lanes(i).inputs.length()); 
     79//                              t_cars_end.set_size(lanes(i).inputs.length()); 
     80 
     81                                for (int j=0;j<rv_recieved_exps.length();j++) { 
    11482                                        int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-"); 
    11583                                        if (result>=0) { 
    116                                                 ivec indexes = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3)); 
    117  
    118                                                 t_cars_begin=recieved_exps(indexes(1)); 
    119                                                 t_cars_end=recieved_exps(indexes(2)); 
     84                                                ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3)); 
     85 
     86                                                t_cars_begin=recieved_exps(ind(1));             //TODO now using only last found exp. 
     87                                                t_cars_end=recieved_exps(ind(2)); 
    12088                                                found=true; 
    12189                                        } 
    12290                                } 
    12391                        } 
     92 
    12493                        if (!found) { 
    12594                                t_cars_begin=0; 
    126                                 t_cars_end=cycle_time; 
    127                         } 
    128  
     95                                t_cars_end=cycle_length; 
     96                        } 
    12997                        //counting rating 
    130                         t_green_begin=green_starts(sg_index(lanes(i).sg)) + planned_offset; 
    131                         ivec indexes = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1)); 
    132                         t_green_end=t_green_begin+inputs(indexes(0)); 
    133  
     98                        t_green_begin=green_starts(sg_index(lanes(i).sg)) + offset; 
     99                        ivec ind = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1)); 
     100                        t_green_end=t_green_begin+inputs(ind(0)); 
     101 
     102                        //TODO use real queues lenghts 
    134103                        if (t_green_begin<t_cars_begin) { 
    135                                 virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving; 
     104                                //virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving; 
     105                                //virtual_queue=(rand()%8)-(t_cars_begin-t_green_begin)/car_leaving; 
     106                                virtual_queue=5-(t_cars_begin-t_green_begin)/car_leaving; 
    136107                        }  
    137108                        else if (t_green_begin>t_cars_begin) { 
    138                                 virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 
    139                         } 
    140                         //TODO lanehs(i)->queue je zatim prazdne => nesmyslne vysledky 
     109                                //virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 
     110                                //virtual_queue=(rand()%9)+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 
     111                                virtual_queue=4+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density(); 
     112                        } 
    141113 
    142114                        t_emptiyng=virtual_queue/(1/car_leaving - lanehs(i)->expected_density()); 
    143115                        t_mixing=min(t_green_end,t_cars_end)-max(t_green_begin,t_cars_begin); 
    144116 
    145                         actual_rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0);   
    146                 } 
    147         } 
    148  
     117                        rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0);   
     118                } 
     119                return rating; 
     120        } 
     121 
     122        //! finds best offset using recieved_exps. Returns found offset 
     123        int find_best_offset(const int center, int interval) { 
     124                //! rating if offset is rised 
     125                double rating_p; 
     126                //! rating if offset is unchaged (=center) 
     127                double rating_c; 
     128                //! rating if offset is lowered 
     129                double rating_n; 
     130 
     131                int new_center; 
     132 
     133                rating_p=count_rating(center+interval); 
     134                rating_c=count_rating(center); 
     135                rating_n=count_rating(center-interval); 
     136 
     137                new_center=center; 
     138                int max_index=max_of_three(rating_p,rating_c,rating_n); 
     139                switch (max_index) { 
     140                        case 0: 
     141                                new_center+=interval; 
     142                                break; 
     143                        case 1: 
     144                                break; 
     145                        case 2: 
     146                                new_center-=interval; 
     147                                break; 
     148                } 
     149 
     150                if (interval>2) { 
     151                        interval/=2; 
     152                        new_center=find_best_offset(new_center,interval); 
     153                } 
     154 
     155                return new_center; 
     156        } 
     157 
     158        //! finds if changing neighbour's offset could have positive effect, returns found offset change 
     159        int find_best_exps(const int offset_change, const string neighbour, double &rating_change) { 
     160                //! expectations recieved from neighbour 
     161                vec original_exps; 
     162                //! expactations after positve change of neighbour's offset 
     163                vec positive_exps; 
     164                //! expactations after negative change of neighbour's offset 
     165                vec negative_exps; 
     166                //! rating if offset is rised 
     167                double rating_p; 
     168                //! rating if offset is unchaged 
     169                double rating_c; 
     170                //! rating if offset is lowered 
     171                double rating_n;                 
     172                original_exps.set_size(recieved_exps.length()); 
     173                 
     174                original_exps=recieved_exps; 
     175                positive_exps=recieved_exps; 
     176                negative_exps=recieved_exps; 
     177 
     178                for (int j=0;j<rv_recieved_exps.length();j++) { 
     179                        int res = rv_recieved_exps.name(j).find("-"+neighbour); 
     180                        if (res>0) { 
     181                                ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3)); 
     182                                rating_n=count_rating(planned_offset); 
     183 
     184                                positive_exps(ind(1))+=offset_change; 
     185                                positive_exps(ind(2))+=offset_change; 
     186 
     187                                negative_exps(ind(1))-=offset_change; 
     188                                negative_exps(ind(2))-=offset_change; 
     189                        } 
     190                } 
     191 
     192                rating_c=count_rating(planned_offset); 
     193 
     194                recieved_exps=positive_exps; 
     195                rating_p=count_rating(planned_offset); 
     196 
     197                recieved_exps=negative_exps; 
     198                rating_n=count_rating(planned_offset); 
     199 
     200                recieved_exps=original_exps; 
     201 
     202                int max_index=max_of_three(rating_p,rating_c,rating_n); 
     203                switch (max_index) { 
     204                        case 0: 
     205                                rating_change=rating_p-rating_c; 
     206                                return offset_change; 
     207                                break; 
     208                        case 1: 
     209                                rating_change=0; 
     210                                return 0; 
     211                                break; 
     212                        case 2: 
     213                                rating_change=rating_n-rating_c; 
     214                                return -offset_change; 
     215                                break; 
     216                } 
     217                rating_change=NULL; 
     218                return NULL; 
     219        } 
     220 
     221        //! returns index of signal group sg 
     222        int sg_index(const string sg) { 
     223                for (int i=0;i<sgs.length();i++) { 
     224                        if (sgs(i)==sg) { 
     225                                return i; 
     226                        } 
     227                } 
     228                return -1; 
     229        } 
     230         
     231        //! returns offset value shifted to fit interval <0;cycle_length> 
     232        int normalize_offset(int offset) { 
     233                while (offset<0 && offset<cycle_length) { 
     234                        if (offset<0) { 
     235                                offset+=cycle_length; 
     236                        } 
     237                        else { 
     238                                offset-=cycle_length; 
     239                        } 
     240                } 
     241                return offset; 
     242        } 
     243 
     244 
     245        //! returns index of maximum of entered values 
     246        int max_of_three(const double a, const double b, const double c) { 
     247                int index = a > b ? 0 : 1; 
     248 
     249                if (index == 0) { 
     250                        index = a > c ? 0 : 2; 
     251                }  
     252                else { 
     253                        index = b > c ? 1 : 2; 
     254                } 
     255                return index; 
     256        } 
     257 
     258public: 
     259        //! offset set in last simulation step 
     260        int last_offset; 
     261        //! actual planned offset to set for next simulation step 
     262        int planned_offset; 
     263        //! rating of actual planned offset 
     264        double planned_rating; 
     265         
     266        //! array of existing signal groups 
     267        Array<string> sgs; 
     268        //! relative starts of green for signal groups 
     269        ivec green_starts; 
     270 
     271        //! avarage speed of cars 
     272        int VP; 
     273 
     274        void validate() { 
     275                rv_action = RV(name+"_offset", 1); // <======= example 
     276 
     277                for (int i=0; i<sgs.length();i++) { 
     278                        rv_inputs.add(RV(name+"_"+sgs(i),1)); 
     279                } 
     280                inputs.set_size(rv_inputs._dsize()); 
     281                 
     282                BaseTrafficAgent::validate(); 
     283 
     284                for (int i=0;i<lanehs.length();i++) { 
     285                        ivec index = rv_queues.dataind(RV(lanes(i).queue,1)); 
     286                        lanehs(i)->queue_index=index(0); 
     287                } 
     288 
     289 
     290        } 
     291 
     292        void adapt(const vec &glob_dt) { 
     293                BaseTrafficAgent::adapt(glob_dt); 
     294                         
     295                for (int i=0;i<lanehs.length();i++) { 
     296                        lanehs(i)->queue=queues(lanehs(i)->queue_index);                 
     297                } 
     298 
     299                planned_offset=last_offset; 
     300                 
     301                //set state variables to default values 
     302                final_state=false; 
     303                new_stable_state=false; 
     304                send_requests=false; 
     305                need_exps=true; 
     306                negot_offset=8; 
     307        } 
    149308 
    150309        void broadcast(Setting& set){ 
    151                          
     310 
    152311                //ask neighbours for exptected arrive times 
    153                 if (true) { 
     312                if (need_exps) { 
    154313                        for (int i=0; i<neighbours.length(); i++){ 
    155314                                Setting &msg =set.add(Setting::TypeGroup); 
     
    159318                                UI::save ( (string)"expected_times_request", msg, "what"); 
    160319                        } 
    161                 } 
    162  
     320                        need_exps=false; 
     321                } 
    163322 
    164323                // broadcast expected cars 
    165324                if (!seznam.empty()) { 
     325                        expected_cars(); 
    166326                        do { 
    167                                 vec value; 
    168                                  
    169                                 expected_cars(); 
    170327                                Setting &msg =set.add(Setting::TypeGroup); 
    171328                                UI::save ( seznam.back(), msg, "to"); 
     
    175332                                UI::save ( outputs, msg, "value"); 
    176333                                seznam.pop_back(); 
    177  
    178                         } while (!seznam.empty()); 
     334                        } while (!seznam.empty());                       
     335                } 
     336 
     337                // broadcast new stable state (new stable expectations) 
     338                if (new_stable_state) { 
     339                        expected_cars(); 
     340                        for (int i=0;i<neighbours.length();i++) { 
     341                                Setting &msg = set.add(Setting::TypeGroup); 
     342                                UI::save ( neighbours(i), msg, "to"); 
     343                                UI::save ( name, msg, "from"); 
     344                                UI::save ( (string)"new_stable_state2", msg, "what"); 
     345                                UI::save ( &(rv_outputs), msg, "rv"); 
     346                                UI::save ( outputs, msg, "value"); 
     347                        } 
     348                        new_stable_state=false; 
     349                } 
     350 
     351                // broadcast requests to change offset(s) 
     352                if (send_requests) { 
     353                        for (int i=0;i<neighbours.length();i++) { 
     354                                Setting &msg = set.add(Setting::TypeGroup); 
     355                                UI::save ( neighbours(i), msg, "to"); 
     356                                UI::save ( name, msg, "from"); 
     357                                UI::save ( (string)"offset_change_request", msg, "what"); 
     358                                UI::save ( &(rv_change_request), msg, "rv"); 
     359                                UI::save ( change_request, msg, "value"); 
     360                        } 
     361                        send_requests=false; 
     362                } 
     363 
     364                /*if (reset_negot_offset) { 
     365                        for (int i=0;i<neighbours.length();i++) { 
     366                                Setting &msg = set.add(Setting::TypeGroup); 
     367                                UI::save ( neighbours(i), msg, "to"); 
     368                                UI::save ( name, msg, "from"); 
     369                                UI::save ( (string)"reset_negot_offset", msg, "what"); 
     370                        } 
     371                }*/ 
     372 
     373 
     374                 
     375                // reached final offset. Log value? 
     376                if (final_state) { 
    179377                         
    180                 } 
    181  
     378                        //cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl; 
     379                        final_state=false; 
     380                } 
    182381        } 
    183382 
     
    185384                string what; 
    186385                string to; 
     386 
    187387                string from; 
    188388                vec value; 
     
    194394                UI::get(rv, msg, "rv"); 
    195395                UI::get(value, msg, "value"); 
     396                 
    196397                if (what=="expected_times_request"){  
    197                         recieved_exp_request=true; 
    198398                        seznam.push_back(from); 
    199  
    200399                }  
    201400                else if (what=="new_expected_cars") { 
    202401                        rv_recieved_exps=*rv; 
    203402                        recieved_exps=value; 
    204                         count_rating(); 
    205                         //TODO skutecne vyjednavani 
    206                 } 
     403                         
     404                        last_offset=planned_offset; 
     405 
     406                        planned_offset=find_best_offset(planned_offset,8); 
     407                        planned_offset=normalize_offset(planned_offset); 
     408                         
     409 
     410                        /*if (planned_offset!=last_offset) { 
     411                                reset_negot_offset=true; 
     412                        }*/ 
     413                        planned_rating=count_rating(planned_offset); 
     414                        // we have new stable state to broadcast 
     415                        new_stable_state=true; 
     416                } 
     417                else if (what=="new_stable_state2") { 
     418                        rv_recieved_exps=*rv; 
     419                        recieved_exps=value; 
     420                        planned_rating=count_rating(planned_offset); 
     421 
     422                        for (int i=0;i<neighbours.length();i++) { 
     423                                rv_change_request.add(RV(neighbours(i)+"_change",2)); 
     424                                change_request.set_size(rv_change_request._dsize());  
     425                                ivec ind=rv_change_request.dataind(RV(neighbours(i)+"_change",2)); 
     426                                // offset change 
     427                                change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change); 
     428                                // rating change 
     429                                change_request(ind(1))=rating_change; 
     430                        } 
     431 
     432                        if (negot_offset>2) {  
     433                                negot_offset/=2; 
     434                                        send_requests=true; 
     435                        } 
     436                        else { 
     437                                final_state=true; 
     438                        } 
     439                } 
     440                else if (what=="offset_change_request") { 
     441                        double final_rating_diff; 
     442 
     443                        rv_recieved_changes=*rv; 
     444                        recieved_changes=value; 
     445 
     446                        for (int i=0;i<rv_recieved_changes.length();i++) { 
     447 
     448                                ivec ind=rv_recieved_changes.dataind(RV(rv_recieved_changes.name(i),2)); 
     449 
     450                                final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)))-recieved_changes(ind(0)); 
     451                                if (final_rating_diff>0) { 
     452                                        planned_offset+=(int)recieved_changes(ind(0)); 
     453                                        planned_rating+=final_rating_diff; 
     454                                        accepted_from=from; 
     455                                } 
     456                        } 
     457                        //need_exps=true;s 
     458                        new_stable_state=true; 
     459                } 
     460                /*else if (what=="reset_negot_offset") { 
     461                        negot_offset=8; 
     462                }*/ 
    207463                else { 
    208464                        BaseTrafficAgent::receive(msg); 
     
    213469                BaseTrafficAgent::ds_register(ds); 
    214470                action2ds.set_connection( ds._urv(), rv_action); 
    215  
    216471        } 
    217472 
     
    219474                BaseTrafficAgent::from_setting(set); 
    220475                 
     476                srand(time(NULL)); 
     477 
    221478                car_leaving=2; 
    222479                VP=45; 
    223                 recieved_exp_request=false; 
     480 
     481                negot_offset=8; 
    224482                 
    225483                // load from file 
     
    232490                vec action; 
    233491                action.set_size(rv_action._dsize()); 
    234                  
     492 
    235493                ivec index = rv_action.dataind(RV(name+"_offset",1)); 
    236494                action(index(0))=planned_offset; 
     495                last_offset=planned_offset; 
    237496 
    238497                action2ds.filldown(action,glob_ut);