1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#![cfg_attr(not(test), no_std)]
use heapless::{FnvIndexMap, Vec};
use nalgebra::{ComplexField, Matrix3, RealField, RowVector3, SMatrix, SVector, Unit, Vector3};
use num_traits::float::TotalOrder;
use rand::RngCore;
#[derive(Debug, PartialEq)]
pub enum LocationError {
/// Not enough measurements or known locations to perform the calculation.
InsufficientData,
/// A node ID from the measurement data was not found in the known locations map.
NodeNotFound,
/// The iterative solver failed to converge or find a solution.
SolverFailed,
/// No real eigenvalue was found, which is necessary for the eigenvalue-based method.
NoRealEigenvalue,
}
/// A solver for 3D localization problems.
///
/// This struct provides methods to solve for a 3D position based on
/// Time Difference of Arrival (TDOA) or trilateration data.
pub struct LocationSolver<'a, NODE, FLOAT, const MAXNNODES: usize> {
/// A map of known node locations, where `NODE` is a unique identifier.
known_locations: &'a FnvIndexMap<NODE, Vector3<FLOAT>, MAXNNODES>,
/// The tolerance used to determine when an iterative solver has converged.
solving_tolerance: FLOAT,
}
#[allow(non_snake_case)]
impl<'a, NODE, FLOAT, const MAXNNODES: usize> LocationSolver<'a, NODE, FLOAT, MAXNNODES>
where
NODE: core::cmp::Eq,
NODE: core::fmt::Debug,
NODE: core::hash::Hash,
NODE: Copy,
FLOAT: num_traits::float::Float,
FLOAT: RealField,
FLOAT: simba::scalar::SubsetOf<f64>,
FLOAT: TotalOrder,
{
/// Creates a new `LocationSolver`.
///
/// # Arguments
///
/// * `known_locations` - A reference to a map of known node positions.
/// * `solving_tolerance` - The tolerance for determining convergence of iterative solvers.
pub fn new(
known_locations: &FnvIndexMap<NODE, Vector3<FLOAT>, MAXNNODES>,
solving_tolerance: FLOAT,
) -> LocationSolver<NODE, FLOAT, MAXNNODES> {
LocationSolver {
known_locations,
solving_tolerance,
}
}
/// Solves for the location using Time Difference of Arrival (TDOA) data.
///
/// This method uses the Levenberg-Marquardt algorithm to iteratively find the position
/// that best fits the given TDOA measurements.
///
/// # Arguments
///
/// * `tdoa_distance_infos` - A map where the key is a pair of nodes `(i, j)` and the value
/// is the difference in distance `d_i - d_j`. Location of `i` and `j` must be known.
/// * `initial_guess` - An optional initial guess for the location. If not provided,
/// the center of the known anchor locations is used.
///
/// # Returns
///
/// A `Result` containing the calculated `Vector3<FLOAT>` position, or a `LocationError` if
/// solving fails.
pub fn tdoa(
&mut self,
tdoa_distance_infos: FnvIndexMap<(NODE, NODE), FLOAT, MAXNNODES>,
initial_guess: Option<Vector3<FLOAT>>,
) -> Result<Vector3<FLOAT>, LocationError> {
if tdoa_distance_infos.len() < 3 || self.known_locations.len() < 4 {
return Err(LocationError::InsufficientData);
}
// If no initial guess is provided, initialize x at the center of the known anchor locations.
let mut x = initial_guess.unwrap_or_else(|| {
let num_known = self.known_locations.len();
if num_known == 0 {
Vector3::zeros()
} else {
let sum_of_positions: Vector3<FLOAT> = self.known_locations.values().sum();
sum_of_positions / FLOAT::from_usize(num_known).unwrap()
}
});
let lambda = FLOAT::from_f64(1e-3).unwrap(); // Levenberg-Marquardt damping factor
let max_iterations: usize = 20;
let len_tdoa_distance_infos = tdoa_distance_infos.len();
for _ in 0..max_iterations {
// Populate Jacobian matrix
let mut residuals = SVector::<FLOAT, MAXNNODES>::zeros();
let mut jacobian = SMatrix::<FLOAT, MAXNNODES, 3>::zeros();
for (k, (&(i, j), &delta_d_ij)) in tdoa_distance_infos.iter().enumerate() {
let Pi = self
.known_locations
.get(&i)
.ok_or(LocationError::NodeNotFound)?;
let Pj = self
.known_locations
.get(&j)
.ok_or(LocationError::NodeNotFound)?;
let vi = x - *Pi;
let di = vi.norm();
let vj = x - *Pj;
let dj = vj.norm();
residuals[k] = di - dj - delta_d_ij;
// ∂r/∂x = (x - Pi)/‖x - Pi‖ - (x - Pj)/‖x - Pj‖
let grad = vi / di - vj / dj;
jacobian.row_mut(k).copy_from(&grad.transpose());
}
// Manually compute Jáµ€J and Jáµ€r to avoid allocation (or too many operations)
let mut jtj = Matrix3::<FLOAT>::zeros(); // Jáµ€J will be a 3x3 matrix
let mut jtr = Vector3::<FLOAT>::zeros(); // Jáµ€r will be a 3x1 vector
for k in 0..len_tdoa_distance_infos {
let row_k: RowVector3<FLOAT> = jacobian.row(k).into(); // Get the k-th row as a RowVector3 (1x3)
let res_k: FLOAT = residuals[k]; // Get the k-th residual (scalar)
// Accumulate Jáµ€J: (3x1) * (1x3) = 3x3 matrix.
// This multiplication is SIMD-optimized by nalgebra.
jtj += row_k.transpose() * row_k;
// Accumulate Jáµ€r: (3x1) * scalar = 3x1 vector.
// This multiplication is SIMD-optimized by nalgebra.
jtr += row_k.transpose() * res_k;
}
// Solve Levenberg-Marquard iteration
let lhs = jtj + Matrix3::identity() * lambda;
let rhs = -jtr;
// Solve (JᵀJ + λI) δ = -Jᵀr
let delta = lhs.lu().solve(&rhs).ok_or(LocationError::SolverFailed)?;
x += delta;
if delta.norm() < self.solving_tolerance {
break;
}
}
Ok(x)
}
/// Solves for the location using trilateration data with a fast iterative method.
///
/// This method uses the Levenberg-Marquardt algorithm to iteratively find the position
/// that best fits the given distance measurements from known locations.
///
/// # Arguments
///
/// * `trilateration_infos` - A map where the key `i` is a node and the value is the
/// measured distance to that node. Location of node `i` must be known.
/// * `initial_guess` - An optional initial guess for the location. If not provided,
/// the center of the known anchor locations is used.
///
/// # Returns
///
/// A `Result` containing the calculated `Vector3<FLOAT>` position, or a `LocationError` if
/// solving fails.
pub fn trilateration_fast(
&mut self,
trilateration_infos: FnvIndexMap<NODE, FLOAT, MAXNNODES>,
initial_guess: Option<Vector3<FLOAT>>,
) -> Result<Vector3<FLOAT>, LocationError> {
if trilateration_infos.len() < 4 || self.known_locations.len() < 4 {
return Err(LocationError::InsufficientData);
}
// If no initial guess is provided, initialize x at the center of the known anchor locations.
let mut x = initial_guess.unwrap_or_else(|| {
let num_known = self.known_locations.len();
if num_known == 0 {
Vector3::zeros()
} else {
let sum_of_positions: Vector3<FLOAT> = self.known_locations.values().sum();
sum_of_positions / FLOAT::from_usize(num_known).unwrap()
}
});
let lambda = FLOAT::from_f64(1e-3).unwrap(); // Levenberg-Marquardt damping
let max_iterations: usize = 50;
let len_trilateration_infos = trilateration_infos.len();
for _ in 0..max_iterations {
// Prepare residuals and Jacobian
let mut residuals = SVector::<FLOAT, MAXNNODES>::zeros();
let mut jacobian = SMatrix::<FLOAT, MAXNNODES, 3>::zeros();
for (k, (&node, &measured_distance)) in trilateration_infos.iter().enumerate() {
let anchor = self
.known_locations
.get(&node)
.ok_or(LocationError::NodeNotFound)?;
let vec = x - *anchor;
let dist = vec.norm();
residuals[k] = dist - measured_distance;
// ∂r/∂x = (x - anchor) / ‖x - anchor‖
jacobian.row_mut(k).copy_from(&(vec / dist).transpose());
}
// Manually compute Jáµ€J and Jáµ€r to avoid allocation (or too many operations)
let mut jtj = Matrix3::<FLOAT>::zeros(); // Jáµ€J will be a 3x3 matrix
let mut jtr = Vector3::<FLOAT>::zeros(); // Jáµ€r will be a 3x1 vector
for k in 0..len_trilateration_infos {
let row_k: RowVector3<FLOAT> = jacobian.row(k).into(); // Get the k-th row as a RowVector3 (1x3)
let res_k: FLOAT = residuals[k]; // Get the k-th residual (scalar)
// Accumulate Jáµ€J: (3x1) * (1x3) = 3x3 matrix.
// This multiplication is SIMD-optimized by nalgebra.
jtj += row_k.transpose() * row_k;
// Accumulate Jáµ€r: (3x1) * scalar = 3x1 vector.
// This multiplication is SIMD-optimized by nalgebra.
jtr += row_k.transpose() * res_k;
}
// Solve Levenberg-Marquard iteration
let lhs = jtj + Matrix3::identity() * lambda;
let rhs = -jtr;
// Solve (JᵀJ + λI) δ = -Jᵀr
let delta = lhs.lu().solve(&rhs).ok_or(LocationError::SolverFailed)?;
x += delta;
if delta.norm() < self.solving_tolerance {
break;
}
}
Ok(x)
}
/// Solves for the location using trilateration data with an eigenvalue-based method.
///
/// This method is based on the paper "Optimal Trilateration is an Eigenvalue Problem"
/// and provides a non-iterative, closed-form solution.
/// See: 10.1109/icassp.2019.8683355
///
/// # Arguments
///
/// * `trilateration_infos` - A map where the key is a node and the value is the
/// measured distance to that node.
/// * `rng` - A random number generator for the eigenvector calculation.
///
/// # Returns
///
/// A `Result` containing the calculated `Vector3<FLOAT>` position, or a `LocationError` if
/// solving fails.
pub fn trilateration<RNG>(
&mut self,
trilateration_infos: FnvIndexMap<NODE, FLOAT, MAXNNODES>,
rng: RNG,
) -> Result<Vector3<FLOAT>, LocationError>
where
RNG: RngCore,
{
// check for 3D sizes
if trilateration_infos.len() < 4 || self.known_locations.len() < 4 {
return Err(LocationError::InsufficientData);
}
// From here follow 10.1109/icassp.2019.8683355, Chapter 1.4, 2.1, 2.2
// get all location, distance^2, weight pairs (weight as in (6) of the paper)
let mut data: Vec<(Vector3<FLOAT>, FLOAT, FLOAT), MAXNNODES> = Vec::new();
for (node, distance) in trilateration_infos {
let _ = data.push((
*self
.known_locations
.get(&node)
.ok_or(LocationError::NodeNotFound)?,
ComplexField::powi(distance, 2),
FLOAT::one() / FLOAT::from(4.0).unwrap() * ComplexField::powi(distance, 2),
));
}
// calculate t as translation value
let mut w_sum = FLOAT::zero();
let mut t: Vector3<FLOAT> = Vector3::zeros();
for (p_i, _, w_i) in &data {
w_sum += *w_i;
t += p_i.map(|x| x * *w_i);
}
t = -(t / w_sum);
// Compute matrix A and b
let mut A: Matrix3<FLOAT> = Matrix3::zeros();
let mut b: Vector3<FLOAT> = Vector3::zeros();
for (p, d_pow2, mut w) in data.clone() {
// normalize w
w /= w_sum;
// translate s
let s: Vector3<FLOAT> = p + t;
// Compute w*((s' * s) - d^2) which results in a scalar (1x1 matrix)
let w_s_ts_minus_dpow2 = w * ((s.transpose() * s)[(0, 0)] - d_pow2);
// Update matrix A (remember, s_ts is a 1x1 matrix, so extract its scalar value with `[(0, 0)]`)
A += Matrix3::from_diagonal_element(w_s_ts_minus_dpow2)
+ (s * s.transpose()).map(|x| w * FLOAT::from(2.0).unwrap() * x);
// Update vector b
b += s.map(|x| x * -w_s_ts_minus_dpow2);
}
// Compute matrix D, containing eigenvalues of A
let A_decomp = A.symmetric_eigen();
let U = A_decomp.eigenvectors;
let D = Matrix3::from_diagonal(&A_decomp.eigenvalues);
// Transform b
b = U.transpose() * b;
// Generate M
let mut M: SMatrix<FLOAT, 7, 7> = SMatrix::zeros(); // Initialize a 9x9 matrix of zeros
// Fill in the blocks
M.view_mut((0, 0), (3, 3)).copy_from(&-D); // -D
M.view_mut((0, 3), (3, 3))
.copy_from(&-Matrix3::from_diagonal(&b));
M.view_mut((3, 3), (3, 3)).copy_from(&-D); // -D
M.view_mut((3, 6), (3, 1)).copy_from(&-b); // -b
M.view_mut((6, 0), (1, 3))
.copy_from(&RowVector3::repeat(FLOAT::one())); // identity
// Get eigenvectors of M corresponding to largest real eigenvalue,
// APPARENTLY: MAX EIGENVALUE CORRESPONDS TO THE OPTIMUM SOLUTION.. WHY? I DON'T KNOW!
let max_real_eigenvalue = M
.complex_eigenvalues()
.into_iter()
.filter(|lambda| lambda.im == FLOAT::zero())
.max_by(|a, b| a.re.total_cmp(&b.re))
.ok_or(LocationError::NoRealEigenvalue)?
.re;
let mut x: SVector<FLOAT, 7> = self.get_ev(&M, max_real_eigenvalue, rng)?;
// scale them by last value,
x /= x[(6, 0)];
// get elements 3:5
let mut x = x.remove_row(6).remove_row(0).remove_row(0).remove_row(0);
// transform to U*x - t
x = U * x - t;
// Return computed solution
Ok(x)
}
/// Computes the eigenvector for a given eigenvalue using inverse iteration.
///
/// This is a helper function for `trilateration` where the eigenvector of the
/// largest eigenvalue is searched.
///
/// # Arguments
///
/// * `A` - The matrix for which to find the eigenvector.
/// * `lambda` - The eigenvalue corresponding to the desired eigenvector.
/// * `rng` - A random number generator to initialize the process.
///
/// # Returns
///
/// A `Result` containing the calculated eigenvector, or `Err(())` if solving fails.
#[allow(non_snake_case)]
fn get_ev<RNG>(
&mut self,
A: &SMatrix<FLOAT, 7, 7>,
lambda: FLOAT,
mut rng: RNG,
) -> Result<SVector<FLOAT, 7>, LocationError>
where
RNG: RngCore,
{
// create 1e-3 deviation of lambda, s.t. matrix to be inversed is non-singular (works better with f64)
let mu = lambda - FLOAT::from(1e-3).unwrap();
// Generate matrix M
let M = A - SMatrix::<FLOAT, 7, 7>::from_diagonal_element(mu);
// Get LU transform to solve M^-1 * b
let M = M.lu();
let mut b = Unit::new_normalize(
SVector::<FLOAT, 7>::zeros()
.map(|_| FLOAT::from(rng.next_u32()).unwrap() / FLOAT::from(u32::MAX).unwrap()),
);
// Should converge pretty fast, anyway restrict iterations by 1000 - most of the time it will take less time
for _ in 0..1000 {
let b_new: Unit<SVector<FLOAT, 7>> =
Unit::new_normalize(M.solve(&b).ok_or(LocationError::SolverFailed)?);
if (*b_new - *b).norm() < self.solving_tolerance {
return Ok(*b);
}
b = b_new;
}
Ok(*b)
}
}