diff --git a/tianshou/core/mcts/unit_test/README.md b/tianshou/core/mcts/unit_test/README.md new file mode 100644 index 0000000..b7d0214 --- /dev/null +++ b/tianshou/core/mcts/unit_test/README.md @@ -0,0 +1,21 @@ +# Unit Test + +This is a two-player zero-sum perfect information extensive game. Player 1 and player 2 iteratively choose actions. At every iteration, player 1 players first and player 2 follows. Both players have choices 0 or 1. + +The number of iterations is given as a fixed number. After one game finished, the game counts the number of 0s and 1s that are choosen. If the number of 1 is more than that of 0, player 1 gets 1 and player 2 gets -1. If the number of 1 is less than that of 0, player 1 gets -1 and player 2 gets 1. Otherwise, they both get 0. + +## Files + ++ game.py: run this file to play the game. ++ agent.py: a class for players. MCTS is used here. ++ ZOgame.py: the game environment. ++ mcts.py: MCTS method. ++ Evaluator: evaluator for MCTS. Rollout policy is also here. + +## Parameters + +Three paramters are given in game.py. + ++ size: the number of iterations ++ searching_step: the number of searching times of MCTS for one step ++ temp: the temporature paramter used to tradeoff exploitation and exploration diff --git a/tianshou/core/mcts/unit_test/ZOGame.py b/tianshou/core/mcts/unit_test/ZOGame.py index 0b3d771..a4ea5e9 100644 --- a/tianshou/core/mcts/unit_test/ZOGame.py +++ b/tianshou/core/mcts/unit_test/ZOGame.py @@ -29,7 +29,7 @@ class ZOTree: length = len(seq) if length != self.depth: raise ValueError("The game is not terminated!") - result = np.sum(seq) + result = np.sum(seq) if result > self.size: winner = 1 elif result < self.size: diff --git a/tianshou/core/mcts/unit_test/agent.py b/tianshou/core/mcts/unit_test/agent.py index 6dd34aa..f2946ce 100644 --- a/tianshou/core/mcts/unit_test/agent.py +++ b/tianshou/core/mcts/unit_test/agent.py @@ -4,13 +4,15 @@ import ZOGame import Evaluator from mcts import MCTS -temp = 1 + class Agent: - def __init__(self, size, color): + def __init__(self, size, color, searching_step, temp): self.size = size self.color = color + self.searching_step = searching_step + self.temp = temp self.simulator = ZOGame.ZOTree(self.size) self.evaluator = Evaluator.rollout_policy(self.simulator, 2) @@ -18,10 +20,9 @@ class Agent: if len(seq) >= 2 * self.size: raise ValueError("Game is terminated.") mcts = MCTS(self.simulator, self.evaluator, [seq, self.color], 2, inverse=True) - mcts.search(max_step=50) + mcts.search(max_step=self.searching_step) N = mcts.root.N - N = np.power(N, 1.0 / temp) + N = np.power(N, 1.0 / self.temp) prob = N / np.sum(N) - print("prob: {}".format(prob)) action = int(np.random.binomial(1, prob[1])) return action diff --git a/tianshou/core/mcts/unit_test/game.py b/tianshou/core/mcts/unit_test/game.py index 6fb504b..92fcea8 100644 --- a/tianshou/core/mcts/unit_test/game.py +++ b/tianshou/core/mcts/unit_test/game.py @@ -3,17 +3,19 @@ import agent if __name__ == '__main__': + size = 10 + seaching_step = 100 + temp = 1 print("Our game has 2 players.") print("Player 1 has color 1 and plays first. Player 2 has color -1 and plays following player 1.") print("Both player choose 1 or 0 for an action.") - size = 2 print("This game has {} iterations".format(size)) print("If the final sequence has more 1 that 0, player 1 wins.") print("If the final sequence has less 1 that 0, player 2 wins.") print("Otherwise, both players get 0.\n") game = ZOGame.ZOTree(size) - player1 = agent.Agent(size, 1) - player2 = agent.Agent(size, -1) + player1 = agent.Agent(size, 1, seaching_step, temp) + player2 = agent.Agent(size, -1, seaching_step, temp) seq = [] print("Sequence is {}\n".format(seq)) diff --git a/tianshou/core/mcts/unit_test/mcts.py b/tianshou/core/mcts/unit_test/mcts.py index 49c9faf..ab566f0 100644 --- a/tianshou/core/mcts/unit_test/mcts.py +++ b/tianshou/core/mcts/unit_test/mcts.py @@ -162,8 +162,6 @@ class MCTS(object): self.expansion_time += exp_time self.backpropagation_time += back_time step += 1 - print("Q = {}".format(self.root.Q)) - print("N = {}".format(self.root.N)) if self.debug: file = open("mcts_profiling.log", "a") file.write("[" + str(self.role) + "]"