root/applications/doprava/traffic_agent_cycle_time.h @ 1139

Revision 1139, 8.9 kB (checked in by jabu, 14 years ago)
Line 
1#include "traffic_agent.cpp"
2const string testMessage = "test_message";
3const double measurement_cycle_time = 90; // s
4const double saturated_stream = 0.5; // car/s
5const int sample_cycles = 3;
6
7
8
9
10
11
12class TrafficAgentCycleTime : public BaseTrafficAgent {
13public:
14        int Tc;
15        static const int minTc = 60;  // nepouziva se
16        static const int maxTc = 300; // nepouziva se
17        static const int stepTc = 8;   
18        static const int d_Tc = 2; // pocet navrhovanych delek cyklu   
19        int idealTc;
20        int cycle_counter;
21        double max_profit;
22        unsigned int n_of_broadcast;
23        Array <int> lane_sum;
24        //Array <SignalGroup*> signal_groups;
25        Array <double> received_Tcs;
26        Array <double> received_profit_sum;
27        Array <double> received_queue_diffs;
28        // pomocne logovaci soubory
29        ofstream Tcs;
30        ofstream Ros;
31        ofstream Qs;
32
33
34// POMOCNE FUNKCE
35
36        void printVector ( RV rv_vector, vec vector, string description ) {
37                cout << endl << description << " " << name << endl;
38                int k = 0;
39                for ( int i = 0; i < rv_vector.length(); i ++ ) {
40                        cout << rv_vector.name(i) << " : ";
41                        for ( int j = 0; j < rv_vector.size(i); j ++ ) {                               
42                                cout << vector(k) << " ";
43                                k ++;
44                        }
45                        cout << endl;
46                }
47                cout << endl;
48        }
49
50        void add2array( Array <double> &arr, double item  ) {
51                arr.set_length( arr.length()+1, true );
52                arr(arr.length()-1) = item;
53                cout << endl << "addin to array value " << item << endl;
54        }
55
56        void echo ( string message ) {
57                cout << name << " hlasi: " << message << endl;         
58        }
59
60        unsigned int find_index ( RV rv_vector, string index_string ) {
61                for ( unsigned int i = 0; i < rv_vector.length(); i ++) {
62                        if ( rv_vector.name(i) == index_string )
63                                return i;
64                }
65                return rv_vector.length();
66        }
67
68        unsigned int find_index ( Array <string> arr, string index_string ) {
69                for ( unsigned int i = 0; i < arr.length(); i ++) {
70                        if ( arr(i) == index_string )
71                                return i;
72                }
73                return arr.length();
74        }
75
76// VYPOCETNI FUNKCE
77
78        // soucet cekacich autocasu
79        double getWT ( double time_cycle ) {
80                double sum = 0;         
81                for ( int i = 0; i <lanehs.length(); i ++ ) {
82                        sum += lanehs(i)->getWT( time_cycle );
83                }               
84                return sum;
85        }
86
87        double getProfit ( double tc ) {
88                return getWT(Tc) - getWT(tc);
89        }
90
91        double getQueue () {
92                double queue = 0;
93                for ( int i = 0; i <lanehs.length(); i ++ ) {
94                        queue += lanehs(i)->getAverageQueueLength();
95                }
96                return queue;
97        }
98
99       
100        double getRelativeQueueDiff() {
101                double dq = 0;
102                for ( int i=0; i< lanehs.length(); i ++ ) {
103                        dq += lanehs(i)->getRelativeQueueDiff();
104                }
105                return dq;
106        }
107
108       
109
110
111        void send2neighbour( Setting &set, int i, string messageName, double messageValue ) {           
112                if ( i < neighbours.length() ) {
113                        Setting &msg =set.add(Setting::TypeGroup);
114                        UI::save( neighbours(i), msg, "to" );
115                        UI::save (name,msg,"from");
116                        UI::save( messageName, msg, "what" );
117                        UI::save( messageValue, msg, "value" );
118                }
119                else {
120                        //throw new Exception("soused "+((string)i)+" neexistuje");
121                        //cout << endl << endl << "soused " << i << mimo
122                        std::stringstream out;
123                        out << "soused " << i << " neexistuje";
124                        //throw out.str();
125                        cout << out.str();
126                }
127        }
128
129        void send2allNeighbours ( Setting &set, string messageName, double messageValue ) {
130                for ( int i = 0; i < neighbours.length(); i++ ) {
131                        send2neighbour( set, i, messageName, messageValue );
132                }
133        }
134
135       
136
137       
138// FUNKCE VOLANE V main_loop NA ZACATKU
139       
140        void validate (){
141                rv_action = RV("Tc",1);
142                rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) );
143                Tc = 80;               
144                idealTc = Tc;
145                max_profit = 0;
146                received_profit_sum.set_length( 2*d_Tc +1 );
147                received_Tcs.set_length( 2*d_Tc +1 );
148                cycle_counter = 0;
149
150                BaseTrafficAgent::validate();
151
152                for ( int i=0; i<lanehs.length(); i ++ ) {
153                        unsigned int index = find_index( green_names, name + "_" + lanehs(i)->getSG() );
154                                lanehs(i)->green_time_ratio = green_times( index );
155                }
156
157                // pomocne logovaci soubory
158                Tcs.open("Tcs.dat");
159                Tcs << "cyklus\tTc\tprofit\n";
160                stringstream Ro_file_name;
161                Ro_file_name << "Ro_" << name << ".dat";
162                if ( name == "495" ) {
163                        Ros.open("Ros_495.dat");
164                        Qs.open("Qs_495.dat");
165                }
166                else { 
167                        Ros.open("Ros_601.dat");
168                        Qs.open("Qs_601.dat");
169                }
170                Ros << "Hustoty provozu ve frontach" << endl << "n";
171                Qs << "Prumerna delka fronty" << endl << "n";
172                for ( int i = 0; i < lanehs.length(); i++ ) {
173                        Ros << "\t" << lanehs(i)->getQueueName();
174                        Qs << "\t" << lanehs(i)->getAverageQueueLength();
175                }
176                Ros << endl;
177        }
178
179// FUNKCE VOLANE V main_loop V KAZDEM CYKLU
180
181        void adapt (const vec &glob_dt) {
182                // vynulovani pole prijatych rozdilu fronty
183                received_queue_diffs.set_length(0);
184
185                // inicializes vars of cycle of broadcast
186                n_of_broadcast = 0;
187                max_profit = 0;
188                //cout << endl << name << ":" << endl << endl;
189                // predani aktualnich dat do LaneHandler lanehs
190                Ros << cycle_counter;
191                Qs << cycle_counter;
192                for ( int i=0; i<lanehs.length(); i ++ ) {
193                        lanehs(i)->Tc = Tc;
194                        lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) );
195                       
196                        lanehs(i)->echo();
197                        // pomocne logovaci soubory
198                        Ros << "\t" << lanehs(i)->getRo();
199                        Qs << "\t" << lanehs(i)->getAverageQueueLength();
200                }
201                Ros << endl;
202                Qs << endl;
203
204                if ( cycle_counter % sample_cycles == 0 ) {
205                        //cout << name << " queue DIFF " << getRelativeQueueDiff() << endl;
206                       
207                        for ( int i=0; i<lanehs.length(); i ++ ) {
208                       
209                       
210                                //lanehs(i)->echo();
211                               
212                       
213                        //cout << name << " " << i << " "  << lanes(i).sg << " " << lanes(i).queue  << " input " << queues(i) << endl;
214                        }
215                }
216
217                for ( int i = 0; i < received_profit_sum.length(); i++ )
218                        received_profit_sum(i) = 0;
219
220
221                BaseTrafficAgent::adapt(glob_dt);
222        }
223
224        void broadcast(Setting &set){           
225               
226                // 1. cycle of communication
227                // sends all tcs in range +- d_tc and expected profits according to tc
228                // format: tc_index, profit_index
229                if ( n_of_broadcast == 0 && ( cycle_counter % sample_cycles == 0 ) ) {
230                        cout << endl << name+" PROFITS:";
231                        for ( int i = -d_Tc; i <= d_Tc; i++ ) {                         
232                                int time_cycle = Tc + i*stepTc;
233                                double profit = getProfit(time_cycle);
234                                cout << "\t" << time_cycle << " " <<profit;
235                                // send tc
236                                stringstream time_cycle_stream;
237                                time_cycle_stream << "tc_" << (i+d_Tc);
238                                send2allNeighbours( set, time_cycle_stream.str(), time_cycle);
239                               
240                                // send profit
241                                stringstream profit_stream;
242                                profit_stream << "profit_" << (i+d_Tc);
243                                send2allNeighbours( set, profit_stream.str(), profit );                         
244                        }
245                       
246                        cout << endl;
247                }
248               
249                n_of_broadcast ++;
250        }
251
252        void receive(const Setting& msg){
253                string what;
254                string from;
255                double val;             
256                UI::get(what, msg, "what", UI::compulsory);
257                UI::get(from, msg, "from", UI::compulsory);
258                UI::get(val, msg, "value");
259
260                               
261                //cout << name << " receiving from " << from << " " << what << " : " << val<<endl;
262               
263                if ( what.substr(0,2) == "tc" ) {                       
264                        istringstream tc_i( what.substr(3, what.length()-3) );
265                        int index;
266                        tc_i >> index;
267                        received_Tcs( index ) = val;
268                        //cout << "tc " << val << endl;
269                }
270                if ( what.substr(0,6) == "profit" ) {                           
271                        istringstream profit_i( what.substr(7, what.length()-7) );
272                        int index;
273                        profit_i >> index;
274                        received_profit_sum( index ) = received_profit_sum( index ) + val;
275                        //cout << "pridavam profit " << val << endl;                   
276                }
277               
278               
279                if ( what == "new_stable_state" ) {
280                        //echo("new_stable_state");
281                        BaseTrafficAgent::receive(msg);         
282                }       
283               
284        }
285
286        void act (vec &glob_ut){
287               
288                if ( cycle_counter % sample_cycles == 0 ) {
289                        // choose tc with max profit           
290                        cout << endl << name << " received + my profits "<<endl;;
291                        for ( int i = 0; i < received_profit_sum.length(); i ++ ) {
292                                int time_cycle = received_Tcs(i);
293                                double profit = received_profit_sum(i) + getProfit(time_cycle);
294                                cout << time_cycle << " " << profit << endl;
295                                if ( profit > max_profit ) {
296                                        max_profit = profit;
297                                        idealTc = time_cycle;
298                                }
299                        }
300                        cout << endl << name << "Tc: " << idealTc << " s celkovym ziskem " << max_profit << endl;
301                        // reseting lanehs
302                        for ( int i = 0; i < lanehs.length(); i ++ ) { 
303                                lanehs(i)->resetQueue();
304                        }
305
306                        Tc = idealTc;
307                        Tcs << cycle_counter << "\t" << Tc << "\t" << max_profit <<  endl;
308
309
310                        // nastaveni delky cyklu na Tc
311                        vec action;
312                        action.set_size(rv_action._dsize());   
313                        int st;
314                        int stage_time_sum = 0;  // soucet delky fazi
315                        action(find_index(rv_action, "Tc")) = Tc;               
316                        for ( int i =0; i < stage_names.length(); i ++) {                       
317                                if ( (i+1) < stage_names.length() ) {
318                                        st = (stage_times(i)/80)*Tc;
319                                        stage_time_sum += st;
320                                        action(find_index(rv_action, stage_names(i))) = st;
321                                }
322                                else // dopocitani posledni faze - oprava zaokrouhlovaci chyby
323                                        action(find_index(rv_action, stage_names(i))) = Tc - stage_time_sum;
324                        }
325                        action2ds.filldown(action,glob_ut);
326                }
327                        cycle_counter ++;
328        }
329       
330        // KONEC
331        void finalize() {
332                Tcs.close();
333        }
334
335
336
337};
338UIREGISTER(TrafficAgentCycleTime);
Note: See TracBrowser for help on using the browser.