| 1 | """Fresh-process duplicate-deal comparison against RLCard's original rule bot. |
| 2 | Example: .venvs/doudizhu/bin/python games/doudizhu/tests/compare.py --output /tmp/ddz.json |
| 3 | The new bot occupies each seat once; the other seats use independent rule bots. |
| 4 | """ |
| 5 | import argparse |
| 6 | import json |
| 7 | import pathlib |
| 8 | import sys |
| 9 | import time |
| 10 | |
| 11 | import rlcard |
| 12 | |
| 13 | ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 14 | sys.path.insert(0, str(ROOT / 'referee')) |
| 15 | from play import player, game |
| 16 | |
| 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument('--start', type=int, default=101) |
| 19 | parser.add_argument('--deals', type=int, default=20) |
| 20 | parser.add_argument('--output', type=pathlib.Path) |
| 21 | args = parser.parse_args() |
| 22 | if not 1 <= args.deals <= 100: |
| 23 | parser.error('--deals must be between 1 and 100') |
| 24 | results, started = [], time.monotonic() |
| 25 | for seed in range(args.start, args.start + args.deals): |
| 26 | for seat in range(3): |
| 27 | bots = [] |
| 28 | try: |
| 29 | for s in range(3): |
| 30 | bots.append(player(str(ROOT / 'field' / ('strategic' if s == seat else 'rule')))) |
| 31 | scores, replay = game(rlcard.make('doudizhu'), bots, seed) |
| 32 | results.append({'seed': seed, 'seat': seat, 'won': scores[seat] > 0, |
| 33 | 'points': scores[seat], 'fallbacks': sum(replay['fallbacks'])}) |
| 34 | finally: |
| 35 | for bot in bots: |
| 36 | bot.close() |
| 37 | print(json.dumps({'games': len(results), 'wins': sum(r['won'] for r in results)}), flush=True) |
| 38 | summary = {'games': len(results), 'wins': sum(r['won'] for r in results), |
| 39 | 'mean_points': sum(r['points'] for r in results) / len(results), |
| 40 | 'fallbacks': sum(r['fallbacks'] for r in results), 'seconds': time.monotonic() - started} |
| 41 | print(json.dumps(summary)) |
| 42 | if args.output: |
| 43 | args.output.write_text(json.dumps({'summary': summary, 'games': results}, indent=2)) |