Download scripts/analyze.py from lukehola/koehandel: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/spaces/lukehola/koehandel/resolve/main/scripts/analyze.py
- Command line
-
hf download hf://spaces/lukehola/koehandel/scripts/analyze.py
-
curl -L -o analyze.py https://huggingface.co/spaces/lukehola/koehandel/resolve/main/scripts/analyze.py
2.81 kB
| #!/usr/bin/env python3 | |
| """EV-analyze a game and save it for the dashboard's Analysis viewer. | |
| By default a checkpoint plays itself and every real decision is EV-graded: | |
| python scripts/analyze.py --checkpoint runs/v1/checkpoints/latest.pt | |
| python scripts/analyze.py --checkpoint runs/v1/checkpoints/latest.pt --rollouts 16 --seed 3 | |
| With no checkpoint, the heuristic agent is analyzed (useful for a quick demo). | |
| Output is written to analyses/<name>.json; open the dashboard and pick it under | |
| the "Analysis (EV)" tab. | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from koehandel.agents import HeuristicAgent # noqa: E402 | |
| from koehandel.agents.neural_agent import NeuralAgent # noqa: E402 | |
| from koehandel.analysis.ev import AnalysisConfig, analyze_game # noqa: E402 | |
| from koehandel.web.app import ANALYSIS_DIR # noqa: E402 | |
| def main(): | |
| p = argparse.ArgumentParser(description=__doc__) | |
| p.add_argument("--checkpoint", default=None) | |
| p.add_argument("--num-players", type=int, default=4) | |
| p.add_argument("--rollouts", type=int, default=8) | |
| p.add_argument("--max-decisions", type=int, default=80) | |
| p.add_argument("--game-seed", type=int, default=0) | |
| p.add_argument("--out", default=None) | |
| args = p.parse_args() | |
| if args.checkpoint: | |
| model = NeuralAgent.from_checkpoint(args.checkpoint, deterministic=True).model | |
| # Sample (not argmax) for the reference game: four identical deterministic | |
| # agents can fall into a symmetric trade loop and never finish. | |
| play_agents = [ | |
| NeuralAgent(model, deterministic=False, name=f"P{i}") for i in range(args.num_players) | |
| ] | |
| rollout_agent = NeuralAgent(model, deterministic=False, name="rollout") | |
| tag = os.path.basename(os.path.dirname(os.path.dirname(args.checkpoint))) | |
| else: | |
| play_agents = [HeuristicAgent(seed=i) for i in range(args.num_players)] | |
| rollout_agent = HeuristicAgent(seed=999) | |
| tag = "heuristic" | |
| cfg = AnalysisConfig(rollouts=args.rollouts, max_decisions=args.max_decisions) | |
| print(f"Analyzing a {args.num_players}-player game ({tag}); " | |
| f"{args.rollouts} rollouts/action ... this can take a bit.", flush=True) | |
| result = analyze_game(play_agents, rollout_agent, game_seed=args.game_seed, config=cfg) | |
| os.makedirs(ANALYSIS_DIR, exist_ok=True) | |
| out = args.out or os.path.join(ANALYSIS_DIR, f"{tag}_seed{args.game_seed}.json") | |
| with open(out, "w") as f: | |
| json.dump(result, f, indent=2) | |
| gc = result["grade_counts"] | |
| print(f"Saved {out}") | |
| print(f"Decisions analyzed: {result['meta']['decisions_analyzed']} grades: {gc}") | |
| print(f"Final scores: {result['scores']}") | |
| if __name__ == "__main__": | |
| main() | |