Files
sama/src/dcos/learning/engine.py
T
Celes Renata 57619860d5 feat: implement DCOS source tree, tests, and build configuration
Complete implementation of the DCOS package including:
- 40+ Python source files across agents, communication, core, learning, memory, protocols, utils
- pyproject.toml build configuration
- 102 unit tests across all subsystems
- Fixed flake.nix (Python 3.12, proper dependencies, pyproject build)
- Fixed .woodpecker.yml (correct paths, removed silent-fail flags)
- Added .gitignore
- Cleared stale pytest cache
2026-08-02 03:29:55 -07:00

45 lines
1.3 KiB
Python

"""
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)