| """Generate replayable opponent-policy cache for official evaluation. |
| |
| The cache can be generated with `--mode llm_live` when API credentials are |
| available, or with `--mode heuristic` for deterministic local/dev packs. |
| """ |
|
|
| import argparse |
| import json |
| import random |
| import sys |
| from pathlib import Path |
|
|
| _ROOT = Path(__file__).parent.parent |
| sys.path.insert(0, str(_ROOT)) |
|
|
| try: |
| from server.opponent_policy import create_opponent_policy |
| except ImportError: |
| from cricket_captain.server.opponent_policy import create_opponent_policy |
|
|
|
|
| def _cache_key(kind: str, context: dict) -> str: |
| parts = [ |
| kind, |
| str(context.get("eval_pack_id", "default")), |
| str(context.get("scenario_id", "")), |
| str(context.get("innings", "first")), |
| str(context.get("game_state", "")), |
| f"{context.get('over', 0)}.{context.get('ball', 0)}", |
| f"{context.get('score', 0)}/{context.get('wickets', 0)}", |
| str(context.get("target", "")), |
| str(context.get("phase", "")), |
| ] |
| return "|".join(parts) |
|
|
|
|
| def _phase(over: int) -> str: |
| if over <= 5: |
| return "powerplay" |
| if over <= 15: |
| return "middle" |
| return "death" |
|
|
|
|
| def _contexts_from_pack(pack: dict, split: str) -> list[dict]: |
| contexts = [] |
| scenarios = pack.get("splits", {}).get(split, []) or pack.get("match_starts", []) |
| for idx, scenario in enumerate(scenarios): |
| game_state = scenario.get("start_state", "batting") |
| over = int(scenario.get("over", 0)) |
| context = { |
| "eval_pack_id": pack.get("eval_pack_id", "default"), |
| "scenario_id": f"{split}:{idx}", |
| "game_state": game_state, |
| "strategic_phase": "pre_ball", |
| "innings": scenario.get("innings_type", "second" if scenario.get("target") else "first"), |
| "over": over, |
| "ball": int(scenario.get("ball", 0)), |
| "score": int(scenario.get("score", 0)), |
| "wickets": int(scenario.get("wickets", 0)), |
| "target": scenario.get("target"), |
| "phase": _phase(over), |
| "field_setting": "Balanced", |
| "current_batter": {"name": "Benchmark Batter", "style": "balanced", "aggression": 0.5}, |
| "current_bowler": {"name": "Benchmark Bowler", "type": "pace", "style": "stock"}, |
| "batting_strategy": {}, |
| "bowling_strategy": {}, |
| "shot_plan": {}, |
| "delivery_plan": {}, |
| } |
| contexts.append(context) |
| return contexts |
|
|
|
|
| def generate_cache(eval_pack_path: str, output_path: str, mode: str, split: str, seed: int): |
| rng = random.Random(seed) |
| with open(eval_pack_path) as f: |
| pack = json.load(f) |
|
|
| policy = create_opponent_policy(mode, rng) |
| records = [] |
| for context in _contexts_from_pack(pack, split): |
| if context["game_state"] == "batting": |
| kind = "bowling" |
| plan = policy.bowling_plan(context) |
| else: |
| kind = "batting" |
| plan = policy.batting_plan(context) |
| records.append({ |
| "key": _cache_key(kind, context), |
| "kind": kind, |
| "context": context, |
| "plan": plan, |
| }) |
|
|
| reflection = policy.reflect_after_ball(context, {"runs": 0, "wicket": False, "extra": False}) |
| records.append({ |
| "key": _cache_key("reflection", {**context, "last_outcome": {"runs": 0, "wicket": False}}), |
| "kind": "reflection", |
| "context": context, |
| "plan": reflection, |
| }) |
|
|
| output = Path(output_path) |
| output.parent.mkdir(parents=True, exist_ok=True) |
| with output.open("w") as f: |
| for record in records: |
| f.write(json.dumps(record) + "\n") |
| print(f"Wrote {len(records)} opponent cache records -> {output}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--eval-pack", default=str(_ROOT / "data" / "eval_packs" / "adaptive_t20_v1.json")) |
| parser.add_argument("--output", default=str(_ROOT / "data" / "opponent_cache" / "adaptive_t20_v1.jsonl")) |
| parser.add_argument("--mode", default="heuristic", choices=["heuristic", "llm_live"]) |
| parser.add_argument("--split", default="official", choices=["dev", "official"]) |
| parser.add_argument("--seed", type=int, default=42) |
| args = parser.parse_args() |
| generate_cache(args.eval_pack, args.output, args.mode, args.split, args.seed) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|