|
| 1 | +"""Tests for g2o core functionality (optimizer, solvers, algorithms).""" |
| 2 | + |
| 3 | +# g2o will be available after conftest.py sets sys.path |
| 4 | +import g2opy as g2o |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +class TestSparseOptimizer: |
| 10 | + """Test SparseOptimizer basic functionality.""" |
| 11 | + |
| 12 | + def test_create_optimizer(self): |
| 13 | + """Test creating a SparseOptimizer.""" |
| 14 | + optimizer = g2o.SparseOptimizer() |
| 15 | + assert optimizer is not None |
| 16 | + assert len(optimizer.vertices()) == 0 |
| 17 | + assert len(optimizer.edges()) == 0 |
| 18 | + |
| 19 | + def test_add_vertex(self, basic_se2_optimizer): |
| 20 | + """Test adding vertices to optimizer.""" |
| 21 | + v = g2o.VertexSE2() |
| 22 | + v.set_id(0) |
| 23 | + v.set_estimate(g2o.SE2(0, 0, 0)) |
| 24 | + |
| 25 | + basic_se2_optimizer.add_vertex(v) |
| 26 | + assert len(basic_se2_optimizer.vertices()) == 1 |
| 27 | + assert basic_se2_optimizer.vertex(0) is not None |
| 28 | + |
| 29 | + def test_add_edge(self, simple_se2_graph): |
| 30 | + """Test adding edges to optimizer.""" |
| 31 | + assert len(simple_se2_graph.edges()) == 2 |
| 32 | + |
| 33 | + def test_vertex_retrieval(self, simple_se2_graph): |
| 34 | + """Test retrieving vertices.""" |
| 35 | + for i in range(3): |
| 36 | + v = simple_se2_graph.vertex(i) |
| 37 | + assert v is not None |
| 38 | + assert v.id() == i |
| 39 | + |
| 40 | + def test_chi2_compute(self, simple_se2_graph): |
| 41 | + """Test computing chi2 error.""" |
| 42 | + initial_chi2 = simple_se2_graph.chi2() |
| 43 | + assert initial_chi2 >= 0 |
| 44 | + |
| 45 | + def test_initialize_optimization(self, simple_se2_graph): |
| 46 | + """Test optimization initialization.""" |
| 47 | + simple_se2_graph.initialize_optimization() |
| 48 | + # Should not raise an exception |
| 49 | + |
| 50 | + def test_set_verbose(self, basic_se2_optimizer): |
| 51 | + """Test setting verbose output.""" |
| 52 | + basic_se2_optimizer.set_verbose(True) |
| 53 | + basic_se2_optimizer.set_verbose(False) |
| 54 | + # Should not raise an exception |
| 55 | + |
| 56 | + |
| 57 | +class TestBlockSolvers: |
| 58 | + """Test BlockSolver configurations.""" |
| 59 | + |
| 60 | + def test_block_solver_se2(self): |
| 61 | + """Test BlockSolverSE2.""" |
| 62 | + solver = g2o.BlockSolverSE2(g2o.LinearSolverEigenSE2()) |
| 63 | + assert solver is not None |
| 64 | + |
| 65 | + def test_block_solver_se3(self): |
| 66 | + """Test BlockSolverSE3.""" |
| 67 | + solver = g2o.BlockSolverSE3(g2o.LinearSolverEigenSE3()) |
| 68 | + assert solver is not None |
| 69 | + |
| 70 | + def test_block_solver_variable(self): |
| 71 | + """Test variable dimension BlockSolverX.""" |
| 72 | + solver = g2o.BlockSolverX(g2o.LinearSolverEigenX()) |
| 73 | + assert solver is not None |
| 74 | + |
| 75 | + |
| 76 | +class TestLinearSolvers: |
| 77 | + """Test various linear solver implementations.""" |
| 78 | + |
| 79 | + def test_linear_solver_eigen_se2(self): |
| 80 | + """Test LinearSolverEigenSE2.""" |
| 81 | + solver = g2o.LinearSolverEigenSE2() |
| 82 | + assert solver is not None |
| 83 | + |
| 84 | + def test_linear_solver_eigen_se3(self): |
| 85 | + """Test LinearSolverEigenSE3.""" |
| 86 | + solver = g2o.LinearSolverEigenSE3() |
| 87 | + assert solver is not None |
| 88 | + |
| 89 | + def test_linear_solver_eigen_x(self): |
| 90 | + """Test LinearSolverEigenX (variable dimension).""" |
| 91 | + solver = g2o.LinearSolverEigenX() |
| 92 | + assert solver is not None |
| 93 | + |
| 94 | + |
| 95 | +class TestOptimizationAlgorithms: |
| 96 | + """Test optimization algorithm implementations.""" |
| 97 | + |
| 98 | + def test_levenberg_marquardt(self, basic_se2_optimizer): |
| 99 | + """Test Levenberg-Marquardt algorithm.""" |
| 100 | + assert basic_se2_optimizer.algorithm() is not None |
| 101 | + |
| 102 | + def test_gauss_newton_creation(self): |
| 103 | + """Test creating Gauss-Newton algorithm.""" |
| 104 | + solver = g2o.BlockSolverSE2(g2o.LinearSolverEigenSE2()) |
| 105 | + algo = g2o.OptimizationAlgorithmGaussNewton(solver) |
| 106 | + assert algo is not None |
| 107 | + |
| 108 | + def test_dogleg_creation(self): |
| 109 | + """Test creating Dogleg algorithm.""" |
| 110 | + solver = g2o.BlockSolverSE2(g2o.LinearSolverEigenSE2()) |
| 111 | + algo = g2o.OptimizationAlgorithmDogleg(solver) |
| 112 | + assert algo is not None |
| 113 | + |
| 114 | + |
| 115 | +class TestErrorComputation: |
| 116 | + """Test error and chi2 computation.""" |
| 117 | + |
| 118 | + def test_compute_active_errors(self, simple_se2_graph): |
| 119 | + """Test computing active errors.""" |
| 120 | + simple_se2_graph.compute_active_errors() |
| 121 | + chi2 = simple_se2_graph.chi2() |
| 122 | + assert chi2 >= 0 |
| 123 | + |
| 124 | + def test_chi2_before_optimization(self, simple_se2_graph): |
| 125 | + """Test chi2 computation before optimization.""" |
| 126 | + simple_se2_graph.initialize_optimization() |
| 127 | + simple_se2_graph.compute_active_errors() |
| 128 | + initial_chi2 = simple_se2_graph.chi2() |
| 129 | + |
| 130 | + simple_se2_graph.optimize(1) |
| 131 | + simple_se2_graph.compute_active_errors() |
| 132 | + final_chi2 = simple_se2_graph.chi2() |
| 133 | + |
| 134 | + # Chi2 should improve or stay the same |
| 135 | + assert final_chi2 <= initial_chi2 + 1e-6 |
0 commit comments