Modification and doc for unit test

This commit is contained in:
JialianLee 2017-12-29 13:45:53 +08:00
parent 01f39f40d3
commit 5849776c9a
5 changed files with 33 additions and 11 deletions

View File

@ -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

View File

@ -29,7 +29,7 @@ class ZOTree:
length = len(seq) length = len(seq)
if length != self.depth: if length != self.depth:
raise ValueError("The game is not terminated!") raise ValueError("The game is not terminated!")
result = np.sum(seq) result = np.sum(seq)
if result > self.size: if result > self.size:
winner = 1 winner = 1
elif result < self.size: elif result < self.size:

View File

@ -4,13 +4,15 @@ import ZOGame
import Evaluator import Evaluator
from mcts import MCTS from mcts import MCTS
temp = 1
class Agent: class Agent:
def __init__(self, size, color): def __init__(self, size, color, searching_step, temp):
self.size = size self.size = size
self.color = color self.color = color
self.searching_step = searching_step
self.temp = temp
self.simulator = ZOGame.ZOTree(self.size) self.simulator = ZOGame.ZOTree(self.size)
self.evaluator = Evaluator.rollout_policy(self.simulator, 2) self.evaluator = Evaluator.rollout_policy(self.simulator, 2)
@ -18,10 +20,9 @@ class Agent:
if len(seq) >= 2 * self.size: if len(seq) >= 2 * self.size:
raise ValueError("Game is terminated.") raise ValueError("Game is terminated.")
mcts = MCTS(self.simulator, self.evaluator, [seq, self.color], 2, inverse=True) 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 = mcts.root.N
N = np.power(N, 1.0 / temp) N = np.power(N, 1.0 / self.temp)
prob = N / np.sum(N) prob = N / np.sum(N)
print("prob: {}".format(prob))
action = int(np.random.binomial(1, prob[1])) action = int(np.random.binomial(1, prob[1]))
return action return action

View File

@ -3,17 +3,19 @@ import agent
if __name__ == '__main__': if __name__ == '__main__':
size = 10
seaching_step = 100
temp = 1
print("Our game has 2 players.") 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("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.") print("Both player choose 1 or 0 for an action.")
size = 2
print("This game has {} iterations".format(size)) 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 more 1 that 0, player 1 wins.")
print("If the final sequence has less 1 that 0, player 2 wins.") print("If the final sequence has less 1 that 0, player 2 wins.")
print("Otherwise, both players get 0.\n") print("Otherwise, both players get 0.\n")
game = ZOGame.ZOTree(size) game = ZOGame.ZOTree(size)
player1 = agent.Agent(size, 1) player1 = agent.Agent(size, 1, seaching_step, temp)
player2 = agent.Agent(size, -1) player2 = agent.Agent(size, -1, seaching_step, temp)
seq = [] seq = []
print("Sequence is {}\n".format(seq)) print("Sequence is {}\n".format(seq))

View File

@ -162,8 +162,6 @@ class MCTS(object):
self.expansion_time += exp_time self.expansion_time += exp_time
self.backpropagation_time += back_time self.backpropagation_time += back_time
step += 1 step += 1
print("Q = {}".format(self.root.Q))
print("N = {}".format(self.root.N))
if self.debug: if self.debug:
file = open("mcts_profiling.log", "a") file = open("mcts_profiling.log", "a")
file.write("[" + str(self.role) + "]" file.write("[" + str(self.role) + "]"