Skip to content

Commit

Permalink
Merge pull request #7 from powei-lin/py-factor
Browse files Browse the repository at this point in the history
Py factor
  • Loading branch information
powei-lin authored Mar 23, 2024
2 parents e7e75cc + 95addae commit e554785
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 137 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
__pycache__
*.so
.DS_Store
.vscode/*
.vscode/*
.venv
.pytest_cache
78 changes: 49 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiny-solver"
version = "0.3.4"
version = "0.4.0"
edition = "2021"
authors = ["Powei Lin <poweilin1994@gmail.com>"]
readme = "README.md"
Expand All @@ -18,23 +18,26 @@ exclude = ["/.github/*", "*.ipynb", "./scripts/*", "examples/*"]
faer = "0.18.2"
faer-ext = { version = "0.1.0", features = ["nalgebra"] }
nalgebra = "0.32.4"
num-dual = "0.8.1"
num-dual = "0.8.1"
num-traits = "0.2.18"
numpy = { version = "0.20.0", features = ["nalgebra"] }
pyo3 = { version = "0.20.3", features = ["abi3", "abi3-py38"] }
rayon = "1.9.0"

[[example]]
name = "m3500_benchmark"
# crate-type = ["bin"]
path = "examples/m3500_benchmark.rs"

[features]
python = ["num-dual/python"]

[dev-dependencies]
itertools = "0.12.1"
plotters = "0.3.5"

[profile.dev.package.faer]
opt-level = 3

[lib]
name = "tiny_solver"
# crate-type = ["cdylib"]
# crate-type = ["staticlib"]
24 changes: 24 additions & 0 deletions examples/python/small_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np
from tiny_solver import Problem, GaussNewtonOptimizer
from tiny_solver.factors import PriorFactor, PyFactor


def cost(x, y, z):
r0 = x[0] + 2*y[0] + 4*z[0]
r1 = y[0] * z[0]
return np.array([r0, r1])

def main():
problem = Problem()
pf = PyFactor(cost)
problem.add_residual_block(2, [('x', 1), ('y', 1), ('z', 1),], pf, None)
pp = PriorFactor(np.array([3.0]))
problem.add_residual_block(1, [('x', 1)], pp, None)
gn = GaussNewtonOptimizer()
init_values = {"x": np.array([0.7]), "y": np.array([-30.2]), "z": np.array([123.9])}
result_values = gn.optimize(problem, init_values)
print(result_values)


if __name__ == '__main__':
main()
17 changes: 15 additions & 2 deletions examples/python/try_import.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import tiny_solver
from tiny_solver import GaussNewtonOptimizer, Problem, LinearSolver, OptimizerOptions
from tiny_solver.factors import PriorFactor, BetweenFactorSE2
from tiny_solver import GaussNewtonOptimizer, Problem, LinearSolver, OptimizerOptions, first_derivative_test
from tiny_solver.factors import PriorFactor, BetweenFactorSE2, PyFactor
from tiny_solver.loss_functions import HuberLoss
import numpy as np

def f(x: np.ndarray, y: np.ndarray):
# print("py ", x*x)
return np.array([2*x[0], x[1]*x[1]*x[1], y[1]*4.0])

def fa():
print("fa")
return 123


def main():
print(f"{tiny_solver.__version__=}")
Expand All @@ -14,6 +22,11 @@ def main():
print(opt_option)
loss = HuberLoss(1.0)
print(loss)
a = np.array([1.0, 2.0])
# j = first_derivative_test(f, a)
# print(j)
a = PyFactor(f)
a.call_func()
exit()
# print(tiny_solver.sum_as_string(1, 2))

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ dependencies = ["numpy"]


[tool.maturin]
features = ["pyo3/extension-module"]
features = ["pyo3/extension-module", "python"]
1 change: 0 additions & 1 deletion src/gauss_newton_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl optimizer::Optimizer for GaussNewtonOptimizer {
let opt_option = optimizer_option.unwrap_or_default();

let mut last_err: f64 = 1.0;

let mut symbolic_pattern: Option<solvers::SymbolicCholesky<usize>> = None;

for i in 0..opt_option.max_iteration {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ pub mod linear;
pub mod loss_functions;
pub mod optimizer;
pub mod problem;
pub mod python;
pub mod residual_block;
pub mod tiny_solver_old;

pub use gauss_newton_optimizer::*;
pub use linear::*;
pub use optimizer::*;
pub use problem::*;
pub use python::*;
pub use residual_block::*;

#[cfg(feature = "python")]
pub mod python;
Loading

0 comments on commit e554785

Please sign in to comment.