Skip to content

Commit

Permalink
Make a class SingleGame to allow playing single games
Browse files Browse the repository at this point in the history
  • Loading branch information
shayakbanerjee committed Dec 21, 2017
1 parent c113abd commit 22ab9c6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 70 deletions.
Binary file modified figures/sequence-of-moves.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 27 additions & 11 deletions game.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
52 changes: 0 additions & 52 deletions plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),...] }
Expand All @@ -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()
25 changes: 20 additions & 5 deletions test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
6 changes: 4 additions & 2 deletions ultimateboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
b.makeMove(GridStates.PLAYER_X, b.getNextBoardLocation(), (1, 1))
b.printBoard()
print b.getBoardState()

0 comments on commit 22ab9c6

Please sign in to comment.