Skip to content

Commit eae37c0

Browse files
committed
readme
1 parent 4633fb6 commit eae37c0

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cargo add tiny-solver
4242
It's not extremely optimized, but it's easy to install and use.
4343

4444
## Usage
45+
Rust
4546
```rust
4647
// define your own Cost/Factor struct
4748
// impl residual function
@@ -103,6 +104,55 @@ fn main() {
103104
}
104105
}
105106
```
107+
Python
108+
```py
109+
import numpy as np
110+
from tiny_solver import Problem, GaussNewtonOptimizer
111+
from tiny_solver.factors import PriorFactor, PyFactor
112+
113+
# define custom cost function in python
114+
def cost(x: np.ndarray, yz: np.ndarray) -> np.ndarray:
115+
r0 = x[0] + 2 * yz[0] + 4 * yz[1]
116+
r1 = yz[0] * yz[0]
117+
return np.array([r0, r1])
118+
119+
120+
def main():
121+
122+
# initialize problem (factor graph)
123+
problem = Problem()
124+
125+
# factor defined in python
126+
custom_factor = PyFactor(cost)
127+
problem.add_residual_block(
128+
2,
129+
[
130+
("x", 1),
131+
("yz", 2),
132+
],
133+
custom_factor,
134+
None,
135+
)
136+
137+
# prior factor import from rust
138+
prior_factor = PriorFactor(np.array([3.0]))
139+
problem.add_residual_block(1, [("x", 1)], prior_factor, None)
140+
141+
# initial values
142+
init_values = {"x": np.array([0.7]), "yz": np.array([-30.2, 123.4])}
143+
144+
# optimizer
145+
optimizer = GaussNewtonOptimizer()
146+
result_values = optimizer.optimize(problem, init_values)
147+
148+
# result
149+
for k, v in result_values.items():
150+
print(f"{k}: {v}")
151+
152+
153+
if __name__ == "__main__":
154+
main()
155+
```
106156

107157
## Example
108158
### Basic example

examples/python/m3500.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def show_pose(init_values, color):
5151

5252

5353
def main():
54-
FORMAT = '%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s'
54+
FORMAT = "%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s"
5555
logging.basicConfig(format=FORMAT)
5656
logging.getLogger().setLevel(logging.INFO)
5757
file_path = "tests/data/input_M3500_g2o.g2o"

examples/python/small_problem.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,44 @@
22
from tiny_solver import Problem, GaussNewtonOptimizer
33
from tiny_solver.factors import PriorFactor, PyFactor
44

5-
6-
def cost(x, y, z):
7-
r0 = x[0] + 2 * y[0] + 4 * z[0]
8-
r1 = y[0] * z[0]
5+
# define custom cost function in python
6+
def cost(x: np.ndarray, yz: np.ndarray) -> np.ndarray:
7+
r0 = x[0] + 2 * yz[0] + 4 * yz[1]
8+
r1 = yz[0] * yz[0]
99
return np.array([r0, r1])
1010

1111

1212
def main():
13+
14+
# initialize problem (factor graph)
1315
problem = Problem()
14-
pf = PyFactor(cost)
16+
17+
# factor defined in python
18+
custom_factor = PyFactor(cost)
1519
problem.add_residual_block(
1620
2,
1721
[
1822
("x", 1),
19-
("y", 1),
20-
("z", 1),
23+
("yz", 2),
2124
],
22-
pf,
25+
custom_factor,
2326
None,
2427
)
25-
pp = PriorFactor(np.array([3.0]))
26-
problem.add_residual_block(1, [("x", 1)], pp, None)
27-
gn = GaussNewtonOptimizer()
28-
init_values = {"x": np.array([0.7]), "y": np.array([-30.2]), "z": np.array([123.9])}
29-
result_values = gn.optimize(problem, init_values)
30-
print(result_values)
28+
29+
# prior factor import from rust
30+
prior_factor = PriorFactor(np.array([3.0]))
31+
problem.add_residual_block(1, [("x", 1)], prior_factor, None)
32+
33+
# initial values
34+
init_values = {"x": np.array([0.7]), "yz": np.array([-30.2, 123.4])}
35+
36+
# optimizer
37+
optimizer = GaussNewtonOptimizer()
38+
result_values = optimizer.optimize(problem, init_values)
39+
40+
# result
41+
for k, v in result_values.items():
42+
print(f"{k}: {v}")
3143

3244

3345
if __name__ == "__main__":

0 commit comments

Comments
 (0)