1 | #include <map> |
---|
2 | |
---|
3 | class GreenWaveTrafficAgent : public BaseTrafficAgent { |
---|
4 | protected: |
---|
5 | double stable_state_loss; |
---|
6 | double best_offset; |
---|
7 | map<string,double> expected; |
---|
8 | map<string,double>::iterator expected_i; |
---|
9 | |
---|
10 | //counts all expected cars to "dest" |
---|
11 | double expected_cars(const string dest) { |
---|
12 | |
---|
13 | double expected=0.0; |
---|
14 | |
---|
15 | //finds lanes which lead to "dest" |
---|
16 | for (int i=0;i<lanes.length();i++) { |
---|
17 | for (int j=0;j<lanes(i).outputs.length();j++) { |
---|
18 | if (lanes(i).outputs(j).substr(0,3)==dest) { |
---|
19 | //we have some matching outputs, count cars |
---|
20 | string group = name+"_"+lanes(i).sg; |
---|
21 | ivec index=rv_greentimes.dataind(RV(group,1)); |
---|
22 | vec green_time=greentimes(index); |
---|
23 | |
---|
24 | expected+=lanehs(i)->expected_output(green_time(0)); |
---|
25 | |
---|
26 | } |
---|
27 | } |
---|
28 | } |
---|
29 | return expected; |
---|
30 | } |
---|
31 | |
---|
32 | public: |
---|
33 | datalink ds2greentimes; |
---|
34 | vec greentimes; |
---|
35 | RV rv_greentimes; |
---|
36 | |
---|
37 | //! array of existing signal groups |
---|
38 | Array<string> sgs; |
---|
39 | |
---|
40 | void validate() { |
---|
41 | BaseTrafficAgent::validate(); |
---|
42 | action_rv = RV(name+"_offset", 1); // <======= example |
---|
43 | |
---|
44 | for (int i=0; i<sgs.length();i++) { |
---|
45 | rv_greentimes.add(RV(name+"_"+sgs(i),1)); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | void adapt(const vec &glob_dt) { |
---|
50 | BaseTrafficAgent::adapt(glob_dt); |
---|
51 | |
---|
52 | ds2greentimes.filldown(glob_dt,greentimes); |
---|
53 | } |
---|
54 | |
---|
55 | void broadcast(Setting& set){ |
---|
56 | |
---|
57 | //ask neighbours for exptected arrive times |
---|
58 | if (true) { |
---|
59 | for (int i=0; i<neighbours.length(); i++){ |
---|
60 | Setting &msg =set.add(Setting::TypeGroup); |
---|
61 | |
---|
62 | UI::save ( neighbours(i), msg, "to"); |
---|
63 | UI::save (name,msg,"from"); |
---|
64 | UI::save ( (string)"expected_times_request", msg, "what"); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | // broadcast expected cars |
---|
70 | while (!expected.empty()) { |
---|
71 | expected_i=expected.begin(); |
---|
72 | |
---|
73 | Setting &msg =set.add(Setting::TypeGroup); |
---|
74 | |
---|
75 | UI::save ( expected_i->first, msg, "to"); |
---|
76 | UI::save ( name, msg, "from"); |
---|
77 | UI::save ( (string)"new_expected_cars", msg, "what"); |
---|
78 | //UI::save ( &(rv_neighbours_out(i)), msg, "rv"); |
---|
79 | UI::save(expected_i->second, msg, "value"); |
---|
80 | expected.erase(expected_i); |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | void receive(const Setting &msg){ |
---|
86 | string what; |
---|
87 | string to; |
---|
88 | string from; |
---|
89 | double value; |
---|
90 | |
---|
91 | |
---|
92 | UI::get(what, msg, "what", UI::compulsory); |
---|
93 | UI::get(to, msg, "to", UI::compulsory); |
---|
94 | UI::get(from, msg, "from"); |
---|
95 | UI::get(value, msg, "value"); |
---|
96 | if (what=="expected_times_request"){ |
---|
97 | expected[from]=expected_cars(from); |
---|
98 | } |
---|
99 | else if (what=="new_expected_cars") { |
---|
100 | cout << "Jsem "<< name << " a dostal jsem od "<< from << " predpoklad: " << value << endl; |
---|
101 | //expected=0; |
---|
102 | } |
---|
103 | else { |
---|
104 | BaseTrafficAgent::receive(msg); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void ds_register(const DS &ds) { |
---|
109 | BaseTrafficAgent::ds_register(ds); |
---|
110 | ds2greentimes.set_connection(rv_greentimes, ds._drv()); |
---|
111 | } |
---|
112 | |
---|
113 | void from_setting(const Setting &set) { |
---|
114 | BaseTrafficAgent::from_setting(set); |
---|
115 | // load from file |
---|
116 | |
---|
117 | UI::get(sgs, set, "sgs", UI::compulsory); |
---|
118 | |
---|
119 | } |
---|
120 | }; |
---|
121 | UIREGISTER(GreenWaveTrafficAgent); |
---|