root/applications/doprava/traffic_agent_LQ.h

Revision 1422, 4.2 kB (checked in by jabu, 12 years ago)

BaseTrafficAgentCt? - zaklad agenta pro rizeni delky cyklu
Master, Observer - rizeni centralnim agentem Master, Observer jen posila data

Line 
1
2#include "BaseTrafficAgentCt.h"
3#include "QuadraticMinimalizator.h"
4
5class TrafficAgentLQ : public BaseTrafficAgentCt {
6protected:
7        QuadraticMinimalizator * minimizer;
8       
9public:
10        // FUNKCE VOLANE V main_loop NA ZACATKU
11       
12        void from_setting(const Setting& set) {
13                BaseTrafficAgentCt::from_setting(set);         
14        }
15
16        void validate (){
17                BaseTrafficAgentCt::validate();                                 
18               
19                cout << "HELLO! " << name << " IS HERE!" << endl;
20        }
21
22        // FUNKCE VOLANE V main_loop V KAZDEM CYKLU
23        void adapt (const vec &glob_dt) {       
24                BaseTrafficAgentCt::adapt(glob_dt);
25
26                Q = 100 * diag( ones( queues.length()) ) + diag(computedQueues);
27                R = "0.0000001";
28                A = diag( ones( queues.length()) );
29                B = zeros( queues.length(), 1); 
30
31                // jeden clen matice B
32                for ( int i=0; i<queues.length(); i ++ ) {
33                        B(i,0) = - s_flow(i) * lanehs(i)->green_time_ratio;
34                }
35
36               
37        }
38
39       
40        void broadcast( Setting &set ) {
41                string dummyDet = "DUMMY_DET";
42                if ( nOfBroadcast == 0 ) {                     
43                        for ( int i = 0; i < lanehs.length(); i++ ) {           
44                                for ( int j = 0; j < lanehs(i)->rv_outputs.length(); j ++ ) {
45                                        if ( dummyDet.compare(lanehs(i)->rv_outputs.name(j)) != 0 ) {                                           
46                                                stringstream coefStr;
47                                                coefStr << "coef" << lanehs(i)->rv_outputs.name(j);
48                                                double val = s_flow(i) * lanehs(i)->green_time_ratio * lanehs(i)->getLane().alpha(j);
49                                                sendToAll<double>(set, coefStr.str(), val);
50                                        }
51                                }                               
52                        }
53                }
54
55
56                if ( nOfBroadcast == 1 ) {
57                        broadcastTc(set);
58                }
59               
60                nOfBroadcast ++;
61        }
62
63        double votingWeight() {
64                //vec computedQueues = getComputedQueues();
65                double qsum = 0;
66                for ( int i = 0; i < computedQueues.length(); i++ ) {
67                        qsum += computedQueues(i);
68                }
69                if ( qsum > 1 )
70                        return qsum;
71                else
72                        return 1;
73        }
74
75        void broadcastTc( Setting & set) {
76                // posli idealni tc
77                Tc_computed = computeTc();
78                stringstream tcstr;
79                tcstr << "timecycle" << name;
80               
81                // posli vahu hlasu (suma front)
82                //vec computedQueues = getComputedQueues();     
83               
84                vec tc_vec = "0.0 0.0";
85                tc_vec(0) = Tc_computed;
86                tc_vec(1) = votingWeight();
87                // posilam vektor [tc, w]
88                sendToAll<vec>(set, tcstr.str(), tc_vec);
89        }
90
91        void receive(const Setting& msg){
92                string what;
93                UI::get(what, msg, "what", UI::compulsory);             
94                if ( what.substr(0,4) == "coef" ) {
95                        string inputName = what.substr(4,what.length()-4);
96                        int i = find_index(rv_inputs, inputName);
97                        double val;
98                        UI::get(val, msg, "value");
99                        B(i,0) = B(i,0) + val;
100                }
101
102                if ( what.substr(0,9) == "timecycle" ) {
103                        vec val;
104                        UI::get(val, msg, "value");
105                        cout << "receiving Tc " << val << endl;
106                        Tc_sum += val(0) * val(1);
107                        Tc_w_sum += val(1);                     
108                }
109
110        }
111
112        void act (vec &glob_ut){               
113                //vec computedQueues = getComputedQueues();
114                cout << endl << name << " ACT" << endl;
115                cout << "Tc computed = " << Tc_computed << endl;
116                cout << "queues = " << round( computedQueues )<< endl;                 
117                double w = votingWeight();
118                Tc_sum += Tc_computed * w;
119                Tc_w_sum += w;
120                int Tc = (int)round( Tc_sum / Tc_w_sum );               
121                setCycleTime(Tc, glob_ut);
122                looper ++;
123        }
124// VYPOCETNI FUNKCE
125
126        // saturovany tok pruhu i
127        double s_flow(int i) {
128                double min_flow = 0.3;
129                double max_flow = 0.5;
130                double input = 0;
131                if ( 2*i < inputs.length() )
132                        input = inputs(2*i);
133               
134                vec q = getComputedQueues();
135                double flow = q(i);
136                if ( input > 0 )
137                        flow += input;
138                flow = flow/T;
139                if ( flow > max_flow )
140                        return max_flow;
141                if ( flow < min_flow )
142                        return min_flow;
143                else
144                        return flow;
145        }
146
147        double computeTc() {
148                B = B * T;
149                //vec computedQueues = getComputedQueues();
150                minimizer = new QuadraticMinimalizator(A, B, Q, R);             
151                mat L_mat = minimizer->L( 100);
152                vec u_vec = L_mat * computedQueues;
153                double u = u_vec(0);
154                double Tc_computed = L / (1 - u);
155                delete minimizer;
156                if ( u >= 1 || Tc_computed > Tc_max ) {
157                        cout << endl << "Tc error : " << Tc_computed << " setting to max" << endl;
158                        Tc_computed = Tc_max;
159                }
160                if ( Tc_computed < Tc_min ) {
161                        cout << endl << "Tc error : " << Tc_computed << " setting to min" << endl;
162                        Tc_computed = Tc_min;   
163                }
164                return Tc_computed;
165        }
166
167       
168
169
170
171       
172
173
174// KONEC
175        ~TrafficAgentLQ() {
176               
177        }
178
179};
180
181UIREGISTER(TrafficAgentLQ);
Note: See TracBrowser for help on using the browser.