[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 |
---|
[820] | 20 | RV rv; |
---|
[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{ |
---|
| 31 | string from; // from agent |
---|
| 32 | Array<string> to; // to signal plans |
---|
| 33 | void from_setting(const Setting &set){ |
---|
| 34 | Detector::from_setting(set); |
---|
| 35 | UI::get(from,set,"from",UI::compulsory); |
---|
| 36 | UI::get(to, set, "to", UI::compulsory); |
---|
| 37 | } |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | //! structure for output detectors |
---|
| 41 | class RequestOut { |
---|
| 42 | public: |
---|
| 43 | string to; // agent |
---|
| 44 | Array<string> from; //detectors |
---|
| 45 | void from_setting(const Setting &set){ |
---|
| 46 | UI::get(from,set,"from",UI::compulsory); |
---|
| 47 | UI::get(to, set, "to", UI::compulsory); |
---|
| 48 | } |
---|
| 49 | }; |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | //! class with physical information about a signal group |
---|
| 53 | class SignalGroup { |
---|
| 54 | public: |
---|
| 55 | string name; //! names of the group |
---|
| 56 | Array<Detector> detectors; //! (possible) detectors |
---|
| 57 | |
---|
| 58 | //! function that loads information from Setting |
---|
| 59 | void from_setting(const Setting &set); |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | //! class that operates on a signal group |
---|
| 63 | class SignalGroupHandler { |
---|
| 64 | protected: |
---|
| 65 | //! pointer to physical signal group |
---|
| 66 | SignalGroup *sg; |
---|
| 67 | |
---|
| 68 | public: |
---|
| 69 | //! actual data from the relevant signal group |
---|
| 70 | vec det_data; |
---|
| 71 | //! description of det_data |
---|
| 72 | RV rv_det_data; |
---|
| 73 | //! link from global measured data |
---|
| 74 | datalink ds2data; |
---|
| 75 | //! link from data to global out_data |
---|
| 76 | datalink data2out; |
---|
| 77 | public: |
---|
| 78 | SignalGroupHandler(SignalGroup *sg0, const string &agent_name):rv_det_data(){ |
---|
| 79 | sg=sg0; |
---|
| 80 | for (int i=0;i<sg->detectors.length();i++){ |
---|
| 81 | rv_det_data.add(RV(agent_name + sg->detectors(i).name, 1)); //TODO intensity occupancy |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?) |
---|
| 87 | double expected_load(){ |
---|
| 88 | if (det_data.length()>0){ |
---|
| 89 | return det_data(0); // whatever |
---|
| 90 | } else { |
---|
| 91 | return 1.0; // mean value |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | class RequestHandler{ |
---|
| 97 | protected: |
---|
| 98 | RequestOut *rq; |
---|
| 99 | public: |
---|
| 100 | RV rv; |
---|
| 101 | datalink outdata2out; |
---|
| 102 | public: |
---|
| 103 | RequestHandler(RequestOut *rq0, const string &agent_name){ |
---|
| 104 | rq=rq0; |
---|
| 105 | for (int i=0;i<rq->from.length();i++){ |
---|
| 106 | rv.add(RV(agent_name + rq->from(i), 1)); //TODO intensity occupancy |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | /*! |
---|
| 112 | \brief Basic Traffic Agent |
---|
| 113 | |
---|
| 114 | */ |
---|
| 115 | class BaseTrafficAgent : public Participant { |
---|
| 116 | protected: |
---|
| 117 | //! Signal Groups |
---|
| 118 | Array<SignalGroup> sg; |
---|
| 119 | |
---|
| 120 | Array<SignalGroupHandler> sgh; |
---|
| 121 | |
---|
| 122 | //!data from messages |
---|
| 123 | vec input_data; |
---|
| 124 | |
---|
| 125 | //! decription of msg_data |
---|
| 126 | RV input_rv; |
---|
| 127 | |
---|
| 128 | //! data to broadcast |
---|
| 129 | vec output_data; |
---|
| 130 | |
---|
| 131 | //! description of broadcast dataind |
---|
| 132 | RV output_rv; |
---|
| 133 | |
---|
| 134 | RV action_rv; |
---|
| 135 | |
---|
| 136 | datalink_part action2ds; |
---|
| 137 | |
---|
| 138 | //! output recepient |
---|
| 139 | Array<RequestOut> requests; |
---|
| 140 | Array<RequestHandler> request_handler; |
---|
| 141 | |
---|
| 142 | public: |
---|
| 143 | void validate(){ |
---|
| 144 | // write internal checks if all was loaded OK |
---|
| 145 | } |
---|
| 146 | void receive(const Setting &msg){ |
---|
| 147 | string what; |
---|
| 148 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 149 | |
---|
| 150 | if (what=="data"){ // |
---|
| 151 | // field data |
---|
| 152 | //! decription of detected data |
---|
| 153 | shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); |
---|
| 154 | ivec ind=rv->dataind(input_rv); // position of rv in in_rv; |
---|
| 155 | if (ind.length()>0){ //data belong here |
---|
| 156 | vec dt; |
---|
| 157 | UI::get(dt, msg, "value",UI::compulsory); |
---|
| 158 | set_subvector(input_data, ind, dt); //check size? |
---|
| 159 | } |
---|
| 160 | } else { |
---|
| 161 | bdm_warning("Unknown message of type "+what); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | void broadcast(Setting& set){ |
---|
| 165 | // broadcast data to all neighbours |
---|
| 166 | for (int i=0; i<request_handler.length(); i++){ |
---|
| 167 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 168 | RequestHandler &R=request_handler(i); |
---|
| 169 | |
---|
| 170 | // copy from create message |
---|
| 171 | Setting &m_to=msg.add("to",Setting::TypeString); |
---|
| 172 | m_to=requests(i).to; |
---|
| 173 | Setting &m_what=msg.add("what",Setting::TypeString); |
---|
| 174 | m_what="data"; |
---|
| 175 | //Setting &m_data=msg.add("data",Setting::TypeGroup); |
---|
| 176 | Setting &m_rv=msg.add("rv",Setting::TypeGroup); |
---|
| 177 | R.rv.to_setting(m_rv); |
---|
| 178 | Setting &m_val=msg.add("value",Setting::TypeGroup); |
---|
| 179 | vec val =R.outdata2out.pushdown(output_data); |
---|
| 180 | UI::save(val, m_val); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | void adapt(const vec &glob_dt){ |
---|
| 184 | // copy data from global vector to sSGHandlers |
---|
| 185 | for (int i=0; i<sgh.length();i++){ |
---|
| 186 | sgh(i).ds2data.filldown(glob_dt, sgh(i).det_data); |
---|
| 187 | } |
---|
[820] | 188 | // copy sg_length ... and others... |
---|
[819] | 189 | } |
---|
| 190 | void act(vec &glob_ut){ |
---|
| 191 | vec needs(sgh.length()); |
---|
| 192 | for (int i=0; i<sgh.length();i++){ |
---|
| 193 | needs(i) = sgh(i).expected_load(); |
---|
| 194 | } |
---|
| 195 | vec action = needs/sum(needs); // trivial stuff |
---|
| 196 | action2ds.filldown(action,glob_ut); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void ds_register(const DS &ds){ |
---|
| 200 | for (int i=0; i<sgh.length(); i++){ |
---|
| 201 | sgh(i).ds2data.set_connection(ds._drv(), sgh(i).rv_det_data); |
---|
| 202 | } |
---|
| 203 | action2ds.set_connection(action_rv, ds._urv()); |
---|
| 204 | } |
---|
| 205 | |
---|
[820] | 206 | void from_setting(const Setting &set); |
---|
[819] | 207 | }; |
---|
| 208 | UIREGISTER(BaseTrafficAgent); |
---|
| 209 | |
---|
| 210 | #endif //TRAGE_H |
---|