def test_stockfish(self):
     board = get_random_board()
     process = get_process_stockfish(board, "level 40 5 0")
     player_blue = StockfishPlayer(Color.BLUE, process, think_time=-1)
     player_red = StockfishPlayer(Color.RED, process, think_time=-1)
     game = Game(player_blue, player_red, board)
     winner = game.run_game(200)
     print(winner)
     print(repr(game.board))
 def test_random(self):
     player_blue = RandomPlayer(Color.BLUE)
     player_red = RandomPlayer(Color.RED)
     board = Board()
     game = Game(player_blue, player_red, board)
     game.run_game(200)
     print(repr(board))
     print(board)
     print(game.get_winner())
def fight(player_blue, player_red, iter_max, print_board=False):
    board = get_random_board()
    game = Game(player_blue, player_red, board)
    winner = game.run_game(iter_max, print_board=print_board)
    print("Winner:", winner)
    print("Score BLUE:", board.get_score(Color.BLUE))
    print("Score RED:", board.get_score(Color.RED))
    print(repr(board))
    print(board)
    print(game.to_uci_usi())
    return winner
Beispiel #4
0
 def test_single_action_random(self):
     n_simulations = 800
     node = MCTSNode()
     player_blue = RandomMCTSPlayer(Color.BLUE,
                                    n_simulations=n_simulations,
                                    current_node=node)
     player_red = RandomMCTSPlayer(Color.RED,
                                   n_simulations=n_simulations,
                                   current_node=node)
     board = get_random_board()
     game = Game(player_blue, player_red, board)
     game.get_next_action()
Beispiel #5
0
 def test_random_vs_random(self):
     n_simulations = 400
     node = MCTSNode()
     player_blue = RandomMCTSPlayer(Color.BLUE,
                                    n_simulations=n_simulations,
                                    current_node=node)
     player_red = RandomMCTSPlayer(Color.RED,
                                   n_simulations=n_simulations,
                                   current_node=node)
     # winner = fight(player_blue, player_red, 200)
     board = get_random_board()
     game = Game(player_blue, player_red, board)
     winner = game.run_game(200)
     self.assertIn(winner, [Color.BLUE, Color.RED])
     print(game.to_json(node))
Beispiel #6
0
 def test_uci_usi(self):
     game = Game.from_uci_usi(
         RandomPlayer(Color.BLUE),
         RandomPlayer(Color.RED),
         "position fen r1bakab1r/9/2ncc4/1pppn2R1/9/9/BPP1P1P1P/2N1CN1C1/4K4/3A1A2R w - - 0 1 "
         "moves g4h4 f10e9 i1g1 d10d9 g1g9 d8d10 h3h5 a10a6 e3i3 e7f5 h5e5 a6e6 i3i10 d10f10 "
         "h7f7 f5h4 e5e3 e6i6 g9g10 h4f3 g10f10")
     self.assertIsNotNone(game)
Beispiel #7
0
 def test_single_action_nn(self):
     n_simulations = 800
     player_blue = NNPlayer(Color.BLUE,
                            n_simulations=n_simulations,
                            janggi_net=JanggiNetwork(),
                            temperature_start=0.01,
                            temperature_threshold=30,
                            temperature_end=0.01)
     player_red = NNPlayer(Color.RED,
                           n_simulations=n_simulations,
                           janggi_net=JanggiNetwork(),
                           temperature_start=0.01,
                           temperature_threshold=30,
                           temperature_end=0.01)
     board = get_random_board()
     game = Game(player_blue, player_red, board)
     game.get_next_action()
Beispiel #8
0
 def run_episode(self):
     examples = []
     board = get_random_board()
     initial_node = MCTSNode(is_initial=True)
     player_blue = NNPlayer(Color.BLUE,
                            n_simulations=self.n_simulations,
                            current_node=initial_node,
                            janggi_net=self.predictor,
                            temperature_start=1,
                            temperature_threshold=30,
                            temperature_end=0.01)
     player_red = NNPlayer(Color.RED,
                           n_simulations=self.n_simulations,
                           current_node=initial_node,
                           janggi_net=self.predictor,
                           temperature_start=1,
                           temperature_threshold=30,
                           temperature_end=0.01)
     game = Game(player_blue, player_red, board)
     while not game.is_finished(self.iter_max):
         new_action = game.get_next_action()
         game.actions.append(new_action)
         if game.current_player == Color.BLUE:
             examples.append([
                 board.get_features(game.current_player, game.round),
                 player_blue.current_node.get_policy(game.current_player),
                 Color.BLUE
             ])
             examples.append([
                 board.get_features(game.current_player,
                                    game.round,
                                    data_augmentation=True),
                 player_blue.current_node.get_policy(
                     game.current_player, data_augmentation=True),
                 Color.BLUE
             ])
         else:
             examples.append([
                 board.get_features(game.current_player,
                                    game.round,
                                    data_augmentation=True),
                 player_red.current_node.get_policy(game.current_player,
                                                    data_augmentation=True),
                 Color.RED
             ])
             examples.append([
                 board.get_features(game.current_player, game.round),
                 player_red.current_node.get_policy(game.current_player),
                 Color.RED
             ])
         game.board.apply_action(new_action)
         game.switch_player()
         game.board.invalidate_action_cache(
             new_action)  # Try to reduce memory usage
         game.round += 1
     winner = game.get_winner()
     set_winner(examples, winner)
     return examples
Beispiel #9
0
 def test_uci_usi4(self):
     game = Game.from_uci_usi(
         RandomPlayer(Color.BLUE),
         RandomPlayer(Color.RED),
         "position fen rbna1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1 "
         "moves b1c3 a7b7 b3e3 a10a4 c3a4 f10e10 e4d4 e7f7 g1e4 e9f9 h1g3 c7c6 h3f3 f7e7 e4g7 b8b2 a4b2 i10i8 a0a9 "
         "f9f8 g4f4")
     action = game.get_current_actions()[0]
     game.apply_action(action, invalidate_cache=False)
     game.reverse_action()
     print(game.get_current_actions())
Beispiel #10
0
 def test_uci_usi5(self):
     game = Game.from_uci_usi(
         RandomMCTSPlayer(Color.BLUE, n_simulations=100),
         RandomMCTSPlayer(Color.RED, n_simulations=100),
         "position fen rbna1anbr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RBNA1ABNR w - - 0 1 moves a4b4 i10i9 "
         "h1g3 e9d8 e4e5 d10d9 g4f4 e7f7 i1i2 d8e8 a1a4 i9h9 e2d2 e8d8 h3f3 h9e9 i2i1 a10a8 a4a1 e9d10 g3i2 i7i6 "
         "b4b5 d10e9 f4g4 e9i9 d1e1 f7e7 c4d4 i9f9 d2d1 a8a10 e5f5 e7f7 c1d3 f9f8 e1e2 b8e8 i2g3 i6i5 d4e4 e8c8 "
         "d3c1 d8e9 e2f2 f7e7 g4f4 c10b8 f5e5 f8d8 e5d5 c8c1 i4i5 h10f7 b5b6 d8f8 b6b7 e9d8 g3i4 g7g6 f1e1 c1c10 "
         "a1a2 c7b7 i5h5 f8e8 d5e5 h8h1 i1i3 g10f8 f2f1 c10i10 a2a7 g6h6 e5e6 e8e9 d1e2 f8h7 a7b7 e7d7 b7b8 a1a1 "
         "f3f7 a1a1 h5i5 a1a1 b8b5 a10a4 b3b7 e9d10 e6f6 f10e10 f6g6 d7d6 f4f5 a4c4 g6g7 h6i6 b5a5 h1h9 i3c3 i6i5 "
         "f5e5 i10i4 c3b3 h9h1 e5d5 c4c2 e2d1 h7g9 f7i7 c2h2 b3d3 h2c2 a5a9 c2c8 d3h3 i4i6 h3h10 g9i10 a9c9 d6d5 "
         "i7e7 d8e9 d1e2 i10h8 c9d9")
     action = game.get_current_actions()[0]
     game.apply_action(action, invalidate_cache=False)
     game.reverse_action()
Beispiel #11
0
def run_game(board, player_blue, player_red, iter_max):
    game = Game(player_blue, player_red, board)
    while not game.is_finished(iter_max):
        new_action = game.get_next_action()
        game.actions.append(new_action)
        game.board.apply_action(new_action)
        game.switch_player()
        game.board.invalidate_action_cache(
            new_action)  # Try to reduce memory usage
        game.round += 1
    return game
Beispiel #12
0
 def test_uci_usi3(self):
     uci_usi = "position fen rbna1anbr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RBNA1ABNR w - - 0 1 moves i4h4 " \
               "g10f8 h1g3 b8g8 a4b4 g7f7 g4f4 g8g2 c1d3 e9e9 b3e3 h8e8 h4g4 g2c2 b1d4 b10d7 h3f3 i10i8 i1h1 d7f4 " \
               "g4f4 i8g8 g3i4 i7h7 h1h7 g8g2 e2e1 f8h7 d1e2 c10d8 f3d1 d10e10 f1f2 c7b7 a1a5 c2f2 e2f2 e7e6 f2e2 " \
               "f7f6 a5h5 g2g7 h5h6 a10d10 d1f1 e9f9 f4f5 f9e9 f5f6 e6d6 f6g6 g7d7 e4e5 e9d9 i4g5 e8b8 g6f6 d7g7 " \
               "g5h7 g7g1 h7i9 g1i1 i9g8 b8g8 h6h10 f10e9 e2f2 g8b8 e1e2 b8e8 d3e1 i1i3 f2f3 b7c7 f1d1 c7d7 d4g6 " \
               "i3i7 f6e6 d8b7 e6d6 i7i2 f3f2 b7d6 e5e6 i2i5 e6d6 d10b10 d6d7 i5d5 g6d4 d5d7 d1d7 b10a10 d7d1 e9d8 " \
               "h10h9 d9d10 d1d8 e10f10 h9h10 d10e10 d8d1 a7b7 d1f1 a10a2 e2d3 a2f2 f1d1 b7c7 h10h8 e10e9 d4g6 " \
               "f2f8 h8h9 e9e10 h9c9 f10e9 c9c10 e9d10 c10c7 e10f10 c7e7 d10e9 e7i7 f10f9 i7i9 f9f10 e1g2 e8e10 " \
               "g2h4 e9f9 h4i6 f8f4 i6h8 f4f1 d3d2 f9f8 g6d4 f1f2 d2d3 f2f3 i9i1 f8e9 i1e1 f3f4 h8g6 f4f6 d3d3 " \
               "e10g10 d3d3 f6f2 e1e2 f2e2 d3e2 g10e10 e2e2 e10e4 e2e2 f10e10 g6e7 e4e8 e7d5 e10f10 d5c7 e8e10 " \
               "d1d6 e10e7 d6d3 e7e10 d3f1 e10e5 e2f2 f10e10 d4g2 e9d9 g2e5 d9e9 e5b7 e9f8 c7e8 f8e9 e8g9 e9f9 " \
               "f1f9 e10f10 f9f1 f10f10"
     game = Game.from_uci_usi(
         RandomPlayer(Color.BLUE),
         RandomPlayer(Color.RED),
         uci_usi)
     self.assertIsNotNone(game)
Beispiel #13
0
from janggi.player import RandomPlayer
from janggi.utils import Color

DIR = "pychess_games/"

if __name__ == "__main__":
    res = []
    for filename in listdir(DIR):
        with open(DIR + filename) as f:
            data = json.load(f)
        uci_usi = data["uci_usi"]
        if not uci_usi:
            continue
        try:
            game = Game.from_uci_usi(RandomPlayer(Color.BLUE),
                                     RandomPlayer(Color.RED),
                                     uci_usi)
        except:
            print("Problem with", filename)
            print(data["uci_usi"])
            print(data['result'])
            continue
        if data["result"] == "1-0":
            winner = Color.BLUE
        elif data["result"] == "0-1":
            winner = Color.RED
        else:
            print("DRAW")
            continue
        if winner == game.current_player:
            game.apply_action(None)
Beispiel #14
0
 def test_game_from_fen(self):
     game = Game.from_fen(RandomPlayer(Color.BLUE),
                          RandomPlayer(Color.RED),
                          "rnba1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1")
     self.assertEqual(game.current_player, Color.BLUE)
     self.assertEqual(game.round, 0)