2017-11-16 13:21:27 +08:00
|
|
|
import numpy as np
|
|
|
|
import math
|
2017-11-16 17:05:54 +08:00
|
|
|
import time
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
c_puct = 5
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-16 17:05:54 +08:00
|
|
|
|
2017-11-26 13:36:52 +08:00
|
|
|
def list2tuple(list):
|
|
|
|
try:
|
|
|
|
return tuple(list2tuple(sub) for sub in list)
|
|
|
|
except TypeError:
|
|
|
|
return list
|
|
|
|
|
|
|
|
|
|
|
|
def tuple2list(tuple):
|
|
|
|
try:
|
|
|
|
return list(tuple2list(sub) for sub in tuple)
|
|
|
|
except TypeError:
|
|
|
|
return tuple
|
|
|
|
|
|
|
|
|
2017-11-16 17:05:54 +08:00
|
|
|
class MCTSNode(object):
|
2017-11-26 13:36:52 +08:00
|
|
|
def __init__(self, parent, action, state, action_num, prior, inverse=False):
|
2017-11-16 13:21:27 +08:00
|
|
|
self.parent = parent
|
|
|
|
self.action = action
|
|
|
|
self.children = {}
|
|
|
|
self.state = state
|
|
|
|
self.action_num = action_num
|
2017-12-05 23:17:20 +08:00
|
|
|
self.prior = np.array(prior).reshape(-1)
|
2017-11-26 13:36:52 +08:00
|
|
|
self.inverse = inverse
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
def selection(self, simulator):
|
2017-11-16 17:05:54 +08:00
|
|
|
raise NotImplementedError("Need to implement function selection")
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
def backpropagation(self, action):
|
2017-11-16 17:05:54 +08:00
|
|
|
raise NotImplementedError("Need to implement function backpropagation")
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-12-05 23:17:20 +08:00
|
|
|
def valid_mask(self, simulator):
|
|
|
|
pass
|
2017-11-16 13:21:27 +08:00
|
|
|
|
|
|
|
class UCTNode(MCTSNode):
|
2017-11-26 13:36:52 +08:00
|
|
|
def __init__(self, parent, action, state, action_num, prior, inverse=False):
|
|
|
|
super(UCTNode, self).__init__(parent, action, state, action_num, prior, inverse)
|
2017-11-16 13:21:27 +08:00
|
|
|
self.Q = np.zeros([action_num])
|
|
|
|
self.W = np.zeros([action_num])
|
|
|
|
self.N = np.zeros([action_num])
|
|
|
|
self.ucb = self.Q + c_puct * self.prior * math.sqrt(np.sum(self.N)) / (self.N + 1)
|
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
def selection(self, simulator):
|
2017-12-05 23:17:20 +08:00
|
|
|
self.valid_mask(simulator)
|
2017-11-21 22:19:52 +08:00
|
|
|
action = np.argmax(self.ucb)
|
|
|
|
if action in self.children.keys():
|
|
|
|
return self.children[action].selection(simulator)
|
2017-11-16 13:21:27 +08:00
|
|
|
else:
|
2017-11-21 22:19:52 +08:00
|
|
|
self.children[action] = ActionNode(self, action)
|
|
|
|
return self.children[action].selection(simulator)
|
|
|
|
|
|
|
|
def backpropagation(self, action):
|
2017-11-21 22:52:17 +08:00
|
|
|
action = int(action)
|
2017-11-21 22:19:52 +08:00
|
|
|
self.N[action] += 1
|
|
|
|
self.W[action] += self.children[action].reward
|
|
|
|
for i in range(self.action_num):
|
|
|
|
if self.N[i] != 0:
|
|
|
|
self.Q[i] = (self.W[i] + 0.) / self.N[i]
|
|
|
|
self.ucb = self.Q + c_puct * self.prior * math.sqrt(np.sum(self.N)) / (self.N + 1.)
|
|
|
|
if self.parent is not None:
|
2017-11-26 13:36:52 +08:00
|
|
|
if self.inverse:
|
|
|
|
self.parent.backpropagation(-self.children[action].reward)
|
|
|
|
else:
|
|
|
|
self.parent.backpropagation(self.children[action].reward)
|
2017-11-16 17:05:54 +08:00
|
|
|
|
2017-12-05 23:17:20 +08:00
|
|
|
def valid_mask(self, simulator):
|
|
|
|
for act in range(self.action_num - 1):
|
|
|
|
if not simulator.is_valid(self.state, act):
|
|
|
|
self.ucb[act] = -float("Inf")
|
|
|
|
|
2017-11-16 17:05:54 +08:00
|
|
|
|
2017-11-16 13:21:27 +08:00
|
|
|
class TSNode(MCTSNode):
|
2017-11-26 13:36:52 +08:00
|
|
|
def __init__(self, parent, action, state, action_num, prior, method="Gaussian", inverse=False):
|
|
|
|
super(TSNode, self).__init__(parent, action, state, action_num, prior, inverse)
|
2017-11-16 13:21:27 +08:00
|
|
|
if method == "Beta":
|
|
|
|
self.alpha = np.ones([action_num])
|
|
|
|
self.beta = np.ones([action_num])
|
|
|
|
if method == "Gaussian":
|
|
|
|
self.mu = np.zeros([action_num])
|
|
|
|
self.sigma = np.zeros([action_num])
|
|
|
|
|
2017-11-16 17:05:54 +08:00
|
|
|
|
2017-12-05 23:17:20 +08:00
|
|
|
class ActionNode(object):
|
2017-11-16 13:21:27 +08:00
|
|
|
def __init__(self, parent, action):
|
|
|
|
self.parent = parent
|
|
|
|
self.action = action
|
|
|
|
self.children = {}
|
2017-11-21 22:19:52 +08:00
|
|
|
self.next_state = None
|
2017-11-26 13:36:52 +08:00
|
|
|
self.origin_state = None
|
|
|
|
self.state_type = None
|
2017-11-21 22:19:52 +08:00
|
|
|
self.reward = 0
|
|
|
|
|
2017-11-26 13:36:52 +08:00
|
|
|
def type_conversion_to_tuple(self):
|
|
|
|
if type(self.next_state) is np.ndarray:
|
|
|
|
self.next_state = self.next_state.tolist()
|
|
|
|
if type(self.next_state) is list:
|
|
|
|
self.next_state = list2tuple(self.next_state)
|
|
|
|
|
|
|
|
def type_conversion_to_origin(self):
|
|
|
|
if self.state_type is np.ndarray:
|
|
|
|
self.next_state = np.array(self.next_state)
|
|
|
|
if self.state_type is list:
|
|
|
|
self.next_state = tuple2list(self.next_state)
|
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
def selection(self, simulator):
|
|
|
|
self.next_state, self.reward = simulator.step_forward(self.parent.state, self.action)
|
2017-11-26 13:36:52 +08:00
|
|
|
self.origin_state = self.next_state
|
|
|
|
self.state_type = type(self.next_state)
|
|
|
|
self.type_conversion_to_tuple()
|
2017-11-21 22:19:52 +08:00
|
|
|
if self.next_state is not None:
|
|
|
|
if self.next_state in self.children.keys():
|
|
|
|
return self.children[self.next_state].selection(simulator)
|
|
|
|
else:
|
|
|
|
return self.parent, self.action
|
|
|
|
else:
|
|
|
|
return self.parent, self.action
|
|
|
|
|
2017-11-26 13:36:52 +08:00
|
|
|
def expansion(self, evaluator, action_num):
|
2017-11-21 22:19:52 +08:00
|
|
|
if self.next_state is not None:
|
2017-11-26 13:36:52 +08:00
|
|
|
prior, value = evaluator(self.next_state)
|
|
|
|
self.children[self.next_state] = UCTNode(self, self.action, self.origin_state, action_num, prior,
|
|
|
|
self.parent.inverse)
|
|
|
|
return value
|
2017-11-21 22:19:52 +08:00
|
|
|
else:
|
2017-12-05 23:17:20 +08:00
|
|
|
return 0.
|
2017-11-21 22:19:52 +08:00
|
|
|
|
|
|
|
def backpropagation(self, value):
|
|
|
|
self.reward += value
|
|
|
|
self.parent.backpropagation(self.action)
|
2017-11-16 17:05:54 +08:00
|
|
|
|
|
|
|
|
2017-12-05 23:17:20 +08:00
|
|
|
class MCTS(object):
|
|
|
|
def __init__(self, simulator, evaluator, root, action_num, method="UCT", inverse=False, max_step=None,
|
2017-11-26 13:36:52 +08:00
|
|
|
max_time=None):
|
2017-11-16 17:05:54 +08:00
|
|
|
self.simulator = simulator
|
|
|
|
self.evaluator = evaluator
|
2017-12-05 23:17:20 +08:00
|
|
|
prior, _ = self.evaluator(root)
|
2017-11-21 22:19:52 +08:00
|
|
|
self.action_num = action_num
|
2017-12-03 19:16:21 +08:00
|
|
|
if method == "":
|
|
|
|
self.root = root
|
2017-11-16 17:05:54 +08:00
|
|
|
if method == "UCT":
|
2017-11-26 13:36:52 +08:00
|
|
|
self.root = UCTNode(None, None, root, action_num, prior, inverse)
|
2017-11-16 17:05:54 +08:00
|
|
|
if method == "TS":
|
2017-11-26 13:36:52 +08:00
|
|
|
self.root = TSNode(None, None, root, action_num, prior, inverse=inverse)
|
|
|
|
self.inverse = inverse
|
2017-11-16 17:05:54 +08:00
|
|
|
if max_step is not None:
|
|
|
|
self.step = 0
|
|
|
|
self.max_step = max_step
|
2017-12-03 19:16:21 +08:00
|
|
|
# TODO: Optimize the stop criteria
|
|
|
|
# else:
|
|
|
|
# self.max_step = 0
|
2017-11-16 17:05:54 +08:00
|
|
|
if max_time is not None:
|
|
|
|
self.start_time = time.time()
|
|
|
|
self.max_time = max_time
|
|
|
|
if max_step is None and max_time is None:
|
|
|
|
raise ValueError("Need a stop criteria!")
|
|
|
|
while (max_step is not None and self.step < self.max_step or max_step is None) \
|
|
|
|
and (max_time is not None and time.time() - self.start_time < self.max_time or max_time is None):
|
|
|
|
self.expand()
|
2017-11-17 15:09:07 +08:00
|
|
|
if max_step is not None:
|
|
|
|
self.step += 1
|
2017-11-16 17:05:54 +08:00
|
|
|
|
|
|
|
def expand(self):
|
2017-11-21 22:19:52 +08:00
|
|
|
node, new_action = self.root.selection(self.simulator)
|
2017-11-26 13:36:52 +08:00
|
|
|
value = node.children[new_action].expansion(self.evaluator, self.action_num)
|
|
|
|
node.children[new_action].backpropagation(value + 0.)
|
2017-11-16 17:05:54 +08:00
|
|
|
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|