1 | #pragma once |
---|
2 | #include "BaseTrafficAgentCt.h" |
---|
3 | #include "QuadraticMinimalizator.h" |
---|
4 | |
---|
5 | class TrafficAgentLQMaster :public BaseTrafficAgentCt { |
---|
6 | protected: |
---|
7 | vec x; |
---|
8 | QuadraticMinimalizator * minimizer; |
---|
9 | public: |
---|
10 | void adapt(const vec &glob_dt) { |
---|
11 | BaseTrafficAgentCt::adapt(glob_dt); |
---|
12 | |
---|
13 | // FUNKCE VOLANE V main_loop V KAZDEM CYKLU |
---|
14 | |
---|
15 | BaseTrafficAgentCt::adapt(glob_dt); |
---|
16 | |
---|
17 | Q = 100 * diag( ones( queues.length()) ) + diag(computedQueues); |
---|
18 | R = "0.0000001"; |
---|
19 | A = diag( ones( queues.length()) ); |
---|
20 | B = zeros( queues.length(), 1); |
---|
21 | |
---|
22 | // jeden clen matice B |
---|
23 | for ( int i=0; i<queues.length(); i ++ ) { |
---|
24 | B(i,0) = - s_flow(i) * lanehs(i)->green_time_ratio; |
---|
25 | } |
---|
26 | |
---|
27 | x = getComputedQueues(); |
---|
28 | I0 = (mat)inputs; |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | void receive(const Setting& msg){ |
---|
33 | string what; |
---|
34 | UI::get(what, msg, "what", UI::compulsory); |
---|
35 | |
---|
36 | cout << endl << endl; |
---|
37 | |
---|
38 | if ( what.substr(0,9) == "QueueData" ) { |
---|
39 | vec val; |
---|
40 | UI::get(val, msg, "value"); |
---|
41 | QueueData qd; |
---|
42 | qd.fromMessageCode(what); |
---|
43 | qd.fromVec(val); |
---|
44 | int ind = findQueueDataById( qd.id ); |
---|
45 | if ( ind >= 0 ) { |
---|
46 | queueData(ind) = qd; |
---|
47 | } else { |
---|
48 | ind = queueData.length(); |
---|
49 | queueData.set_length( ind+1, true ); |
---|
50 | queueData(ind) = qd; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | void setDummy() { |
---|
58 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
59 | for ( int j = 0; j < queueData(i).outputs.length(); j ++ ) { |
---|
60 | for ( int k = 0; k < queueData.length(); k ++ ) { |
---|
61 | if ( queueData(k).idInput == queueData(i).outputs(j) ) { |
---|
62 | queueData(k).dummy = false; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | void setx() { |
---|
70 | x = ones( queueData.length() + 1 ); |
---|
71 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
72 | x(i) = queueData(i).queue; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | // az po setDummy !!! |
---|
77 | void setI0(){ |
---|
78 | I0 = zeros(queueData.length(),1); |
---|
79 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
80 | if ( queueData(i).dummy ) |
---|
81 | I0(i,0) = queueData(i).input; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // az po setI0 !!! |
---|
86 | void setA() { |
---|
87 | A = concat_vertical( |
---|
88 | concat_horizontal( diag( ones( queueData.length() ) ), I0 ), |
---|
89 | concat_horizontal( zeros( 1, queueData.length() ) , ones(1,1) ) |
---|
90 | ); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | void setB() { |
---|
95 | B = zeros(x.length(), 1); |
---|
96 | //cout << "B' 1 ["; |
---|
97 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
98 | B(i,0) = B(i,0) - queueData(i).ss() * queueData(i).green; |
---|
99 | //cout << B(i,0) << " "; |
---|
100 | } |
---|
101 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
102 | //B(i,0) = B(i,0) - queueData(i).ss() * queueData(i).green; |
---|
103 | for ( int j = 0; j < queueData(i).outputs.length(); j ++ ) { |
---|
104 | for ( int k = 0; k < queueData.length(); k ++ ) { |
---|
105 | if ( queueData(k).idInput == queueData(i).outputs(j) ) { |
---|
106 | B(k,0) = B(k,0) + queueData(i).alphas(j) * queueData(i).ss() * queueData(i).green; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | //cout << endl << "B' 2 " << endl << B.transpose() << endl; |
---|
113 | } |
---|
114 | |
---|
115 | void setQ() { |
---|
116 | Q = 1000 * diag( ones(x.length()) ); |
---|
117 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
118 | Q(i,i) += queueData(i).queue * 100; |
---|
119 | Q(i,i) += queueData(i).input * 100; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | double computeTc() { |
---|
124 | B = B * T; |
---|
125 | //vec computedQueues = getComputedQueues(); |
---|
126 | minimizer = new QuadraticMinimalizator(A, B, Q, R); |
---|
127 | mat L_mat = minimizer->L( 200); |
---|
128 | vec u_vec = L_mat * x; |
---|
129 | double u = u_vec(0); |
---|
130 | double Tc_computed = L / (1 - u); |
---|
131 | delete minimizer; |
---|
132 | /* |
---|
133 | if ( u >= 1 || Tc_computed > Tc_max ) { |
---|
134 | cout << endl << "Tc error : " << Tc_computed << " setting to max" << endl; |
---|
135 | Tc_computed = Tc_max; |
---|
136 | } |
---|
137 | if ( Tc_computed < Tc_min ) { |
---|
138 | cout << endl << "Tc error : " << Tc_computed << " setting to min" << endl; |
---|
139 | Tc_computed = Tc_min; |
---|
140 | } |
---|
141 | */ |
---|
142 | return Tc_computed; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | void act (vec &glob_ut){ |
---|
149 | BaseTrafficAgentCt::act(glob_ut); |
---|
150 | cout << endl; |
---|
151 | cout << endl; |
---|
152 | cout << "queues "; |
---|
153 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
154 | cout << queueData(i).queue << " "; |
---|
155 | } |
---|
156 | cout << endl; |
---|
157 | cout << endl; |
---|
158 | cout << "inputs "; |
---|
159 | for ( int i = 0; i < queueData.length(); i ++ ) { |
---|
160 | cout << queueData(i).input << " "; |
---|
161 | } |
---|
162 | cout << endl; |
---|
163 | |
---|
164 | |
---|
165 | setDummy(); |
---|
166 | setx(); |
---|
167 | setI0(); |
---|
168 | setA(); |
---|
169 | setB(); |
---|
170 | setQ(); |
---|
171 | |
---|
172 | R = "0.001"; |
---|
173 | |
---|
174 | cout << endl << endl; |
---|
175 | |
---|
176 | Tc_computed = computeTc(); |
---|
177 | cout << "L = " << L << endl; |
---|
178 | cout << "TC = " << Tc_computed << endl; |
---|
179 | |
---|
180 | //cout << "A" << endl << A << endl << endl; |
---|
181 | //cout << "I0'" << endl << I0.transpose() << endl << endl; |
---|
182 | |
---|
183 | |
---|
184 | cout << endl << endl; |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | cout << endl << name << " ACT" << endl; |
---|
190 | |
---|
191 | looper ++; |
---|
192 | } |
---|
193 | |
---|
194 | }; |
---|
195 | UIREGISTER(TrafficAgentLQMaster); |
---|