[819] | 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 | |
---|
| 13 | using namespace bdm; |
---|
| 14 | |
---|
| 15 | //! detector of traffic variables |
---|
| 16 | class Detector{ |
---|
| 17 | public: |
---|
| 18 | string name; //! detector name |
---|
| 19 | int distance; //! distance from stop-line |
---|
[836] | 20 | |
---|
[819] | 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); |
---|
[820] | 25 | |
---|
[819] | 26 | } |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | //! detector fo incomming flow |
---|
| 30 | class DetectorIn : public Detector{ |
---|
[840] | 31 | public: |
---|
[836] | 32 | Array<string> to_sg; // to signal plans |
---|
[819] | 33 | void from_setting(const Setting &set){ |
---|
| 34 | Detector::from_setting(set); |
---|
[836] | 35 | UI::get(to_sg, set, "to_sg", UI::compulsory); |
---|
[819] | 36 | } |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | //! structure for output detectors |
---|
| 40 | class RequestOut { |
---|
| 41 | public: |
---|
| 42 | string to; // agent |
---|
[842] | 43 | Array<string> detectors; //detectors |
---|
[819] | 44 | void from_setting(const Setting &set){ |
---|
[842] | 45 | UI::get(detectors,set,"detectors",UI::compulsory); |
---|
[819] | 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 | |
---|
| 61 | //! class that operates on a signal group |
---|
| 62 | class 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 | public: |
---|
[840] | 75 | void connect_sg(SignalGroup &sg0, const string &agent_name){ |
---|
| 76 | sg=&sg0; |
---|
[819] | 77 | for (int i=0;i<sg->detectors.length();i++){ |
---|
[840] | 78 | rv_det_data.add(RV(agent_name +"_"+ sg->detectors(i).name, 2)); //TODO intensity occupancy |
---|
[819] | 79 | } |
---|
| 80 | |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | //! 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: |
---|
[840] | 99 | void connect_request(RequestOut &rq0, const string &agent_name){ |
---|
| 100 | rq=&rq0; |
---|
[842] | 101 | for (int i=0;i<rq->detectors.length();i++){ |
---|
| 102 | rv.add(RV(rq->detectors(i), 2)); //TODO intensity occupancy |
---|
[819] | 103 | } |
---|
| 104 | } |
---|
| 105 | }; |
---|
| 106 | |
---|
| 107 | /*! |
---|
| 108 | \brief Basic Traffic Agent |
---|
| 109 | |
---|
| 110 | */ |
---|
| 111 | class BaseTrafficAgent : public Participant { |
---|
| 112 | protected: |
---|
| 113 | //! Signal Groups |
---|
| 114 | Array<SignalGroup> sg; |
---|
| 115 | |
---|
| 116 | Array<SignalGroupHandler> sgh; |
---|
| 117 | |
---|
| 118 | //!data from messages |
---|
| 119 | vec input_data; |
---|
| 120 | |
---|
| 121 | //! decription of msg_data |
---|
| 122 | RV input_rv; |
---|
| 123 | |
---|
| 124 | //! data to broadcast |
---|
| 125 | vec output_data; |
---|
| 126 | |
---|
| 127 | //! description of broadcast dataind |
---|
| 128 | RV output_rv; |
---|
[842] | 129 | |
---|
| 130 | //! datalink from DS to output variables |
---|
| 131 | datalink ds2output; |
---|
[840] | 132 | |
---|
| 133 | //! output recepient |
---|
| 134 | Array<RequestOut> requests; |
---|
| 135 | Array<RequestHandler> request_handler; |
---|
| 136 | |
---|
| 137 | //! action description |
---|
[819] | 138 | RV action_rv; |
---|
| 139 | |
---|
| 140 | datalink_part action2ds; |
---|
| 141 | |
---|
| 142 | public: |
---|
| 143 | void validate(){ |
---|
| 144 | // write internal checks if all was loaded OK |
---|
[840] | 145 | |
---|
| 146 | // set action variable == this is a feature of the agent! |
---|
| 147 | action_rv = RV(name+"_Tc", 1); // <======= example |
---|
[819] | 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 |
---|
[840] | 155 | // extract decription of teh received datavector |
---|
[819] | 156 | shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); |
---|
[840] | 157 | // find if it is needed |
---|
[819] | 158 | ivec ind=rv->dataind(input_rv); // position of rv in in_rv; |
---|
[840] | 159 | if (ind.length()>0){ //data are interesting |
---|
[819] | 160 | vec dt; |
---|
[840] | 161 | UI::get(dt, msg, "value",UI::compulsory); // get data |
---|
[819] | 162 | set_subvector(input_data, ind, dt); //check size? |
---|
| 163 | } |
---|
| 164 | } else { |
---|
| 165 | bdm_warning("Unknown message of type "+what); |
---|
| 166 | } |
---|
| 167 | } |
---|
[840] | 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 | |
---|
[819] | 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 |
---|
[840] | 194 | // create msg with fields {to=..., what=data, rv=..., value = ...} |
---|
[842] | 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"); |
---|
[819] | 199 | } |
---|
| 200 | } |
---|
[840] | 201 | void adapt(const vec &glob_dt){ |
---|
[819] | 202 | // 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 | } |
---|
[842] | 206 | //copy data fro neighbours |
---|
| 207 | ds2output.filldown(glob_dt, output_data); |
---|
[820] | 208 | // copy sg_length ... and others... |
---|
[819] | 209 | } |
---|
| 210 | 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 | } |
---|
[840] | 215 | vec action; // trivial stuff |
---|
[819] | 216 | action2ds.filldown(action,glob_ut); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void ds_register(const DS &ds){ |
---|
[842] | 220 | //register ds2output |
---|
| 221 | ds2output.set_connection(output_rv, ds._drv()); |
---|
| 222 | output_data.set_size(output_rv._dsize()); |
---|
[819] | 223 | for (int i=0; i<sgh.length(); i++){ |
---|
[840] | 224 | sgh(i).ds2data.set_connection(sgh(i).rv_det_data, ds._drv()); |
---|
[819] | 225 | } |
---|
[840] | 226 | action2ds.set_connection( ds._urv(), action_rv); |
---|
[819] | 227 | } |
---|
| 228 | |
---|
[820] | 229 | void from_setting(const Setting &set); |
---|
[819] | 230 | }; |
---|
| 231 | UIREGISTER(BaseTrafficAgent); |
---|
| 232 | |
---|
| 233 | #endif //TRAGE_H |
---|