View Javadoc
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.RadioSource;
21  import com.irurueta.navigation.indoor.RadioSourceLocated;
22  import com.irurueta.navigation.indoor.RangingFingerprint;
23  import com.irurueta.navigation.indoor.RangingReading;
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 ranging readings
31   * at 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 ranging readings at an unknown location of different radio sources
34   * whose 2D locations are known.
35   */
36  public class LMedSRobustRangingPositionEstimator2D extends RobustRangingPositionEstimator2D {
37  
38      /**
39       * Constructor.
40       */
41      public LMedSRobustRangingPositionEstimator2D() {
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 LMedSRobustRangingPositionEstimator2D(final List<? extends RadioSourceLocated<Point2D>> sources) {
54          super();
55          init();
56          internalSetSources(sources);
57      }
58  
59      /**
60       * Constructor.
61       *
62       * @param fingerprint fingerprint containing ranging readings at an unknown location
63       *                    for provided located radio sources.
64       * @throws IllegalArgumentException if provided fingerprint is null.
65       */
66      public LMedSRobustRangingPositionEstimator2D(
67              final RangingFingerprint<? extends RadioSource, ? extends RangingReading<?
68                      extends RadioSource>> fingerprint) {
69          super();
70          init();
71          internalSetFingerprint(fingerprint);
72      }
73  
74      /**
75       * Constructor.
76       *
77       * @param sources     located radio sources used for lateration.
78       * @param fingerprint fingerprint containing ranging readings at an unknown location
79       *                    for provided located radio sources.
80       * @throws IllegalArgumentException if either provided sources or fingerprint is null
81       *                                  or the number of provided sources is less than the required minimum.
82       */
83      public LMedSRobustRangingPositionEstimator2D(
84              final List<? extends RadioSourceLocated<Point2D>> sources,
85              final RangingFingerprint<? extends RadioSource, ? extends RangingReading<?
86                      extends RadioSource>> fingerprint) {
87          super();
88          init();
89          internalSetSources(sources);
90          internalSetFingerprint(fingerprint);
91      }
92  
93      /**
94       * Constructor.
95       *
96       * @param listener listener in charge of handling events.
97       */
98      public LMedSRobustRangingPositionEstimator2D(final RobustRangingPositionEstimatorListener<Point2D> listener) {
99          super(listener);
100         init();
101     }
102 
103     /**
104      * Constructor.
105      *
106      * @param sources  located radio sources used for lateration.
107      * @param listener listener in charge of handling events.
108      * @throws IllegalArgumentException if provided sources is null or the number of
109      *                                  provided sources is less than the required minimum.
110      */
111     public LMedSRobustRangingPositionEstimator2D(
112             final List<? extends RadioSourceLocated<Point2D>> sources,
113             final RobustRangingPositionEstimatorListener<Point2D> listener) {
114         super(listener);
115         init();
116         internalSetSources(sources);
117     }
118 
119     /**
120      * Constructor.
121      *
122      * @param fingerprint fingerprint containing ranging readings at an unknown
123      *                    location for provided location radio sources.
124      * @param listener    listener in charge of handling events.
125      * @throws IllegalArgumentException if provided fingerprint is null.
126      */
127     public LMedSRobustRangingPositionEstimator2D(
128             final RangingFingerprint<? extends RadioSource, ? extends RangingReading<?
129                     extends RadioSource>> fingerprint,
130             final RobustRangingPositionEstimatorListener<Point2D> listener) {
131         super(listener);
132         init();
133         internalSetFingerprint(fingerprint);
134     }
135 
136     /**
137      * Constructor.
138      *
139      * @param sources     located radio sources used for lateration.
140      * @param fingerprint fingerprint containing readings at an unknown location for
141      *                    provided located radio sources.
142      * @param listener    listener in charge of handling events.
143      * @throws IllegalArgumentException if either provided sources or fingerprint is
144      *                                  null or the number of provided sources is less than the required minimum.
145      */
146     public LMedSRobustRangingPositionEstimator2D(
147             final List<? extends RadioSourceLocated<Point2D>> sources,
148             final RangingFingerprint<? extends RadioSource, ? extends RangingReading<?
149                     extends RadioSource>> fingerprint,
150             final RobustRangingPositionEstimatorListener<Point2D> listener) {
151         super(listener);
152         init();
153         internalSetSources(sources);
154         internalSetFingerprint(fingerprint);
155     }
156 
157     /**
158      * Returns threshold to be used to keep the algorithm iterating in case that
159      * best estimated threshold using median of residuals is not small enough.
160      * Once a solution is found that generates a threshold below this value, the
161      * algorithm will stop.
162      * The stop threshold can be used to prevent the LMedS algorithm to iterate
163      * too many times in cases where samples have a very similar accuracy.
164      * For instance, in cases where proportion of outliers is very small (close
165      * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
166      * iterate for a long time trying to find the best solution when indeed
167      * there is no need to do that if a reasonable threshold has already been
168      * reached.
169      * Because of this behaviour the stop threshold can be set to a value much
170      * lower than the one typically used in RANSAC, and yet the algorithm could
171      * still produce even smaller thresholds in estimated results.
172      *
173      * @return stop threshold to stop the algorithm prematurely when a certain
174      * accuracy has been reached.
175      */
176     public double getStopThreshold() {
177         return ((LMedSRobustLateration2DSolver) laterationSolver).getStopThreshold();
178     }
179 
180     /**
181      * Sets threshold to be used to keep the algorithm iterating in case that
182      * best estimated threshold using median of residuals is not small enough.
183      * Once a solution is found that generates a threshold below this value,
184      * the algorithm will stop.
185      * The stop threshold can be used to prevent the LMedS algorithm to iterate
186      * too many times in cases where samples have a very similar accuracy.
187      * For instance, in cases where proportion of outliers is very small (close
188      * to 0%), and samples are very accurate (i.e. 1e-6), the algorithm would
189      * iterate for a long time trying to find the best solution when indeed
190      * there is no need to do that if a reasonable threshold has already been
191      * reached.
192      * Because of this behaviour the stop threshold can be set to a value much
193      * lower than the one typically used in RANSAC, and yet the algorithm could
194      * still produce even smaller thresholds in estimated results.
195      *
196      * @param stopThreshold stop threshold to stop the algorithm prematurely
197      *                      when a certain accuracy has been reached.
198      * @throws IllegalArgumentException if provided value is zero or negative.
199      * @throws LockedException          if this solver is locked.
200      */
201     public void setStopThreshold(final double stopThreshold) throws LockedException {
202         ((LMedSRobustLateration2DSolver) laterationSolver).setStopThreshold(stopThreshold);
203     }
204 
205     /**
206      * Returns method being used for robust estimation.
207      *
208      * @return method being used for robust estimation.
209      */
210     @Override
211     public RobustEstimatorMethod getMethod() {
212         return RobustEstimatorMethod.LMEDS;
213     }
214 
215     /**
216      * Initializes robust lateration solver.
217      */
218     private void init() {
219         laterationSolver = new LMedSRobustLateration2DSolver(trilaterationSolverListener);
220     }
221 }