Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.88 KB | None | 0 0
  1. from __future__ import division
  2. from random import shuffle, randint
  3. import matplotlib.pyplot as plt
  4. from math import exp
  5.  
  6. INVALID = "Invalid Move."
  7. TIE = "No winner, it's a tie!"
  8.  
  9.  
  10. class Board():
  11.     def __init__(self):
  12.         self.structure = [[None, None, None] for i in range(3)]
  13.  
  14.     def put_marker(self, marker, coords):
  15.         try:
  16.             if self.structure[coords[0]][coords[1]] is None:
  17.                 self.structure[coords[0]][coords[1]] = marker
  18.             else:
  19.                 return INVALID
  20.         except:
  21.             return INVALID
  22.  
  23.     def display(self):
  24.         for row in self.structure:
  25.             line = '= ' + str(row[0]) + ';' + str(row[1]) + ';' + str(row[2]) + ' ='
  26.             print(line.replace("None", " "))
  27.  
  28.  
  29. class Game():
  30.     def __init__(self, player1, player2):
  31.         self.board, self.turn, self.winner = Board(), 0, None
  32.         self.playerList = [player1, player2]
  33.         shuffle(self.playerList)
  34.  
  35.     def play(self):
  36.         turn = 0
  37.         while self.winner is None:
  38.             turn += 1
  39.             if self.board.put_marker(self.playerList[turn % 2].marker,
  40.                                      self.playerList[turn % 2].get_move(self.board)) != INVALID:
  41.                 self.check(self.playerList[turn % 2])
  42.             else:
  43.                 self.winner = self.playerList[turn % 2 - 1]
  44.         if self.winner != TIE:
  45.             self.winner.winner()
  46.             if self.playerList[0] == self.winner:
  47.                 self.playerList[1].loser()
  48.             else:
  49.                 self.playerList[0].loser()
  50.  
  51.     def check(self, candidate):
  52.         self.tieCheck = 0
  53.         for row in self.board.structure:
  54.             if all(row) is True:
  55.                 self.tieCheck += 1
  56.             if row[0] == row[1] == row[2] and row[0] is not None:
  57.                 self.winner = candidate
  58.         if self.tieCheck == 3:
  59.             self.winner = TIE
  60.             self.playerList[0].tie()
  61.             self.playerList[1].tie()
  62.         for i in range(3):
  63.             if self.board.structure[0][i] == self.board.structure[1][i] == self.board.structure[2][i] and \
  64.                self.board.structure[0][i] is not None:
  65.                 self.winner = candidate
  66.         if self.board.structure[1][1] is not None and (
  67.            (self.board.structure[0][0] == self.board.structure[1][1] == self.board.structure[2][2]) or (
  68.                 self.board.structure[0][2] == self.board.structure[1][1] == self.board.structure[2][0])):
  69.                 self.winner = candidate
  70.  
  71.  
  72. class HumanPlayer():
  73.     def __init__(self, name, marker):
  74.         self.name, self.marker = name, marker
  75.         self.winCount, self.loseCount, self.tieCount = 0, 0, 0
  76.  
  77.     def get_move(self, board):
  78.         board.display()
  79.         return input(self.name + ' what are your commands? ')
  80.  
  81.     def winner(self):
  82.         print(self.name + ', you won!')
  83.  
  84.     def loser(self):
  85.         print(self.name + ', you lose!')
  86.  
  87.     def tie(self):
  88.         print("It's a tie!")
  89.  
  90.  
  91. class NeuralPlayer():
  92.     def __init__(self, name, marker):
  93.         self.name, self.marker = name, marker
  94.         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
  95.                                                                                                             range(9)]
  96.         self.winCount, self.loseCount, self.tieCount = 0, 0, 0
  97.         self.learning = True
  98.         self.history = []
  99.  
  100.     def get_move(self, board):
  101.         self.reset_output()
  102.         self.links = [[] for i in range(9)]
  103.         for i in range(len(board.structure)):
  104.             for j in range(len(board.structure[i])):
  105.                 if board.structure[i][j] != None and board.structure[i][j] != self.marker:
  106.                     for k in range(len(self.network[0][i][j].connections)):
  107.                         if self.network[0][i][j].connections[k] != 0:
  108.                             self.output[k] += self.network[0][i][j].connections[k]
  109.                             self.links[k].append(self.network[0][i][j])
  110.                 elif board.structure[i][j] == self.marker:
  111.                     for k in range(len(self.network[1][i][j].connections)):
  112.                         if self.network[1][i][j].connections[k] != 0:
  113.                             self.output[k] += self.network[1][i][j].connections[k]
  114.                             self.links[k].append(self.network[1][i][j])
  115.         self.move = self.output.index(max(self.output))
  116.         self.history.append([self.move, self.links[self.move]])
  117.         return [self.move % 3, self.move // 3]
  118.  
  119.     def winner(self):
  120.         if not self.learning:
  121.             #print(self.name + ' is the winner!')
  122.             self.winCount += 1
  123.             return None
  124.         self.currentTurn, self.turns = 0, len(self.history)
  125.         #print(self.name + ' is the winner!')
  126.         for moves in self.history:
  127.             self.currentTurn += 1
  128.             for neurons in moves[1]:
  129.                 neurons.connections[self.move] += 100 * exp(self.currentTurn - self.turns)
  130.         #if self.history != []:
  131.             #for neurons in self.history[-1][1]:
  132.                 #neurons.connections[self.move] += 100
  133.         self.history = []
  134.  
  135.     def loser(self):
  136.         if not (self.learning):
  137.             self.loseCount += 1
  138.             return None
  139.         self.currentTurn, self.turns = 0, len(self.history)
  140.         for moves in self.history:
  141.             self.currentTurn += 1
  142.             for neurons in moves[1]:
  143.                 neurons.connections[self.move] -= 250 * exp(self.currentTurn - self.turns)
  144.         #if self.history != []:
  145.             #for neurons in self.history[-1][1]:
  146.                 #neurons.connections[self.move] -= 250
  147.         self.history = []
  148.  
  149.     def tie(self):
  150.         if not (self.learning):
  151.             self.tieCount += 1
  152.             #print('Tie.')
  153.             return None
  154.         self.currentTurn, self.turns = 0, len(self.history)
  155.         #print('Tie.')
  156.         for moves in self.history:
  157.             self.currentTurn += 1
  158.             for neurons in moves[1]:
  159.                 neurons.connections[self.move] += 10 * exp(self.currentTurn - self.turns)
  160.         self.history = []
  161.  
  162.     def reset_output(self):
  163.         self.output = [0 for i in range(9)]
  164.  
  165.     def enable_learning(self):
  166.         self.learning = True
  167.         print('[' + self.name + ']' + ' Learning now enabled.')
  168.  
  169.     def disable_learning(self):
  170.         self.learning = False
  171.         print('[' + self.name + ']' + ' Learning now disabled.')
  172.  
  173.     def normalize(self):
  174.         for layer in self.network:
  175.             for row in layer:
  176.                 for neuron in row:
  177.                     neuron.normalize()
  178.  
  179.  
  180. class RandomPlayer():
  181.     def __init__(self, isSmart=True):
  182.         self.intelligence = isSmart
  183.         self.name, self.marker = 'Random', 'R'
  184.         self.winCount, self.loseCount, self.tieCount = 0, 0, 0
  185.  
  186.     def get_move(self, board):
  187.         candidate = randint(1, 9)
  188.         if self.intelligence:
  189.             self.notValid = True
  190.             while self.notValid:
  191.                 candidate = randint(0, 8)
  192.                 if board.structure[candidate % 3][candidate // 3] == None:
  193.                     self.notValid = False
  194.         return [candidate % 3, candidate // 3]
  195.  
  196.     def winner(self):
  197.         #print(self.name + ' is the winner!')
  198.         self.winCount += 1
  199.  
  200.     def loser(self):
  201.         self.loseCount += 1
  202.  
  203.     def tie(self):
  204.         self.tieCount += 1
  205.  
  206.  
  207. class Neuron():
  208.     def __init__(self, synapses):
  209.         self.connections = [1 for i in range(synapses)]
  210.  
  211.     def normalize(self):
  212.         print('Wait, what?')
  213.         self.absConnections = [abs(i) for i in self.connections]
  214.         self.coefficient = 100000 / max(self.absConnections)
  215.         self.newConnections = [i * self.coefficient for i in self.connections]
  216.         self.connections = self.newConnections[:]
  217.  
  218.  
  219. def manual_trainer(nplayer):
  220.     while True:
  221.         game = Game(h1, nplayer)
  222.         game.play()
  223.  
  224.  
  225. def auto_trainer(p1, p2, ngame=100):
  226.     while ngame > 0:
  227.         ngame -= 1
  228.         game = Game(p1, p2)
  229.         game.play()
  230.  
  231.  
  232. def evaluate_level(player, ngame=100):
  233.     player.winCount, player.tieCount, player.loseCount = 0, 0, 0
  234.     randomPlayer = RandomPlayer()
  235.     player.disable_learning()
  236.     while ngame > 0:
  237.         ngame -= 1
  238.         game = Game(randomPlayer, player)
  239.         game.play()
  240.     print(str((player.winCount + player.tieCount) / (
  241.         player.winCount + player.tieCount + player.loseCount) * 100) + '% victory / tie.')
  242.     player.enable_learning()
  243.     return (player.winCount + player.tieCount) / (
  244.         player.winCount + player.tieCount + player.loseCount) * 100
  245.  
  246.  
  247. def convergence_analysis(player1, player2, dG=2, games=100000):
  248.     game, x, y = 1, [], []
  249.     while game <= games:
  250.         print(game)
  251.         auto_trainer(player1, player2, game)
  252.         x.append(game)
  253.         y.append(evaluate_level(player1, 1000))
  254.         game *= dG
  255.     plt.plot(x, y)
  256.     plt.xscale('symlog')
  257.     plt.show()
  258.  
  259.  
  260. def hybrid_training(player1, player2, player3, dG=2, initialPool = 36000, finalPool = 250000):
  261.     game, x, y, first = 1, [], [], True
  262.     while game <= finalPool:
  263.         while game <= initialPool:
  264.             print(game)
  265.             auto_trainer(player1, player3, game)
  266.             auto_trainer(player2, player3, game)
  267.             x.append(game)
  268.             y.append(evaluate_level(player1, 1000))
  269.             game *= dG
  270.         if first:
  271.             player1.normalize()
  272.             player2.normalize()
  273.         print(game)
  274.         auto_trainer(player1, player2, game)
  275.         x.append(game)
  276.         y.append(evaluate_level(player1, 1000))
  277.         game *= dG
  278.         first = False
  279.     plt.plot(x, y)
  280.     plt.xscale('symlog')
  281.     plt.show()
  282.  
  283.  
  284. h1 = HumanPlayer('Julien', 'J')
  285. h2 = HumanPlayer('Test', 'T')
  286. r1 = RandomPlayer()
  287. SSATR = NeuralPlayer('SSATR', 'S')
  288. OLC = NeuralPlayer('OLC', 'O')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement