def test_default_can_play_2048(self): n = Net() b = Board() data = b._tiles.flatten() output = n.run(data) self.assertEqual(output.shape, (n.outputs, )) b.move(output.argmax(), suppress_invalid=True)
def test_square_can_process_input(self): hidden_weights = ( numpy.array([[3.0, 2.0, 0.5], [3.0, 0.5, 2.0], [0.5, 3.0, 2.0]]), numpy.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]), ) n = Net(hidden_sizes=(3, ), weights=hidden_weights, inputs=3, outputs=3) # Layer one: # 10.5 12 10.5 # Layer two: # 33 33 33 # Softmax: # 0.33 0.33 0.33 data = numpy.array([1, 2, 3]) output = n.run(data) for i in output: self.assertAlmostEqual(i, 0.333, 3)
def test_nets_can_use_callable_weights(self): n = Net(weights=Net.random_weights) data = numpy.random.random((n.inputs, )) output = n.run(data) self.assertEqual(output.shape, (n.outputs, ))
def test_default_can_process_input(self): n = Net() data = numpy.random.random((n.inputs, )) output = n.run(data) self.assertEqual(output.shape, (n.outputs, ))
class Game(object): def __init__(self, net_number=None, epoch_number=None, experiment_name=None, use_naive_opponent=False, play_solo=False, **kwargs): self.board = BoardTTT(**kwargs) self.opponent = None if use_naive_opponent: self.opponent = NaiveTTTPlayer() elif (not play_solo and epoch_number is not None and experiment_name is not None and net_number is not None): from nets import Net from trainer import GeneticNetTrainer try: net_data = GeneticNetTrainer.load_single_net( idx=net_number, epoch_num=epoch_number, experiment_name=experiment_name) # Hardcoding is hard net_hidden_sizes = (3, 3) # 2D Tic Tac Toe sizes net_inputs = 9 net_outputs = 9 self.opponent = Net(hidden_sizes=net_hidden_sizes, weights=net_data, inputs=net_inputs, outputs=net_outputs) except IOError: pass def run(self): last_move = None last_last_move = None player_to_move = 1 try: while True: self.board.display() print("{} to move".format( self.board.player_to_tile[player_to_move])) if player_to_move == 2 and self.opponent: # AI always see themselves as player one data = self.board.normalize_board(player_to_move) output = self.opponent.run(data) print("{} to take {}".format( self.board.player_to_tile[player_to_move], output.argmax())) if self.board.loop(output.argmax(), player_to_move, suppress_invalid=True): # 2 player game player_to_move = (player_to_move % 2) + 1 else: print( "The AI made an invalid move and probably can't" " find a valid move. Sorry. :(") self.board.game_over(invalid_move=True) continue s = raw_input().strip().lower() is_number = False temp_move = -1 try: temp_move = int(s) is_number = True except ValueError: pass print("") if s.startswith('m'): print("Moves: {}".format(self.board.valid_moves)) continue elif s.startswith('u'): if self.board.undo(): last_move = last_last_move continue elif s.startswith('q'): self.board.game_over() elif (s.startswith('h') or not is_number): print("Commands:") print("- [H]elp (this menu)") print("- [M]oves (# of moves)") print("- [U]ndo (revert last move)") print("- [Q]uit (leave game)") print("- Type a number to place your symbol there") continue if self.board.loop(temp_move, player_to_move): last_last_move = last_move last_move = temp_move player_to_move = (player_to_move % 2) + 1 except GameOver as go: self.board.display() print("Game over! {} wins".format( self.board.player_to_tile.get(int(str(go)), "Nobody")))
class Game(object): def __init__( self, net_number=None, epoch_number=None, experiment_name=None, use_naive_opponent=False, play_solo=False, **kwargs): self.board = BoardTTT(**kwargs) self.opponent = None if use_naive_opponent: self.opponent = NaiveTTTPlayer() elif (not play_solo and epoch_number is not None and experiment_name is not None and net_number is not None): from nets import Net from trainer import GeneticNetTrainer try: net_data = GeneticNetTrainer.load_single_net( idx=net_number, epoch_num=epoch_number, experiment_name=experiment_name) # Hardcoding is hard net_hidden_sizes = (3, 3) # 2D Tic Tac Toe sizes net_inputs = 9 net_outputs = 9 self.opponent = Net( hidden_sizes=net_hidden_sizes, weights=net_data, inputs=net_inputs, outputs=net_outputs) except IOError: pass def run(self): last_move = None last_last_move = None player_to_move = 1 try: while True: self.board.display() print("{} to move".format( self.board.player_to_tile[player_to_move])) if player_to_move == 2 and self.opponent: # AI always see themselves as player one data = self.board.normalize_board(player_to_move) output = self.opponent.run(data) print("{} to take {}".format( self.board.player_to_tile[player_to_move], output.argmax())) if self.board.loop( output.argmax(), player_to_move, suppress_invalid=True): # 2 player game player_to_move = (player_to_move % 2) + 1 else: print("The AI made an invalid move and probably can't" " find a valid move. Sorry. :(") self.board.game_over(invalid_move=True) continue s = raw_input().strip().lower() is_number = False temp_move = -1 try: temp_move = int(s) is_number = True except ValueError: pass print("") if s.startswith('m'): print("Moves: {}".format(self.board.valid_moves)) continue elif s.startswith('u'): if self.board.undo(): last_move = last_last_move continue elif s.startswith('q'): self.board.game_over() elif (s.startswith('h') or not is_number): print("Commands:") print("- [H]elp (this menu)") print("- [M]oves (# of moves)") print("- [U]ndo (revert last move)") print("- [Q]uit (leave game)") print("- Type a number to place your symbol there") continue if self.board.loop(temp_move, player_to_move): last_last_move = last_move last_move = temp_move player_to_move = (player_to_move % 2) + 1 except GameOver as go: self.board.display() print("Game over! {} wins".format( self.board.player_to_tile.get(int(str(go)), "Nobody")))