root/applications/doprava/QuadraticMinimalizator.h

Revision 1422, 3.7 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#ifndef QUADRATICMINIMALIZATOR_H
3#define QUADRATICMINIMALIZATOR_H
4
5#include <cstdlib>
6#include <itpp/itbase.h>
7#include <iostream>
8
9
10using namespace itpp;
11using namespace std;
12
13class QuadraticMinimalizator {
14protected:
15    mat A;
16    mat B;
17    mat Q;
18    mat R;
19    double maxdif;
20
21    bool test() {
22        if ( !isDiagonal(Q) ) {
23            stringstream e;
24            e << "Matrix Q is not diagonal" << endl;
25            e << Q << endl;
26            it_warning(e.str());
27                        return false;
28        }
29        if ( !isDiagonal(R) ) {
30            stringstream e;
31            e << "Matrix R is not diagonal" << endl;
32            e << R << endl;
33            it_warning(e.str());
34                        return false;
35        }
36        if ( !isPositiveDefinite(Q) ) {
37            stringstream e;
38            e << "Matrix Q is not positive definite" << endl;
39            e << Q << endl;
40            it_warning(e.str());
41                        return false;
42        }
43        if ( !isPositiveDefinite(R) ) {
44            stringstream e;
45            e << "Matrix R is not positive definite" << endl;
46            e << R << endl;
47            it_warning(e.str());
48                        return false;
49        }
50                return true;
51    }
52
53public:
54    QuadraticMinimalizator( const mat &A, const mat &B, const mat &Q, const mat &R ) {
55        maxdif = 0.00000001;
56        this->A = A;
57        this->B = B;
58        this->Q = Q;
59        this->R = R;
60        test();
61    }
62
63    /**
64     * x(t+1) = Ax(t) + Bu(t)
65     * @param horizont
66     * @param C
67     * @return L: u(t) = Lx(t)
68     */
69    mat L( const int horizont ) {
70        int xdim = A.cols();
71        int udim = B.cols();
72        mat sqQ = sqrt(Q);
73        mat sqR = sqrt(R);
74        mat M0, M;
75        mat qrQ;
76        mat Lall, Ls, Lx, Lu, L;
77        M0 = concat_vertical(
78                concat_horizontal( sqQ * B,     sqQ * A ),
79                concat_horizontal( sqR,         zeros( udim, xdim ) )
80            );
81        M = M0;
82        for (int h = horizont; h >= 0; h --) {
83            qr(M, Lall);
84            Lu = Lall(0,    udim-1,       0,    udim-1);
85            Lx = Lall(udim, udim+xdim-1,  udim, udim+xdim-1);
86            Ls = Lall(0,    udim-1,       udim, udim+xdim-1);
87            M = concat_vertical(
88                    M0,
89                    concat_horizontal(Lx * B, Lx * A )
90                );
91        }
92        L = - inv(Lu) * Ls;
93        return L;
94    }
95
96        /*
97    double maxDif( const mat & M1, const mat & M2 ) {
98        return max(max(abs(M1 - M2), 1));
99    }
100
101    bool isSymmetric( const mat & M) {
102        return isSymmetric(M, maxdif);
103    }
104
105    bool isSymmetric( const mat & M, double maxdif ) {
106        mat D = abs(M - M.transpose());
107        double m = max(max(D, 1));
108        if ( m < maxdif )
109            return true;
110        else
111            return false;
112    }
113*/
114    bool isDiagonal(  const mat M ) {
115        mat D = abs(M);
116        for ( int i = 0; i < M.rows(); i ++ ) {
117            for ( int j = 0; j < M.cols(); j ++ ) {
118                if ( i != j && D(i,j) > maxdif ) {
119                    return false;
120                }
121            }
122        }
123        return true;
124    }
125       
126
127    bool isPositiveDefinite( const mat M ) {
128        int dim = M.rows();
129        if ( dim != M.cols() ) {
130            stringstream err;
131            err << "Matrix " << M << " is not square";
132            it_warning(err.str());
133        } else {
134            for ( int i = dim-1; i >= 0; i-- ) {
135                // subdeterminanty
136                if ( det(M(0,i,0,i)) <= 0 ) {
137                    return false;
138                }
139            }
140            return true;
141        }
142        return false;
143    }
144
145
146
147
148};
149
150#endif  /* QUADRATICMINIMALIZATOR_H */
Note: See TracBrowser for help on using the browser.