""" Learning engine — reinforcement learning and model updates for agents. """ from __future__ import annotations from typing import Any, Dict, List, Optional from dataclasses import dataclass, field @dataclass class Experience: state: Dict[str, Any] action: str reward: float next_state: Optional[Dict[str, Any]] = None class LearningEngine: """Reinforcement learning engine that improves agent behavior over time.""" def __init__(self) -> None: self._experiences: List[Experience] = [] self._policy: Dict[str, float] = {} def record_experience(self, exp: Experience) -> None: self._experiences.append(exp) self._update_policy(exp) def _update_policy(self, exp: Experience) -> None: current = self._policy.get(exp.action, 0.0) self._policy[exp.action] = current + exp.reward * 0.1 def get_action_score(self, action: str) -> float: return self._policy.get(action, 0.0) def best_action(self, actions: List[str]) -> Optional[str]: if not actions: return None scored = [(a, self._policy.get(a, 0.0)) for a in actions] scored.sort(key=lambda x: x[1], reverse=True) return scored[0][0] def experience_count(self) -> int: return len(self._experiences)