apex-solver 1.3.0

High-performance nonlinear least squares optimization with Lie group support for SLAM and bundle adjustment
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Integration tests for Apex Solver
//!
//! These tests verify end-to-end optimization performance on real G2O datasets.
//! They ensure that the optimizers converge correctly and produce expected results.
//!
//! # Test Coverage
//!
//! - **SE3 (3D) Datasets**: sphere2500.g2o, parking-garage.g2o
//! - **SE2 (2D) Datasets**: intel.g2o, ring.g2o
//!
//! # Metrics Verified
//!
//! Each test verifies:
//! - Number of vertices and edges match expected values
//! - Optimization converges successfully
//! - Cost improvement exceeds threshold (>85%)
//! - Execution time is reasonable
//! - Iterations don't hit maximum limit
//! - Final cost is finite (not NaN or Inf)
//!
//! # Running Tests
//!
//! ```bash
//! # Run fast tests only (ring, intel)
//! cargo test
//!
//! # Run all tests including slow ones (sphere2500, parking-garage)
//! cargo test -- --include-ignored
//!
//! # Run only slow tests
//! cargo test -- --ignored
//! ```

use apex_io::{G2oLoader, GraphLoader, ODOMETRY_DATA_DIR_2D, ODOMETRY_DATA_DIR_3D};
use apex_solver::JacobianMode;
use apex_solver::ManifoldType;
use apex_solver::core::loss_functions::HuberLoss;
use apex_solver::core::problem::Problem;
use apex_solver::factors::{BetweenFactor, PriorFactor};
use apex_solver::optimizer::OptimizationStatus;
use apex_solver::optimizer::levenberg_marquardt::{LevenbergMarquardt, LevenbergMarquardtConfig};
use nalgebra::dvector;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Test result capturing all optimization metrics
#[derive(Debug)]
struct TestResult {
    vertices: usize,
    edges: usize,
    final_cost: f64,
    improvement_pct: f64,
    iterations: usize,
    elapsed_time: Duration,
    status: OptimizationStatus,
    success: bool,
}

/// Run SE3 (3D) optimization on a dataset
///
/// # Arguments
///
/// * `dataset_name` - Name of the dataset file (without .g2o extension)
/// * `max_iterations` - Maximum number of optimization iterations
/// * `use_prior` - Whether to add a prior factor on the first vertex
///
/// # Returns
///
/// `TestResult` containing all optimization metrics
fn run_se3_optimization(
    dataset_name: &str,
    max_iterations: usize,
    use_prior: bool,
) -> Result<TestResult, Box<dyn std::error::Error>> {
    // Load the G2O graph file (from workspace root, relative to crate)
    let dataset_path = format!("{}/{}.g2o", ODOMETRY_DATA_DIR_3D, dataset_name);
    let graph = G2oLoader::load(&dataset_path)?;

    let num_vertices = graph.vertices_se3.len();
    let num_edges = graph.edges_se3.len();

    // Create optimization problem
    let mut problem = Problem::new(JacobianMode::Sparse);
    let mut initial_values = HashMap::new();

    // Add SE3 vertices as variables
    let mut vertex_ids: Vec<_> = graph.vertices_se3.keys().cloned().collect();
    vertex_ids.sort();

    for &id in &vertex_ids {
        if let Some(vertex) = graph.vertices_se3.get(&id) {
            let var_name = format!("x{}", id);
            let quat = vertex.pose.rotation_quaternion();
            let trans = vertex.pose.translation();
            let se3_data = dvector![trans.x, trans.y, trans.z, quat.w, quat.i, quat.j, quat.k];
            initial_values.insert(var_name, (ManifoldType::SE3, se3_data));
        }
    }

    // Add prior factor if requested (for gauge freedom)
    if use_prior
        && let Some(&first_id) = vertex_ids.first()
        && let Some(first_vertex) = graph.vertices_se3.get(&first_id)
    {
        let var_name = format!("x{}", first_id);
        let quat = first_vertex.pose.rotation_quaternion();
        let trans = first_vertex.pose.translation();
        let prior_value = dvector![trans.x, trans.y, trans.z, quat.w, quat.i, quat.j, quat.k];

        let prior_factor = PriorFactor {
            data: prior_value.clone(),
        };
        let huber_loss = HuberLoss::new(1.0)?;
        problem.add_residual_block(
            &[&var_name],
            Box::new(prior_factor),
            Some(Box::new(huber_loss)),
        );
    }

    // Add SE3 between factors
    for edge in &graph.edges_se3 {
        let id0 = format!("x{}", edge.from);
        let id1 = format!("x{}", edge.to);
        let relative_pose = edge.measurement.clone();

        let between_factor = BetweenFactor::new(relative_pose);
        problem.add_residual_block(&[&id0, &id1], Box::new(between_factor), None);
    }

    // Configure and run Levenberg-Marquardt optimizer
    let config = LevenbergMarquardtConfig::new()
        .with_max_iterations(max_iterations)
        .with_cost_tolerance(1e-4)
        .with_parameter_tolerance(1e-4)
        .with_damping(1e-3);

    let mut solver = LevenbergMarquardt::with_config(config);

    let start_time = Instant::now();
    let result = solver.optimize(&problem, &initial_values)?;
    let elapsed_time = start_time.elapsed();

    // Calculate improvement percentage
    let improvement_pct = if result.initial_cost > 0.0 {
        ((result.initial_cost - result.final_cost) / result.initial_cost) * 100.0
    } else {
        0.0
    };

    let success = matches!(
        result.status,
        OptimizationStatus::Converged
            | OptimizationStatus::CostToleranceReached
            | OptimizationStatus::ParameterToleranceReached
            | OptimizationStatus::GradientToleranceReached
    );

    let test_result = TestResult {
        vertices: num_vertices,
        edges: num_edges,
        final_cost: result.final_cost,
        improvement_pct,
        iterations: result.iterations,
        elapsed_time,
        status: result.status.clone(),
        success,
    };

    Ok(test_result)
}

/// Run SE2 (2D) optimization on a dataset
///
/// # Arguments
///
/// * `dataset_name` - Name of the dataset file (without .g2o extension)
/// * `max_iterations` - Maximum number of optimization iterations
/// * `use_prior` - Whether to add a prior factor on the first vertex
///
/// # Returns
///
/// `TestResult` containing all optimization metrics
fn run_se2_optimization(
    dataset_name: &str,
    max_iterations: usize,
    use_prior: bool,
) -> Result<TestResult, Box<dyn std::error::Error>> {
    // Load the G2O graph file (from workspace root, relative to crate)
    let dataset_path = format!("{}/{}.g2o", ODOMETRY_DATA_DIR_2D, dataset_name);
    let graph = G2oLoader::load(&dataset_path)?;

    let num_vertices = graph.vertices_se2.len();
    let num_edges = graph.edges_se2.len();

    // Create optimization problem
    let mut problem = Problem::new(JacobianMode::Sparse);
    let mut initial_values = HashMap::new();

    // Add SE2 vertices as variables
    let mut vertex_ids: Vec<_> = graph.vertices_se2.keys().cloned().collect();
    vertex_ids.sort();

    for &id in &vertex_ids {
        if let Some(vertex) = graph.vertices_se2.get(&id) {
            let var_name = format!("x{}", id);
            let pose = &vertex.pose;
            let se2_data = dvector![pose.x(), pose.y(), pose.angle()];
            initial_values.insert(var_name, (ManifoldType::SE2, se2_data));
        }
    }

    // Add prior factor if requested (for gauge freedom)
    if use_prior
        && let Some(&first_id) = vertex_ids.first()
        && let Some(first_vertex) = graph.vertices_se2.get(&first_id)
    {
        let var_name = format!("x{}", first_id);
        let pose = &first_vertex.pose;
        let prior_value = dvector![pose.x(), pose.y(), pose.angle()];

        let prior_factor = PriorFactor {
            data: prior_value.clone(),
        };
        let huber_loss = HuberLoss::new(1.0)?;
        problem.add_residual_block(
            &[&var_name],
            Box::new(prior_factor),
            Some(Box::new(huber_loss)),
        );
    }

    // Add SE2 between factors
    for edge in &graph.edges_se2 {
        let id0 = format!("x{}", edge.from);
        let id1 = format!("x{}", edge.to);

        let between_factor = BetweenFactor::new(edge.measurement.clone());
        problem.add_residual_block(&[&id0, &id1], Box::new(between_factor), None);
    }

    // Configure and run Levenberg-Marquardt optimizer
    let config = LevenbergMarquardtConfig::new()
        .with_max_iterations(max_iterations)
        .with_cost_tolerance(1e-4)
        .with_parameter_tolerance(1e-4)
        .with_damping(1e-3);

    let mut solver = LevenbergMarquardt::with_config(config);

    let start_time = Instant::now();
    let result = solver.optimize(&problem, &initial_values)?;
    let elapsed_time = start_time.elapsed();

    // Calculate improvement percentage
    let improvement_pct = if result.initial_cost > 0.0 {
        ((result.initial_cost - result.final_cost) / result.initial_cost) * 100.0
    } else {
        0.0
    };

    let success = matches!(
        result.status,
        OptimizationStatus::Converged
            | OptimizationStatus::CostToleranceReached
            | OptimizationStatus::ParameterToleranceReached
            | OptimizationStatus::GradientToleranceReached
    );

    let test_result = TestResult {
        vertices: num_vertices,
        edges: num_edges,
        final_cost: result.final_cost,
        improvement_pct,
        iterations: result.iterations,
        elapsed_time,
        status: result.status.clone(),
        success,
    };

    Ok(test_result)
}

// ============================================================================
// SE2 (2D) Integration Tests
// ============================================================================

/// Test optimization on intel.g2o (medium SE2 dataset)
///
/// The Intel Research Lab dataset has 1,228 vertices and 1,483 edges.
/// This is a real-world indoor SLAM dataset.
#[test]
fn test_intel_se2_converges() -> Result<(), Box<dyn std::error::Error>> {
    apex_io::ensure_odometry_dataset("intel")?;
    let result = run_se2_optimization("intel", 100, true)?;

    // Verify dataset size
    assert_eq!(
        result.vertices, 1228,
        "intel.g2o should have 1228 vertices, got {}",
        result.vertices
    );
    assert_eq!(
        result.edges, 1483,
        "intel.g2o should have 1483 edges, got {}",
        result.edges
    );

    // Verify convergence
    assert!(
        result.success,
        "Optimization did not converge. Status: {:?}, Iterations: {}, Final cost: {}",
        result.status, result.iterations, result.final_cost
    );

    // Verify cost improvement
    assert!(
        result.improvement_pct > 85.0,
        "Cost improvement too low: {:.2}% (expected >85%)",
        result.improvement_pct
    );

    // Verify iterations
    assert!(
        result.iterations < 100,
        "Hit maximum iterations: {}",
        result.iterations
    );

    // Verify numerical stability
    assert!(
        result.final_cost.is_finite(),
        "Final cost is not finite: {}",
        result.final_cost
    );

    // Verify performance (should complete in <5 seconds)
    assert!(
        result.elapsed_time.as_secs() < 5,
        "Optimization took too long: {:?}",
        result.elapsed_time
    );

    Ok(())
}

// ============================================================================
// SE3 (3D) Integration Tests
// ============================================================================

/// Test optimization on sphere2500.g2o (medium SE3 dataset)
///
///
/// The sphere2500 dataset has 2,500 vertices and 4,949 edges, representing
/// a spherical topology commonly used for benchmarking.
#[test]
fn test_sphere2500_se3_converges() -> Result<(), Box<dyn std::error::Error>> {
    apex_io::ensure_odometry_dataset("sphere2500")?;
    let result = run_se3_optimization("sphere2500", 100, true)?;

    // Verify dataset size
    assert_eq!(
        result.vertices, 2500,
        "sphere2500.g2o should have 2500 vertices, got {}",
        result.vertices
    );
    assert_eq!(
        result.edges, 4949,
        "sphere2500.g2o should have 4949 edges, got {}",
        result.edges
    );

    // Verify convergence
    assert!(
        result.success,
        "Optimization did not converge. Status: {:?}, Iterations: {}, Final cost: {}",
        result.status, result.iterations, result.final_cost
    );

    // Verify cost improvement
    assert!(
        result.improvement_pct > 99.0,
        "Cost improvement too low: {:.2}% (expected >99.0%)",
        result.improvement_pct
    );

    // Verify iterations
    assert!(
        result.iterations < 20,
        "Should complete in < 20 iterations: {}",
        result.iterations
    );

    // Verify numerical stability
    assert!(
        result.final_cost.is_finite(),
        "Final cost is not finite: {}",
        result.final_cost
    );

    // Verify performance (should complete in < 30 seconds)
    assert!(
        result.elapsed_time.as_secs() < 30,
        "Optimization took too long: {:?}",
        result.elapsed_time
    );

    Ok(())
}

/// Test optimization on parking-garage.g2o (medium SE3 dataset)
///
///
/// The parking-garage dataset has 1,661 vertices and 6,275 edges,
/// representing a real-world indoor SLAM scenario.
#[test]
fn test_parking_garage_se3_converges() -> Result<(), Box<dyn std::error::Error>> {
    apex_io::ensure_odometry_dataset("parking-garage")?;
    let result = run_se3_optimization("parking-garage", 100, true)?;

    // Verify dataset size
    assert_eq!(
        result.vertices, 1661,
        "parking-garage.g2o should have 1661 vertices, got {}",
        result.vertices
    );
    assert_eq!(
        result.edges, 6275,
        "parking-garage.g2o should have 6275 edges, got {}",
        result.edges
    );

    // Verify convergence
    assert!(
        result.success,
        "Optimization did not converge. Status: {:?}, Iterations: {}, Final cost: {}",
        result.status, result.iterations, result.final_cost
    );

    // Verify cost improvement
    assert!(
        result.improvement_pct > 99.00,
        "Cost improvement too low: {:.2}% (expected >99.00%)",
        result.improvement_pct
    );

    // Verify iterations
    assert!(
        result.iterations < 20,
        "Should complete in < 20 iterations: {}",
        result.iterations
    );

    // Verify numerical stability
    assert!(
        result.final_cost.is_finite(),
        "Final cost is not finite: {}",
        result.final_cost
    );

    // Verify performance (should complete in < 20 seconds)
    assert!(
        result.elapsed_time.as_secs() < 20,
        "Optimization took too long: {:?}",
        result.elapsed_time
    );

    Ok(())
}