Skip to content

Commit f9aac0a

Browse files
jumpinthefireHossam Rabbouh
andauthored
Allow removing residual blocks. (#46)
* Pre-compute symbolic matrix for jacobians * Remove reserve * Formatting * Try to fix pipeline * Try to fix pipeline * whoops * Allow removal of residual blocks. * Formatting. --------- Co-authored-by: Hossam Rabbouh <[email protected]>
1 parent 49c29f4 commit f9aac0a

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tiny-solver"
3-
version = "0.15.1"
3+
version = "0.16.0"
44
edition = "2021"
55
authors = ["Powei Lin <[email protected]>, Hossam R. <[email protected]>"]
66
readme = "README.md"

src/problem.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use crate::manifold::Manifold;
1010
use crate::parameter_block::ParameterBlock;
1111
use crate::{factors, loss_functions, residual_block};
1212

13+
type ResidualBlockId = usize;
14+
1315
pub struct Problem {
1416
pub total_residual_dimension: usize,
1517
residual_id_count: usize,
16-
residual_blocks: HashMap<usize, residual_block::ResidualBlock>,
18+
residual_blocks: HashMap<ResidualBlockId, residual_block::ResidualBlock>,
1719
pub fixed_variable_indexes: HashMap<String, HashSet<usize>>,
1820
pub variable_bounds: HashMap<String, HashMap<usize, (f64, f64)>>,
1921
pub variable_manifold: HashMap<String, Arc<dyn Manifold + Sync + Send>>,
@@ -108,7 +110,7 @@ impl Problem {
108110
variable_key_size_list: &[&str],
109111
factor: Box<dyn factors::FactorImpl + Send>,
110112
loss_func: Option<Box<dyn loss_functions::Loss + Send>>,
111-
) {
113+
) -> ResidualBlockId {
112114
self.residual_blocks.insert(
113115
self.residual_id_count,
114116
residual_block::ResidualBlock::new(
@@ -120,9 +122,23 @@ impl Problem {
120122
loss_func,
121123
),
122124
);
125+
let block_id = self.residual_id_count;
123126
self.residual_id_count += 1;
124127

125128
self.total_residual_dimension += dim_residual;
129+
130+
block_id
131+
}
132+
pub fn remove_residual_block(
133+
&mut self,
134+
block_id: ResidualBlockId,
135+
) -> Option<residual_block::ResidualBlock> {
136+
if let Some(residual_block) = self.residual_blocks.remove(&block_id) {
137+
self.total_residual_dimension -= residual_block.dim_residual;
138+
Some(residual_block)
139+
} else {
140+
None
141+
}
126142
}
127143
pub fn fix_variable(&mut self, var_to_fix: &str, idx: usize) {
128144
if let Some(var_mut) = self.fixed_variable_indexes.get_mut(var_to_fix) {

tests/test_problem.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,34 @@ mod tests {
1616
#[test]
1717
fn add_residual_block() {
1818
let mut problem = tiny_solver::Problem::new();
19-
problem.add_residual_block(
19+
let block_id1 = problem.add_residual_block(
20+
1,
21+
&["x"],
22+
Box::new(tiny_solver::factors::PriorFactor {
23+
v: na::dvector![3.0],
24+
}),
25+
None,
26+
);
27+
28+
assert_eq!(problem.total_residual_dimension, 1);
29+
30+
let block_id2 = problem.add_residual_block(
31+
1,
32+
&["y"],
33+
Box::new(tiny_solver::factors::PriorFactor {
34+
v: na::dvector![3.0],
35+
}),
36+
None,
37+
);
38+
39+
assert!(block_id1 != block_id2);
40+
assert_eq!(problem.total_residual_dimension, 2);
41+
}
42+
43+
#[test]
44+
fn remove_residual_block() {
45+
let mut problem = tiny_solver::Problem::new();
46+
let block_id = problem.add_residual_block(
2047
1,
2148
&["x"],
2249
Box::new(tiny_solver::factors::PriorFactor {
@@ -26,6 +53,14 @@ mod tests {
2653
);
2754

2855
assert_eq!(problem.total_residual_dimension, 1);
56+
57+
let mut block = problem.remove_residual_block(block_id);
58+
assert!(block.is_some());
59+
assert_eq!(problem.total_residual_dimension, 0);
60+
61+
block = problem.remove_residual_block(block_id);
62+
assert!(block.is_none());
63+
assert_eq!(problem.total_residual_dimension, 0);
2964
}
3065

3166
#[test]

0 commit comments

Comments
 (0)