Skip to content

Commit

Permalink
Stored statistics for each AI run in an attribute so that callers can…
Browse files Browse the repository at this point in the history
… access.
  • Loading branch information
jbradberry committed Aug 13, 2016
1 parent 4cca938 commit c06d600
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='UltimateTicTacToe',
version='0.1dev',
version='0.7',
author='Jeff Bradberry',
author_email='[email protected]',
packages=['t3'],
Expand Down
34 changes: 25 additions & 9 deletions t3/mcts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ def __init__(self, board, **kwargs):
self.C = kwargs.get('C', 1.4)
self.wins = {1: {}, 2: {}}
self.plays = {1: {}, 2: {}}
self.max_depth = 0
self.stats = None

def get_play(self):
self.max_depth = 0
self.stats = {}

state = self.states[-1]
player = state[-1]
legal = self.board.legal_plays(state)
Expand All @@ -47,27 +52,36 @@ def get_play(self):
self.random_game()
games += 1

print games, datetime.datetime.utcnow() - begin
self.stats.update(games=games, max_depth=self.max_depth,
time=str(datetime.datetime.utcnow() - begin))
print self.stats['games'], self.stats['time']
print "Maximum depth searched:", self.max_depth

self.stats['moves'] = sorted(
({'move': p,
'percent': 100 * self.wins[player].get(S,0) / self.plays[player].get(S,1),
'wins': self.wins[player].get(S,0),
'plays': self.plays[player].get(S,0)}
for p, S in states),
key=lambda x: (x['percent'], x['plays']),
reverse=True,
)
for m in self.stats['moves']:
print "{move}: {percent:.2f}% ({wins} / {plays})".format(**m)

move = max(
(self.wins[player].get(S,0) / self.plays[player].get(S,1), p)
for p, S in states
)[1]

for x in sorted(((100 * self.wins[player].get(S,0)
/ self.plays[player].get(S,1),
self.wins[player].get(S,0),
self.plays[player].get(S,0), p)
for p, S in states), reverse=True):
print "{3}: {0:.2f}% ({1} / {2})".format(*x)

return move

def random_game(self):
game_moves = {1: set(), 2: set()}
new_states = self.states[:]

expand = True
for t in xrange(self.max_moves):
for t in xrange(1, self.max_moves + 1):
state = new_states[-1]
player = state[-1]
legal = self.board.legal_plays(state)
Expand All @@ -88,6 +102,8 @@ def random_game(self):
expand = False
plays[state] = 0
wins[state] = 0
if t > self.max_depth:
self.max_depth = t

game_moves[player].add(state)

Expand Down

0 comments on commit c06d600

Please sign in to comment.