class TestVictory(unittest.TestCase): def setUp(self): self.example_ticTacToe_game = TicTacToe(4) self.example_ticTacToe_game.make_move((0, 0)) self.example_ticTacToe_game.make_move((0, 1)) self.example_ticTacToe_game.make_move((0, 2)) self.example_ticTacToe_game.make_move((0, 3)) self.example_ticTacToe_game.make_move((1, 0)) self.example_ticTacToe_game.make_move((2, 0)) self.example_ticTacToe_game.make_move((1, 1)) self.example_ticTacToe_game.make_move((2, 1)) self.example_ticTacToe_game.make_move((1, 2)) self.example_ticTacToe_game.make_move((3, 0)) self.example_ticTacToe_game.make_move((2, 2)) self.example_ticTacToe_game.make_move((3, 1)) def test_no_victory(self): self.assertFalse(self.example_ticTacToe_game.is_victory()) def test_only_horizontal_victory(self): self.example_ticTacToe_game.make_move((1, 3)) self.assertTrue(self.example_ticTacToe_game.is_victory()) def test_only_vertical_victory(self): self.example_ticTacToe_game.make_move((3, 2)) self.assertTrue(self.example_ticTacToe_game.is_victory()) def test_only_diagonal_victory(self): self.example_ticTacToe_game.make_move((3, 3)) self.assertTrue(self.example_ticTacToe_game.is_victory())
def testAgainstSecondMoveHeuristikAgentIn100Testgames(self): heuristicSearchAgentWins = 0 tdqAgent1000Wins = 0 for testGameCount in range(100): ttt = TicTacToe(4) tdqAgent1000 = TicTacToeTDQLearningAgent( TICTACTOE_4x4_TDQ_AGENT_1000_NAME, 4) while not ttt.is_terminal(): action = tdqAgent1000.suggestAction(ttt) print action ttt.make_move(action) if not ttt.is_terminal(): HeuristicSearchAgentTicTacToe.processAction(ttt) print ttt.printable_game_matrix() if ttt.is_victory() and ttt.get_player_which_moved_last() == 'X': tdqAgent1000Wins += 1 elif ttt.is_victory() and ttt.get_player_which_moved_last() == 'O': heuristicSearchAgentWins += 1 print 'Second move heuristic search agent wins: ' + str( heuristicSearchAgentWins ) + ' games against TD-Q-Agent-1000 in 16 field Tic Tac Toe!' print 'First move TD-Q-Agent-1000 wins: ' + str( tdqAgent1000Wins ) + ' games against heuristic search agent in 16 field Tic Tac Toe!' self.assertTrue(tdqAgent1000Wins >= 50)
def create_random_tictactoe_training_example(): tictactoe = TicTacToe(4) round_count = 0 target_value = '' action_sequence = [] while target_value == '': actions = tictactoe.get_possible_moves() random_action = randint(0, (len(actions) - 1)) action_sequence.append(actions[random_action]) if (round_count % 2) == 0: tictactoe.make_move(actions[random_action]) else: tictactoe.make_move(actions[random_action]) if round_count == 15 and not tictactoe.is_victory(): target_value = 'draw' if (round_count % 2) == 0 and tictactoe.is_victory(): target_value = 'win' if (round_count % 2) == 1 and tictactoe.is_victory(): target_value = 'lost' round_count += 1 return [action_sequence, target_value, tictactoe]
def getRandomNonTerminalTicTacToeState(): ttt = TicTacToe(4) randomDepth = randint(0, 15) for depth in range(randomDepth): if not ttt.is_victory(): ttt.make_move(getRandomAction(ttt.get_possible_moves())) if ttt.is_victory(): ttt.undo_move() return ttt
def testAgainstFirstMoveRandomAgentIn100Testgames(self): randomAgentWins = 0 tdqAgent1000Wins = 0 for testGameCount in range(100): ttt = TicTacToe(3) tdqAgent1000 = TicTacToeTDQLearningAgent(TICTACTOE_3x3_TDQ_AGENT_1000_NAME, 3) while not ttt.is_terminal(): RandomAgent.processTicTacToeAction(ttt) if not ttt.is_terminal(): ttt.make_move(tdqAgent1000.suggestAction(ttt)) print ttt.printable_game_matrix() if ttt.is_victory() and ttt.get_player_which_moved_last() == 'X': randomAgentWins += 1 elif ttt.is_victory() and ttt.get_player_which_moved_last() == 'O': tdqAgent1000Wins += 1 print 'First Move random agent wins: ' + str( randomAgentWins) + ' games against TD-Q-Agent-1000 in 9 field Tic Tac Toe!' print 'Second Move TD-Q-Agent-1000 wins: ' + str( tdqAgent1000Wins) + ' games against random agent in 9 field Tic Tac Toe!' self.assertTrue(tdqAgent1000Wins >= 50)
def testAgainstFirstMoveRandomAgentIn9FieldTicTacToe(self): randomAgentWins = 0 heuristicSearchAgentWins = 0 for testGameCount in range(100): ttt = TicTacToe(3) while not ttt.is_terminal(): RandomAgent.processTicTacToeAction(ttt) if not ttt.is_terminal(): HeuristicSearchAgentTicTacToe.processAction(ttt) print ttt.printable_game_matrix() if ttt.is_victory() and ttt.get_player_which_moved_last() == 'X': randomAgentWins += 1 elif ttt.is_victory() and ttt.get_player_which_moved_last() == 'O': heuristicSearchAgentWins += 1 print 'First Move random agent wins: ' + str( randomAgentWins ) + ' games against heuristic search agent in 9 field Tic Tac Toe!' print 'Second Move heuristic search agent wins: ' + str( heuristicSearchAgentWins ) + ' games against random agent in 9 field Tic Tac Toe!' self.assertTrue(heuristicSearchAgentWins >= 60)