Source code for terrarium.environments.dcops.personal_assistant.personal_assistant_env

# pyright: basic
"""
PersonalAssistant Environment Adaptor

Adaptor to integrate the PersonalAssistant domain (outfit coordination)
with the black_boards_v5 communication protocol framework.

The PersonalAssistant environment involves agents coordinating to select outfits
that satisfy personal preferences and inter-agent constraints (color matching).
"""

from pathlib import Path
import random
from typing import Dict, List, Any, TYPE_CHECKING, Tuple, Mapping

# CoLLAB v2 problem-layer imports (made available via terrarium.environments.dcops)
from problem_layer.personal_assistant import PersonalAssistantConfig, generate_instance
from problem_layer.personal_assistant.problem import Outfit
from problem_layer.base import ProblemDefinition
import logging

# Import abstract environment interface and logger
from terrarium.environments.abstract_environment import AbstractEnvironment
from terrarium.utils import (
    clear_seed_directories,
    extract_model_info,
    get_tag_model_subdir,
    get_run_timestamp,
    build_log_dir,
)
from .personal_assistant_prompts import PersonalAssistantPrompts

logger = logging.getLogger(__name__)

# Use TYPE_CHECKING to avoid circular import (BaseAgent → ToolsetDiscovery → EnvironmentTools → Environment → BaseAgent)
if TYPE_CHECKING:
    from terrarium.agents.base import BaseAgent


[docs] class PersonalAssistantEnvironment(AbstractEnvironment): """ PersonalAssistant environment adaptor for outfit coordination tasks. Agents coordinate to select outfits that satisfy personal preferences and inter-agent constraints (color matching/avoiding). """ def __init__(self, communication_protocol, config, tool_logger): """Initialize the PersonalAssistant environment.""" self.full_config = config self.env_config: Dict[str, Any] = config["environment"] self.simulation_config = config["simulation"] # Get the correct seed from simulation config self.current_seed = int(self.simulation_config["seed"]) # Instance management # Partial joint assignment: variable_name -> chosen outfit number (1-based) self.assignment: Dict[str, Any] = {} self.outfit_selections: Dict[str, Outfit] = {} self.tool_logger = tool_logger self.agent_names: List[str] = [] # Agent names (renamed from agents_list) self.communication_protocol = communication_protocol self.communication_protocol.environment = self self.run_timestamp = get_run_timestamp(self.full_config) self.current_iteration = 0 self.max_iterations = self.simulation_config.get("max_iterations", None) self.max_planning_rounds = self.simulation_config.get( "max_planning_rounds", None ) # Clear seed directories FIRST to ensure clean state for this run clear_seed_directories( self.__class__.__name__, self.current_seed, self.full_config ) # ---- Build CoLLAB v2 instance ------------------------------------------------- network_cfg = config.get("communication_network") or {} assert network_cfg is not None and network_cfg != {}, ( "communication_network config must be specified" ) num_agents = network_cfg.get("num_agents") assert ( num_agents is not None and isinstance(num_agents, int) and not isinstance(num_agents, bool) ), "communication_network.num_agents in config must be specified as an integer" min_outfits = self.env_config.get("min_outfits_per_agent", 4) max_outfits = self.env_config.get("max_outfits_per_agent", 6) density = self.env_config.get("density") if density is None: # Back-compat: approximate density from max_degree if present max_degree = self.env_config.get("max_degree", 3) try: density = float(max_degree) / max(1.0, float(num_agents) - 1.0) except Exception: density = 0.3 density = max(0.0, min(1.0, float(density))) collab_cfg = PersonalAssistantConfig( num_agents=int(num_agents), density=float(density), min_outfits_per_agent=int(min_outfits), max_outfits_per_agent=int(max_outfits), rng_seed=int(self.current_seed), ) dcops_root = Path(__file__).resolve().parents[1] instance_dir = ( dcops_root / "outputs" / "collab_instances" / "personal_assistant" / f"seed_{self.current_seed}" ) self.instance = generate_instance(collab_cfg, instance_dir) self.problem: ProblemDefinition = self.instance.problem # Score tracking self.joint_reward_history: List[float] = [] self.agent_names = list(self.problem.agents.keys()) self.agents: List["BaseAgent"] = [] # Set this later in main.py in case agents get different clients or settings self.max_joint_reward = self.compute_max_joint_reward() # Initialize prompts (put this after all other instance variables) self.prompts = PersonalAssistantPrompts(self, self.full_config) # Initialize score tracking self.agent_rewards_history: Dict[str, List[float]] = { agent: [] for agent in self.agent_names } logger.info( "%s initialized with %s agents", self.__class__.__name__, len(self.agent_names), ) logger.info("Agent Names: %s", ", ".join(self.agent_names))
[docs] async def async_init(self): """Async initialization - create communication blackboards from the supplied network.""" await super().async_init()
[docs] def build_agent_context( self, agent_name: str, phase: str, iteration: int, **kwargs ) -> Dict[str, Any]: """ Build environment-specific context for an agent's turn. This is used in the planning and execution phases in CommunicationProtocol. Args: agent_name: Name of the agent phase: Current phase ("planning" or "execution") iteration: Current iteration number **kwargs: Additional context Returns: Dictionary with agent context """ # Clear outfit selections at the start of each new iteration's planning phase # to allow agents to make new choices if ( phase == "planning" and iteration > 1 and (self.outfit_selections or self.assignment) ): logger.info( "%s: Clearing selections for iteration %s", self.__class__.__name__, iteration, ) self.outfit_selections = {} self.assignment = {} if not self.instance: return {"error": "Environment not initialized"} context = { "agent_name": agent_name, "phase": phase, "iteration": iteration, "outfit_selections": self.outfit_selections.copy(), "total_agents": len(self.agent_names), "selections_made": len(self.outfit_selections), "selections_remaining": len(self.agent_names) - len(self.outfit_selections), } # Add configuration info (consistent with other DCOP environments) assert self.env_config is not None, "Config not available" context["max_iterations"] = self.env_config.get("max_iterations", 1) # Add additional context from kwargs (like planning_round) for key, value in kwargs.items(): context[key] = value return context
[docs] def done(self, iteration: int) -> bool: """Return True when the environment is finished.""" # Check max iterations first (consistent with other DCOP environments) assert self.env_config is not None, "Config not available" max_iterations = self.env_config.get("max_iterations", 1) if iteration > max_iterations: logger.info( "Reached max iterations (%s) - stopping simulation", max_iterations ) return True # Stop early if all variables assigned and max reward reached total_vars = len(self.problem.variables) if len(self.assignment) == total_vars and self.instance: joint_reward = self.joint_reward(self.assignment) if self.max_joint_reward and joint_reward >= self.max_joint_reward: logger.info( "All constraints satisfied (score: %s/%s) - simulation complete", joint_reward, self.max_joint_reward, ) return True logger.info( "All agents selected but constraints not fully satisfied (score: %s/%s) - continuing", joint_reward, self.max_joint_reward, ) return False
[docs] def compute_max_joint_reward(self) -> float: """Return the optimal joint reward for the environment.""" return float(getattr(self.instance, "max_utility", 0.0))
[docs] def joint_reward(self, actions: Mapping[str, Any]) -> float: """Return the (partial) joint reward for a joint assignment.""" total_reward, _ = self._rewards(actions) return total_reward
[docs] def agent_reward(self, actions: Mapping[str, Any], agent: str) -> float: """Return the reward attributed to a single agent.""" _, local_rewards = self._rewards(actions) assert agent in local_rewards, f"Agent {agent} not found in local rewards" local_reward = local_rewards.get(agent) assert local_reward is not None, f"Local reward for agent {agent} is None" return local_reward
def _rewards(self, actions: Mapping[str, Any]) -> Tuple[float, Dict[str, float]]: """ Compute joint reward and per-agent rewards for a given joint assignment. Factors whose full scope has been assigned contribute to the total reward. Per-agent rewards are attributed evenly to variable owners in scope. """ total_reward = 0.0 local_rewards: Dict[str, float] = {agent: 0.0 for agent in self.agent_names} for factor in self.problem.factors: if not all(var in actions for var in factor.scope): continue try: reward = factor.evaluate(actions) except Exception: continue total_reward += reward owners = { self.problem.variables[v].owner for v in factor.scope if v in self.problem.variables } if owners: share = reward / len(owners) for owner in owners: local_rewards[owner] += share return total_reward, local_rewards
[docs] def log_iteration(self, iteration: int) -> None: """ Log the current state of the environment. Args: iteration: Current iteration number """ logger.info( "=== %s State - Iteration %s ===", self.__class__.__name__, iteration ) logger.info( "Agents: %s total, %s selected outfits", len(self.agent_names), len(self.outfit_selections), ) if self.outfit_selections: logger.info("Current Selections:") for agent, outfit in self.outfit_selections.items(): logger.info(" %s: %s, %s", agent, outfit.article, outfit.color) remaining = [ agent for agent in self.agent_names if agent not in self.outfit_selections ] if remaining: logger.info("Remaining agents: %s", ", ".join(remaining)) joint_reward, agent_rewards = self._rewards(self.assignment) ratio = joint_reward / self.max_joint_reward if self.max_joint_reward else 0.0 logger.info( "Current Joint Reward: %.2f (ratio %.2f%%)", joint_reward, ratio * 100.0 ) # Track scores for every iteration self._track_scores(iteration, joint_reward, agent_rewards)
def _track_scores( self, iteration: int, joint_reward: float, agent_rewards: Dict[str, float] ) -> None: """Track scores and write logs.""" import json from datetime import datetime # Update score histories self.joint_reward_history.append(joint_reward) for agent, reward in agent_rewards.items(): if agent in self.agent_rewards_history: self.agent_rewards_history[agent].append(reward) # Create logs directory with seed subdirectory # Get tag_model subdirectory tag_model = get_tag_model_subdir(self.full_config) log_dir = build_log_dir( self.__class__.__name__, tag_model, self.current_seed, self.run_timestamp ) log_dir.mkdir(parents=True, exist_ok=True) # Log scores to JSON score_entry = { "environment": self.__class__.__name__, "iteration": iteration, "timestamp": datetime.now().isoformat(), "joint_reward": joint_reward, "joint_reward_ratio": joint_reward / self.max_joint_reward, "max_joint_reward": self.max_joint_reward, "agent_rewards": agent_rewards, "average_agent_reward": sum(agent_rewards.values()) / len(agent_rewards), "model_info": extract_model_info(self.full_config), "full_config": self.full_config, "total_agents": len(agent_rewards), "variables_assigned": len(self.assignment), "total_variables": len(self.problem.variables), } data_file = log_dir / f"data_iteration_{iteration}.json" with open(data_file, "w") as f: json.dump(score_entry, f, indent=2, ensure_ascii=False) def _fill_random_unassigned_variables(self) -> int: """Assign uniform-random domain values to any missing/invalid variables.""" problem = getattr(self, "problem", None) instance = getattr(self, "instance", None) if problem is None: return 0 seed = int(getattr(self, "current_seed", 0)) rng = random.Random(seed + 99173) filled = 0 filled_assignment: Dict[str, Any] = dict(self.assignment) for var_name in sorted(problem.variables): spec = problem.variables[var_name] domain = list(getattr(spec, "domain", []) or []) if not domain: continue value = filled_assignment.get(var_name) if value not in domain: # Common case: a stringified int selection. try: cast_value = int(value) except Exception: cast_value = None if cast_value in domain: value = cast_value else: value = rng.choice(domain) filled += 1 filled_assignment[var_name] = value self.assignment = filled_assignment # Keep the (agent -> Outfit) view consistent with the filled assignment so # summaries include all agents even if some never acted. wardrobe = getattr(instance, "wardrobe", None) if instance is not None else None if isinstance(wardrobe, dict) and wardrobe: selections: Dict[str, Outfit] = {} for var_name in sorted(problem.variables): spec = problem.variables[var_name] agent_id = spec.owner options = wardrobe.get(agent_id) or [] if not options: continue try: idx = int(filled_assignment[var_name]) - 1 except Exception: idx = 0 idx = max(0, min(len(options) - 1, idx)) selections[agent_id] = options[idx] self.outfit_selections = selections return filled
[docs] def get_final_summary(self) -> Dict[str, Any]: """Get a final summary of the entire simulation.""" total_vars = len(self.problem.variables) assignment_filling = self.assignment_filling_enabled() random_fallback_fills = ( self._fill_random_unassigned_variables() if assignment_filling and self.instance else 0 ) notes = [self.UNASSIGNED_VARIABLE_FALLBACK_NOTE] if assignment_filling else [] final_selections = ( f"{len(self.outfit_selections)}/{len(self.agent_names)} agents" ) if not self.instance or len(self.assignment) != total_vars: return { "status": "incomplete", "variables_assigned": len(self.assignment), "total_variables": total_vars, "total_agents": len(self.agent_names), "final_selections": final_selections, "random_fallback_fills": random_fallback_fills, "notes": notes, } joint_reward, agent_rewards = self._rewards(self.assignment) return { "status": "complete", "joint_reward": joint_reward / self.max_joint_reward if self.max_joint_reward > 0 else 0.0, "raw_joint_reward": joint_reward, "average_agent_reward": sum(agent_rewards.values()) / len(agent_rewards) if agent_rewards else 0.0, "agent_rewards": agent_rewards, "outfit_selections": { agent: {"article": outfit.article, "color": outfit.color} for agent, outfit in self.outfit_selections.items() }, "total_variables": total_vars, "variables_assigned": len(self.assignment), "total_agents": len(self.agent_names), "final_selections": final_selections, "random_fallback_fills": random_fallback_fills, "notes": notes, }