-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.py
89 lines (74 loc) · 2.75 KB
/
experiment.py
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
import game as g
# Used to run multiple games at a time and assess agent performance.
class Experiment:
def __init__(self, trials, playerOneType, playerTwoType, printTrials, graphics):
self.trials = trials
self.playerOneType = playerOneType
self.playerTwoType = playerTwoType
self.printTrials = printTrials
self.graphics = graphics
# Runs the experiment.
def run(self):
wins = [0, 0, 0] # To store the number of draws, player 1 wins, and player 2 wins.
numTurns = [] # To store the number of turns taken for each game.
threshold = 5000 # Maximum number of turns allowed per game: exceeding implies a draw.
print "\n"
print "##############################"
print "RUNNING EXPERIMENT: " + self.playerOneType + " vs. " + self.playerTwoType
print "Number of trials: " + str(self.trials)
print "Draw threshold: " + str(threshold)
print "##############################"
print "\n"
# Run <self.trials> games, each time recording who won and number of turns.
for i in range(1, self.trials+1):
turns = 0
game = g.Game(self.playerOneType, self.playerTwoType, self.graphics)
gameBoard = game.getGameBoard()
self.graphics.setGameBoard(gameBoard)
self.graphics.updateGraphics()
if self.printTrials:
game.printState()
# If the game is not over, advance game by one turn.
while not game.isEnded():
if turns > threshold:
break
game.takeTurn()
turns += 1
if self.printTrials:
game.printState()
# Record the winner and number of turns.
winner = game.getWinner()
if winner == None:
wins[0] += 1
else:
wins[winner] += 1
numTurns.append(turns)
# Print out experiment progress.
if self.trials > i:
tenth = self.trials/10
if i%tenth == 0:
print "Experiment progress: " + str((i/(tenth))*10) + "%"
print "\n"
print "##############################"
print "SUMMARY STATISTICS:"
print "##############################"
print "Draw rate: " + str(wins[0]/float(self.trials))
player1WinRate = wins[1]/float(self.trials)
player2WinRate = wins[2]/float(self.trials)
print "Player 1 (" + self.playerOneType + ") win rate: " + str(player1WinRate)
print "Player 2 (" + self.playerTwoType + ") win rate: " + str(player2WinRate)
print "\n"
print "Least number of turns: " + str(min(numTurns))
print "Greatest number of turns: " + str(max(numTurns))
print "Average number of turns: " + str(sum(numTurns)/len(numTurns))
# Compute median number of turns.
numTurns.sort()
if self.trials%2 == 0:
index = int((float(self.trials)/2) - 1)
median = (numTurns[index] + numTurns[index+1])/float(2)
else:
index = int((float(self.trials)/2))
median = numTurns[index]
print "Median number of turns: " + str(median)
def getGameBoard(self):
return self.gameBoard