|
| 1 | +import json |
| 2 | +import shlex |
| 3 | +import subprocess |
| 4 | + |
| 5 | +from codeclash.agents.player import Player |
| 6 | +from codeclash.arenas.arena import CodeArena, RoundStats |
| 7 | +from codeclash.constants import RESULT_TIE |
| 8 | +from codeclash.utils.environment import assert_zero_exit_code |
| 9 | + |
| 10 | +RESULTS_JSON = "abides_results.json" |
| 11 | +CRASH_SCORE = -1_000_000.0 |
| 12 | + |
| 13 | + |
| 14 | +class ABIDESArena(CodeArena): |
| 15 | + name: str = "ABIDES" |
| 16 | + submission: str = "abides_agent.py" |
| 17 | + description: str = """ABIDES is an agent-based market simulator for financial-market research. |
| 18 | +
|
| 19 | +Your bot is a Python file named `abides_agent.py` that defines a class named `MyAgent`. |
| 20 | +`MyAgent` should be an ABIDES trading agent class, for example: |
| 21 | +
|
| 22 | + from agent.ValueAgent import ValueAgent as MyAgent |
| 23 | +
|
| 24 | +Each round runs several compact ABIDES market simulations. Every submitted agent is evaluated in |
| 25 | +identical seeded market worlds with the same exchange, market maker, and background traders. The |
| 26 | +objective is to maximize average mark-to-market profit across all simulations in the round. |
| 27 | +""" |
| 28 | + default_args: dict = { |
| 29 | + "sims_per_round": 3, |
| 30 | + "market_minutes": 5, |
| 31 | + "background_agents": 3, |
| 32 | + "timeout": 240, |
| 33 | + } |
| 34 | + |
| 35 | + def _game_arg(self, key: str): |
| 36 | + return self.game_config.get("args", {}).get(key, self.game_config.get(key, self.default_args[key])) |
| 37 | + |
| 38 | + def validate_code(self, agent: Player) -> tuple[bool, str | None]: |
| 39 | + quoted_submission = shlex.quote(self.submission) |
| 40 | + file_check = agent.environment.execute(f"test -f {quoted_submission} && echo exists") |
| 41 | + if "exists" not in file_check["output"]: |
| 42 | + return False, f"Submission file `{self.submission}` not found in the workspace root" |
| 43 | + |
| 44 | + content = agent.environment.execute(f"cat {quoted_submission}")["output"] |
| 45 | + if not content.strip(): |
| 46 | + return False, f"`{self.submission}` is empty" |
| 47 | + |
| 48 | + syntax_check = agent.environment.execute(f"python -m py_compile {quoted_submission}") |
| 49 | + if syntax_check["returncode"] != 0: |
| 50 | + return False, f"Python syntax error in `{self.submission}`:\n{syntax_check['output']}" |
| 51 | + |
| 52 | + import_check = agent.environment.execute( |
| 53 | + "python - <<'PY'\n" |
| 54 | + "import importlib.util\n" |
| 55 | + "import numpy as np\n" |
| 56 | + "from agent.TradingAgent import TradingAgent\n" |
| 57 | + f"spec = importlib.util.spec_from_file_location('submission_agent', {self.submission!r})\n" |
| 58 | + "module = importlib.util.module_from_spec(spec)\n" |
| 59 | + "spec.loader.exec_module(module)\n" |
| 60 | + "assert hasattr(module, 'MyAgent'), 'MyAgent class not found'\n" |
| 61 | + "assert issubclass(module.MyAgent, TradingAgent), 'MyAgent must inherit from an ABIDES TradingAgent class'\n" |
| 62 | + "module.MyAgent(\n" |
| 63 | + " id=1,\n" |
| 64 | + " name='validation',\n" |
| 65 | + " type='ValidationAgent',\n" |
| 66 | + " symbol='JPM',\n" |
| 67 | + " starting_cash=10000000,\n" |
| 68 | + " log_orders=False,\n" |
| 69 | + " random_state=np.random.RandomState(seed=1),\n" |
| 70 | + ")\n" |
| 71 | + "PY" |
| 72 | + ) |
| 73 | + if import_check["returncode"] != 0: |
| 74 | + return ( |
| 75 | + False, |
| 76 | + f"Could not import and instantiate `MyAgent` from `{self.submission}`:\n{import_check['output']}", |
| 77 | + ) |
| 78 | + |
| 79 | + return True, None |
| 80 | + |
| 81 | + def execute_round(self, agents: list[Player]) -> None: |
| 82 | + agent_args = [] |
| 83 | + for agent in agents: |
| 84 | + agent_args.extend(["--agent", f"{agent.name}=/{agent.name}/{self.submission}"]) |
| 85 | + |
| 86 | + cmd = [ |
| 87 | + "python", |
| 88 | + "run_abides.py", |
| 89 | + "--sims", |
| 90 | + str(self.game_config.get("sims_per_round", self.default_args["sims_per_round"])), |
| 91 | + "--market-minutes", |
| 92 | + str(self._game_arg("market_minutes")), |
| 93 | + "--background-agents", |
| 94 | + str(self._game_arg("background_agents")), |
| 95 | + "--output", |
| 96 | + str(self.log_env / RESULTS_JSON), |
| 97 | + *agent_args, |
| 98 | + ] |
| 99 | + full_cmd = " ".join(shlex.quote(part) for part in cmd) |
| 100 | + self.logger.info(f"Running game: {full_cmd}") |
| 101 | + try: |
| 102 | + response = self.environment.execute(full_cmd, timeout=int(self._game_arg("timeout"))) |
| 103 | + except subprocess.TimeoutExpired as exc: |
| 104 | + raise RuntimeError("ABIDES round timed out") from exc |
| 105 | + assert_zero_exit_code(response, logger=self.logger) |
| 106 | + |
| 107 | + def get_results(self, agents: list[Player], round_num: int, stats: RoundStats): |
| 108 | + result_file = self.log_round(round_num) / RESULTS_JSON |
| 109 | + if not result_file.exists(): |
| 110 | + self.logger.error(f"Missing result file: {result_file}") |
| 111 | + stats.winner = RESULT_TIE |
| 112 | + for agent in agents: |
| 113 | + stats.scores[agent.name] = 0.0 |
| 114 | + stats.player_stats[agent.name].score = 0.0 |
| 115 | + return |
| 116 | + |
| 117 | + with open(result_file) as f: |
| 118 | + result = json.load(f) |
| 119 | + |
| 120 | + scores = {agent.name: 0.0 for agent in agents} |
| 121 | + for player, score in result.get("average_scores", {}).items(): |
| 122 | + if player in scores: |
| 123 | + scores[player] = float(score) |
| 124 | + missing_players = sorted(set(scores) - set(result.get("average_scores", {}))) |
| 125 | + for player in missing_players: |
| 126 | + scores[player] = CRASH_SCORE |
| 127 | + stats.details.append( |
| 128 | + json.dumps( |
| 129 | + { |
| 130 | + "player": player, |
| 131 | + "score": CRASH_SCORE, |
| 132 | + "status": "error", |
| 133 | + "error": "missing ABIDES score", |
| 134 | + }, |
| 135 | + sort_keys=True, |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + stats.scores = scores |
| 140 | + stats.details.extend(result.get("details", [])) |
| 141 | + for player, score in scores.items(): |
| 142 | + stats.player_stats[player].score = score |
| 143 | + |
| 144 | + if not scores: |
| 145 | + stats.winner = RESULT_TIE |
| 146 | + return |
| 147 | + |
| 148 | + top_score = max(scores.values()) |
| 149 | + winners = [player for player, score in scores.items() if score == top_score] |
| 150 | + stats.winner = winners[0] if len(winners) == 1 else RESULT_TIE |
0 commit comments