diff --git a/figures/sequence-of-moves.png b/figures/sequence-of-moves.png index 42d4c81..0b680fa 100644 Binary files a/figures/sequence-of-moves.png and b/figures/sequence-of-moves.png differ diff --git a/game.py b/game.py index 6b70cd7..426ccdd 100644 --- a/game.py +++ b/game.py @@ -1,19 +1,18 @@ from board import TTTBoardDecision, GridStates, TTTBoard -class GameSequence(object): - def __init__(self, numberOfGames, player1, player2, BoardClass=TTTBoard, BoardDecisionClass=TTTBoardDecision): +class SingleGame(object): + def __init__(self, player1, player2, BoardClass=TTTBoard, BoardDecisionClass=TTTBoardDecision): self.player1 = player1 self.player2 = player2 - self.numberOfGames = numberOfGames - self.BoardClass = BoardClass + self.board = BoardClass() self.BoardDecisionClass = BoardDecisionClass - def playAGame(self, board): + def playAGame(self): self.player1.startNewGame() self.player2.startNewGame() - while board.getBoardDecision() == self.BoardDecisionClass.ACTIVE: - self.player1.setBoard(board, GridStates.PLAYER_X) - self.player2.setBoard(board, GridStates.PLAYER_O) + while self.board.getBoardDecision() == self.BoardDecisionClass.ACTIVE: + self.player1.setBoard(self.board, GridStates.PLAYER_X) + self.player2.setBoard(self.board, GridStates.PLAYER_O) pState1 = self.player1.makeNextMove() self.player1.learnFromMove(pState1) self.player2.learnFromMove(pState1) @@ -22,14 +21,31 @@ def playAGame(self, board): self.player2.learnFromMove(pState2) self.player1.finishGame() self.player2.finishGame() - return board.getBoardDecision() + return self.board.getBoardDecision() + +class GameSequence(object): + def __init__(self, numberOfGames, player1, player2, BoardClass=TTTBoard, BoardDecisionClass=TTTBoardDecision): + self.player1 = player1 + self.player2 = player2 + self.numberOfGames = numberOfGames + self.BoardClass = BoardClass + self.BoardDecisionClass = BoardDecisionClass def playGamesAndGetWinPercent(self): results = [] for i in range(self.numberOfGames): - board = self.BoardClass() - results.append(self.playAGame(board)) + game = SingleGame(self.player1, self.player2, self.BoardClass, self.BoardDecisionClass) + results.append(game.playAGame()) xpct, opct, drawpct = float(results.count(self.BoardDecisionClass.WON_X))/float(self.numberOfGames), \ float(results.count(self.BoardDecisionClass.WON_O))/float(self.numberOfGames), \ float(results.count(self.BoardDecisionClass.DRAW))/float(self.numberOfGames) return (xpct, opct, drawpct) + +if __name__ == '__main__': + from ultimateplayer import RandomUTTTPlayer + from ultimateboard import UTTTBoard, UTTTBoardDecision + player1, player2 = RandomUTTTPlayer(), RandomUTTTPlayer() + game = SingleGame(player1, player2, UTTTBoard, UTTTBoardDecision) + game.playAGame() + gameSeq = GameSequence(10, player1, player2, UTTTBoard, UTTTBoardDecision) + print gameSeq.playGamesAndGetWinPercent() \ No newline at end of file diff --git a/plotting.py b/plotting.py index 398d929..ab6988b 100644 --- a/plotting.py +++ b/plotting.py @@ -6,15 +6,6 @@ import matplotlib import numpy as np -def setFontProperties(): - #fontProperties = {'family':'sans-serif','sans-serif':['Arial'], 'weight' : 'normal', 'size' : 12} - #fontProperties = fm.FontProperties(fname='/Users/sevan/Library/Fonts/avenir-light.ttf') - fontProperties = fm.FontProperties(fname='/Users/Shayak/Library/Fonts/avenir-light.ttf') - matplotlib.rc('font', family=fontProperties.get_family()) - matplotlib.rc('font', serif=fontProperties.get_name()) - matplotlib.rc('text', usetex=False) - matplotlib.rcParams.update({'font.size': 14}) - def drawXYPlotByFactor(dataDict, xlabel='', ylabel='', legend=None, title=None, logy=False, location=5): # Assuming that the data is in the format { factor: [(x1, y1),(x2,y2),...] } @@ -36,46 +27,3 @@ def drawXYPlotByFactor(dataDict, xlabel='', ylabel='', legend=None, if title is not None: plt.title(title) plt.show() - -def drawGroupedBarChart(dataDict, xlabel='', ylabel='', - title=None, fileName=None): - #PLOT_COLORS = ['r', 'b', 'g', 'y', 'c', 'm', 'k'] - PLOT_COLORS = ['0.25', '#009CE0', 'g', 'y', 'c', 'm', 'k'] - setFontProperties() - index = np.arange(len(dataDict[dataDict.keys()[0]])) - tickLabels = [u[0] for u in dataDict[dataDict.keys()[0]]] - bar_width = 0.15 - opacity = 0.4 - rects = [] - for i, dataKey in enumerate(sorted(dataDict.keys())): - color = PLOT_COLORS[min(len(PLOT_COLORS)-1, i)] - data = [u[1] for u in sorted(dataDict[dataKey], key=lambda x: x[0])] - rects.append(plt.bar(index+i*bar_width, data, bar_width, - alpha=opacity, color=color, linewidth=0, label=dataKey)) - plt.xlabel(xlabel) - plt.ylabel(ylabel) - if title is not None: - plt.title(title) - plt.xticks(index + bar_width, tuple(tickLabels)) - plt.legend() - if fileName is None: - plt.show() - else: - plt.savefig(fileName) - -def drawGroupedHistogram(dataDict, numBins=20, xlabel='', ylabel='', title=None, fileName=None): - PLOT_COLORS = ['0.25', '#009CE0', 'g', 'y', 'c', 'm', 'k'] - #setFontProperties() - for i, dataKey in enumerate(sorted(dataDict.keys())): - color = PLOT_COLORS[min(len(PLOT_COLORS)-1, i)] - plt.hist(dataDict[dataKey], bins=numBins, histtype='stepfilled', - color=color, alpha=0.4, linewidth=0, label=dataKey) - plt.xlabel(xlabel) - plt.ylabel(ylabel) - if title is not None: - plt.title(title) - plt.legend() - if fileName is not None: - plt.savefig(fileName) - else: - plt.show() diff --git a/test_scripts.py b/test_scripts.py index 90cc852..7506290 100644 --- a/test_scripts.py +++ b/test_scripts.py @@ -41,11 +41,17 @@ def playUltimateAndPlotResults(): drawXYPlotByFactor(plotValues, 'Number of Sets (of 100 Games)', 'Fraction',title='RL Player (O) vs. Random Player (X)') def playUltimateForTraining(): - learningPlayer = RLUTTTPlayer(TableLearning()) + learningModel = TableLearning() + learningPlayer = RLUTTTPlayer(learningModel) randomPlayer = RandomUTTTPlayer() - games = GameSequence(4000, learningPlayer, randomPlayer, BoardClass=UTTTBoard, BoardDecisionClass=UTTTBoardDecision) - games.playGamesAndGetWinPercent() - learningPlayer.saveLearning(NNUltimateLearning.TABLE_LEARNING_FILE) + 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: @@ -63,8 +69,17 @@ def plotResultsFromFile(resultsFile): '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() + #playUltimateAndPlotResults() #plotResultsFromFile('results/ultimate_nn1_results_o.csv') + plotMemoryUsageFromFile('results/memory_scaling.csv') diff --git a/ultimateboard.py b/ultimateboard.py index 61cc9f5..ad48acc 100644 --- a/ultimateboard.py +++ b/ultimateboard.py @@ -114,9 +114,11 @@ def getBoardDecision(self): b.makeMove(GridStates.PLAYER_X, (1,1), (1,1)) b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (1, 2)) b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1)) - b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (0, 2)) + b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (0, 0)) b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1)) b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (2, 2)) b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1)) b.makeMove(GridStates.PLAYER_O, b.getNextBoardLocation(), (2, 1)) - b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1)) \ No newline at end of file + b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1)) + b.printBoard() + print b.getBoardState() \ No newline at end of file