Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from random import shuffle, randint
- import matplotlib.pyplot as plt
- from math import exp
- INVALID = "Invalid Move."
- TIE = "No winner, it's a tie!"
- class Board():
- def __init__(self):
- self.structure = [[None, None, None] for i in range(3)]
- def put_marker(self, marker, coords):
- try:
- if self.structure[coords[0]][coords[1]] is None:
- self.structure[coords[0]][coords[1]] = marker
- else:
- return INVALID
- except:
- return INVALID
- def display(self):
- for row in self.structure:
- line = '= ' + str(row[0]) + ';' + str(row[1]) + ';' + str(row[2]) + ' ='
- print(line.replace("None", " "))
- class Game():
- def __init__(self, player1, player2):
- self.board, self.turn, self.winner = Board(), 0, None
- self.playerList = [player1, player2]
- shuffle(self.playerList)
- def play(self):
- turn = 0
- while self.winner is None:
- turn += 1
- if self.board.put_marker(self.playerList[turn % 2].marker,
- self.playerList[turn % 2].get_move(self.board)) != INVALID:
- self.check(self.playerList[turn % 2])
- else:
- self.winner = self.playerList[turn % 2 - 1]
- if self.winner != TIE:
- self.winner.winner()
- if self.playerList[0] == self.winner:
- self.playerList[1].loser()
- else:
- self.playerList[0].loser()
- def check(self, candidate):
- self.tieCheck = 0
- for row in self.board.structure:
- if all(row) is True:
- self.tieCheck += 1
- if row[0] == row[1] == row[2] and row[0] is not None:
- self.winner = candidate
- if self.tieCheck == 3:
- self.winner = TIE
- self.playerList[0].tie()
- self.playerList[1].tie()
- for i in range(3):
- if self.board.structure[0][i] == self.board.structure[1][i] == self.board.structure[2][i] and \
- self.board.structure[0][i] is not None:
- self.winner = candidate
- if self.board.structure[1][1] is not None and (
- (self.board.structure[0][0] == self.board.structure[1][1] == self.board.structure[2][2]) or (
- self.board.structure[0][2] == self.board.structure[1][1] == self.board.structure[2][0])):
- self.winner = candidate
- class HumanPlayer():
- def __init__(self, name, marker):
- self.name, self.marker = name, marker
- self.winCount, self.loseCount, self.tieCount = 0, 0, 0
- def get_move(self, board):
- board.display()
- return input(self.name + ' what are your commands? ')
- def winner(self):
- print(self.name + ', you won!')
- def loser(self):
- print(self.name + ', you lose!')
- def tie(self):
- print("It's a tie!")
- class NeuralPlayer():
- def __init__(self, name, marker):
- self.name, self.marker = name, marker
- self.network, self.output = [[[Neuron(9) for i in range(3)] for j in range(3)] for k in range(2)], [0 for i in
- range(9)]
- self.winCount, self.loseCount, self.tieCount = 0, 0, 0
- self.learning = True
- self.history = []
- def get_move(self, board):
- self.reset_output()
- self.links = [[] for i in range(9)]
- for i in range(len(board.structure)):
- for j in range(len(board.structure[i])):
- if board.structure[i][j] != None and board.structure[i][j] != self.marker:
- for k in range(len(self.network[0][i][j].connections)):
- if self.network[0][i][j].connections[k] != 0:
- self.output[k] += self.network[0][i][j].connections[k]
- self.links[k].append(self.network[0][i][j])
- elif board.structure[i][j] == self.marker:
- for k in range(len(self.network[1][i][j].connections)):
- if self.network[1][i][j].connections[k] != 0:
- self.output[k] += self.network[1][i][j].connections[k]
- self.links[k].append(self.network[1][i][j])
- self.move = self.output.index(max(self.output))
- self.history.append([self.move, self.links[self.move]])
- return [self.move % 3, self.move // 3]
- def winner(self):
- if not self.learning:
- #print(self.name + ' is the winner!')
- self.winCount += 1
- return None
- self.currentTurn, self.turns = 0, len(self.history)
- #print(self.name + ' is the winner!')
- for moves in self.history:
- self.currentTurn += 1
- for neurons in moves[1]:
- neurons.connections[self.move] += 100 * exp(self.currentTurn - self.turns)
- #if self.history != []:
- #for neurons in self.history[-1][1]:
- #neurons.connections[self.move] += 100
- self.history = []
- def loser(self):
- if not (self.learning):
- self.loseCount += 1
- return None
- self.currentTurn, self.turns = 0, len(self.history)
- for moves in self.history:
- self.currentTurn += 1
- for neurons in moves[1]:
- neurons.connections[self.move] -= 250 * exp(self.currentTurn - self.turns)
- #if self.history != []:
- #for neurons in self.history[-1][1]:
- #neurons.connections[self.move] -= 250
- self.history = []
- def tie(self):
- if not (self.learning):
- self.tieCount += 1
- #print('Tie.')
- return None
- self.currentTurn, self.turns = 0, len(self.history)
- #print('Tie.')
- for moves in self.history:
- self.currentTurn += 1
- for neurons in moves[1]:
- neurons.connections[self.move] += 10 * exp(self.currentTurn - self.turns)
- self.history = []
- def reset_output(self):
- self.output = [0 for i in range(9)]
- def enable_learning(self):
- self.learning = True
- print('[' + self.name + ']' + ' Learning now enabled.')
- def disable_learning(self):
- self.learning = False
- print('[' + self.name + ']' + ' Learning now disabled.')
- def normalize(self):
- for layer in self.network:
- for row in layer:
- for neuron in row:
- neuron.normalize()
- class RandomPlayer():
- def __init__(self, isSmart=True):
- self.intelligence = isSmart
- self.name, self.marker = 'Random', 'R'
- self.winCount, self.loseCount, self.tieCount = 0, 0, 0
- def get_move(self, board):
- candidate = randint(1, 9)
- if self.intelligence:
- self.notValid = True
- while self.notValid:
- candidate = randint(0, 8)
- if board.structure[candidate % 3][candidate // 3] == None:
- self.notValid = False
- return [candidate % 3, candidate // 3]
- def winner(self):
- #print(self.name + ' is the winner!')
- self.winCount += 1
- def loser(self):
- self.loseCount += 1
- def tie(self):
- self.tieCount += 1
- class Neuron():
- def __init__(self, synapses):
- self.connections = [1 for i in range(synapses)]
- def normalize(self):
- print('Wait, what?')
- self.absConnections = [abs(i) for i in self.connections]
- self.coefficient = 100000 / max(self.absConnections)
- self.newConnections = [i * self.coefficient for i in self.connections]
- self.connections = self.newConnections[:]
- def manual_trainer(nplayer):
- while True:
- game = Game(h1, nplayer)
- game.play()
- def auto_trainer(p1, p2, ngame=100):
- while ngame > 0:
- ngame -= 1
- game = Game(p1, p2)
- game.play()
- def evaluate_level(player, ngame=100):
- player.winCount, player.tieCount, player.loseCount = 0, 0, 0
- randomPlayer = RandomPlayer()
- player.disable_learning()
- while ngame > 0:
- ngame -= 1
- game = Game(randomPlayer, player)
- game.play()
- print(str((player.winCount + player.tieCount) / (
- player.winCount + player.tieCount + player.loseCount) * 100) + '% victory / tie.')
- player.enable_learning()
- return (player.winCount + player.tieCount) / (
- player.winCount + player.tieCount + player.loseCount) * 100
- def convergence_analysis(player1, player2, dG=2, games=100000):
- game, x, y = 1, [], []
- while game <= games:
- print(game)
- auto_trainer(player1, player2, game)
- x.append(game)
- y.append(evaluate_level(player1, 1000))
- game *= dG
- plt.plot(x, y)
- plt.xscale('symlog')
- plt.show()
- def hybrid_training(player1, player2, player3, dG=2, initialPool = 36000, finalPool = 250000):
- game, x, y, first = 1, [], [], True
- while game <= finalPool:
- while game <= initialPool:
- print(game)
- auto_trainer(player1, player3, game)
- auto_trainer(player2, player3, game)
- x.append(game)
- y.append(evaluate_level(player1, 1000))
- game *= dG
- if first:
- player1.normalize()
- player2.normalize()
- print(game)
- auto_trainer(player1, player2, game)
- x.append(game)
- y.append(evaluate_level(player1, 1000))
- game *= dG
- first = False
- plt.plot(x, y)
- plt.xscale('symlog')
- plt.show()
- h1 = HumanPlayer('Julien', 'J')
- h2 = HumanPlayer('Test', 'T')
- r1 = RandomPlayer()
- SSATR = NeuralPlayer('SSATR', 'S')
- OLC = NeuralPlayer('OLC', 'O')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement