示例#1
0
文件: Train.py 项目: lygztq/gomoku
    def networkValidation(self):
        board = deepcopy(self.board)
        TrainServer.resetPlayer(self.player, Board.randomPlayer())
        self.player.self_play = False
        oppo_player = PureMCTSPlayer(
            Board.opponent(self.player.color),
            compute_budget=self.validation_player_compute_budget)

        TrainServer.log_output("[validation...]")

        num_validation_game = self.config["num_validation_game"]
        num_win_game = 0
        for _ in range(num_validation_game):
            TrainServer.resetPlayer(self.player, Board.randomPlayer())
            TrainServer.resetPlayer(oppo_player,
                                    Board.opponent(self.player.color))

            if self.player.color == Board.kPlayerBlack:
                player1 = self.player
                player2 = oppo_player
            else:
                player1 = oppo_player
                player2 = self.player

            val_game_saver = GameServer(board,
                                        mode=GameServer.kNormalPlayGame,
                                        player1=player1,
                                        player2=player2,
                                        silent=True)

            winner = val_game_saver.startGame()
            if winner == self.player.color:
                num_win_game += 1
        self.player.self_play = True
        return num_win_game * 1.0 / num_validation_game
示例#2
0
文件: 3000.py 项目: fantianwen/gomoku
def run():
    board = Board(width=9, height=9)
    player1 = PureMCTSPlayer(Board.kPlayerBlack, name="AI 1",weight_c=5, compute_budget=3000)
    # player1 = HumanPlayer(Board.kPlayerBlack, name="player 1")

    player2 = PureMCTSPlayer(Board.kPlayerWhite, name="AI 2", weight_c=5, compute_budget=10000)
    # player2 = HumanPlayer(Board.kPlayerWhite, name="player 2")

    server = GameServer(board, GameServer.kNormalPlayGame, player1, player2)
    server.startGame()
示例#3
0
文件: Train.py 项目: lygztq/gomoku
    def __init__(self, network, config, reuse=False):
        self.config = config
        self.board = Board(width=config["board_width"],
                           height=config["board_height"],
                           numberToWin=config["number_to_win"])
        self.entropy_upper_bound = ln(config["board_width"] *
                                      config["board_height"])

        # check the network is a Neural network.
        if not isinstance(network, NeuralNetwork):
            raise TypeError("The type of given network is invaild.")

        # check size
        if (network.width != config["board_width"]
                or network.height != config["board_height"]):
            raise ValueError("The size of given neural network is not equal to"
                             "the size of board.")
        self.network = network
        self.reuse = reuse
        if reuse:
            print("Using last time network parameters.")
            self.network.restore(config["model_path"])

        self.player = DNNMCTSPlayer(
            Board.kPlayerBlack,
            self.network,
            weight_c=config["MCTS_exploration_weight"],
            compute_budget=config["MCTS_compute_budget"],
            exploration_level=config["player_exploration_level"],
            self_play=True)

        self.game_server = GameServer(self.board,
                                      GameServer.kSelfPlayGame,
                                      self.player,
                                      silent=True)
        self.state_batch_buffer = None
        self.policy_batch_buffer = None
        self.winner_vec_buffer = None
        self.validation_player_compute_budget = config[
            "validation_player_compute_budget"]
示例#4
0
import sys
sys.path.extend(['./src'])

from pygomoku.Board import Board
from pygomoku.Player import HumanPlayer, PureMCTSPlayer
from pygomoku.GameServer import GameServer

board = Board(width=9, height=9)
player1 = PureMCTSPlayer(Board.kPlayerBlack,
                         name="AI 1",
                         weight_c=5,
                         compute_budget=20000)
# player1 = HumanPlayer(Board.kPlayerBlack, name="player 1")

# player2 = PureMCTSPlayer(Board.kPlayerWhite, name="AI 2", weight_c=5, compute_budget=5000)
player2 = HumanPlayer(Board.kPlayerWhite, name="player 2")

server = GameServer(board, GameServer.kNormalPlayGame, player1, player2)
server.startGame()
示例#5
0
 def setUp(self):
     self.board = Board()
示例#6
0
class TestGomokuBoard(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_moveToLocation(self):
        error_report = "Got error in moveToLocation"
        self.assertEqual(self.board.moveToLocation(15), [1, 0], error_report)
        self.assertEqual(self.board.moveToLocation(0), [0, 0], error_report)
        self.assertEqual(self.board.moveToLocation(17), [1, 2], error_report)

    def test_initBoard(self):
        error_report = "Got error in initBoard"
        self.board.initBoard(Board.kPlayerWhite)
        self.assertEqual(self.board.current_player,
                         Board.kPlayerWhite, error_report + ' -> initial player')
        self.assertEqual(self.board.last_move, None,
                         error_report + " -> last move")
        self.assertEqual(self.board.width, 15, error_report + " -> width")
        self.assertEqual(self.board.height, 15, error_report + " -> height")

    def test_locationToMove(self):
        error_report = "Got error in locationToMove"
        self.assertEqual(self.board.locationToMove([1, 0]), 15, error_report)
        self.assertEqual(self.board.locationToMove([0, 0]), 0, error_report)
        self.assertEqual(self.board.locationToMove([1, 2]), 17, error_report)

    def test_play_and_undo(self):
        self.assertFalse(self.board.undo(),
                         "Got error in undo when moved is empty")
        self.board.play(17)
        self.assertEqual(self.board.last_move, 17, "Got error in move")
        self.assertFalse(self.board.play(
            17), "Got error in put stone on a stone")
        self.board.undo()
        self.assertEqual(self.board.last_move, None, "Got error in undo")

    def test_gameEnd(self):
        for i in range(4):
            self.board.play(i)
            self.board.play(i+self.board.width)
        self.board.play(4)
        self.assertListEqual(list(self.board.gameEnd()), [
                             True, Board.kPlayerBlack], "Got error in gameEnd")
        
        self.board.initBoard()

        for i in range(4):
            self.board.play(i+7*self.board.width)
            self.board.play(i+8*self.board.width)

        for i in range(2):
            self.board.play(i)
            self.board.play(i+3*self.board.width)
        self.board.play(2)
        self.board.play(3)
        self.board.play(4)
        dubug_result = self.board.gameEnd()
        self.assertEqual(list(self.board.gameEnd()), [False, None], "Got error in gameEnd")
示例#7
0
 def setUp(self):
     self.player = Player.HumanPlayer(Board.kPlayerBlack)
     self.board = Board()