File size: 11,582 Bytes
2b9a95b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | """
Action Space Schema Definitions
Defines data structures for flexible action spaces including:
- Numerical actions (continuous values like DEF)
- Discrete actions (categorical choices like equipment)
- Boolean actions (on/off flags)
- Equipment effects (hidden from agent)
"""
from dataclasses import dataclass, field, asdict
from typing import Dict, List, Any, Optional
from enum import Enum
class ActionType(Enum):
"""Types of actions in the action space"""
NUMERICAL = "numerical"
DISCRETE = "discrete"
BOOLEAN = "boolean"
class DiscreteType(Enum):
"""Types of discrete action selection"""
SINGLE_CHOICE = "single_choice"
MULTI_CHOICE = "multi_choice"
@dataclass
class NumericalAction:
"""
A continuous numerical action (e.g., engine_def: 0-50)
"""
name: str
min: float
max: float
default: float
step: float = 1.0
description: str = ""
visible: bool = True
def validate(self, value: float) -> tuple[bool, Optional[str]]:
"""Validate a value for this action"""
if value < self.min:
return False, f"{self.name} must be >= {self.min}, got {value}"
if value > self.max:
return False, f"{self.name} must be <= {self.max}, got {value}"
return True, None
def to_agent_dict(self) -> Dict[str, Any]:
"""Convert to dict for agent view (no hidden info)"""
return {
"min": self.min,
"max": self.max,
"default": self.default,
"step": self.step,
"description": self.description
}
@dataclass
class DiscreteOption:
"""
A single option within a discrete action
"""
id: str
name: str
description: str
cost: float = 0.0
prerequisites: List[str] = field(default_factory=list)
incompatible_with: List[str] = field(default_factory=list)
def to_agent_dict(self) -> Dict[str, Any]:
"""Convert to dict for agent view"""
result = {
"name": self.name,
"description": self.description
}
if self.cost > 0:
result["cost"] = self.cost
if self.prerequisites:
result["requires"] = self.prerequisites
if self.incompatible_with:
result["incompatible_with"] = self.incompatible_with
return result
@dataclass
class DiscreteAction:
"""
A categorical choice action (e.g., coating: standard/stealth/armor)
"""
name: str
type: DiscreteType
description: str
options: Dict[str, DiscreteOption]
default: str
visible: bool = True
def validate(self, value: str) -> tuple[bool, Optional[str]]:
"""Validate a choice for this action"""
if value not in self.options:
valid_options = list(self.options.keys())
return False, f"Invalid {self.name}: '{value}'. Valid options: {valid_options}"
return True, None
def to_agent_dict(self) -> Dict[str, Any]:
"""Convert to dict for agent view"""
return {
"description": self.description,
"type": self.type.value,
"options": {
oid: opt.to_agent_dict()
for oid, opt in self.options.items()
},
"default": self.default
}
@dataclass
class BooleanAction:
"""
A boolean on/off action
"""
name: str
description: str
default: bool = False
visible: bool = True
def to_agent_dict(self) -> Dict[str, Any]:
"""Convert to dict for agent view"""
return {
"description": self.description,
"default": self.default
}
@dataclass
class EquipmentEffects:
"""
Hidden effects applied when equipment is selected.
These modify DroneSheet properties but are NOT visible to the agent.
"""
# Detection and stealth
detection_modifier: float = 0.0
signal_strength_modifier: float = 0.0
# Physical properties
agility_modifier: float = 0.0
weight_modifier: float = 0.0
def_multiplier: float = 1.0
# Special resistances
emi_resistance: float = 0.0
thermal_resistance: float = 0.0
# Component-specific HP modifiers
hp_modifiers: Dict[str, int] = field(default_factory=dict)
# Custom effects for experiment-specific mechanics
custom_effects: Dict[str, Any] = field(default_factory=dict)
def combine(self, other: 'EquipmentEffects') -> 'EquipmentEffects':
"""Combine two effect sets (additive for most, multiplicative for multipliers)"""
combined_hp = dict(self.hp_modifiers)
for comp, mod in other.hp_modifiers.items():
combined_hp[comp] = combined_hp.get(comp, 0) + mod
combined_custom = dict(self.custom_effects)
combined_custom.update(other.custom_effects)
return EquipmentEffects(
detection_modifier=self.detection_modifier + other.detection_modifier,
signal_strength_modifier=self.signal_strength_modifier + other.signal_strength_modifier,
agility_modifier=self.agility_modifier + other.agility_modifier,
weight_modifier=self.weight_modifier + other.weight_modifier,
def_multiplier=self.def_multiplier * other.def_multiplier,
emi_resistance=min(1.0, self.emi_resistance + other.emi_resistance),
thermal_resistance=min(1.0, self.thermal_resistance + other.thermal_resistance),
hp_modifiers=combined_hp,
custom_effects=combined_custom
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'EquipmentEffects':
"""Create from dictionary (for loading from JSON)"""
return cls(
detection_modifier=data.get('detection_modifier', 0.0),
signal_strength_modifier=data.get('signal_strength_modifier', 0.0),
agility_modifier=data.get('agility_modifier', 0.0),
weight_modifier=data.get('weight_modifier', 0.0),
def_multiplier=data.get('def_multiplier', 1.0),
emi_resistance=data.get('emi_resistance', 0.0),
thermal_resistance=data.get('thermal_resistance', 0.0),
hp_modifiers=data.get('hp_modifier', data.get('hp_modifiers', {})),
custom_effects=data.get('custom_effects', {})
)
@dataclass
class Constraints:
"""
Constraints on the action space
"""
total_def_budget: Optional[int] = None
total_equipment_slots: Optional[int] = None
max_weight: Optional[float] = None
custom_rules: List[str] = field(default_factory=list)
def to_agent_dict(self) -> Dict[str, Any]:
"""Convert to dict for agent view"""
result = {}
if self.total_def_budget is not None:
result["total_def_budget"] = self.total_def_budget
if self.total_equipment_slots is not None:
result["total_equipment_slots"] = self.total_equipment_slots
if self.max_weight is not None:
result["max_weight"] = self.max_weight
return result
@dataclass
class ActionSpaceConfig:
"""
Complete action space configuration for an experiment.
Contains:
- Numerical actions (DEF values, etc.)
- Discrete actions (equipment choices)
- Boolean actions (flags)
- Constraints
- Hidden effects mapping
"""
numerical: Dict[str, NumericalAction] = field(default_factory=dict)
discrete: Dict[str, DiscreteAction] = field(default_factory=dict)
boolean: Dict[str, BooleanAction] = field(default_factory=dict)
constraints: Constraints = field(default_factory=Constraints)
effects: Dict[str, EquipmentEffects] = field(default_factory=dict)
# Metadata
schema_version: str = "1.0"
experiment_name: str = ""
def get_agent_view(self) -> Dict[str, Any]:
"""
Return action space configuration visible to agent.
EXCLUDES hidden effects!
"""
result = {}
# Numerical actions
if self.numerical:
result["numerical"] = {
name: action.to_agent_dict()
for name, action in self.numerical.items()
if action.visible
}
# Discrete actions
if self.discrete:
result["discrete"] = {
name: action.to_agent_dict()
for name, action in self.discrete.items()
if action.visible
}
# Boolean actions
if self.boolean:
result["boolean"] = {
name: action.to_agent_dict()
for name, action in self.boolean.items()
if action.visible
}
# Constraints (excluding internal ones)
constraints = self.constraints.to_agent_dict()
if constraints:
result["constraints"] = constraints
return result
def get_defaults(self) -> Dict[str, Any]:
"""Get default values for all actions"""
defaults = {}
# Numerical defaults
for name, action in self.numerical.items():
defaults[name] = action.default
# Discrete defaults
equipment = {}
for name, action in self.discrete.items():
equipment[name] = action.default
if equipment:
defaults["equipment"] = equipment
# Boolean defaults
for name, action in self.boolean.items():
defaults[name] = action.default
return defaults
def validate_design(self, design: Dict[str, Any]) -> tuple[bool, Optional[str]]:
"""
Validate a complete design against this action space.
Args:
design: {"engine_def": 20, "equipment": {"coating": "stealth"}, ...}
Returns:
(is_valid, error_message)
"""
# Validate numerical values
for name, action in self.numerical.items():
if name in design:
valid, error = action.validate(design[name])
if not valid:
return False, error
# Validate discrete choices
equipment = design.get("equipment", {})
for name, action in self.discrete.items():
if name in equipment:
valid, error = action.validate(equipment[name])
if not valid:
return False, error
# Check DEF budget constraint
if self.constraints.total_def_budget is not None:
total_def = sum(
design.get(name, action.default)
for name, action in self.numerical.items()
if name.endswith("_def")
)
if total_def > self.constraints.total_def_budget:
return False, f"Total DEF ({total_def}) exceeds budget ({self.constraints.total_def_budget})"
return True, None
def compute_effects(self, design: Dict[str, Any]) -> EquipmentEffects:
"""
Compute combined equipment effects for a design.
This is the HIDDEN mapping from choices to effects.
"""
combined = EquipmentEffects()
equipment = design.get("equipment", {})
for slot, choice in equipment.items():
if choice in self.effects:
combined = combined.combine(self.effects[choice])
# Boolean flags can also have effects
for name, action in self.boolean.items():
if design.get(name, action.default):
if name in self.effects:
combined = combined.combine(self.effects[name])
return combined
|