| 1106 | |
| 1107 | TEST (LQGU_Filatov){ |
| 1108 | /* |
| 1109 | system from: |
| 1110 | Adaptive Predictive Control Policy for Nonlinear Stochastic Systems |
| 1111 | (N. M. Filatov, H. Unbehauen) |
| 1112 | VII. Simulations |
| 1113 | |
| 1114 | q(k+1) = 1.654q(k) - 1.022q(k-1) + 0.2019q(k-2) |
| 1115 | +0.1098u(k) + 0.0792u(k-1) - 0.0229u(k-2) |
| 1116 | J(k) = (w(k+1) - q(k+1))^2 + 0.0005(u(k))^2 |
| 1117 | w(k) == 5 |
| 1118 | K = 50 |
| 1119 | q(>=0) = 0.1; |
| 1120 | |
| 1121 | ! used without noise |
| 1122 | */ |
| 1123 | |
| 1124 | int K = 50; |
| 1125 | double w = 5.0; |
| 1126 | double q0 = 0.1; |
| 1127 | |
| 1128 | LQG_universal lq; |
| 1129 | lq.rv = RV("u", 1, 0); |
| 1130 | RV rvc; |
| 1131 | rvc = RV("q", 1, 0); |
| 1132 | rvc.add(RV("q", 1, -1)); |
| 1133 | rvc.add(RV("q", 1, -2)); |
| 1134 | rvc.add(RV("u", 1, -1)); |
| 1135 | rvc.add(RV("u", 1, -2)); |
| 1136 | rvc.add(RV("w", 1, 0)); |
| 1137 | rvc.add(RV("1", 1, 0)); |
| 1138 | lq.set_rvc(rvc); |
| 1139 | lq.horizon = K; |
| 1140 | |
| 1141 | Array<linfnEx> model(7); |
| 1142 | model(0).A = mat("1.654"); |
| 1143 | model(0).B = vec("0"); |
| 1144 | model(0).rv = RV("q", 1, 0); |
| 1145 | model(0).rv_ret = RV("q", 1, 1); |
| 1146 | model(1).A = mat("-1.022"); |
| 1147 | model(1).B = vec("0"); |
| 1148 | model(1).rv = RV("q", 1, -1); |
| 1149 | model(1).rv_ret = RV("q", 1, 1); |
| 1150 | model(2).A = mat("0.2019"); |
| 1151 | model(2).B = vec("0"); |
| 1152 | model(2).rv = RV("q", 1, -2); |
| 1153 | model(2).rv_ret = RV("q", 1, 1); |
| 1154 | model(3).A = mat("0.1098"); |
| 1155 | model(3).B = vec("0"); |
| 1156 | model(3).rv = RV("u", 1, 0); |
| 1157 | model(3).rv_ret = RV("q", 1, 1); |
| 1158 | model(4).A = mat("0.0792"); |
| 1159 | model(4).B = vec("0"); |
| 1160 | model(4).rv = RV("u", 1, -1); |
| 1161 | model(4).rv_ret = RV("q", 1, 1); |
| 1162 | model(5).A = mat("-0.0229"); |
| 1163 | model(5).B = vec("0"); |
| 1164 | model(5).rv = RV("u", 1, -2); |
| 1165 | model(5).rv_ret = RV("q", 1, 1); |
| 1166 | model(6).A = mat("1"); |
| 1167 | model(6).B = vec("0"); |
| 1168 | model(6).rv = RV("w", 1, 0); |
| 1169 | model(6).rv_ret = RV("w", 1, 1); |
| 1170 | lq.Models = model; |
| 1171 | |
| 1172 | Array<quadraticfn> qloss(2); |
| 1173 | qloss(0).Q.setCh(mat("1 -1")); |
| 1174 | qloss(0).rv = RV("q", 1, 1); |
| 1175 | qloss(0).rv.add(RV("w", 1, 1)); |
| 1176 | qloss(1).Q = mat("0.0005");//automatic sqrt |
| 1177 | qloss(1).rv = RV("u", 1, 0); |
| 1178 | lq.Losses = qloss; |
| 1179 | |
| 1180 | lq.finalLoss.Q.setCh(mat("1 -1")); |
| 1181 | lq.finalLoss.rv = RV("q", 1, 1); |
| 1182 | lq.finalLoss.rv.add(RV("w", 1, 1)); |
| 1183 | |
| 1184 | lq.validate(); |
| 1185 | |
| 1186 | lq.resetTime(); |
| 1187 | lq.redesign(); |
| 1188 | |
| 1189 | int k; |
| 1190 | |
| 1191 | for(k = K-1; k > 0; k--) lq.redesign(); |
| 1192 | |
| 1193 | cout << "L: " << lq.getL() << endl; |
| 1194 | |
| 1195 | vec q(50); |
| 1196 | q(0) = q0; |
| 1197 | |
| 1198 | vec u(50); |
| 1199 | |
| 1200 | //rvc = [q(k), q(k-1), q(k-2), u(k-1), u(k-2), w(k)] |
| 1201 | vec xrvc(6); |
| 1202 | xrvc(5) = w; |
| 1203 | |
| 1204 | |
| 1205 | //first step |
| 1206 | xrvc(0) = q(0); |
| 1207 | xrvc(1) = q0; |
| 1208 | xrvc(2) = q0; |
| 1209 | xrvc(3) = 0; |
| 1210 | xrvc(4) = 0; |
| 1211 | u(0) = (lq.ctrlaction(xrvc))(0); |
| 1212 | q(1) = 1.654 * q(0) - 1.022*q0 + 0.2019*q0 + 0.1098*u(0); |
| 1213 | |
| 1214 | //second step |
| 1215 | xrvc(0) = q(1); |
| 1216 | xrvc(1) = q(0); |
| 1217 | xrvc(2) = q0; |
| 1218 | xrvc(3) = u(0); |
| 1219 | xrvc(4) = 0; |
| 1220 | u(1) = (lq.ctrlaction(xrvc))(0); |
| 1221 | q(2) = 1.654 * q(1) - 1.022*q(0) + 0.2019*q0 + 0.1098*u(1) + 0.0792*u(0); |
| 1222 | |
| 1223 | //cycle |
| 1224 | for(k = 2; k < K-1; k++){ |
| 1225 | xrvc(0) = q(k); |
| 1226 | xrvc(1) = q(k-1); |
| 1227 | xrvc(2) = q(k-2); |
| 1228 | xrvc(3) = u(k-1); |
| 1229 | xrvc(4) = u(k-2); |
| 1230 | u(k) = (lq.ctrlaction(xrvc))(0); |
| 1231 | q(k+1) = 1.654 * q(k) - 1.022*q(k-1) + 0.2019*q(k-2) + 0.1098*u(k) + 0.0792*u(k-1) - 0.0229*u(k-2); |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | ofstream log; |
| 1236 | log.open ("log.txt"); |
| 1237 | for(k = 0; k < K; k++) |
| 1238 | log << k << "\t" << q(k) << endl; |
| 1239 | log.close(); |
| 1240 | } |
| 1241 | |
| 1242 | /*TEST (matrixdivisionspeedtest){ |
| 1243 | Real_Timer tmr; |
| 1244 | Uniform_RNG urng; |
| 1245 | |
| 1246 | mat A("0.0319219"); //1 1 1 1; 0 2 2 2 2; 0 0 3 3 3; 0 0 0 4 4; 0 0 0 0 5"); |
| 1247 | mat B("0.24835 -0.112361 0.000642142 -1.3721");// = urng(2,6); |
| 1248 | mat C(1,4); |
| 1249 | C.zeros(); |
| 1250 | |
| 1251 | int n = 1; |
| 1252 | int j,k,l,m; |
| 1253 | |
| 1254 | //cout << "A:" << endl << A << endl << "B:" << endl << B << endl; |
| 1255 | |
| 1256 | cout << "Using inv function: "; |
| 1257 | tmr.tic(); |
| 1258 | for(j = 0; j < n; j++) C = inv(A)*B; |
| 1259 | tmr.toc_print(); |
| 1260 | cout << endl << C << endl; |
| 1261 | |
| 1262 | cout << "Using ldutg function: "; |
| 1263 | tmr.tic(); |
| 1264 | for(j = 0; j < n; j++) ldutg(A,B,C); |
| 1265 | tmr.toc_print(); |
| 1266 | cout << endl << C << endl;*/ |
| 1267 | |
| 1268 | /*cout << "Using Gauss elimination: "; |
| 1269 | int utrows = A.rows(); |
| 1270 | int longrow = utrows + B.cols(); |
| 1271 | mat Cc(utrows,longrow); |
| 1272 | Cc.zeros(); |
| 1273 | tmr.tic(); |
| 1274 | for(j = 0; j < n; j++){ |
| 1275 | Cc.set_submatrix(0,utrows-1,0,utrows-1,A);//rrccM |
| 1276 | Cc.set_submatrix(0,utrows-1,utrows,longrow-1,B); |
| 1277 | for(k = utrows-1; k >= 0; k--){ |
| 1278 | Cc.set_row(k, Cc.get_row(k) / A(k,k)); |
| 1279 | for(l = 0;l < k; l++){ |
| 1280 | Cc.set_row(l,Cc.get_row(l) - A(l,k)*Cc.get_row(k)); |
| 1281 | } |
| 1282 | } |
| 1283 | C = Cc(0,utrows-1,utrows,longrow-1); |
| 1284 | } |
| 1285 | tmr.toc_print(); |
| 1286 | cout << endl << C << endl;*/ |
| 1287 | |
| 1288 | /*cout << "Using Gauss elimination Direct access!: "; |
| 1289 | //data da pointer co jede jako pole po sloupcich |
| 1290 | tmr.tic(); |
| 1291 | for(j = 0; j < n; j++){ |
| 1292 | |
| 1293 | for(l=0;l<25;l++) |
| 1294 | (Cc._data())[l] = (A._data())[l]; |
| 1295 | for(l=25;l<50;l++) |
| 1296 | Cc._data()[l] = B._data()[l-25]; |
| 1297 | |
| 1298 | |
| 1299 | for(k = utrows-1; k >= 0; k--){ |
| 1300 | for(l=0;l<10;l++) Cc._data()[k+5*l] /= A._data()[k+5*k]; |
| 1301 | for(l = 0;l < k; l++){ |
| 1302 | for(m=0;m<10;m++) Cc._data()[l+5*m] -= A._data()[l+5*k]*Cc._data()[k+5*m]; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | for(k=0;k<25;k++) |
| 1307 | |
| 1308 | C._data()[k] = Cc._data()[k+25]; |
| 1309 | } |
| 1310 | |
| 1311 | tmr.toc_print(); |
| 1312 | cout << endl << C << endl; |
| 1313 | |
| 1314 | cout << "Using Gauss elimination ARRAY: "; |
| 1315 | tmr.tic(); |
| 1316 | double arA[5][5]; |
| 1317 | double arB[5][5]; |
| 1318 | double arC[5][5]; |
| 1319 | double arCc[5][10]; |
| 1320 | for(k=0;k<5;k++){ |
| 1321 | for(l=0;l<5;l++){ |
| 1322 | arA[k][l] = A(k,l); |
| 1323 | arB[k][l] = B(k,l); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | for(j = 0; j < n; j++){ |
| 1330 | for(k=0;k<5;k++){ |
| 1331 | for(l=0;l<5;l++) |
| 1332 | arCc[k][l] = arA[k][l]; |
| 1333 | for(l=5;l<10;l++) |
| 1334 | arCc[k][l] = arB[k][l-5]; |
| 1335 | } |
| 1336 | |
| 1337 | for(k = utrows-1; k >= 0; k--){ |
| 1338 | for(l=0;l<10;l++) arCc[k][l] /= arA[k][k]; |
| 1339 | for(l = 0;l < k; l++){ |
| 1340 | for(m=0;m<10;m++) arCc[l][m] -= arA[l][k]*arCc[k][m]; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | for(k=0;k<5;k++) |
| 1345 | for(l=0;l<5;l++) |
| 1346 | arC[k][l] = arCc[k][l+5]; |
| 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | for(k=0;k<5;k++) |
| 1352 | for(l=0;l<5;l++) |
| 1353 | C(k,l) = arC[k][l]; |
| 1354 | tmr.toc_print(); |
| 1355 | cout << endl << C << endl;*/ |
| 1356 | //} |