1 /*
2 * Copyright (C) 2019 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.navigation.indoor.position;
17
18 import com.irurueta.geometry.Point2D;
19 import com.irurueta.navigation.LockedException;
20 import com.irurueta.navigation.indoor.Fingerprint;
21 import com.irurueta.navigation.indoor.RadioSource;
22 import com.irurueta.navigation.indoor.RadioSourceLocated;
23 import com.irurueta.navigation.indoor.Reading;
24 import com.irurueta.navigation.lateration.LMedSRobustLateration2DSolver;
25 import com.irurueta.numerical.robust.RobustEstimatorMethod;
26
27 import java.util.List;
28
29 /**
30 * Robustly estimates 2D position using located radio sources and their readings at
31 * unknown locations and using LMedS algorithm to discard outliers.
32 * This kind of estimator can be used to robustly determine the 2D position of a given
33 * device by getting readings at an unknown location of different radio sources whose
34 * 2D locations are known.
35 */
36 public class LMedSRobustMixedPositionEstimator2D extends RobustMixedPositionEstimator2D {
37
38 /**
39 * Constructor.
40 */
41 public LMedSRobustMixedPositionEstimator2D() {
42 super();
43 init();
44 }
45
46 /**
47 * Constructor.
48 *
49 * @param sources located radio sources used for lateration.
50 * @throws IllegalArgumentException if provided sources is null or the number of
51 * provided sources is less than the required minimum.
52 */
53 public LMedSRobustMixedPositionEstimator2D(final List<? extends RadioSourceLocated<Point2D>> sources) {
54 super();
55 init();
56 internalSetSources(sources);
57 }
58
59 /**
60 * Constructor.
61 *
62 * @param fingerprint fingerprint containing readings at an unknown
63 * location for provided located radio sources.
64 * @throws IllegalArgumentException if provided fingerprint is null.
65 */
66 public LMedSRobustMixedPositionEstimator2D(
67 final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint) {
68 super();
69 init();
70 internalSetFingerprint(fingerprint);
71 }
72
73 /**
74 * Constructor.
75 *
76 * @param sources located radio sources used for lateration.
77 * @param fingerprint fingerprint containing readings at an unknown
78 * location for provided located radio sources.
79 * @throws IllegalArgumentException if either provided sources or fingerprint is null
80 * or the number of provided sources is less than the required minimum.
81 */
82 public LMedSRobustMixedPositionEstimator2D(
83 final List<? extends RadioSourceLocated<Point2D>> sources,
84 final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint) {
85 super();
86 init();
87 internalSetSources(sources);
88 internalSetFingerprint(fingerprint);
89 }
90
91 /**
92 * Constructor.
93 *
94 * @param listener listener in charge of handling events.
95 */
96 public LMedSRobustMixedPositionEstimator2D(final RobustMixedPositionEstimatorListener<Point2D> listener) {
97 super(listener);
98 init();
99 }
100
101 /**
102 * Constructor.
103 *
104 * @param sources located radio sources used for lateration.
105 * @param listener listener in charge of handling events.
106 * @throws IllegalArgumentException if provided sources is null or the number of
107 * provided sources is less than the required minimum.
108 */
109 public LMedSRobustMixedPositionEstimator2D(
110 final List<? extends RadioSourceLocated<Point2D>> sources,
111 final RobustMixedPositionEstimatorListener<Point2D> listener) {
112 super(listener);
113 init();
114 internalSetSources(sources);
115 }
116
117 /**
118 * Constructor.
119 *
120 * @param fingerprint fingerprint containing readings at an unknown
121 * location for provided location radio sources.
122 * @param listener listener in charge of handling events.
123 * @throws IllegalArgumentException if provided fingerprint is null.
124 */
125 public LMedSRobustMixedPositionEstimator2D(
126 final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint,
127 final RobustMixedPositionEstimatorListener<Point2D> listener) {
128 super(listener);
129 init();
130 internalSetFingerprint(fingerprint);
131 }
132
133 /**
134 * Constructor.
135 *
136 * @param sources located radio sources used for lateration.
137 * @param fingerprint fingerprint containing readings at an unknown
138 * location for provided located radio sources.
139 * @param listener listener in charge of handling events.
140 * @throws IllegalArgumentException if either provided sources or fingerprint is
141 * null or the number of provided sources is less than the required minimum.
142 */
143 public LMedSRobustMixedPositionEstimator2D(
144 final List<? extends RadioSourceLocated<Point2D>> sources,
145 final Fingerprint<? extends RadioSource, ? extends Reading<? extends RadioSource>> fingerprint,
146 final RobustMixedPositionEstimatorListener<Point2D> listener) {
147 super(listener);
148 init();
149 internalSetSources(sources);
150 internalSetFingerprint(fingerprint);
151 }
152
153 /**
154 * Returns threshold to be used to keep the algorithm iterating in case that
155 * best estimated threshold using median of residuals is not small enough.
156 * Once a solution is found that generates a threshold below this value, the
157 * algorithm will stop.
158 * The stop threshold can be used to prevent the LMedS algorithm to iterate
159 * too many times in cases where samples have a very similar accuracy.
160 * For instance, in cases where proportion of outliers is very small (close
161 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
162 * iterate for a long time trying to find the best solution when indeed
163 * there is no need to do that if a reasonable threshold has already been
164 * reached.
165 * Because of this behaviour the stop threshold can be set to a value much
166 * lower than the one typically used in RANSAC, and yet the algorithm could
167 * still produce even smaller thresholds in estimated results.
168 *
169 * @return stop threshold to stop the algorithm prematurely when a certain
170 * accuracy has been reached.
171 */
172 public double getStopThreshold() {
173 return ((LMedSRobustLateration2DSolver) laterationSolver).getStopThreshold();
174 }
175
176 /**
177 * Sets threshold to be used to keep the algorithm iterating in case that
178 * best estimated threshold using median of residuals is not small enough.
179 * Once a solution is found that generates a threshold below this value,
180 * the algorithm will stop.
181 * The stop threshold can be used to prevent the LMedS algorithm to iterate
182 * too many times in cases where samples have a very similar accuracy.
183 * For instance, in cases where proportion of outliers is very small (close
184 * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
185 * iterate for a long time trying to find the best solution when indeed
186 * there is no need to do that if a reasonable threshold has already been
187 * reached.
188 * Because of this behaviour the stop threshold can be set to a value much
189 * lower than the one typically used in RANSAC, and yet the algorithm could
190 * still produce even smaller thresholds in estimated results.
191 *
192 * @param stopThreshold stop threshold to stop the algorithm prematurely
193 * when a certain accuracy has been reached.
194 * @throws IllegalArgumentException if provided value is zero or negative.
195 * @throws LockedException if this solver is locked.
196 */
197 public void setStopThreshold(final double stopThreshold) throws LockedException {
198 ((LMedSRobustLateration2DSolver) laterationSolver).setStopThreshold(stopThreshold);
199 }
200
201 /**
202 * Returns method being used for robust estimation.
203 *
204 * @return method being used for robust estimation.
205 */
206 @Override
207 public RobustEstimatorMethod getMethod() {
208 return RobustEstimatorMethod.LMEDS;
209 }
210
211 /**
212 * Initializes robust lateration solver.
213 */
214 private void init() {
215 laterationSolver = new LMedSRobustLateration2DSolver(trilaterationSolverListener);
216 }
217 }