示例#1
0
    def test_priornet_tictactoe_self_play(self):
        ttt = TicTacToe()
        nn = NeuralNetwork(ttt, PriorNet)

        t = Trainer(ttt, nn, num_simulations=2, num_games=1, num_updates=0, buffer_size_limit=None, cpuct=1, num_threads=4)
        data = t.self_play(temperature=0)

        np.testing.assert_equal(data[:,-1], np.array([1, -1, 1, -1, 1, -1, 1]))
        s = ttt.get_initial_state()
        np.testing.assert_equal(data[0,0], s)
        np.testing.assert_equal(data[0,1], np.array([0, 1, 0, 0, 0, 0, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[0,1,0],[0,0,0],[0,0,0]])) # Top-middle X
        np.testing.assert_equal(data[1,0], s) 
        np.testing.assert_equal(data[1,1], np.array([1, 0, 0, 0, 0, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[1,0,0],[0,0,0],[0,0,0]])) # Top-left O
        np.testing.assert_equal(data[2,0], s)
        np.testing.assert_equal(data[2,1], np.array([1, 0, 0, 0, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[0,0,1],[0,0,0],[0,0,0]])) # Top-right X
        np.testing.assert_equal(data[3,0], s)
        np.testing.assert_equal(data[3,1], np.array([1, 0, 0, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[0,0,0],[1,0,0],[0,0,0]])) # Mid-left O
        np.testing.assert_equal(data[4,0], s)
        np.testing.assert_equal(data[4,1], np.array([1, 0, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[0,0,0],[0,1,0],[0,0,0]])) # Mid-mid X
        np.testing.assert_equal(data[5,0], s)
        np.testing.assert_equal(data[5,1], np.array([1, 0, 0, 0]))
        s = ttt.take_action(s, np.array([[0,0,0],[0,0,1],[0,0,0]])) # Mid-right O
        np.testing.assert_equal(data[6,0], s)
        np.testing.assert_equal(data[6,1], np.array([1, 0, 0]))
示例#2
0
 def test_policy_iteration(self):
     ttt = TicTacToe()
     nn = NeuralNetwork(ttt, PriorNet)
     t = Trainer(ttt, nn, num_simulations=2, num_games=100, num_updates=0, buffer_size_limit=None, cpuct=1, num_threads=4)
     t.policy_iteration()
     states = t.training_data[:,0]
     inits = 0
     for s in states:
         if (s.astype(np.float32) == ttt.get_initial_state()).all():
             inits += 1
     self.assertEqual(inits, 100)
示例#3
0
 def test_8(self):
     game = TicTacToe(['X', 1, 'X', 3, 'O', 5, 6, 'O', 'X'])
     self.assertEqual({
         3: {
             'X': 0,
             'O': 0
         },
         2: {
             'X': 2,
             'O': 1
         }
     }, game.count_lines(game.state))
示例#4
0
 def test_5(self):
     game = TicTacToe(['X', 'X', 'X', 'O', 'O', 'X', 'X', 'O', 'O'])
     self.assertEqual({
         3: {
             'X': 1,
             'O': 0
         },
         2: {
             'X': 0,
             'O': 0
         }
     }, game.count_lines(game.state))
示例#5
0
 def test_1(self):
     game = TicTacToe(['O', 1, 2, 3, 4, 5, 6, 7, 8])
     self.assertEqual({
         3: {
             'X': 0,
             'O': 0
         },
         2: {
             'X': 0,
             'O': 0
         }
     }, game.count_lines(game.state))
示例#6
0
 def get_game_from_name(game_name):
     if game_name.endswith("tic_tac_toe"):
         return TicTacToe()
     elif game_name.endswith("connect_four"):
         return ConnectFour()
     elif game_name.endswith("reversi"):
         return Reversi()
示例#7
0
文件: runner.py 项目: dsemi/web-ai
    def __init__(self):
        self.db = MongoClient('mongodb://localhost:27017/').ai_data
        self.process_strings = []
        self.emails = []
        
        count = 2
        for person in self.db.posts.find({'game':'tic tac toe'}):
            if not count:
                break
            if person['lang'] == 'java':
                with open('bridges/java/src/com/ai/api/MyAI.java', 'w') as f:
                    f.write(person['code'])
                os.chdir('bridges/java')
                p = Popen(shlex.split('mvn install'))
                p.communicate()
                os.chdir('../..')
                self.process_strings.append('java -jar bridges/java/target/JavaAPI-0.0.1-SNAPSHOT-jar-with-dependencies.jar')
                self.emails.append(person['email'])
            elif person['lang'] == 'javascript':
                with open('bridges/javascript/myAI.js', 'w') as f:
                    f.write(person['code'])
                self.process_strings.append('node bridges/javascript/bridge.js')
                self.emails.append(person['email'])
            else:
                pass
            count -= 1

        while len(self.process_strings) < 2:
            self.process_strings.append(('java -jar bridges/java/target/JavaAPI-0.0.1-SNAPSHOT-jar-with-dependencies.jar', 'node bridges/javascript/bridge.js')[random.randint(0,1)])
            
        self.game = TicTacToe()
        self.processes = []
    def move(self, game: TicTacToe, possible_steps=None):
        available = game.get_possible_next_steps()
        while True:
            choice = int(input('Your move? [0-8]'))
            if choice in available:
                return choice

            print('Not available')
 def test_resets_work(self):
     game = TicTacToe()
     p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1)
     for _ in range(100):
         scores, outcomes = play_match(game, [p1, p2])
         self.assertEqual(scores, {p1: 1.0, p2: 0.0})
         self.assertEqual(outcomes, {p1: "Win", p2: "Lose"})
     for _ in range(100):
         scores, outcomes = play_match(game, [p1, p2], permute=True)
         self.assertEqual(scores, {p1: 0.5, p2: 0.5})
         self.assertEqual(outcomes, {p1: "Tie", p2: "Tie"})
示例#10
0
    def __init__(self):
        self.db = MongoClient('mongodb://localhost:27017/').ai_data
        self.process_strings = []
        self.emails = []

        count = 2
        for person in self.db.posts.find({'game': 'tic tac toe'}):
            if not count:
                break
            if person['lang'] == 'java':
                with open('bridges/java/src/com/ai/api/MyAI.java', 'w') as f:
                    f.write(person['code'])
                os.chdir('bridges/java')
                p = Popen(shlex.split('mvn install'))
                p.communicate()
                os.chdir('../..')
                self.process_strings.append(
                    'java -jar bridges/java/target/JavaAPI-0.0.1-SNAPSHOT-jar-with-dependencies.jar'
                )
                self.emails.append(person['email'])
            elif person['lang'] == 'javascript':
                with open('bridges/javascript/myAI.js', 'w') as f:
                    f.write(person['code'])
                self.process_strings.append(
                    'node bridges/javascript/bridge.js')
                self.emails.append(person['email'])
            else:
                pass
            count -= 1

        while len(self.process_strings) < 2:
            self.process_strings.append((
                'java -jar bridges/java/target/JavaAPI-0.0.1-SNAPSHOT-jar-with-dependencies.jar',
                'node bridges/javascript/bridge.js')[random.randint(0, 1)])

        self.game = TicTacToe()
        self.processes = []
示例#11
0
def main(board_obj: TicTacToe):
    """
    The main function to play a game of Tic Tac Toe.
    
    :param board_obj: The Board object to run the game on.
    :return: 
    """

    print("Welcome to Tic Tac Toe.")
    print("Possible moves are:")
    board_obj.print_move_grid()

    while board_obj.active:
        # Print a newline.
        print()

        # Get the move the player made.
        move = input(
            f"{board_obj.get_player_icon(board_obj.current_player)} > ")

        # Check the validity of the move.
        while not board_obj.check_move_validity(move):
            print("Invalid move.")
            move = input(
                f"{board_obj.get_player_icon(board_obj.current_player)} > ")

        # Convert the move to an int.
        move = int(move)

        # Make the move.
        board_obj.make_move(move)

        # Notify the player of the new board.
        board_obj.print_grid()

    # Print the winner.
    print("\nThe game ended!")
    print(f"Player {board_obj.get_player_icon(board_obj.winner)} won!")
示例#12
0
文件: runner.py 项目: dsemi/web-ai
class Runner:
    def __init__(self):
        self.db = MongoClient('mongodb://*****:*****@osai.com", "AI Results", "Your AI code has finished running:<br>", 'http://127.0.0.1:5000/static/replay.html?id='+str(ident))
            for email in self.emails:
                message.add_to(email)

            s.smtp.send(message)
示例#13
0
class Runner:
    def __init__(self):
        self.db = MongoClient('mongodb://*****:*****@osai.com", "AI Results",
                "Your AI code has finished running:<br>",
                'http://127.0.0.1:5000/static/replay.html?id=' + str(ident))
            for email in self.emails:
                message.add_to(email)

            s.smtp.send(message)
 def test_permutation(self):
     game = TicTacToe()
     p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1)
     scores, outcomes = play_match(game, [p1, p2], permute=True)
     self.assertEqual(scores, {p1: 0.5, p2: 0.5})
     self.assertEqual(outcomes, {p1: "Tie", p2: "Tie"})
示例#15
0
    # Print the winner.
    print("\nThe game ended!")
    print(f"Player {board_obj.get_player_icon(board_obj.winner)} won!")


if __name__ == '__main__':
    try:
        # Ask the user for parameters.
        grid_size = int(
            validate_input("Size of the grid: ", lambda x: x.isnumeric()))
        amount_of_players = int(
            validate_input("Amount of players: ", lambda x: x.isnumeric()))
        player_symbols = validate_input(
            "Enter the symbols for the players, separated by space.\n",
            lambda x: len(set(x.split())) >= amount_of_players).split(
            )[:amount_of_players]
        player_symbols = [x[:1] for x in player_symbols
                          ]  # Only use the first character of each symbol.

        # Create the Board class
        board = TicTacToe(grid_size=grid_size,
                          players=amount_of_players,
                          player_symbols=player_symbols)

        # Run the main function.
        main(board)

    except KeyboardInterrupt:
        print("\n\nThanks for playing.")
        exit(0)
        window = 1
        error = np.convolve(error, np.ones(window), mode="valid") / window
        min_len = len(error) if min_len is None else min(min_len, len(error))
        plt.plot(error, label=model_class.__name__)

    plt.title("Training loss for {}".format(game.__class__.__name__))
    ax.set_xlim(left=0, right=min_len)
    ax.set_ylabel("Error")
    ax.set_xlabel("Iteration")
    plt.legend()
    plt.show()


if __name__ == "__main__":
    checkpoint = 13
    game = TicTacToe()
    model_class = MiniVGG
    sims = 50
    cuda = False

    print("*** Rank Checkpoints ***")
    rank_checkpoints(game, model_class, sims, cuda)
    print("*** One vs All ***")
    one_vs_all(checkpoint, game, model_class, sims, cuda)
    print("*** Effective Model Power ***")
    effective_model_power(checkpoint, game, model_class, sims, cuda)
    plot_train_loss(game, [model_class], [cuda])
    '''
    x = [10,20,40,80,160,320,640,1280,2560,5120,10240,20480]
    control = [1., .5, .5, .25, .25, .25, .25, .25, .25, .25, .5, .5]
    senet = [1., .75, .75, .75, .75, .75, .75, .5, .5, .5, .5, .5]
示例#17
0
 def test_buffer_size_limit_100(self):
     ttt = TicTacToe()
     nn = NeuralNetwork(ttt, PriorNet)
     t = Trainer(ttt, nn, num_simulations=2, num_games=100, num_updates=0, buffer_size_limit=100, cpuct=1, num_threads=4)
     t.policy_iteration()
     self.assertEqual(len(t.training_data), 100)
 def test_simple(self):
     game = TicTacToe()
     p1, p2 = UninformedMCTSPlayer(game, 1), UninformedMCTSPlayer(game, 1)
     scores, outcomes = play_match(game, [p1, p2])
     self.assertEqual(scores, {p1: 1.0, p2: 0.0})
     self.assertEqual(outcomes, {p1: "Win", p2: "Lose"})