-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun
executable file
·130 lines (105 loc) · 3.73 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
import sys
import random
import argparse
import yaml
import numpy as np
from c4.board import Board, DRAW
from c4.engine import (
GreedyEngine, WeightedGreedyEngine, RandomEngine,
MonteCarloTreeSearch,
NegamaxEngine, AlphaBetaEngine, ABCachedEngine, ABDeepEngine,
PVSEngine, PVSCachedEngine, PVSDeepEngine
)
from c4.engine.human import HumanEngine
from c4.game import GameHandler
from c4.arena import arena
engine_map = {
'greedy': GreedyEngine,
'weighted': WeightedGreedyEngine,
'mcts': MonteCarloTreeSearch,
'random': RandomEngine,
'negamax': NegamaxEngine,
'alphabeta': AlphaBetaEngine,
'abcached': ABCachedEngine,
'abdeep': ABDeepEngine,
'pvs': PVSEngine,
'pvscached': PVSCachedEngine,
'pvsdeep': PVSDeepEngine,
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-s', '--static-seed', default=None, type=int,
help='Force a static seed for reproducible experiments')
subparsers = parser.add_subparsers(title='Commands',
description='c4 builin commands')
game_parser = subparsers.add_parser('game', help='Play with an engine')
game_parser.add_argument(
'engine', metavar='ENGINE',
help='Engine to use. Format: engine_name:par1:par2:...')
game_parser.add_argument(
'--player2', default=False, action='store_true',
help='Play as player 2')
game_parser.set_defaults(cmd=run_game)
arena_parser = subparsers.add_parser('arena', help='Run arena')
arena_parser.add_argument('config', metavar='CONFIGFILE',
type=argparse.FileType('r'))
arena_parser.add_argument('-r', '--rounds', type=int, default=1,
help='Number of rounds')
arena_parser.set_defaults(cmd=run_arena)
bm_parser = subparsers.add_parser('bm', help='Select the bestmove')
bm_parser.add_argument(
'engine', metavar='ENGINE',
help='Engine to use. Format: engine_name:par1:par2:...')
bm_parser.set_defaults(cmd=run_bm)
args = parser.parse_args()
if args.static_seed is not None:
np.random.seed(args.static_seed)
random.seed(args.static_seed)
args.cmd(args)
def run_game(args):
engine_name, *engine_args = args.engine.split(':')
engine_class = engine_map[engine_name]
engine = engine_class(*engine_args)
name = input('Your name: ')
human = HumanEngine(name)
if not args.player2:
p1 = human
p2 = engine
else:
p1 = engine
p2 = human
b, winner, looser = GameHandler(p1, p2).play()
if winner is human:
print('Congratulation: you win')
else:
print('Sorry, try again!')
def run_arena(args):
config = yaml.load(args.config)
engines = []
subscribed_engines = set()
for i, engine_cfg in enumerate(config):
engine_class = engine_map[engine_cfg.pop('class')]
engine_name = engine_cfg.pop('name', None)
engine = engine_class(**engine_cfg)
if engine_name is None:
engine_name = str(engine)
if engine_name not in subscribed_engines:
subscribed_engines.add(engine_name)
else:
print('Error: engine name %s collides, '
'use "name" attribute to make it unique' % engine_name,
file=sys.stderr)
sys.exit(1)
engines.append((engine_name, engine))
arena(engines, args.rounds)
def run_bm(args):
engine_name, *engine_args = args.engine.split(':')
engine_class = engine_map[engine_name]
engine = engine_class(*engine_args)
move = engine.choose(Board())
print('Move: %d' % (move + 1))
if __name__ == '__main__':
random.seed()
sys.exit(main())