|
| 1 | +import argparse |
| 2 | +import importlib.util |
| 3 | +import json |
| 4 | +import random |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from scml.oneshot import SCML2024OneShotWorld |
| 10 | + |
| 11 | + |
| 12 | +def safe_class_name(player_name: str) -> str: |
| 13 | + safe = re.sub(r"\W+", "_", player_name) |
| 14 | + if not safe or safe[0].isdigit(): |
| 15 | + safe = f"player_{safe}" |
| 16 | + return f"CodeClash_{safe}" |
| 17 | + |
| 18 | + |
| 19 | +def load_agent_class(player_name: str, path: str): |
| 20 | + module_name = f"codeclash_scml_{safe_class_name(player_name).lower()}" |
| 21 | + spec = importlib.util.spec_from_file_location(module_name, path) |
| 22 | + if spec is None or spec.loader is None: |
| 23 | + raise RuntimeError(f"Could not load module spec from {path}") |
| 24 | + module = importlib.util.module_from_spec(spec) |
| 25 | + spec.loader.exec_module(module) |
| 26 | + if not hasattr(module, "MyAgent"): |
| 27 | + raise RuntimeError(f"{path} does not define MyAgent") |
| 28 | + base_class = module.MyAgent |
| 29 | + return type(safe_class_name(player_name), (base_class,), {"__module__": module.__name__}) |
| 30 | + |
| 31 | + |
| 32 | +def run_world(agent_classes: dict[str, type], *, sim_idx: int, steps: int, lines: int) -> dict: |
| 33 | + seed = 1729 + sim_idx |
| 34 | + random.seed(seed) |
| 35 | + np.random.seed(seed) |
| 36 | + |
| 37 | + player_names = list(agent_classes.keys()) |
| 38 | + offset = sim_idx % len(player_names) |
| 39 | + ordered_names = player_names[offset:] + player_names[:offset] |
| 40 | + wrapped_classes = [agent_classes[name] for name in ordered_names] |
| 41 | + class_to_player = {cls.__name__: player for player, cls in agent_classes.items()} |
| 42 | + |
| 43 | + config = SCML2024OneShotWorld.generate( |
| 44 | + agent_types=wrapped_classes, |
| 45 | + agent_processes=[0 for _ in wrapped_classes], |
| 46 | + n_steps=steps, |
| 47 | + n_processes=1, |
| 48 | + n_lines=lines, |
| 49 | + random_agent_types=False, |
| 50 | + ) |
| 51 | + world = SCML2024OneShotWorld( |
| 52 | + **config, |
| 53 | + no_logs=True, |
| 54 | + compact=True, |
| 55 | + fast=True, |
| 56 | + agent_name_reveals_type=True, |
| 57 | + agent_name_reveals_position=True, |
| 58 | + ) |
| 59 | + world.run() |
| 60 | + |
| 61 | + raw_scores = world.scores() |
| 62 | + player_scores = {player: 0.0 for player in player_names} |
| 63 | + details = [] |
| 64 | + for agent_id, score in raw_scores.items(): |
| 65 | + world_agent = world.agents[agent_id] |
| 66 | + player = class_to_player.get(world_agent.short_type_name) |
| 67 | + if player is None: |
| 68 | + continue |
| 69 | + numeric_score = float(score) |
| 70 | + player_scores[player] = numeric_score |
| 71 | + details.append( |
| 72 | + { |
| 73 | + "sim": sim_idx, |
| 74 | + "player": player, |
| 75 | + "world_agent_id": agent_id, |
| 76 | + "score": numeric_score, |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + return {"scores": player_scores, "details": details} |
| 81 | + |
| 82 | + |
| 83 | +def parse_agent_arg(value: str) -> tuple[str, str]: |
| 84 | + if "=" not in value: |
| 85 | + raise argparse.ArgumentTypeError("--agent values must be NAME=/path/to/scml_agent.py") |
| 86 | + name, path = value.split("=", 1) |
| 87 | + if not name: |
| 88 | + raise argparse.ArgumentTypeError("agent name cannot be empty") |
| 89 | + if not Path(path).exists(): |
| 90 | + raise argparse.ArgumentTypeError(f"agent path does not exist: {path}") |
| 91 | + return name, path |
| 92 | + |
| 93 | + |
| 94 | +def main() -> None: |
| 95 | + parser = argparse.ArgumentParser() |
| 96 | + parser.add_argument("--agent", action="append", type=parse_agent_arg, required=True) |
| 97 | + parser.add_argument("--sims", type=int, default=3) |
| 98 | + parser.add_argument("--steps", type=int, default=10) |
| 99 | + parser.add_argument("--lines", type=int, default=2) |
| 100 | + parser.add_argument("--output", required=True) |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + agent_classes = {name: load_agent_class(name, path) for name, path in args.agent} |
| 104 | + totals = {name: 0.0 for name in agent_classes} |
| 105 | + details = [] |
| 106 | + |
| 107 | + for sim_idx in range(args.sims): |
| 108 | + result = run_world(agent_classes, sim_idx=sim_idx, steps=args.steps, lines=args.lines) |
| 109 | + for player, score in result["scores"].items(): |
| 110 | + totals[player] += score |
| 111 | + details.extend(result["details"]) |
| 112 | + |
| 113 | + averages = {player: score / args.sims for player, score in totals.items()} |
| 114 | + output = { |
| 115 | + "average_scores": averages, |
| 116 | + "total_scores": totals, |
| 117 | + "sims": args.sims, |
| 118 | + "details": [json.dumps(item, sort_keys=True) for item in details], |
| 119 | + } |
| 120 | + Path(args.output).parent.mkdir(parents=True, exist_ok=True) |
| 121 | + Path(args.output).write_text(json.dumps(output, indent=2, sort_keys=True)) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments