capability_grower_configuration/
weighted_node_variant_policy_with_limits.rs

1// ---------------- [ File: capability-grower-configuration/src/weighted_node_variant_policy_with_limits.rs ]
2crate::ix!();
3
4/// This policy is the same as Weighted, but with built-in depth constraints to forbid aggregator or
5/// dispatch nodes beyond a certain depth. We can use it to forbid leaves before a certain depth.
6#[derive(
7    SaveLoad,Debug, Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate,
8    AiJsonTemplateWithJustification,
9    Builder, Default, Getters
10)]
11#[builder(pattern="owned", setter(into))]
12#[getset(get="pub")]
13pub struct WeightedNodeVariantPolicyWithLimits {
14
15    /// Probability weight for aggregator selection
16    aggregator_weight:  f32,
17
18    /// Probability weight for dispatch selection
19    dispatch_weight:    f32,
20
21    /// Probability weight for leaf-holder selection
22    leaf_holder_weight: f32,
23
24    /// Aggregator disallowed deeper than this level (if set).
25    #[serde(default)]
26    #[builder(default)]
27    #[serde(deserialize_with = "fuzzy_option_u8")]
28    aggregator_max_depth: Option<u8>,
29
30    /// Dispatch disallowed deeper than this level (if set).
31    #[serde(default)]
32    #[builder(default)]
33    #[serde(deserialize_with = "fuzzy_option_u8")]
34    dispatch_max_depth: Option<u8>,
35
36    /// Leaf-holder disallowed before this level (if set).
37    #[serde(default)]
38    #[builder(default)]
39    #[serde(deserialize_with = "fuzzy_option_u8")]
40    leaf_min_depth: Option<u8>,
41}
42
43impl FuzzyFromJsonValue for JustifiedWeightedNodeVariantPolicyWithLimits {
44    fn fuzzy_from_json_value(value: &serde_json::Value)
45        -> Result<Self, FuzzyFromJsonValueError>
46    {
47        trace!("(JustifiedWeightedNodeVariantPolicyWithLimits) Entering fuzzy_from_json_value");
48
49        if value.is_null() {
50            debug!("(JustifiedWeightedNodeVariantPolicyWithLimits) Null => aggregator_weight=0, etc.");
51            return Ok(Self {
52                aggregator_weight: 0.0,
53                aggregator_weight_confidence: 0.0,
54                aggregator_weight_justification: String::new(),
55                dispatch_weight: 0.0,
56                dispatch_weight_confidence: 0.0,
57                dispatch_weight_justification: String::new(),
58                leaf_holder_weight: 0.0,
59                leaf_holder_weight_confidence: 0.0,
60                leaf_holder_weight_justification: String::new(),
61
62                aggregator_max_depth: None,
63                aggregator_max_depth_confidence: 0.0,
64                aggregator_max_depth_justification: String::new(),
65
66                dispatch_max_depth: None,
67                dispatch_max_depth_confidence: 0.0,
68                dispatch_max_depth_justification: String::new(),
69
70                leaf_min_depth: None,
71                leaf_min_depth_confidence: 0.0,
72                leaf_min_depth_justification: String::new(),
73            });
74        }
75
76        let mut obj = match value.as_object() {
77            Some(m) => {
78                trace!("(JustifiedWeightedNodeVariantPolicyWithLimits) Found object => flattening if needed.");
79                m.clone()
80            }
81            None => {
82                error!("(JustifiedWeightedNodeVariantPolicyWithLimits) Not an object => fail!");
83                return Err(FuzzyFromJsonValueError::NotAnObject {
84                    target_type: "JustifiedWeightedNodeVariantPolicyWithLimits",
85                    actual: value.clone(),
86                });
87            }
88        };
89
90        flatten_all_fields(&mut obj);
91
92        // aggregator_weight => f32
93        let agg_f64 = get_f64_field(&obj, "aggregator_weight", "JustifiedWeightedNodeVariantPolicyWithLimits")
94            .unwrap_or(0.0);
95        let aggregator_weight = agg_f64 as f32;
96        let agg_conf = get_f64_field(&obj, "aggregator_weight_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
97            .unwrap_or(0.0);
98        let agg_just = get_string_field(&obj, "aggregator_weight_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
99            .unwrap_or_default();
100
101        // dispatch_weight => f32
102        let disp_f64 = get_f64_field(&obj, "dispatch_weight", "JustifiedWeightedNodeVariantPolicyWithLimits")
103            .unwrap_or(0.0);
104        let dispatch_weight = disp_f64 as f32;
105        let disp_conf = get_f64_field(&obj, "dispatch_weight_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
106            .unwrap_or(0.0);
107        let disp_just = get_string_field(&obj, "dispatch_weight_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
108            .unwrap_or_default();
109
110        // leaf_holder_weight => f32
111        let leaf_f64 = get_f64_field(&obj, "leaf_holder_weight", "JustifiedWeightedNodeVariantPolicyWithLimits")
112            .unwrap_or(0.0);
113        let leaf_holder_weight = leaf_f64 as f32;
114        let leaf_conf = get_f64_field(&obj, "leaf_holder_weight_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
115            .unwrap_or(0.0);
116        let leaf_just = get_string_field(&obj, "leaf_holder_weight_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
117            .unwrap_or_default();
118
119        // aggregator_max_depth => fuzzy_option_u8
120        let amd_val = obj.get("aggregator_max_depth");
121        let aggregator_max_depth = match amd_val {
122            Some(val) => {
123                match fuzzy_option_u8_from_value(val) {
124                    Ok(opt) => opt,
125                    Err(e) => {
126                        error!("(JustifiedWeightedNodeVariantPolicyWithLimits) aggregator_max_depth parse error => {:?}", e);
127                        return Err(FuzzyFromJsonValueError::Other {
128                            target_type: "JustifiedWeightedNodeVariantPolicyWithLimits",
129                            detail: format!("Error aggregator_max_depth: {:?}", e),
130                        });
131                    }
132                }
133            }
134            None => None,
135        };
136        let amd_conf = get_f64_field(&obj, "aggregator_max_depth_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
137            .unwrap_or(0.0);
138        let amd_just = get_string_field(&obj, "aggregator_max_depth_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
139            .unwrap_or_default();
140
141        // dispatch_max_depth => fuzzy_option_u8
142        let dmd_val = obj.get("dispatch_max_depth");
143        let dispatch_max_depth = match dmd_val {
144            Some(val) => {
145                match fuzzy_option_u8_from_value(val) {
146                    Ok(opt) => opt,
147                    Err(e) => {
148                        error!("(JustifiedWeightedNodeVariantPolicyWithLimits) dispatch_max_depth parse error => {:?}", e);
149                        return Err(FuzzyFromJsonValueError::Other {
150                            target_type: "JustifiedWeightedNodeVariantPolicyWithLimits",
151                            detail: format!("Error dispatch_max_depth: {:?}", e),
152                        });
153                    }
154                }
155            }
156            None => None,
157        };
158        let dmd_conf = get_f64_field(&obj, "dispatch_max_depth_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
159            .unwrap_or(0.0);
160        let dmd_just = get_string_field(&obj, "dispatch_max_depth_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
161            .unwrap_or_default();
162
163        // leaf_min_depth => fuzzy_option_u8
164        let lmd_val = obj.get("leaf_min_depth");
165        let leaf_min_depth = match lmd_val {
166            Some(val) => {
167                match fuzzy_option_u8_from_value(val) {
168                    Ok(opt) => opt,
169                    Err(e) => {
170                        error!("(JustifiedWeightedNodeVariantPolicyWithLimits) leaf_min_depth parse error => {:?}", e);
171                        return Err(FuzzyFromJsonValueError::Other {
172                            target_type: "JustifiedWeightedNodeVariantPolicyWithLimits",
173                            detail: format!("Error leaf_min_depth: {:?}", e),
174                        });
175                    }
176                }
177            }
178            None => None,
179        };
180        let lmd_conf = get_f64_field(&obj, "leaf_min_depth_confidence", "JustifiedWeightedNodeVariantPolicyWithLimits")
181            .unwrap_or(0.0);
182        let lmd_just = get_string_field(&obj, "leaf_min_depth_justification", "JustifiedWeightedNodeVariantPolicyWithLimits")
183            .unwrap_or_default();
184
185        trace!("(JustifiedWeightedNodeVariantPolicyWithLimits) Successfully parsed => returning final struct.");
186        Ok(Self {
187            aggregator_weight,
188            aggregator_weight_confidence: agg_conf,
189            aggregator_weight_justification: agg_just,
190            dispatch_weight,
191            dispatch_weight_confidence: disp_conf,
192            dispatch_weight_justification: disp_just,
193            leaf_holder_weight,
194            leaf_holder_weight_confidence: leaf_conf,
195            leaf_holder_weight_justification: leaf_just,
196
197            aggregator_max_depth,
198            aggregator_max_depth_confidence: amd_conf,
199            aggregator_max_depth_justification: amd_just,
200
201            dispatch_max_depth,
202            dispatch_max_depth_confidence: dmd_conf,
203            dispatch_max_depth_justification: dmd_just,
204
205            leaf_min_depth,
206            leaf_min_depth_confidence: lmd_conf,
207            leaf_min_depth_justification: lmd_just,
208        })
209    }
210}