root/applications/doprava/traffic_agent.h @ 840

Revision 840, 5.8 kB (checked in by smidl, 14 years ago)

aimsunDS minor correction + improvements in Agents

Line 
1/*!
2  \file
3  \brief Traffic Light Agents
4  \author Vaclav Smidl.
5*/
6
7#ifndef TRAGE_H
8#define TRAGE_H
9
10
11#include <base/participants.h>
12       
13using namespace bdm;
14
15//! detector of traffic variables
16class Detector{
17        public:
18                string name;             //! detector name
19                int distance;                //! distance from stop-line
20
21                //! function loading info from Setting
22                void from_setting(const Setting &set){
23                        UI::get(name,set,"name",UI::compulsory);
24                        UI::get(distance,set,"distance",UI::compulsory);
25                       
26                }
27};
28
29//! detector fo incomming flow
30class 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
40class RequestOut {
41        public:
42        string to; // agent
43        Array<string> from; //detectors
44        void from_setting(const Setting &set){
45                UI::get(from,set,"from",UI::compulsory);
46                UI::get(to, set, "to", UI::compulsory);
47        }
48};
49
50
51//! class with physical information about a signal group
52class 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
61//! class that operates on a signal group
62class SignalGroupHandler {
63        protected:
64                //! pointer to physical signal group
65                SignalGroup *sg;
66               
67        public:
68                //! actual data from the relevant signal group
69                vec det_data;
70                //! description of det_data
71                RV rv_det_data;
72                //! link from global measured data
73                datalink ds2data;
74                //! link from data to global out_data
75                datalink data2out;
76        public:
77                void connect_sg(SignalGroup &sg0, const string &agent_name){
78                        sg=&sg0;
79                        for (int i=0;i<sg->detectors.length();i++){
80                                rv_det_data.add(RV(agent_name +"_"+ sg->detectors(i).name, 2)); //TODO intensity occupancy
81                        }
82                       
83                }
84               
85                //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?)
86                double expected_load(){
87                        if (det_data.length()>0){
88                                return det_data(0); // whatever
89                        } else {
90                                return 1.0; // mean value
91                        }
92                }
93};
94
95class RequestHandler{
96        protected:
97                RequestOut *rq;
98        public:
99                RV rv;
100                datalink outdata2out;
101        public:
102                void connect_request(RequestOut &rq0, const string &agent_name){
103                        rq=&rq0;
104                        for (int i=0;i<rq->from.length();i++){
105                                rv.add(RV(agent_name +"_"+ rq->from(i), 2)); //TODO intensity occupancy
106                        }
107                }
108};
109
110/*!
111\brief  Basic Traffic Agent
112
113*/
114class BaseTrafficAgent : public Participant {
115        protected:
116                //! Signal Groups
117                Array<SignalGroup> sg;
118
119                Array<SignalGroupHandler> sgh;
120                                                                               
121                //!data from messages
122                vec input_data;
123               
124                //! decription of msg_data
125                RV input_rv;
126               
127                //! data to broadcast
128                vec output_data;
129               
130                //! description of broadcast dataind
131                RV output_rv;
132                                       
133                //! output recepient
134                Array<RequestOut> requests;
135                Array<RequestHandler> request_handler;
136               
137                //! action description
138                RV action_rv;
139               
140                datalink_part action2ds;
141               
142        public:
143                void validate(){
144                        // write internal checks if all was loaded OK
145                       
146                        // set action variable == this is a feature of the agent!
147                        action_rv = RV(name+"_Tc", 1); // <======= example
148                }
149                void receive(const Setting &msg){
150                        string what;
151                        UI::get(what, msg, "what", UI::compulsory);
152                       
153                        if (what=="data"){ //
154                                // field data
155                                // extract decription of teh received datavector
156                                shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory);
157                                // find if it is needed
158                                ivec ind=rv->dataind(input_rv); // position of rv in in_rv;
159                                if (ind.length()>0){ //data are interesting
160                                        vec dt;
161                                        UI::get(dt, msg, "value",UI::compulsory); // get data
162                                        set_subvector(input_data, ind,  dt); //check size?
163                                }                               
164                        } else {
165                                bdm_warning("Unknown message of type "+what);
166                        }
167                }
168                void log_register(logger &L, const string &prefix){
169                        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_");
177                }
178                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++;
185                }
186               
187                void broadcast(Setting& set){
188                        // broadcast data to all neighbours
189                        for (int i=0; i<request_handler.length(); i++){
190                                Setting &msg =set.add(Setting::TypeGroup);
191                                RequestHandler &R=request_handler(i);
192                               
193                                // copy from create message
194                                // create msg with fields {to=..., what=data, rv=..., value = ...}
195                                Setting &m_to=msg.add("to",Setting::TypeString);
196                                m_to=requests(i).to;
197                                Setting &m_what=msg.add("what",Setting::TypeString);
198                                m_what="data";
199                                //Setting &m_data=msg.add("data",Setting::TypeGroup);
200                                Setting &m_rv=msg.add("rv",Setting::TypeGroup);
201                                R.rv.to_setting(m_rv);
202                                Setting &m_val=msg.add("value",Setting::TypeGroup);
203                                vec val =R.outdata2out.pushdown(output_data);
204                                UI::save(val, m_val);
205                        }
206                }
207                void adapt(const vec &glob_dt){
208                        // copy data from global vector to sSGHandlers
209                        for (int i=0; i<sgh.length();i++){
210                                sgh(i).ds2data.filldown(glob_dt, sgh(i).det_data);
211                        }
212                        // copy sg_length ... and others...
213                }
214                void act(vec &glob_ut){
215                        vec needs(sgh.length());
216                        for (int i=0; i<sgh.length();i++){
217                                needs(i) = sgh(i).expected_load();
218                        }
219                        vec action; // trivial stuff
220                        action2ds.filldown(action,glob_ut);
221                }
222               
223                void ds_register(const DS &ds){
224                        for (int i=0; i<sgh.length(); i++){
225                                sgh(i).ds2data.set_connection(sgh(i).rv_det_data, ds._drv());
226                        }
227                        action2ds.set_connection( ds._urv(), action_rv);
228                }
229               
230                void from_setting(const Setting &set);
231};
232UIREGISTER(BaseTrafficAgent);
233
234#endif //TRAGE_H
Note: See TracBrowser for help on using the browser.