[941] | 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 | class BaseTrafficAgent; |
---|
| 16 | |
---|
| 17 | //! detector of traffic variables |
---|
| 18 | class Lane{ |
---|
| 19 | public: |
---|
| 20 | Array<string> inputs; |
---|
| 21 | Array<string> outputs; |
---|
| 22 | vec alpha; //size of outputs |
---|
| 23 | string queue; |
---|
| 24 | string sg; |
---|
| 25 | |
---|
| 26 | //! function loading info from Setting |
---|
| 27 | void from_setting(const Setting &set){ |
---|
| 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); |
---|
| 33 | |
---|
| 34 | } |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | //! class that operates on a signal group |
---|
| 38 | class LaneHandler { |
---|
| 39 | protected: |
---|
| 40 | //! pointer to physical lane |
---|
| 41 | const Lane &lane; |
---|
| 42 | //! agent pointer |
---|
| 43 | BaseTrafficAgent *agent; |
---|
| 44 | |
---|
| 45 | public: |
---|
| 46 | //! actual data from the relevant signal group |
---|
| 47 | vec inputs; |
---|
| 48 | //! |
---|
| 49 | double queue; |
---|
| 50 | //! |
---|
| 51 | //! description of det_data |
---|
| 52 | RV rv_inputs; |
---|
| 53 | //! description of det_data |
---|
| 54 | RV rv_outputs; |
---|
| 55 | //! description of det_data |
---|
| 56 | RV rv_queue; |
---|
| 57 | //! link from global measured data |
---|
| 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); |
---|
| 75 | |
---|
| 76 | //! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?) |
---|
| 77 | double expected_output(double green_time); |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | /*! |
---|
| 81 | \brief Basic Traffic Agent |
---|
| 82 | |
---|
| 83 | */ |
---|
| 84 | class BaseTrafficAgent : public Participant { |
---|
| 85 | LOG_LEVEL(BaseTrafficAgent,logdata); |
---|
| 86 | public: |
---|
| 87 | //! Signal Groups |
---|
| 88 | Array<Lane> lanes; |
---|
| 89 | |
---|
| 90 | Array<LaneHandler*> lanehs; |
---|
| 91 | |
---|
| 92 | //!data from messages |
---|
| 93 | vec inputs; |
---|
| 94 | |
---|
| 95 | //! decription of msg_data |
---|
| 96 | RV rv_inputs; |
---|
| 97 | |
---|
| 98 | //! data to broadcast |
---|
| 99 | vec outputs; |
---|
| 100 | |
---|
| 101 | //! description of broadcast dataind |
---|
| 102 | RV rv_outputs; |
---|
| 103 | |
---|
| 104 | vec queues; |
---|
| 105 | |
---|
| 106 | //! description of queues |
---|
| 107 | RV rv_queues; |
---|
| 108 | |
---|
| 109 | //! datalink from DS to input variables |
---|
| 110 | datalink ds2inputs; |
---|
| 111 | |
---|
| 112 | //! datalink from DS to output variables |
---|
| 113 | datalink ds2queues; |
---|
| 114 | |
---|
| 115 | //! action description |
---|
| 116 | RV action_rv; |
---|
| 117 | |
---|
| 118 | datalink_part action2ds; |
---|
| 119 | |
---|
| 120 | Array<string> neighbours; |
---|
| 121 | |
---|
| 122 | Array<RV> rv_neighbours_out; |
---|
| 123 | |
---|
| 124 | Array<datalink> output2neighbour; |
---|
| 125 | |
---|
| 126 | //! simulator's step length in seconds |
---|
| 127 | int step_time; |
---|
| 128 | |
---|
| 129 | //! lenght of cycle in seconds |
---|
| 130 | int cycle_time; |
---|
| 131 | |
---|
| 132 | public: |
---|
| 133 | void validate(){ |
---|
| 134 | //TODO Tohle asi ne |
---|
| 135 | step_time=90; |
---|
| 136 | cycle_time=80; |
---|
| 137 | |
---|
| 138 | lanehs.set_length(lanes.length()); |
---|
| 139 | for (int l=0; l<lanes.length(); l++){ |
---|
| 140 | lanehs(l) = new LaneHandler(lanes(l)); |
---|
| 141 | |
---|
| 142 | rv_inputs.add(lanehs(l)->rv_inputs); |
---|
| 143 | rv_outputs.add(lanehs(l)->rv_outputs); |
---|
| 144 | rv_queues.add(lanehs(l)->rv_queue); |
---|
| 145 | } |
---|
| 146 | inputs.set_size(rv_inputs._dsize()); |
---|
| 147 | outputs.set_size(rv_outputs._dsize()); |
---|
| 148 | queues.set_size(rv_queues._dsize()); |
---|
| 149 | |
---|
| 150 | for (int l=0; l<lanes.length(); l++){ |
---|
| 151 | lanehs(l)->connect_data(*this); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | //for -- rv_outputs -- |
---|
| 155 | // TODO vybrat rv pro sousedy |
---|
| 156 | rv_neighbours_out.set_length(neighbours.length()); |
---|
| 157 | output2neighbour.set_length(neighbours.length()); |
---|
| 158 | |
---|
| 159 | for (int i=0; i<neighbours.length(); i++){ |
---|
| 160 | for (int r=0; r<rv_outputs.length(); r++){ |
---|
| 161 | int str_pos = rv_outputs.name(r).compare(neighbours(i)); |
---|
| 162 | if (str_pos>neighbours(i).length()){ |
---|
| 163 | rv_neighbours_out(i).add(rv_outputs.subselect(vec_1(r))); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | // connect datasource |
---|
| 167 | output2neighbour(i).set_connection(rv_neighbours_out(i), rv_outputs); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | // lanehs knows RVS |
---|
| 171 | // write internal checks if all was loaded OK |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | void receive(const Setting &msg){ |
---|
| 175 | string what; |
---|
| 176 | UI::get(what, msg, "what", UI::compulsory); |
---|
| 177 | |
---|
| 178 | if (what=="new_stable_state"){ // |
---|
| 179 | // field data |
---|
| 180 | // extract decription of teh received datavector |
---|
| 181 | shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory); |
---|
| 182 | // find if it is needed |
---|
| 183 | ivec ind=rv->dataind(rv_inputs); // position of rv in in_rv; |
---|
| 184 | if (ind.length()>0){ //data are interesting |
---|
| 185 | vec dt; |
---|
| 186 | UI::get(dt, msg, "value",UI::compulsory); // get data |
---|
| 187 | set_subvector(inputs, ind, dt); //check size? |
---|
| 188 | } |
---|
| 189 | } else { |
---|
| 190 | Participant::receive(msg); |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | void log_register(logger &L, const string &prefix){ |
---|
| 194 | root::log_register ( L, prefix ); |
---|
| 195 | if ( log_level[logdata]){ |
---|
| 196 | L.add_vector ( log_level, logdata, RV ( 1 ), prefix ); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | void log_write() const { |
---|
| 200 | if (log_level[logdata]){ |
---|
| 201 | log_level.store(logdata, inputs); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void broadcast(Setting& set){ |
---|
| 206 | // broadcast data to all neighbours |
---|
| 207 | for (int i=0; i<neighbours.length(); i++){ |
---|
| 208 | Setting &msg =set.add(Setting::TypeGroup); |
---|
| 209 | |
---|
| 210 | // if... |
---|
| 211 | // copy from create message |
---|
| 212 | // create msg with fields {to=..., what=data, rv=..., value = ...} |
---|
| 213 | UI::save ( neighbours(i), msg, "to"); |
---|
| 214 | UI::save ( (string)"new_stable_state", msg, "what"); |
---|
| 215 | UI::save ( &(rv_neighbours_out(i)), msg, "rv"); |
---|
| 216 | UI::save( output2neighbour(i).pushdown(outputs), msg, "value"); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | } |
---|
| 220 | void adapt(const vec &glob_dt){ |
---|
| 221 | // copy data from global vector to sSGHandlers |
---|
| 222 | ds2inputs.filldown(glob_dt, inputs); |
---|
| 223 | //copy data from neighbours |
---|
| 224 | ds2queues.filldown(glob_dt, queues); |
---|
| 225 | // copy sg_length ... and others... |
---|
| 226 | } |
---|
| 227 | void act(vec &glob_ut){ |
---|
| 228 | vec action; // trivial stuff |
---|
| 229 | action2ds.filldown(action,glob_ut); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void ds_register(const DS &ds){ |
---|
| 233 | //register ds2output |
---|
| 234 | ds2inputs.set_connection(rv_inputs, ds._drv()); |
---|
| 235 | ds2queues.set_connection(rv_queues, ds._drv()); |
---|
| 236 | |
---|
| 237 | inputs.set_size(rv_inputs._dsize()); |
---|
| 238 | action2ds.set_connection( ds._urv(), action_rv); |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | void from_setting(const Setting &set); |
---|
| 243 | }; |
---|
| 244 | UIREGISTER(BaseTrafficAgent); |
---|
| 245 | |
---|
| 246 | #endif //TRAGE_H |
---|