omega_cognitive_kernel/l2/
modulator.rs1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use crate::l2::fixed_point::FixedPointResult;
5use crate::types::RuleEffect;
6use alloc::collections::BTreeMap;
7
8pub struct MetaModulator;
10
11impl MetaModulator {
12 pub fn apply(
14 &self,
15 fp: &FixedPointResult,
16 mut biases: BTreeMap<String, f64>,
17 mut constraints: BTreeMap<String, f64>,
18 ) -> (BTreeMap<String, f64>, BTreeMap<String, f64>) {
19 for (k, v) in &fp.modulation.bias_delta {
20 *biases.entry(k.clone()).or_insert(0.0) += *v;
21 }
22 for (k, v) in &fp.modulation.constraint_delta {
23 let entry = constraints.entry(k.clone()).or_insert(0.65);
24 *entry = (*entry + *v).clamp(0.1, 0.95);
25 }
26 (biases, constraints)
27 }
28
29 pub fn apply_scaled(
31 &self,
32 fp: &FixedPointResult,
33 gain: f64,
34 mut biases: BTreeMap<String, f64>,
35 mut constraints: BTreeMap<String, f64>,
36 ) -> (BTreeMap<String, f64>, BTreeMap<String, f64>) {
37 let gain = gain.clamp(0.0, 1.0);
38 for (k, v) in &fp.modulation.bias_delta {
39 *biases.entry(k.clone()).or_insert(0.0) += *v * gain;
40 }
41 for (k, v) in &fp.modulation.constraint_delta {
42 let entry = constraints.entry(k.clone()).or_insert(0.65);
43 *entry = (*entry + *v * gain).clamp(0.1, 0.95);
44 }
45 (biases, constraints)
46 }
47
48 pub fn apply_to_effect(&self, fp: &FixedPointResult, effect: &RuleEffect) -> RuleEffect {
51 let mut e = effect.clone();
52 e.explore_bonus += fp.modulation.explore_bonus_delta;
53 e.risk_tolerance += fp.modulation.risk_tolerance_delta;
54 e.sanitize()
55 }
56
57 pub fn apply_to_effect_scaled(
59 &self,
60 fp: &FixedPointResult,
61 gain: f64,
62 effect: &RuleEffect,
63 ) -> RuleEffect {
64 let mut e = effect.clone();
65 let gain = gain.clamp(0.0, 1.0);
66 e.explore_bonus += fp.modulation.explore_bonus_delta * gain;
67 e.risk_tolerance += fp.modulation.risk_tolerance_delta * gain;
68 e.sanitize()
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use crate::l2::fixed_point::Modulation;
76
77 #[test]
78 fn soft_merge_clamps_via_sanitize() {
79 let modu = MetaModulator;
80 let fp = FixedPointResult {
81 mode: "t".into(),
82 aligned: true,
83 alignment_score: 0.9,
84 score: 0.9,
85 breakdown: BTreeMap::new(),
86 modulation: Modulation {
87 bias_delta: BTreeMap::new(),
88 constraint_delta: BTreeMap::new(),
89 explore_bonus_delta: 5.0, risk_tolerance_delta: -2.0,
91 },
92 };
93 let base = RuleEffect::default();
94 let merged = modu.apply_to_effect(&fp, &base);
95 assert!(merged.explore_bonus <= 1.5);
96 assert!(merged.risk_tolerance >= 0.15);
97 }
98}