capability_grower_configuration_comparison/
compare_leaf_granularity.rs1crate::ix!();
3
4pub trait CompareLeafGranularity {
5 fn compare_leaf_granularity(&self, other: &GrowerTreeConfiguration) -> CompareOutcome;
6}
7
8impl CompareLeafGranularity for GrowerTreeConfiguration {
9 fn compare_leaf_granularity(&self, other: &GrowerTreeConfiguration) -> CompareOutcome {
10 let g1 = self.leaf_granularity();
11 let g2 = other.leaf_granularity();
12 let diff = (g1 - g2).abs();
13 if diff < 0.01 {
14 CompareOutcome::Exact
15 } else if diff < 0.15 {
16 let score = 1.0 - diff * 3.0;
17 CompareOutcome::Partial(score.max(0.0))
18 } else {
19 CompareOutcome::Incompatible
20 }
21 }
22}
23
24#[cfg(test)]
25mod compare_leaf_granularity_tests {
26 use super::*;
27 use traced_test::traced_test;
28
29 #[traced_test]
30 fn exact_match() {
31 let a = GrowerTreeConfiguration::base_config(5,2,2)
32 .to_builder().leaf_granularity(0.5).build().unwrap();
33 let b = a.clone();
34 assert_eq!(a.compare_leaf_granularity(&b), CompareOutcome::Exact);
35 }
36
37 #[traced_test]
38 fn partial_match() {
39 let a = GrowerTreeConfiguration::base_config(5,2,2)
40 .to_builder().leaf_granularity(0.3).build().unwrap();
41 let b = a.to_builder().leaf_granularity(0.45).build().unwrap();
42 match a.compare_leaf_granularity(&b) {
43 CompareOutcome::Partial(_) => {},
44 other => panic!("Expected partial, got {:?}", other),
45 }
46 }
47
48 #[traced_test]
49 fn incompatible() {
50 let a = GrowerTreeConfiguration::base_config(5,2,2)
51 .to_builder().leaf_granularity(0.1).build().unwrap();
52 let b = a.to_builder().leaf_granularity(0.8).build().unwrap();
53 assert_eq!(a.compare_leaf_granularity(&b), CompareOutcome::Incompatible);
54 }
55}