1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.navigation.inertial.calibration.intervals.thresholdfactor;
17
18 import com.irurueta.navigation.LockedException;
19 import com.irurueta.navigation.NavigationException;
20 import com.irurueta.navigation.NotReadyException;
21 import com.irurueta.navigation.inertial.calibration.BodyKinematicsSequence;
22 import com.irurueta.navigation.inertial.calibration.gyroscope.GyroscopeCalibratorMeasurementOrSequenceType;
23 import com.irurueta.navigation.inertial.calibration.gyroscope.GyroscopeNonLinearCalibrator;
24 import com.irurueta.numerical.EvaluationException;
25 import com.irurueta.numerical.InvalidBracketRangeException;
26 import com.irurueta.numerical.NumericalException;
27 import com.irurueta.numerical.SingleDimensionFunctionEvaluatorListener;
28 import com.irurueta.numerical.optimization.BracketedSingleOptimizer;
29 import com.irurueta.numerical.optimization.BrentSingleOptimizer;
30 import com.irurueta.numerical.optimization.OnIterationCompletedListener;
31
32
33
34
35
36
37
38
39
40
41 public class BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer extends
42 GyroscopeIntervalDetectorThresholdFactorOptimizer {
43
44
45
46
47 private BracketedSingleOptimizer mseOptimizer;
48
49
50
51
52 private SingleDimensionFunctionEvaluatorListener optimizerListener;
53
54
55
56
57 private OnIterationCompletedListener iterationCompletedListener;
58
59
60
61
62 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer() {
63 super();
64 initializeOptimizerListeners();
65 try {
66 setMseOptimizer(new BrentSingleOptimizer());
67 } catch (final LockedException ignore) {
68
69 }
70 }
71
72
73
74
75
76
77 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(
78 final GyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource) {
79 super(dataSource);
80 initializeOptimizerListeners();
81 try {
82 setMseOptimizer(new BrentSingleOptimizer());
83 } catch (final LockedException ignore) {
84
85 }
86 }
87
88
89
90
91
92
93
94
95
96 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(final GyroscopeNonLinearCalibrator calibrator) {
97 super(calibrator);
98 initializeOptimizerListeners();
99 try {
100 setMseOptimizer(new BrentSingleOptimizer());
101 } catch (final LockedException ignore) {
102
103 }
104 }
105
106
107
108
109
110
111
112
113
114
115 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(
116 final GyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
117 final GyroscopeNonLinearCalibrator calibrator) {
118 super(dataSource, calibrator);
119 initializeOptimizerListeners();
120 try {
121 setMseOptimizer(new BrentSingleOptimizer());
122 } catch (final LockedException ignore) {
123
124 }
125 }
126
127
128
129
130
131
132
133 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(final BracketedSingleOptimizer mseOptimizer) {
134 initializeOptimizerListeners();
135 try {
136 setMseOptimizer(mseOptimizer);
137 } catch (final LockedException ignore) {
138
139 }
140 }
141
142
143
144
145
146
147
148
149 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(
150 final GyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
151 final BracketedSingleOptimizer mseOptimizer) {
152 super(dataSource);
153 initializeOptimizerListeners();
154 try {
155 setMseOptimizer(mseOptimizer);
156 } catch (final LockedException ignore) {
157
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(
172 final GyroscopeNonLinearCalibrator calibrator, final BracketedSingleOptimizer mseOptimizer) {
173 super(calibrator);
174 initializeOptimizerListeners();
175 try {
176 setMseOptimizer(mseOptimizer);
177 } catch (final LockedException ignore) {
178
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 public BracketedGyroscopeIntervalDetectorThresholdFactorOptimizer(
194 final GyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
195 final GyroscopeNonLinearCalibrator calibrator,
196 final BracketedSingleOptimizer mseOptimizer) {
197 super(dataSource, calibrator);
198 initializeOptimizerListeners();
199 try {
200 setMseOptimizer(mseOptimizer);
201 } catch (final LockedException ignore) {
202
203 }
204 }
205
206
207
208
209
210
211
212
213 public BracketedSingleOptimizer getMseOptimizer() {
214 return mseOptimizer;
215 }
216
217
218
219
220
221
222
223
224
225 public void setMseOptimizer(final BracketedSingleOptimizer optimizer)
226 throws LockedException {
227 if (running) {
228 throw new LockedException();
229 }
230
231 try {
232 if (optimizer != null) {
233 optimizer.setBracket(minThresholdFactor, minThresholdFactor, maxThresholdFactor);
234 optimizer.setListener(optimizerListener);
235 optimizer.setOnIterationCompletedListener(iterationCompletedListener);
236 }
237 mseOptimizer = optimizer;
238 } catch (final com.irurueta.numerical.LockedException e) {
239 throw new LockedException(e);
240 } catch (final InvalidBracketRangeException ignore) {
241
242 }
243 }
244
245
246
247
248
249
250 @Override
251 public boolean isReady() {
252 return super.isReady() && mseOptimizer != null;
253 }
254
255
256
257
258
259
260
261
262
263
264
265
266 @Override
267 public double optimize() throws NotReadyException, LockedException,
268 IntervalDetectorThresholdFactorOptimizerException {
269 if (running) {
270 throw new LockedException();
271 }
272
273 if (!isReady()) {
274 throw new NotReadyException();
275 }
276
277 try {
278 running = true;
279
280 initProgress();
281
282 if (listener != null) {
283 listener.onOptimizeStart(this);
284 }
285
286 minMse = Double.MAX_VALUE;
287 mseOptimizer.minimize();
288
289 if (listener != null) {
290 listener.onOptimizeEnd(this);
291 }
292
293 return optimalThresholdFactor;
294 } catch (final NumericalException e) {
295 throw new IntervalDetectorThresholdFactorOptimizerException(e);
296 } finally {
297 running = false;
298 }
299 }
300
301
302
303
304 private void initializeOptimizerListeners() {
305 optimizerListener = point -> {
306 try {
307 return evaluateForThresholdFactor(point);
308 } catch (final NavigationException e) {
309 throw new EvaluationException(e);
310 }
311 };
312
313 iterationCompletedListener = (optimizer, iteration, maxIterations) -> {
314 if (maxIterations == null) {
315 return;
316 }
317
318 progress = (float) iteration / (float) maxIterations;
319 checkAndNotifyProgress();
320 };
321 }
322 }