7个不稳定版本 (3个破坏性更新)

0.6.0 2024年7月21日
0.5.0 2024年4月12日
0.4.1 2024年3月24日
0.3.4 2024年3月22日

#288 in 数学

Download history 59/week @ 2024-07-15 70/week @ 2024-07-22 9/week @ 2024-07-29

每月138次下载

MIT/Apache

250KB
761

tiny-solver-rs

警告!此项目仍在开发中。

crate PyPI - Version PyPI - Python Version

ceres-solvertiny-solverminisam的启发。

这是一个用Rust编写的通用优化器,包括Python的绑定。如果您熟悉ceres-solver或因子图优化器,您会发现它非常容易使用。

安装

python

Python包可以直接从PyPI安装

pip install tiny-solver

rust

cargo add tiny-solver

当前功能

  • 使用num-dual自动求导
  • 稀疏QR、使用faer的稀疏Cholesky
  • GaussNewtonOptimizer
  • 多线程雅可比矩阵
  • 损失函数(Huber)
  • 在Python中定义因子

TODO

  • LevenbergMarquardtOptimizer
  • 信息矩阵

基准测试

数据集 tiny-solver gtsam minisam
m3500 161.1ms 130.7ms 123.6 ms

它不是特别优化,但安装和使用都很简单。

用法

Rust

// define your own Cost/Factor struct
// impl residual function
// and the jacobian will be auto generated
struct CustomFactor {}
impl tiny_solver::factors::Factor for CustomFactor {
    fn residual_func(
        &self,
        params: &[nalgebra::DVector<num_dual::DualDVec64>],
    ) -> nalgebra::DVector<num_dual::DualDVec64> {
        let x = &params[0][0];
        let y = &params[1][0];
        let z = &params[1][1];

        na::dvector![x + y.clone().mul(2.0) + z.clone().mul(4.0), y.mul(z)]
    }
}

fn main() {
    // init logger, `export RUST_LOG=trace` to see more log
    env_logger::init();

    // init problem (factor graph)
    let mut problem = tiny_solver::Problem::new();

    // add residual blocks (factors)
    // add residual x needs to be close to 3.0
    problem.add_residual_block(
        1,
        vec![("x".to_string(), 1)],
        Box::new(tiny_solver::factors::PriorFactor {
            v: na::dvector![3.0],
        }),
        None,
    );
    // add custom residual for x and yz
    problem.add_residual_block(
        2,
        vec![("x".to_string(), 1), ("yz".to_string(), 2)],
        Box::new(CustomFactor {}),
        None,
    );

    // the initial values for x is 0.7 and yz is [-30.2, 123.4]
    let initial_values = HashMap::<String, na::DVector<f64>>::from([
        ("x".to_string(), na::dvector![0.7]),
        ("yz".to_string(), na::dvector![-30.2, 123.4]),
    ]);

    // initialize optimizer
    let optimizer = tiny_solver::GaussNewtonOptimizer {};

    // optimize
    let result = optimizer.optimize(&problem, &initial_values, None);

    // result
    for (k, v) in result {
        println!("{}: {}", k, v);
    }
}

Python

import numpy as np
from tiny_solver import Problem, GaussNewtonOptimizer
from tiny_solver.factors import PriorFactor, PyFactor

# define custom cost function in python
# the trade off is the jacobian for the problem cannot be done in parallel
# because of gil
def cost(x: np.ndarray, yz: np.ndarray) -> np.ndarray:
    r0 = x[0] + 2 * yz[0] + 4 * yz[1]
    r1 = yz[0] * yz[0]
    return np.array([r0, r1])


def main():

    # initialize problem (factor graph)
    problem = Problem()

    # factor defined in python
    custom_factor = PyFactor(cost)
    problem.add_residual_block(
        2,
        [
            ("x", 1),
            ("yz", 2),
        ],
        custom_factor,
        None,
    )

    # prior factor import from rust
    prior_factor = PriorFactor(np.array([3.0]))
    problem.add_residual_block(1, [("x", 1)], prior_factor, None)

    # initial values
    init_values = {"x": np.array([0.7]), "yz": np.array([-30.2, 123.4])}

    # optimizer
    optimizer = GaussNewtonOptimizer()
    result_values = optimizer.optimize(problem, init_values)

    # result
    for k, v in result_values.items():
        print(f"{k}: {v}")


if __name__ == "__main__":
    main()

示例

基本示例

cargo run -r --example small_problem

M3500数据集

m3500 dataset rust result.
git clone https://github.com/powei-lin/tiny-solver-rs.git
cd tiny-solver-rs

# run rust version
cargo run -r --example m3500_benchmar

# run python version
pip install tiny-solver matplotlib
python3 examples/python/m3500.py

依赖关系

~16–22MB
~381K SLoC