1 | package parties.in4010.q12015.group10;
|
---|
2 |
|
---|
3 | import static java.lang.Math.pow;
|
---|
4 |
|
---|
5 | import agents.Jama.Matrix;
|
---|
6 |
|
---|
7 | public class boulwareParameterEstimator {
|
---|
8 |
|
---|
9 | static void printMatrix(Matrix myMat) {
|
---|
10 | int n = myMat.getColumnDimension();
|
---|
11 | int m = myMat.getRowDimension();
|
---|
12 | System.out.println("------\nm=" + m + ", n=" + n);
|
---|
13 | // now loop through the rows of valsTransposed to print
|
---|
14 | for (int row = 0; row < m; row++) {
|
---|
15 | for (int col = 0; col < n; col++) {
|
---|
16 | System.out.print(myMat.get(row, col) + ", ");
|
---|
17 | }
|
---|
18 | System.out.print("\n");
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public static double[] leastSquaresFit(double[] x, double[] time,
|
---|
23 | int maxLoops, double powerIncrement) {
|
---|
24 |
|
---|
25 | // ////////////////////////////////////////////////////////////
|
---|
26 | double[] a = new double[maxLoops];
|
---|
27 | double[] b = new double[maxLoops];
|
---|
28 | double[] c = new double[maxLoops];
|
---|
29 | double[] d = new double[maxLoops];
|
---|
30 | double[] varError = new double[maxLoops];
|
---|
31 | double minimumVarError = 1000;
|
---|
32 | int minimumVarErrorLoopNumber = 0;
|
---|
33 |
|
---|
34 | double powerT = 1; // initial boulware parameter
|
---|
35 |
|
---|
36 | for (int loopNumber = 0; loopNumber < maxLoops; loopNumber++) {
|
---|
37 | // Perform least squares to find best fit for current boulware
|
---|
38 | // parameter
|
---|
39 | Matrix A = new Matrix(time.length, 3);
|
---|
40 | Matrix B = new Matrix(3, 1);
|
---|
41 | Matrix y = new Matrix(time.length, 1);
|
---|
42 | powerT = powerT + powerIncrement;
|
---|
43 | for (int n = 0; n < time.length; n++) {
|
---|
44 | A.set(n, 0, 1);
|
---|
45 | A.set(n, 1, time[n]);
|
---|
46 | A.set(n, 2, pow(time[n], powerT));
|
---|
47 | y.set(n, 0, x[n] - 1);
|
---|
48 | }
|
---|
49 | Matrix AT = A.transpose();
|
---|
50 | Matrix ATA = AT.times(A);
|
---|
51 | if (ATA.rank() < ATA.getColumnDimension()) {
|
---|
52 | a[loopNumber] = 1;
|
---|
53 | b[loopNumber] = 0;
|
---|
54 | c[loopNumber] = 0;
|
---|
55 | d[loopNumber] = 0;
|
---|
56 | } else {
|
---|
57 | try {
|
---|
58 | Matrix invATA = ATA.inverse();
|
---|
59 | Matrix invATAA = invATA.times(AT);
|
---|
60 | B = invATAA.times(y);
|
---|
61 | } catch (Exception e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | a[loopNumber] = 1;
|
---|
64 | b[loopNumber] = 0;
|
---|
65 | c[loopNumber] = 0;
|
---|
66 | d[loopNumber] = 0;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | a[loopNumber] = Math.min(B.get(0, 0), 1);
|
---|
71 | b[loopNumber] = Math.min(B.get(1, 0), 0);
|
---|
72 | c[loopNumber] = Math.min(B.get(2, 0), 0);
|
---|
73 | d[loopNumber] = powerT;
|
---|
74 |
|
---|
75 | double[] xEst = new double[time.length];
|
---|
76 | for (int n = 0; n < time.length; n++) {
|
---|
77 | xEst[n] = a[loopNumber] + b[loopNumber] * time[n]
|
---|
78 | + c[loopNumber] * pow(time[n], d[loopNumber]);
|
---|
79 | }
|
---|
80 | varError[loopNumber] = MeanAndVariance.getVarianceError(x, xEst);
|
---|
81 | if (minimumVarError > varError[loopNumber]) {
|
---|
82 | minimumVarError = varError[loopNumber];
|
---|
83 | minimumVarErrorLoopNumber = loopNumber;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | double[] parOUTabcE = new double[5];
|
---|
87 |
|
---|
88 | parOUTabcE[0] = a[minimumVarErrorLoopNumber];
|
---|
89 | parOUTabcE[1] = b[minimumVarErrorLoopNumber];
|
---|
90 | parOUTabcE[2] = c[minimumVarErrorLoopNumber];
|
---|
91 | parOUTabcE[3] = d[minimumVarErrorLoopNumber];
|
---|
92 | parOUTabcE[4] = minimumVarError;
|
---|
93 |
|
---|
94 | return parOUTabcE;
|
---|
95 | // ////////////////////////////////////
|
---|
96 | }
|
---|
97 |
|
---|
98 | static double[] getMinTime(double[] x, double[] time, int spacing,
|
---|
99 | int beginOffset) {
|
---|
100 | // Extract the minimum utility peaks
|
---|
101 | int numberOfBlocks = time.length / spacing;
|
---|
102 | if (numberOfBlocks * spacing > time.length) {
|
---|
103 | numberOfBlocks = numberOfBlocks - 1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | double[] xMin = new double[numberOfBlocks - 1];
|
---|
107 | double[] tMin = new double[numberOfBlocks - 1];
|
---|
108 | int[] indexMin = new int[numberOfBlocks - 1];
|
---|
109 |
|
---|
110 | for (int blockNumber = 0; blockNumber < numberOfBlocks - 1; blockNumber++) {
|
---|
111 | xMin[blockNumber] = 1;
|
---|
112 | indexMin[blockNumber] = 0;
|
---|
113 | for (int indexInBlock = 0; indexInBlock < spacing; indexInBlock++) {
|
---|
114 | if (xMin[blockNumber] > x[blockNumber * spacing + indexInBlock]) {
|
---|
115 | xMin[blockNumber] = x[blockNumber * spacing + indexInBlock];
|
---|
116 | indexMin[blockNumber] = blockNumber * spacing
|
---|
117 | + indexInBlock;
|
---|
118 | tMin[blockNumber] = time[indexMin[blockNumber]];
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | }
|
---|
123 | return tMin;
|
---|
124 | }
|
---|
125 |
|
---|
126 | static double[] getMinEval(double[] x, double[] time, int spacing,
|
---|
127 | int beginOffset) {
|
---|
128 | // Extract the minimum utility peaks
|
---|
129 |
|
---|
130 | int numberOfBlocks = time.length / spacing;
|
---|
131 | if (numberOfBlocks * spacing > time.length) {
|
---|
132 | numberOfBlocks = numberOfBlocks - 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | double[] xMin = new double[numberOfBlocks - 1];
|
---|
136 |
|
---|
137 | for (int blockNumber = 0; blockNumber < numberOfBlocks - 1; blockNumber++) {
|
---|
138 | xMin[blockNumber] = 1;
|
---|
139 | for (int indexInBlock = 0; indexInBlock < spacing; indexInBlock++) {
|
---|
140 | if (xMin[blockNumber] > x[blockNumber * spacing + indexInBlock]) {
|
---|
141 | xMin[blockNumber] = x[blockNumber * spacing + indexInBlock];
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | }
|
---|
146 | return xMin;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static int[] getMinIndex(double[] x, double[] time, int spacing,
|
---|
150 | int beginOffset) {
|
---|
151 | // Extract the minimum utility peaks
|
---|
152 |
|
---|
153 | int numberOfBlocks = time.length / spacing;
|
---|
154 | if (numberOfBlocks * spacing > time.length) {
|
---|
155 | numberOfBlocks = numberOfBlocks - 1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | double[] xMin = new double[numberOfBlocks - 1];
|
---|
159 | int[] indexMin = new int[numberOfBlocks - 1];
|
---|
160 |
|
---|
161 | for (int blockNumber = 0; blockNumber < numberOfBlocks - 1; blockNumber++) {
|
---|
162 | xMin[blockNumber] = 1;
|
---|
163 | indexMin[blockNumber] = 0;
|
---|
164 | for (int indexInBlock = 0; indexInBlock < spacing; indexInBlock++) {
|
---|
165 | if (xMin[blockNumber] > x[blockNumber * spacing + indexInBlock]) {
|
---|
166 | xMin[blockNumber] = x[blockNumber * spacing + indexInBlock];
|
---|
167 | indexMin[blockNumber] = blockNumber * spacing
|
---|
168 | + indexInBlock;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 | return indexMin;
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|