root/applications/doprava/traffic_agent_offset.h @ 986

Revision 986, 6.0 kB (checked in by ondrak, 14 years ago)

added queues 601_Q1 and 601_Q2 to aimsun_ds.cpp
added function to count expected_density to LaneHandler?
added compulsory input_distances and output_distances to cfg
action_rv renamed to rv_action (to match convention)
some work on GreenWaveTrafficAgent?

Line 
1#include "traffic_agent.h"
2#include <list>
3
4class GreenWaveTrafficAgent : public BaseTrafficAgent {
5protected:
6        double stable_state_loss;
7        double best_offset;
8
9        RV rv_recieved_exps;
10        vec recieved_exps;
11       
12        list<string> seznam;
13       
14
15        double car_leaving; //s; how long is 1 car leaving queue
16
17        bool recieved_exp_request;
18                       
19        //counts all expected cars going from each lane
20        void expected_cars(/*const string dest*/) {
21                double start_time;
22
23                for (int i=0;i<lanes.length();i++) {
24                        for (int j=0;j<lanes(i).outputs.length();j++) {
25                                //              if (lanes(i).outputs(j).substr(0,3)==dest) {
26                                //
27                                string group = name+"_"+lanes(i).sg;            //e.g. 495_VA
28                                ivec index=rv_inputs.dataind(RV(group,1));
29                                vec green_time=inputs(index);
30
31
32                                rv_outputs.add(RV(lanes(i).outputs(j)+"-"+group,3));
33                                outputs.set_size(rv_outputs._dsize());
34
35                                ivec indexes = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3));
36
37                                //Number of cars
38                                outputs(indexes(0))=lanehs(i)->expected_output(green_time(0)); 
39
40                                start_time = green_starts(sg_index(lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset;
41                                //first car arrive time
42                                outputs(indexes(1))=start_time;                                                                 
43                                //last car arrive time
44                                outputs(indexes(2))= start_time + green_time(0);
45
46                                //}
47                        }
48                }
49        }
50
51public:
52        int last_offset;
53        int planned_offset;
54        double actual_rating;
55       
56        //! array of existing signal groups
57        Array<string> sgs;
58        //! relative starts of green for signal groups
59        ivec green_starts;
60
61        //! avarage speed of cars
62        int VP;
63
64        void validate() {
65                rv_action = RV(name+"_offset", 1); // <======= example
66
67                for (int i=0; i<sgs.length();i++) {
68                        rv_inputs.add(RV(name+"_"+sgs(i),1));
69                       
70                        //place for expected inputs
71                        //rv_expected_traffic.add(RV(name+"_"+sgs(i)+"_exp",3));
72                }
73
74                inputs.set_size(rv_inputs._dsize());
75                //expected_traffic.set_size(rv_expected_traffic._dsize());
76               
77                BaseTrafficAgent::validate();
78        }
79
80        //! returns index of signal group sg
81        int sg_index(string sg) {
82                for (int i=0;i<sgs.length();i++) {
83                        if (sgs(i)==sg) {
84                                return i;
85                        }
86                }
87                return -1;
88        }
89
90        void adapt(const vec &glob_dt) {
91                BaseTrafficAgent::adapt(glob_dt);
92
93                planned_offset=last_offset;
94        }
95        //! counts actual rating using planned_offset and recieved_exps
96        void count_rating() {
97                double virtual_queue;
98                double t_emptiyng;
99                double t_green_begin;
100                double t_green_end;
101                double t_cars_begin;
102                double t_cars_end;
103                double t_mixing;
104                bool found;
105
106                actual_rating=0.0;
107
108                for (int i=0;i<lanehs.length();i++) {
109
110                        //Finding, if we have some expectations
111                        found=false;
112                        for (int j=0;j<rv_recieved_exps.length();j++) {
113                                for (int k=0;k<lanes(i).inputs.length();k++) {
114                                        int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-");
115                                        if (result>=0) {
116                                                ivec indexes = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3));
117
118                                                t_cars_begin=recieved_exps(indexes(1));
119                                                t_cars_end=recieved_exps(indexes(2));
120                                                found=true;
121                                        }
122                                }
123                        }
124                        if (!found) {
125                                t_cars_begin=0;
126                                t_cars_end=cycle_time;
127                        }
128
129                        //counting rating
130                        t_green_begin=green_starts(sg_index(lanes(i).sg)) + planned_offset;
131                        ivec indexes = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1));
132                        t_green_end=t_green_begin+inputs(indexes(0));
133
134                        if (t_green_begin<t_cars_begin) {
135                                virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving;
136                        } 
137                        else if (t_green_begin>t_cars_begin) {
138                                virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density();
139                        }
140                        //TODO lanehs(i)->queue je zatim prazdne => nesmyslne vysledky
141
142                        t_emptiyng=virtual_queue/(1/car_leaving - lanehs(i)->expected_density());
143                        t_mixing=min(t_green_end,t_cars_end)-max(t_green_begin,t_cars_begin);
144
145                        actual_rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0); 
146                }
147        }
148
149
150        void broadcast(Setting& set){
151                       
152                //ask neighbours for exptected arrive times
153                if (true) {
154                        for (int i=0; i<neighbours.length(); i++){
155                                Setting &msg =set.add(Setting::TypeGroup);
156
157                                UI::save ( neighbours(i), msg, "to");
158                                UI::save (name,msg,"from");
159                                UI::save ( (string)"expected_times_request", msg, "what");
160                        }
161                }
162
163
164                // broadcast expected cars
165                if (!seznam.empty()) {
166                        do {
167                                vec value;
168                               
169                                expected_cars();
170                                Setting &msg =set.add(Setting::TypeGroup);
171                                UI::save ( seznam.back(), msg, "to");
172                                UI::save ( name, msg, "from");
173                                UI::save ( (string)"new_expected_cars", msg, "what");
174                                UI::save ( &(rv_outputs), msg, "rv");
175                                UI::save ( outputs, msg, "value");
176                                seznam.pop_back();
177
178                        } while (!seznam.empty());
179                       
180                }
181
182        }
183
184        void receive(const Setting &msg){
185                string what;
186                string to;
187                string from;
188                vec value;
189                RV *rv;
190               
191                UI::get(what, msg, "what", UI::compulsory);
192                UI::get(to, msg, "to", UI::compulsory);
193                UI::get(from, msg, "from");
194                UI::get(rv, msg, "rv");
195                UI::get(value, msg, "value");
196                if (what=="expected_times_request"){ 
197                        recieved_exp_request=true;
198                        seznam.push_back(from);
199
200                } 
201                else if (what=="new_expected_cars") {
202                        rv_recieved_exps=*rv;
203                        recieved_exps=value;
204                        count_rating();
205                        //TODO skutecne vyjednavani
206                }
207                else {
208                        BaseTrafficAgent::receive(msg);
209                }
210        }
211       
212        void ds_register(const DS &ds) {
213                BaseTrafficAgent::ds_register(ds);
214                action2ds.set_connection( ds._urv(), rv_action);
215
216        }
217
218        void from_setting(const Setting &set) {
219                BaseTrafficAgent::from_setting(set);
220               
221                car_leaving=2;
222                VP=45;
223                recieved_exp_request=false;
224               
225                // load from file
226                UI::get(sgs, set, "sgs", UI::compulsory);       
227                UI::get(green_starts, set, "green_starts", UI::compulsory);
228                UI::get(last_offset, set, "offset", UI::compulsory);
229       
230        }
231        void act(vec &glob_ut){
232                vec action;
233                action.set_size(rv_action._dsize());
234               
235                ivec index = rv_action.dataind(RV(name+"_offset",1));
236                action(index(0))=planned_offset;
237
238                action2ds.filldown(action,glob_ut);
239        }
240
241};
242UIREGISTER(GreenWaveTrafficAgent);
Note: See TracBrowser for help on using the browser.