""" Resource allocator — manages compute and memory resource distribution among agents. """ from __future__ import annotations from typing import Dict, List, Optional, Tuple class ResourceAllocator: """Allocates system resources (compute, memory, bandwidth) among agents.""" def __init__(self) -> None: self._total_cpu: float = 100.0 self._total_memory: float = 1024.0 self._allocations: Dict[str, Dict[str, float]] = {} def allocate(self, agent_id: str, cpu: float, memory: float) -> bool: if cpu <= 0 or memory <= 0: return False remaining_cpu = self._total_cpu - sum( a.get("cpu", 0) for a in self._allocations.values() ) remaining_memory = self._total_memory - sum( a.get("memory", 0) for a in self._allocations.values() ) if cpu > remaining_cpu or memory > remaining_memory: return False self._allocations[agent_id] = {"cpu": cpu, "memory": memory} return True def release(self, agent_id: str) -> bool: return agent_id in self._allocations and bool( self._allocations.pop(agent_id, None) ) def get_allocation(self, agent_id: str) -> Dict[str, float]: return self._allocations.get(agent_id, {"cpu": 0.0, "memory": 0.0}) def utilization(self) -> Dict[str, float]: total_cpu = sum(a.get("cpu", 0) for a in self._allocations.values()) total_mem = sum(a.get("memory", 0) for a in self._allocations.values()) return { "cpu": total_cpu / self._total_cpu, "memory": total_mem / self._total_memory, }