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