Skip to main content

omega_cognitive_kernel/planning/
mod.rs

1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use alloc::collections::BTreeMap;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct PlanStep {
9    pub action: String,
10    pub args: BTreeMap<String, f64>,
11    pub rationale: String,
12    pub estimated_risk: f64,
13    pub estimated_progress: f64,
14    pub score: f64,
15}
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct Plan {
19    pub horizon: usize,
20    pub steps: Vec<PlanStep>,
21    pub total_score: f64,
22}
23
24#[derive(Debug, Clone)]
25pub struct ShortHorizonPlanner {
26    pub horizon: usize,
27    pub progress_weight: f64,
28    pub risk_weight: f64,
29}
30
31impl Default for ShortHorizonPlanner {
32    fn default() -> Self {
33        Self {
34            horizon: 3,
35            progress_weight: 1.0,
36            risk_weight: 0.45,
37        }
38    }
39}
40
41impl ShortHorizonPlanner {
42    pub fn new(horizon: usize) -> Self {
43        Self {
44            horizon: horizon.max(1),
45            ..Self::default()
46        }
47    }
48
49    pub fn plan(
50        &self,
51        state: &BTreeMap<String, f64>,
52        goal: &BTreeMap<String, f64>,
53        candidates: &[(String, f64)],
54        stall: bool,
55    ) -> Plan {
56        let mut sim = state.clone();
57        let mut steps = Vec::new();
58        let mut total_score = 0.0;
59        let blend = if stall { 0.45 } else { 0.30 };
60
61        for _ in 0..self.horizon {
62            let mut best: Option<PlanStep> = None;
63            for (action, risk) in candidates {
64                let mut next_state = sim.clone();
65                let mut args = BTreeMap::new();
66                let mut dist_before = 0.0;
67                let mut dist_after = 0.0;
68
69                for (k, target) in goal {
70                    let cur = sim.get(k).copied().unwrap_or(0.0);
71                    let next_val = cur + blend * (target - cur);
72                    dist_before += libm::fabs(cur - target);
73                    dist_after += libm::fabs(next_val - target);
74                    args.insert(k.clone(), next_val);
75                    next_state.insert(k.clone(), next_val);
76                }
77
78                let progress = (dist_before - dist_after).max(0.0);
79                let score = self.progress_weight * progress - self.risk_weight * (*risk);
80                let replace = best.as_ref().map(|b| score > b.score).unwrap_or(true);
81                if replace {
82                    best = Some(PlanStep {
83                        action: action.clone(),
84                        args,
85                        rationale: if stall {
86                            "stall-explore".into()
87                        } else {
88                            "greedy".into()
89                        },
90                        estimated_risk: *risk,
91                        estimated_progress: progress,
92                        score,
93                    });
94                }
95            }
96            if let Some(step) = best {
97                for (k, v) in &step.args {
98                    sim.insert(k.clone(), *v);
99                }
100                total_score += step.score;
101                steps.push(step);
102            } else {
103                break;
104            }
105        }
106
107        Plan {
108            horizon: steps.len(),
109            steps,
110            total_score,
111        }
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn planner_proposes_horizon_steps_without_executing() {
121        let planner = ShortHorizonPlanner::new(2);
122        let mut state = BTreeMap::new();
123        state.insert("x".into(), 0.0);
124        let mut goal = BTreeMap::new();
125        goal.insert("x".into(), 1.0);
126        let candidates = vec![("measure".into(), 0.1), ("risky".into(), 0.8)];
127
128        let plan = planner.plan(&state, &goal, &candidates, false);
129        assert_eq!(plan.horizon, 2);
130        assert_eq!(plan.steps[0].action, "measure");
131        assert!(plan.total_score > 0.0);
132    }
133}