-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_scripts.py
85 lines (77 loc) · 4.04 KB
/
test_scripts.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
from ultimateboard import UTTTBoard, UTTTBoardDecision
from player import RandomTTTPlayer, RLTTTPlayer
from ultimateplayer import RandomUTTTPlayer, RLUTTTPlayer
from learning import NNUltimateLearning, TableLearning
from plotting import drawXYPlotByFactor
import os, csv
from game import GameSequence
LEARNING_FILE = 'ultimate_player_nn1.h5'
WIN_PCT_FILE = 'win_pct_player_1.csv'
def playTTTAndPlotResults():
learningPlayer = RLTTTPlayer()
randomPlayer = RandomTTTPlayer()
results = []
numberOfSetsOfGames = 50
for i in range(numberOfSetsOfGames):
games = GameSequence(100, learningPlayer, randomPlayer)
results.append(games.playGamesAndGetWinPercent())
plotValues = {'X Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[0], results)),
'O Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[1], results)),
'Draw Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Number of Sets (of 100 Games)', 'Fraction', title='RL Player (X) vs. Random Player (O)')
def playUltimateAndPlotResults():
learningModel = NNUltimateLearning(UTTTBoardDecision)
learningPlayer = RLUTTTPlayer(learningModel)
randomPlayer = RandomUTTTPlayer()
results = []
numberOfSetsOfGames = 60
if os.path.isfile(LEARNING_FILE):
learningPlayer.loadLearning(LEARNING_FILE)
for i in range(numberOfSetsOfGames):
games = GameSequence(100, learningPlayer, randomPlayer, BoardClass=UTTTBoard, BoardDecisionClass=UTTTBoardDecision)
results.append(games.playGamesAndGetWinPercent())
learningPlayer.saveLearning(LEARNING_FILE)
writeResultsToFile(results)
plotValues = {'X Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[0], results)),
'O Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[1], results)),
'Draw Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Number of Sets (of 100 Games)', 'Fraction',title='RL Player (O) vs. Random Player (X)')
def playUltimateForTraining():
learningModel = TableLearning()
learningPlayer = RLUTTTPlayer(learningModel)
randomPlayer = RandomUTTTPlayer()
results, tempFileName = [], 'temp_learning.json'
for i in range(40):
games = GameSequence(1000, learningPlayer, randomPlayer, BoardClass=UTTTBoard, BoardDecisionClass=UTTTBoardDecision)
games.playGamesAndGetWinPercent()
learningPlayer.saveLearning(tempFileName)
results.append(os.path.getsize(tempFileName))
print '\n'.join(map(str, results))
os.remove(tempFileName)
def writeResultsToFile(results):
with open(WIN_PCT_FILE, 'a') as outfile:
for result in results:
outfile.write('%s,%s,%s\n'%(result[0], result[1], result[2]))
def plotResultsFromFile(resultsFile):
results = []
with open(resultsFile, 'r') as infile:
reader = csv.reader(infile)
results = map(tuple, reader)
numberOfSetsOfGames = len(results)
plotValues = {'X Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[0], results)),
'O Win Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[1], results)),
'Draw Fraction': zip(range(numberOfSetsOfGames), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Number of Sets (of 100 Games)', 'Fraction', title='RL Player (O) vs. Random Player (X)')
def plotMemoryUsageFromFile(memoryFile):
results = []
with open(memoryFile, 'r') as infile:
reader = csv.reader(infile)
results = map(tuple, reader)
plotValues = {'Memory Usage': zip(map(lambda x: x[1], results), map(lambda x: x[2], results))}
drawXYPlotByFactor(plotValues, 'Number of Simulations', 'Memory Usage (MB)')
if __name__ == '__main__':
#playTTTAndPlotResults()
#playUltimateForTraining()
#playUltimateAndPlotResults()
#plotResultsFromFile('results/ultimate_nn1_results_o.csv')
plotMemoryUsageFromFile('results/memory_scaling.csv')