apex-solver 1.4.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use std::collections::HashMap;
use std::time::Instant;
use tracing::{error, info, warn};

use apex_solver::JacobianMode;
use apex_solver::apex_io::{
    G2oLoader, Graph, GraphLoader, ODOMETRY_DATA_DIR_2D, ODOMETRY_DATA_DIR_3D,
};
use apex_solver::apex_manifolds::ManifoldType;
use apex_solver::core::VarKey;
use apex_solver::core::loss_functions::*;
use apex_solver::core::problem::Problem;
use apex_solver::factors::BetweenFactor;
use apex_solver::init_logger;
use apex_solver::optimizer::dog_leg::DogLegConfig;
use apex_solver::optimizer::gauss_newton::GaussNewtonConfig;
use apex_solver::optimizer::levenberg_marquardt::LevenbergMarquardtConfig;
use apex_solver::optimizer::{DogLeg, GaussNewton, LevenbergMarquardt, OptimizationStatus};
use clap::Parser;
use nalgebra::dvector;

#[derive(Parser)]
#[command(name = "loss_function_comparison")]
#[command(about = "Compare robust loss functions with multiple optimizers on pose graph datasets")]
struct Args {
    /// Maximum number of optimization iterations
    #[arg(short, long, default_value = "50")]
    max_iterations: usize,

    /// Enable verbose output
    #[arg(short, long)]
    verbose: bool,

    /// Cost tolerance for convergence
    #[arg(long, default_value = "1e-4")]
    cost_tolerance: f64,

    /// Parameter tolerance for convergence
    #[arg(long, default_value = "1e-4")]
    parameter_tolerance: f64,

    /// Output CSV file path (optional)
    #[arg(short, long)]
    output: Option<String>,
}

#[derive(Clone)]
struct BenchmarkResult {
    dataset: String,
    manifold: String,
    optimizer: String,
    loss_function: String,
    scale_param: f64,
    vertices: usize,
    edges: usize,
    initial_cost: f64,
    final_cost: f64,
    improvement: f64,
    iterations: usize,
    time_ms: u128,
    status: String,
}

fn print_summary_table(results: &[BenchmarkResult]) {
    info!("{}", "=".repeat(170));
    info!("=== ROBUST LOSS FUNCTION BENCHMARK RESULTS ===");

    info!(
        "{:<12} | {:<4} | {:<10} | {:<18} | {:<5} | {:<4} | {:<5} | {:<12} | {:<12} | {:<10} | {:<5} | {:<8} | {:<10}",
        "Dataset",
        "Man",
        "Optimizer",
        "Loss Function",
        "Scale",
        "Verts",
        "Edges",
        "Init Cost",
        "Final Cost",
        "Improv %",
        "Iters",
        "Time(ms)",
        "Status"
    );
    info!("{}", "-".repeat(170));

    for result in results {
        info!(
            "{:<12} | {:<4} | {:<10} | {:<18} | {:<5.2} | {:<4} | {:<5} | {:<12.6e} | {:<12.6e} | {:>9.2}% | {:<5} | {:<8} | {:<10}",
            result.dataset,
            result.manifold,
            result.optimizer,
            result.loss_function,
            result.scale_param,
            result.vertices,
            result.edges,
            result.initial_cost,
            result.final_cost,
            result.improvement,
            result.iterations,
            result.time_ms,
            result.status
        );
    }

    info!("{}", "-".repeat(170));
}

fn write_csv(
    results: &[BenchmarkResult],
    filepath: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    use std::fs::File;
    use std::io::Write;

    let mut file = File::create(filepath)?;
    writeln!(
        file,
        "dataset,manifold,optimizer,loss_function,scale_param,vertices,edges,initial_cost,final_cost,improvement,iterations,time_ms,status"
    )?;
    for result in results {
        writeln!(
            file,
            "{},{},{},{},{},{},{},{},{},{},{},{},{}",
            result.dataset,
            result.manifold,
            result.optimizer,
            result.loss_function,
            result.scale_param,
            result.vertices,
            result.edges,
            result.initial_cost,
            result.final_cost,
            result.improvement,
            result.iterations,
            result.time_ms,
            result.status
        )?;
    }
    info!("Results written to: {}", filepath);
    Ok(())
}

fn make_loss(
    name: &str,
    scale: f64,
) -> Result<Option<Box<dyn LossFunction + Send>>, Box<dyn std::error::Error>> {
    let loss: Box<dyn LossFunction + Send> = match name {
        "L2" => Box::new(L2Loss),
        "L1" => Box::new(L1Loss),
        "Huber" => Box::new(HuberLoss::new(scale)?),
        "Cauchy" => Box::new(CauchyLoss::new(scale)?),
        "Fair" => Box::new(FairLoss::new(scale)?),
        "Welsch" => Box::new(WelschLoss::new(scale)?),
        "Tukey" => Box::new(TukeyBiweightLoss::new(scale)?),
        "GemanMcClure" => Box::new(GemanMcClureLoss::new(scale)?),
        "Andrews" => Box::new(AndrewsWaveLoss::new(scale)?),
        "Ramsay" => Box::new(RamsayEaLoss::new(scale)?),
        "TrimmedMean" => Box::new(TrimmedMeanLoss::new(scale)?),
        "Lp(1.5)" => Box::new(LpNormLoss::new(scale)?),
        "Barron(a=0)" => Box::new(BarronGeneralLoss::new(0.0, scale)?),
        "Barron(a=1)" => Box::new(BarronGeneralLoss::new(1.0, scale)?),
        "Barron(a=-2)" => Box::new(BarronGeneralLoss::new(-2.0, scale)?),
        "TDist(v=5)" => Box::new(TDistributionLoss::new(scale)?),
        "AdaptiveBarron" => Box::new(AdaptiveBarronLoss::new(0.0, scale)?),
        _ => return Ok(None),
    };
    Ok(Some(loss))
}

fn build_se3_problem(
    graph: &Graph,
    loss_name: &str,
    scale: f64,
) -> Result<(Problem, HashMap<usize, VarKey>), Box<dyn std::error::Error>> {
    let mut problem = Problem::new(JacobianMode::Sparse);
    let mut var_keys: HashMap<usize, VarKey> = HashMap::new();

    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 quat = vertex.pose.rotation_quaternion();
            let trans = vertex.pose.translation();
            let key = problem.add_variable(
                ManifoldType::SE3,
                dvector![trans.x, trans.y, trans.z, quat.w, quat.i, quat.j, quat.k],
            );
            var_keys.insert(id, key);
        }
    }

    for edge in &graph.edges_se3 {
        if let (Some(&k0), Some(&k1)) = (var_keys.get(&edge.from), var_keys.get(&edge.to)) {
            let loss = make_loss(loss_name, scale)?;
            problem.add_residual_block(
                &[k0, k1],
                Box::new(BetweenFactor::new(edge.measurement.clone())),
                loss,
            );
        }
    }

    Ok((problem, var_keys))
}

fn build_se2_problem(
    graph: &Graph,
    loss_name: &str,
    scale: f64,
) -> Result<(Problem, HashMap<usize, VarKey>), Box<dyn std::error::Error>> {
    let mut problem = Problem::new(JacobianMode::Sparse);
    let mut var_keys: HashMap<usize, VarKey> = HashMap::new();

    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 trans = vertex.pose.translation();
            let angle = vertex.pose.rotation_angle();
            let key = problem.add_variable(ManifoldType::SE2, dvector![trans.x, trans.y, angle]);
            var_keys.insert(id, key);
        }
    }

    for edge in &graph.edges_se2 {
        if let (Some(&k0), Some(&k1)) = (var_keys.get(&edge.from), var_keys.get(&edge.to)) {
            let loss = make_loss(loss_name, scale)?;
            problem.add_residual_block(
                &[k0, k1],
                Box::new(BetweenFactor::new(edge.measurement.clone())),
                loss,
            );
        }
    }

    Ok((problem, var_keys))
}

#[allow(clippy::type_complexity)]
fn run_optimization(
    problem: &mut Problem,
    optimizer_name: &str,
    max_iterations: usize,
    cost_tolerance: f64,
    parameter_tolerance: f64,
) -> Result<(f64, f64, usize, OptimizationStatus, u128), Box<dyn std::error::Error>> {
    let start = Instant::now();

    let result = match optimizer_name {
        "LM" => {
            let config = LevenbergMarquardtConfig::new()
                .with_max_iterations(max_iterations)
                .with_cost_tolerance(cost_tolerance)
                .with_parameter_tolerance(parameter_tolerance);
            let mut solver = LevenbergMarquardt::with_config(config);
            solver.optimize(problem)?
        }
        "GN" => {
            let config = GaussNewtonConfig::new()
                .with_max_iterations(max_iterations)
                .with_cost_tolerance(cost_tolerance)
                .with_parameter_tolerance(parameter_tolerance);
            let mut solver = GaussNewton::with_config(config);
            solver.optimize(problem)?
        }
        "DL" => {
            let config = DogLegConfig::new()
                .with_max_iterations(max_iterations)
                .with_cost_tolerance(cost_tolerance)
                .with_parameter_tolerance(parameter_tolerance);
            let mut solver = DogLeg::with_config(config);
            solver.optimize(problem)?
        }
        _ => unreachable!(),
    };

    let elapsed = start.elapsed().as_millis();
    Ok((
        result.initial_cost,
        result.final_cost,
        result.iterations,
        result.status,
        elapsed,
    ))
}

const LOSS_CONFIGS: &[(&str, f64)] = &[
    ("L2", 1.0),
    ("L1", 1.0),
    ("Huber", 1.345),
    ("Cauchy", 2.3849),
    ("Fair", 1.3999),
    ("Welsch", 2.9846),
    ("Tukey", 4.6851),
    ("GemanMcClure", 1.0),
    ("Andrews", 1.339),
    ("Ramsay", 0.3),
    ("TrimmedMean", 2.0),
    ("Lp(1.5)", 1.5),
    ("Barron(a=0)", 1.0),
    ("Barron(a=1)", 1.0),
    ("Barron(a=-2)", 1.0),
    ("TDist(v=5)", 5.0),
    ("AdaptiveBarron", 1.0),
];

fn benchmark_dataset_se3(
    graph_path: &str,
    dataset_name: &str,
    args: &Args,
) -> Result<Vec<BenchmarkResult>, Box<dyn std::error::Error>> {
    info!("Loading SE3 dataset: {}", dataset_name);
    let graph = G2oLoader::load(graph_path)?;
    let num_vertices = graph.vertices_se3.len();
    let num_edges = graph.edges_se3.len();
    info!("Loaded {} vertices, {} edges", num_vertices, num_edges);

    let optimizers = ["LM", "GN", "DL"];
    let mut results = Vec::new();

    for optimizer_name in &optimizers {
        info!("--- Testing Optimizer: {} ---", optimizer_name);

        for &(loss_name, scale) in LOSS_CONFIGS {
            info!("  Testing {} (scale={:.4})...", loss_name, scale);

            let (mut problem, _) = build_se3_problem(&graph, loss_name, scale)?;

            match run_optimization(
                &mut problem,
                optimizer_name,
                args.max_iterations,
                args.cost_tolerance,
                args.parameter_tolerance,
            ) {
                Ok((initial_cost, final_cost, iterations, status, time_ms)) => {
                    let improvement = if initial_cost > 0.0 {
                        ((initial_cost - final_cost) / initial_cost) * 100.0
                    } else {
                        0.0
                    };
                    let status_str = match status {
                        OptimizationStatus::Converged => "CONVERGED",
                        OptimizationStatus::MaxIterationsReached => "MAX_ITERS",
                        _ => "OTHER",
                    };

                    if args.verbose {
                        info!(
                            "    Init: {:.4e}, Final: {:.4e}, Improv: {:.2}%, Iters: {}, Time: {}ms [{}]",
                            initial_cost, final_cost, improvement, iterations, time_ms, status_str
                        );
                    }

                    results.push(BenchmarkResult {
                        dataset: dataset_name.to_string(),
                        manifold: "SE3".to_string(),
                        optimizer: optimizer_name.to_string(),
                        loss_function: loss_name.to_string(),
                        scale_param: scale,
                        vertices: num_vertices,
                        edges: num_edges,
                        initial_cost,
                        final_cost,
                        improvement,
                        iterations,
                        time_ms,
                        status: status_str.to_string(),
                    });
                }
                Err(e) => {
                    error!("    {}", e);
                }
            }
        }
    }

    Ok(results)
}

fn benchmark_dataset_se2(
    graph_path: &str,
    dataset_name: &str,
    args: &Args,
) -> Result<Vec<BenchmarkResult>, Box<dyn std::error::Error>> {
    info!("Loading SE2 dataset: {}", dataset_name);
    let graph = G2oLoader::load(graph_path)?;
    let num_vertices = graph.vertices_se2.len();
    let num_edges = graph.edges_se2.len();
    info!("Loaded {} vertices, {} edges", num_vertices, num_edges);

    let optimizers = ["LM", "GN", "DL"];
    let mut results = Vec::new();

    for optimizer_name in &optimizers {
        info!("--- Testing Optimizer: {} ---", optimizer_name);

        for &(loss_name, scale) in LOSS_CONFIGS {
            info!("  Testing {} (scale={:.4})...", loss_name, scale);

            let (mut problem, _) = build_se2_problem(&graph, loss_name, scale)?;

            match run_optimization(
                &mut problem,
                optimizer_name,
                args.max_iterations,
                args.cost_tolerance,
                args.parameter_tolerance,
            ) {
                Ok((initial_cost, final_cost, iterations, status, time_ms)) => {
                    let improvement = if initial_cost > 0.0 {
                        ((initial_cost - final_cost) / initial_cost) * 100.0
                    } else {
                        0.0
                    };
                    let status_str = match status {
                        OptimizationStatus::Converged => "CONVERGED",
                        OptimizationStatus::MaxIterationsReached => "MAX_ITERS",
                        _ => "OTHER",
                    };

                    if args.verbose {
                        info!(
                            "    Init: {:.4e}, Final: {:.4e}, Improv: {:.2}%, Iters: {}, Time: {}ms [{}]",
                            initial_cost, final_cost, improvement, iterations, time_ms, status_str
                        );
                    }

                    results.push(BenchmarkResult {
                        dataset: dataset_name.to_string(),
                        manifold: "SE2".to_string(),
                        optimizer: optimizer_name.to_string(),
                        loss_function: loss_name.to_string(),
                        scale_param: scale,
                        vertices: num_vertices,
                        edges: num_edges,
                        initial_cost,
                        final_cost,
                        improvement,
                        iterations,
                        time_ms,
                        status: status_str.to_string(),
                    });
                }
                Err(e) => {
                    error!("    {}", e);
                }
            }
        }
    }

    Ok(results)
}

fn print_analysis(results: &[BenchmarkResult]) {
    info!("{}", "=".repeat(80));
    info!("=== ANALYSIS AND RECOMMENDATIONS ===");
    info!("{}", "=".repeat(80));

    let mut datasets_vec: Vec<String> = results
        .iter()
        .map(|r| r.dataset.clone())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    datasets_vec.sort();

    for dataset in &datasets_vec {
        info!("Dataset: {}", dataset);

        let converged: Vec<&BenchmarkResult> = results
            .iter()
            .filter(|r| r.dataset == *dataset && r.status == "CONVERGED")
            .collect();

        if converged.is_empty() {
            info!("  No converged results");
            continue;
        }

        if let Some(best) = converged
            .iter()
            .min_by(|a, b| a.final_cost.total_cmp(&b.final_cost))
        {
            info!(
                "  Best Overall: {} + {} (cost: {:.4e}, {:.1}% improv, {} iters, {}ms)",
                best.optimizer,
                best.loss_function,
                best.final_cost,
                best.improvement,
                best.iterations,
                best.time_ms
            );
        }

        for opt in &["LM", "GN", "DL"] {
            let opt_results: Vec<_> = converged.iter().filter(|r| r.optimizer == *opt).collect();
            if let Some(best) = opt_results
                .iter()
                .min_by(|a, b| a.final_cost.total_cmp(&b.final_cost))
            {
                info!(
                    "  Best {}: {} (cost: {:.4e}, {} iters)",
                    opt, best.loss_function, best.final_cost, best.iterations
                );
            }
        }
    }

    info!("{}", "=".repeat(80));
    info!("RECOMMENDED DEFAULTS");

    let mut loss_stats: HashMap<String, (usize, usize, f64)> = HashMap::new();
    for result in results {
        let entry = loss_stats
            .entry(result.loss_function.clone())
            .or_insert((0, 0, 0.0));
        entry.0 += 1;
        if result.status == "CONVERGED" {
            entry.1 += 1;
            entry.2 += result.improvement;
        }
    }

    info!(
        "{:<18} | {:>12} | {:>12} | {:>15}",
        "Loss Function", "Conv Rate", "Avg Improv", "Recommendation"
    );
    info!("{}", "-".repeat(65));

    let mut loss_vec: Vec<_> = loss_stats.iter().collect();
    loss_vec.sort_by(|a, b| {
        let rate_a = a.1.1 as f64 / a.1.0 as f64;
        let rate_b = b.1.1 as f64 / b.1.0 as f64;
        rate_b.total_cmp(&rate_a)
    });

    for (loss, (total, converged, sum_improv)) in loss_vec {
        let conv_rate = (*converged as f64 / *total as f64) * 100.0;
        let avg_improv = if *converged > 0 {
            sum_improv / *converged as f64
        } else {
            0.0
        };
        let recommendation = if conv_rate >= 95.0 && avg_improv >= 95.0 {
            "Excellent"
        } else if conv_rate >= 80.0 && avg_improv >= 90.0 {
            "Good"
        } else if conv_rate >= 70.0 {
            "Fair"
        } else {
            "Poor"
        };
        info!(
            "{:<18} | {:>11.0}% | {:>11.1}% | {:>15}",
            loss, conv_rate, avg_improv, recommendation
        );
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    init_logger();

    info!("ROBUST LOSS FUNCTION COMPARISON BENCHMARK");

    let mut all_results = Vec::new();

    for (path, name) in &[
        (
            format!("{}/sphere2500.g2o", ODOMETRY_DATA_DIR_3D),
            "sphere2500",
        ),
        (
            format!("{}/parking-garage.g2o", ODOMETRY_DATA_DIR_3D),
            "parking-garage",
        ),
    ] {
        if std::path::Path::new(path.as_str()).exists() {
            match benchmark_dataset_se3(path.as_str(), name, &args) {
                Ok(mut results) => all_results.append(&mut results),
                Err(e) => warn!("Failed to benchmark {}: {}", name, e),
            }
        } else {
            warn!("Skipping {} (file not found)", name);
        }
    }

    for (path, name) in &[
        (format!("{}/intel.g2o", ODOMETRY_DATA_DIR_2D), "intel"),
        (format!("{}/mit.g2o", ODOMETRY_DATA_DIR_2D), "mit"),
        (format!("{}/ring.g2o", ODOMETRY_DATA_DIR_2D), "ring"),
    ] {
        if std::path::Path::new(path.as_str()).exists() {
            match benchmark_dataset_se2(path.as_str(), name, &args) {
                Ok(mut results) => all_results.append(&mut results),
                Err(e) => warn!("Failed to benchmark {}: {}", name, e),
            }
        } else {
            info!("Skipping {} (file not found)", name);
        }
    }

    if !all_results.is_empty() {
        print_summary_table(&all_results);
        if let Some(output_path) = &args.output {
            write_csv(&all_results, output_path)?;
        }
        print_analysis(&all_results);
    } else {
        info!("No results to display");
    }

    Ok(())
}