Changeset 927

Show
Ignore:
Timestamp:
05/06/10 17:42:05 (14 years ago)
Author:
smidl
Message:

traffic agents -- pro BDM > r904

Files:
12 modified

Legend:

Unmodified
Added
Removed
  • applications/doprava/Zlicingw.cfg

    r921 r927  
    22        { // agent 1 
    33        class = "GreenWaveTrafficAgent"; 
    4           name = "495"; 
    5           sg = ( 
    6             { name = "VA"; 
    7               detectors = ( 
    8                         { name = "DVA1";  distance = 10;},  
    9               {name = "DVA";  distance = 20; } 
     4                name = "495"; 
     5                lanes = ( 
     6                        { sg="VA"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); alpha= [1.0 ]; queue="495_Q1";},  
     7                        { sg="VB"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); alpha= [1.0 ]; queue="495_Q2";} 
    108              ); 
    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  
     9                neighbours = ("601"); 
    3610  offset = 40; //s 
    3711  moje_special=14; 
     
    3913{ //agent 2 
    4014        class = "GreenWaveTrafficAgent"; 
    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                neighbours = ("495"); 
    5717} 
    5818); 
  • applications/doprava/aimsun_bdm/aimsun_ds.cpp

    r902 r927  
    5656        "601_DVA 601_DVAa 601_DVB  601_DVBa 601_DVB1 601_DVC  601_DVD  601_DVD1 601_DSE 601_DVE 601_DSE1 601_DVE1 " 
    5757    "601_S6  601_S6a  601_S7   601_S8   601_S9   601_S9a " 
     58    "495_Q1 495_Q2 " 
    5859    "}", 
    5960        "    1,      1,      1,      1,      1,       1,      " 
     
    6364        "    2,      2,      2,      2,      2,       2,       2,       2,       2,       2,       2,       2," 
    6465        "    2,      2,      2,      2,      2,       2" 
     66        "    1,      1" 
    6567        ); 
    6668  /* Remember the size of the data vector. */ 
  • applications/doprava/aimsun_bdm/aimsun_fake.cpp

    r851 r927  
    1616        "601_DVA 601_DVAa 601_DVB  601_DVBa 601_DVB1 601_DVC  601_DVD  601_DVD1 601_DSE 601_DVE 601_DSE1 601_DVE1 " 
    1717    "601_S6  601_S6a  601_S7   601_S8   601_S9   601_S9a " 
     18    "495_Q1 495_Q2 " 
    1819    "}", 
    1920        "    1,      1,      1,      1,      1,       1,      " 
     
    2324        "    2,      2,      2,      2,      2,       2,       2,       2,       2,       2,       2,       2," 
    2425        "    2,      2,      2,      2,      2,       2" 
     26        "    1,      1" 
    2527        ); 
    2628  /* Remember the size of the data vector. */ 
  • applications/doprava/main_loop.cpp

    r921 r927  
    6969                        Ags(i) -> adapt(glob_dt); 
    7070                } 
     71                 
     72                // NEGOTIATION CYCLE 
    7173                for ( int i=0; i<Ags.length(); i++ ) { 
    7274                        Ags(i) -> broadcast(Queue); 
  • applications/doprava/traffic_agent.cpp

    r842 r927  
    11#include "traffic_agent.h" 
    22 
    3 void SignalGroup::from_setting(const libconfig::Setting& set) 
     3void LaneHandler::connect_data(BaseTrafficAgent& agent0) 
    44{ 
    5 UI::get(name,set, "name", UI::compulsory); 
    6 UI::get(detectors, set, "detectors", UI::optional); 
    7 // 
    8 // TODO make some analysis of the detectors 
     5        agent = &agent0; 
     6        agentin2input.set_connection(rv_inputs, agent->rv_inputs); 
     7        ivec queue_index_tmp = agent->rv_queues.dataind(rv_queue); 
     8        if (queue_index_tmp.length()==1){ 
     9                queue_index=queue_index_tmp(0); 
     10        } else { 
     11                bdm_error("queue " + lane.queue + " not found"); 
     12        }                
     13} 
    914 
     15double LaneHandler::expected_output(double green_time){ 
     16        agentin2input.filldown(agent->inputs, inputs); 
     17        queue = agent->queues(queue_index); 
     18         
     19        double exp_output=0.0; 
     20        ///////// 
     21         
     22         
     23        //////// 
     24        return exp_output; 
    1025} 
     26 
    1127 
    1228void BaseTrafficAgent::from_setting(const Setting& set) 
     
    1531         
    1632        // load from file 
    17         UI::get(sg,set,"sg", UI::compulsory); 
    18         UI::get(requests,set,"output",UI::compulsory); 
    19         Array<DetectorIn> det_in; 
    20         UI::get(det_in, set, "input",UI::compulsory); 
    21          
    22         // process sg == create handles 
    23         sgh.set_length(sg.length()); 
    24         for(int i=0;i<sg.length();i++){ 
    25                 sgh(i).connect_sg(sg(i),name); 
     33        if (set.exists("lanes")){ 
     34                Setting &Slanes=set["lanes"]; 
     35                lanes.set_length(Slanes.getLength()); 
     36                for(int l=0; l<lanes.length(); l++){ 
     37                        lanes(l).from_setting(Slanes[l]); 
     38                } 
    2639        } 
    27          
    28         // process output == handles 
    29         request_handler.set_length(requests.length()); 
    30         for(int i=0;i<requests.length();i++){ 
    31                 request_handler(i).connect_request(requests(i),name); 
    32                 output_rv.add(request_handler(i).rv); 
    33         } 
    34  
    35         // process DetIn - create input 
    36         for (int i=0; i<det_in.length(); i++){ 
    37                 input_rv.add(RV(det_in(i).name,2)); 
    38         } 
    39         input_data.set_size(input_rv._dsize()); 
     40        //UI::get(lanes,set,"lanes", UI::compulsory); 
     41        UI::get(neighbours, set, "neighbours", UI::optional); 
    4042} 
  • applications/doprava/traffic_agent.h

    r842 r927  
    1313using namespace bdm; 
    1414 
     15class BaseTrafficAgent; 
     16 
    1517//! detector of traffic variables 
    16 class Detector{ 
    17         public: 
    18                 string name;             //! detector name 
    19                 int distance;                //! distance from stop-line 
     18class Lane{ 
     19        public: 
     20                Array<string> inputs; 
     21                Array<string> outputs; 
     22                vec alpha; //size of outputs 
     23                string queue; 
     24                string sg; 
    2025 
    2126                //! function loading info from Setting 
    2227                void from_setting(const Setting &set){ 
    23                         UI::get(name,set,"name",UI::compulsory); 
    24                         UI::get(distance,set,"distance",UI::compulsory); 
     28                        UI::get(inputs,set,"inputs",UI::compulsory); 
     29                        UI::get(outputs,set,"outputs",UI::compulsory); 
     30                        UI::get(alpha,set,"alpha",UI::compulsory); 
     31                        UI::get(queue,set,"queue",UI::compulsory); 
     32                        UI::get(sg,set,"sg",UI::compulsory); 
    2533                         
    2634                } 
    2735}; 
    2836 
    29 //! detector fo incomming flow 
    30 class DetectorIn : public Detector{ 
    31         public: 
    32         Array<string> to_sg; // to signal plans 
    33         void from_setting(const Setting &set){ 
    34                 Detector::from_setting(set); 
    35                 UI::get(to_sg, set, "to_sg", UI::compulsory); 
    36         } 
    37 }; 
    38  
    39 //! structure for output detectors 
    40 class RequestOut { 
    41         public: 
    42         string to; // agent 
    43         Array<string> detectors; //detectors 
    44         void from_setting(const Setting &set){ 
    45                 UI::get(detectors,set,"detectors",UI::compulsory); 
    46                 UI::get(to, set, "to", UI::compulsory); 
    47         } 
    48 }; 
    49  
    50  
    51 //! class with physical information about a signal group 
    52 class SignalGroup { 
    53         public: 
    54                 string name;                 //! names of the group 
    55                 Array<Detector> detectors;   //! (possible) detectors 
    56                  
    57                 //! function that loads information from Setting 
    58                 void from_setting(const Setting &set);           
    59 }; 
    60  
    6137//! class that operates on a signal group 
    62 class SignalGroupHandler { 
     38class LaneHandler { 
    6339        protected: 
    64                 //! pointer to physical signal group 
    65                 SignalGroup *sg; 
     40                //! pointer to physical lane 
     41                const Lane &lane; 
     42                //! agent pointer 
     43                BaseTrafficAgent *agent; 
    6644                 
    6745        public: 
    6846                //! actual data from the relevant signal group 
    69                 vec det_data; 
     47                vec inputs; 
     48                //! 
     49                double queue; 
     50                //!  
    7051                //! description of det_data 
    71                 RV rv_det_data; 
     52                RV rv_inputs; 
     53                //! description of det_data 
     54                RV rv_outputs; 
     55                //! description of det_data 
     56                RV rv_queue; 
    7257                //! link from global measured data  
    73                 datalink ds2data; 
    74         public: 
    75                 void connect_sg(SignalGroup &sg0, const string &agent_name){ 
    76                         sg=&sg0; 
    77                         for (int i=0;i<sg->detectors.length();i++){ 
    78                                 rv_det_data.add(RV(agent_name +"_"+ sg->detectors(i).name, 2)); //TODO intensity occupancy 
    79                         } 
    80                          
    81                 } 
     58                datalink agentin2input; 
     59                //! 
     60                datalink output2agentout; 
     61                //! 
     62                int queue_index; 
     63        public: 
     64                LaneHandler(const Lane &lane0): lane(lane0){ 
     65                        for (int i=0;i<lane0.inputs.length();i++){ 
     66                                rv_inputs.add(RV(lane.inputs(i), 2)); 
     67                        } 
     68                        for (int i=0;i<lane0.outputs.length();i++){ 
     69                                rv_outputs.add(RV(lane.outputs(i), 2));  
     70                        } 
     71                        rv_queue.add(RV(lane.queue, 1));  
     72                } 
     73                 
     74                void connect_data(BaseTrafficAgent &agent0); 
    8275                 
    8376                //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?) 
    84                 double expected_load(){ 
    85                         if (det_data.length()>0){ 
    86                                 return det_data(0); // whatever 
    87                         } else { 
    88                                 return 1.0; // mean value 
    89                         } 
    90                 } 
    91 }; 
    92  
    93 class RequestHandler{ 
    94         protected: 
    95                 RequestOut *rq; 
    96         public: 
    97                 RV rv; 
    98         public: 
    99                 void connect_request(RequestOut &rq0, const string &agent_name){ 
    100                         rq=&rq0; 
    101                         for (int i=0;i<rq->detectors.length();i++){ 
    102                                 rv.add(RV(rq->detectors(i), 2)); //TODO intensity occupancy 
    103                         } 
    104                 } 
     77                double expected_output(double green_time); 
    10578}; 
    10679 
     
    11083*/ 
    11184class BaseTrafficAgent : public Participant { 
    112         protected: 
     85        LOG_LEVEL(BaseTrafficAgent,logdata); 
     86        public: 
    11387                //! Signal Groups 
    114                 Array<SignalGroup> sg; 
    115  
    116                 Array<SignalGroupHandler> sgh; 
     88                Array<Lane> lanes; 
     89 
     90                Array<LaneHandler*> lanehs; 
    11791                                                                                 
    11892                //!data from messages 
    119                 vec input_data; 
     93                vec inputs; 
    12094                 
    12195                //! decription of msg_data 
    122                 RV input_rv; 
     96                RV rv_inputs; 
    12397                 
    12498                //! data to broadcast 
    125                 vec output_data; 
     99                vec outputs; 
    126100                 
    127101                //! description of broadcast dataind 
    128                 RV output_rv; 
     102                RV rv_outputs; 
     103                 
     104                vec queues; 
     105                 
     106                //! description of queues 
     107                RV rv_queues; 
    129108                 
    130109                //! datalink from DS to output variables 
    131                 datalink ds2output; 
    132                                          
    133                 //! output recepient 
    134                 Array<RequestOut> requests; 
    135                 Array<RequestHandler> request_handler; 
     110                datalink ds2inputs; 
     111 
     112                //! datalink from DS to output variables 
     113                datalink ds2queues; 
    136114                 
    137115                //! action description 
    138116                RV action_rv; 
    139                  
     117                                 
    140118                datalink_part action2ds; 
    141119                 
     120                Array<string> neighbours; 
     121                 
     122                Array<RV> rv_neighbours_out; 
     123                 
     124                Array<datalink> output2neighbour; 
     125                 
    142126        public: 
    143127                void validate(){ 
     128                        lanehs.set_length(lanes.length()); 
     129                        for (int l=0; l<lanes.length(); l++){ 
     130                                lanehs(l) = new LaneHandler(lanes(l)); 
     131                                 
     132                                rv_inputs.add(lanehs(l)->rv_inputs); 
     133                                rv_outputs.add(lanehs(l)->rv_outputs); 
     134                                rv_queues.add(lanehs(l)->rv_queue); 
     135                        } 
     136                        inputs.set_size(rv_inputs._dsize()); 
     137                        outputs.set_size(rv_outputs._dsize()); 
     138                        queues.set_size(rv_queues._dsize()); 
     139                         
     140                        for (int l=0; l<lanes.length(); l++){ 
     141                                lanehs(l)->connect_data(*this); 
     142                        } 
     143                         
     144                        //for -- rv_outputs --  
     145                        // TODO vybrat rv pro sousedy  
     146                        rv_neighbours_out.set_length(neighbours.length()); 
     147                        output2neighbour.set_length(neighbours.length()); 
     148                         
     149                        for (int i=0; i<neighbours.length(); i++){ 
     150                                for (int r=0; r<rv_outputs.length(); r++){ 
     151                                        int str_pos = rv_outputs.name(r).compare(neighbours(i)); 
     152                                        if (str_pos>neighbours(i).length()){ 
     153                                                rv_neighbours_out(i).add(rv_outputs.subselect(vec_1(r))); 
     154                                        } 
     155                                } 
     156                                // connect datasource 
     157                                output2neighbour(i).set_connection(rv_neighbours_out(i), rv_outputs); 
     158                        } 
     159                         
     160                        // lanehs knows RVS 
    144161                        // write internal checks if all was loaded OK 
    145162                         
    146                         // set action variable == this is a feature of the agent! 
    147                         action_rv = RV(name+"_Tc", 1); // <======= example 
    148163                } 
    149164                void receive(const Setting &msg){ 
     
    151166                        UI::get(what, msg, "what", UI::compulsory); 
    152167                         
    153                         if (what=="data"){ //  
     168                        if (what=="new_stable_state"){ //  
    154169                                // field data  
    155170                                // extract decription of teh received datavector 
    156171                                shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); 
    157172                                // find if it is needed 
    158                                 ivec ind=rv->dataind(input_rv); // position of rv in in_rv; 
     173                                ivec ind=rv->dataind(rv_inputs); // position of rv in in_rv; 
    159174                                if (ind.length()>0){ //data are interesting 
    160175                                        vec dt; 
    161176                                        UI::get(dt, msg, "value",UI::compulsory); // get data 
    162                                         set_subvector(input_data, ind,  dt); //check size? 
     177                                        set_subvector(inputs, ind,  dt); //check size? 
    163178                                }                                
    164179                        } else { 
    165                                 bdm_warning("Unknown message of type "+what); 
     180                                Participant::receive(msg); 
    166181                        } 
    167182                } 
    168183                void log_register(logger &L, const string &prefix){ 
    169184                        root::log_register ( L, prefix ); 
    170                         logrec->ids.set_size(sg.length()+2); 
    171                         int i; 
    172                         for (i=0;i <sg.length(); i++) { 
    173                                 logrec->ids(i)=logrec->L.add_vector(sgh(i).rv_det_data, ""); 
    174                         } 
    175                         logrec->ids(i)=logrec->L.add_vector(input_rv,"in_"); i++; 
    176                         logrec->ids(i)=logrec->L.add_vector(output_rv,"out_"); 
     185                        if ( log_level[logdata]){ 
     186                                L.add_vector ( log_level, logdata, RV ( 1 ), prefix );   
     187                        } 
    177188                } 
    178189                void log_write() const { 
    179                         int i; 
    180                         for (i=0;i <sg.length(); i++) { 
    181                                 logrec->L.log_vector(logrec->ids(i), sgh(i).det_data); 
    182                         } 
    183                         logrec->L.log_vector(logrec->ids(i),input_data); i++; 
    184                         logrec->L.log_vector(logrec->ids(i),output_data); i++; 
     190                        if (log_level[logdata]){ 
     191                                log_level.store(logdata, inputs); 
     192                        } 
    185193                } 
    186194                 
    187195                void broadcast(Setting& set){ 
    188196                        // broadcast data to all neighbours 
    189                         for (int i=0; i<request_handler.length(); i++){ 
     197                        for (int i=0; i<neighbours.length(); i++){ 
    190198                                Setting &msg =set.add(Setting::TypeGroup); 
    191                                 RequestHandler &R=request_handler(i); 
    192199                                 
     200                                // if... 
    193201                                // copy from create message 
    194202                                // create msg with fields {to=..., what=data, rv=..., value = ...} 
    195                                 UI::save ( requests(i).to, msg, "to"); 
    196                                 UI::save ( (string)"data", msg, "what"); 
    197                                 UI::save ( &R.rv, msg, "rv"); 
    198                                 UI::save(output_data, msg, "value"); 
    199                         } 
     203                                UI::save ( neighbours(i), msg, "to"); 
     204                                UI::save ( (string)"new_stable_state", msg, "what"); 
     205                                UI::save ( &(rv_neighbours_out(i)), msg, "rv"); 
     206                                UI::save( output2neighbour(i).pushdown(outputs), msg, "value"); 
     207                        } 
     208                         
    200209                } 
    201210                void adapt(const vec &glob_dt){ 
    202211                        // copy data from global vector to sSGHandlers 
    203                         for (int i=0; i<sgh.length();i++){ 
    204                                 sgh(i).ds2data.filldown(glob_dt, sgh(i).det_data); 
    205                         } 
     212                        ds2inputs.filldown(glob_dt, inputs); 
    206213                        //copy data fro neighbours 
    207                         ds2output.filldown(glob_dt, output_data); 
     214                        ds2queues.filldown(glob_dt, queues); 
    208215                        // copy sg_length ... and others... 
    209216                } 
    210217                void act(vec &glob_ut){ 
    211                         vec needs(sgh.length()); 
    212                         for (int i=0; i<sgh.length();i++){ 
    213                                 needs(i) = sgh(i).expected_load(); 
    214                         } 
    215218                        vec action; // trivial stuff 
    216219                        action2ds.filldown(action,glob_ut); 
     
    219222                void ds_register(const DS &ds){ 
    220223                        //register ds2output 
    221                         ds2output.set_connection(output_rv, ds._drv()); 
    222                         output_data.set_size(output_rv._dsize()); 
    223                         for (int i=0; i<sgh.length(); i++){ 
    224                                 sgh(i).ds2data.set_connection(sgh(i).rv_det_data, ds._drv()); 
    225                         } 
     224                        ds2inputs.set_connection(rv_inputs, ds._drv()); 
     225                        ds2queues.set_connection(rv_queues, ds._drv()); 
     226                        inputs.set_size(rv_inputs._dsize()); 
    226227                        action2ds.set_connection( ds._urv(), action_rv); 
    227228                } 
  • applications/doprava/traffic_agent_offset.h

    r921 r927  
    11class GreenWaveTrafficAgent : public BaseTrafficAgent { 
    2 /*public: 
    3         void GreeWaveTrafficAgent();*/ 
     2protected: 
     3        double stable_state_loss; 
     4         
     5        double best_offset; 
     6public: 
     7        void validate() { 
     8                BaseTrafficAgent::validate(); 
     9                action_rv = RV(name+"_offset", 1); // <======= example 
     10        } 
     11        void GreeWaveTrafficAgent(); 
    412}; 
    513UIREGISTER(GreenWaveTrafficAgent); 
  • library/bdm/base/bdmbase.cpp

    r915 r927  
    532532        } 
    533533        if( log_level[logut] ) {  
    534                 bdm_warning("We are sorry, but DS::log_write() is not able to log \"ut\" at the moment, would you be so kind and code this functionality?"); 
     534                // NOT_IMPLEMENTED 
    535535        } 
    536536} 
  • library/bdm/base/bdmbase.h

    r923 r927  
    307307        //! this method adds new id to its proper position and return the name of this position 
    308308        string store_id_and_give_name( enum T::log_level_enums const log_level_enum,  int enum_subindex, int id ) { 
    309                 int ids_len = ids(log_level_enum).length(); 
     309//              int ids_len = ids(log_level_enum).length(); <== compiler suggest to remove 
    310310                if( ids(log_level_enum).length() <= enum_subindex ) 
    311311                        ids(log_level_enum).set_size( enum_subindex+1, true ); 
  • library/bdm/base/datasources.cpp

    r924 r927  
    7373 
    7474        string orientation; 
    75         if( UI::get ( orientation, set, "orientation", UI::optional ) & orientation == "BY_ROW" )  
     75        if( UI::get ( orientation, set, "orientation", UI::optional ) & (orientation == "BY_ROW") )  
    7676                transpose ( Data, Data ); 
    7777} 
  • library/bdm/base/participants.h

    r819 r927  
    5757        virtual void broadcast(Setting& queue){} 
    5858        //! Receive one message from the queue 
    59         virtual void receive(const Setting& msg){} 
     59        virtual void receive(const Setting& msg){ 
     60                bdm_warning("unhandled message"); 
     61        } 
    6062        //! Design control strategy 
    6163        virtual void act(vec &glob_ut){} 
  • library/bdm/base/user_info.h

    r907 r927  
    380380                        from_setting ( array_to_load ( i ), link.result[i] ); 
    381381        } 
    382  
     382/* 
    383383        //! This is dummy version of the from_setting method for other, unsupported types. It just throws an exception. 
    384384        //! 
    385385        //! At the moment, this is the only way how to compile the library without obtaining the compiler error c2665. 
    386386        //! The exception can help to find the place where the template is misused and also to correct it. 
    387 //      template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 
    388 //              std::string msg = "UIException: from_setting is not implemented for type "; 
    389 //              try{ 
    390 //                      variable_to_load.from_setting(element); 
    391 /*              } catch (...){ 
    392                 msg += typeid ( T ).name(); 
    393                 msg += '.'; 
    394                 throw UISettingException ( msg, element ); 
    395                 }*/ 
    396 //      } 
    397  
     387        template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 
     388                std::string msg = "UIException: from_setting is not implemented for type "; 
     389                try{ 
     390                        variable_to_load.from_setting(element); 
     391                } catch (...){ 
     392                        msg += typeid ( T ).name(); 
     393                        msg += '.'; 
     394                        throw UISettingException ( msg, element ); 
     395                } 
     396        } 
     397*/ 
    398398 
    399399protected: