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-26 13:36:52 +08:00
|
|
|
|
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-12-24 17:43:45 +08:00
|
|
|
def __init__(self, parent, action, state, action_num, prior, mcts, inverse=False):
|
2017-11-26 13:36:52 +08:00
|
|
|
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])
|
2017-12-23 02:48:53 +08:00
|
|
|
self.c_puct = c_puct
|
|
|
|
self.ucb = self.Q + self.c_puct * self.prior * math.sqrt(np.sum(self.N)) / (self.N + 1)
|
2017-12-08 23:41:31 +08:00
|
|
|
self.mask = None
|
2017-12-24 01:07:46 +08:00
|
|
|
self.elapse_time = 0
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts = mcts
|
2017-11-16 13:21:27 +08:00
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
def selection(self, simulator):
|
2017-12-24 01:07:46 +08:00
|
|
|
head = time.time()
|
2017-12-05 23:17:20 +08:00
|
|
|
self.valid_mask(simulator)
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts.valid_mask_time += time.time() - head
|
2017-11-21 22:19:52 +08:00
|
|
|
action = np.argmax(self.ucb)
|
|
|
|
if action in self.children.keys():
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts.state_selection_time += time.time() - head
|
2017-11-21 22:19:52 +08:00
|
|
|
return self.children[action].selection(simulator)
|
2017-11-16 13:21:27 +08:00
|
|
|
else:
|
2017-12-24 17:43:45 +08:00
|
|
|
self.children[action] = ActionNode(self, action, mcts=self.mcts)
|
|
|
|
self.mcts.state_selection_time += time.time() - head
|
2017-11-21 22:19:52 +08:00
|
|
|
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):
|
2017-12-23 15:58:06 +08:00
|
|
|
# let all invalid actions be illegal in mcts
|
|
|
|
if not hasattr(simulator, 'simulate_get_mask'):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if self.mask is None:
|
|
|
|
self.mask = simulator.simulate_get_mask(self.state, range(self.action_num))
|
|
|
|
self.ucb[self.mask] = -float("Inf")
|
2017-12-05 23:17:20 +08:00
|
|
|
|
2017-12-27 14:08:34 +08:00
|
|
|
# Code reserved for Thompson Sampling
|
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-12-24 17:43:45 +08:00
|
|
|
def __init__(self, parent, action, mcts):
|
2017-11-16 13:21:27 +08:00
|
|
|
self.parent = parent
|
|
|
|
self.action = action
|
|
|
|
self.children = {}
|
2017-11-21 22:19:52 +08:00
|
|
|
self.next_state = None
|
2017-12-27 14:08:34 +08:00
|
|
|
self.next_state_hashable = None
|
2017-11-26 13:36:52 +08:00
|
|
|
self.state_type = None
|
2017-11-21 22:19:52 +08:00
|
|
|
self.reward = 0
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts = mcts
|
2017-11-21 22:19:52 +08:00
|
|
|
|
|
|
|
def selection(self, simulator):
|
2017-12-24 17:43:45 +08:00
|
|
|
head = time.time()
|
2017-12-19 16:51:50 +08:00
|
|
|
self.next_state, self.reward = simulator.simulate_step_forward(self.parent.state, self.action)
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts.simulate_sf_time += time.time() - head
|
2017-12-27 11:43:04 +08:00
|
|
|
if self.next_state is None: # next_state is None means that self.parent.state is the terminate state
|
|
|
|
self.mcts.action_selection_time += time.time() - head
|
2017-12-27 21:11:40 +08:00
|
|
|
return self
|
2017-12-28 01:16:24 +08:00
|
|
|
head = time.time()
|
|
|
|
self.next_state_hashable = simulator.simulate_hashable_conversion(self.next_state)
|
|
|
|
self.mcts.hash_time += time.time() - head
|
2017-12-27 14:08:34 +08:00
|
|
|
if self.next_state_hashable in self.children.keys(): # next state has already visited before
|
2017-12-27 11:43:04 +08:00
|
|
|
self.mcts.action_selection_time += time.time() - head
|
2017-12-27 14:08:34 +08:00
|
|
|
return self.children[self.next_state_hashable].selection(simulator)
|
2017-12-27 11:43:04 +08:00
|
|
|
else: # next state is a new state never seen before
|
2017-12-24 17:43:45 +08:00
|
|
|
self.mcts.action_selection_time += time.time() - head
|
2017-12-27 21:11:40 +08:00
|
|
|
return self
|
2017-11-21 22:19:52 +08:00
|
|
|
|
2017-12-27 20:49:54 +08:00
|
|
|
def expansion(self, prior, action_num):
|
2017-11-21 22:19:52 +08:00
|
|
|
if self.next_state is not None:
|
2017-12-27 14:08:34 +08:00
|
|
|
# note that self.next_state was assigned already at the selection function
|
2017-12-27 20:49:54 +08:00
|
|
|
# self.next_state is None means MCTS selected a terminate node
|
2017-12-27 14:08:34 +08:00
|
|
|
self.children[self.next_state_hashable] = UCTNode(self, self.action, self.next_state, action_num, prior,
|
2017-12-24 17:43:45 +08:00
|
|
|
mcts=self.mcts, inverse=self.parent.inverse)
|
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):
|
2017-12-26 22:19:10 +08:00
|
|
|
def __init__(self, simulator, evaluator, start_state, action_num, method="UCT",
|
2017-12-24 01:07:46 +08:00
|
|
|
role="unknown", debug=False, inverse=False):
|
2017-11-16 17:05:54 +08:00
|
|
|
self.simulator = simulator
|
|
|
|
self.evaluator = evaluator
|
2017-12-24 01:07:46 +08:00
|
|
|
self.role = role
|
|
|
|
self.debug = debug
|
2017-12-26 22:19:10 +08:00
|
|
|
prior, _ = self.evaluator(start_state)
|
2017-11-21 22:19:52 +08:00
|
|
|
self.action_num = action_num
|
2017-12-03 19:16:21 +08:00
|
|
|
if method == "":
|
2017-12-26 22:19:10 +08:00
|
|
|
self.root = start_state
|
2017-11-16 17:05:54 +08:00
|
|
|
if method == "UCT":
|
2017-12-26 22:19:10 +08:00
|
|
|
self.root = UCTNode(None, None, start_state, action_num, prior, mcts=self, inverse=inverse)
|
2017-11-16 17:05:54 +08:00
|
|
|
if method == "TS":
|
2017-12-26 22:19:10 +08:00
|
|
|
self.root = TSNode(None, None, start_state, action_num, prior, inverse=inverse)
|
2017-11-26 13:36:52 +08:00
|
|
|
self.inverse = inverse
|
2017-12-20 16:43:42 +08:00
|
|
|
|
2017-12-24 17:43:45 +08:00
|
|
|
# time spend on each step
|
|
|
|
self.selection_time = 0
|
|
|
|
self.expansion_time = 0
|
|
|
|
self.backpropagation_time = 0
|
|
|
|
self.action_selection_time = 0
|
|
|
|
self.state_selection_time = 0
|
|
|
|
self.simulate_sf_time = 0
|
|
|
|
self.valid_mask_time = 0
|
2017-12-28 01:16:24 +08:00
|
|
|
self.hash_time = 0
|
2017-12-24 17:43:45 +08:00
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def search(self, max_step=None, max_time=None):
|
|
|
|
step = 0
|
|
|
|
start_time = time.time()
|
|
|
|
if max_step is None:
|
|
|
|
max_step = int("Inf")
|
|
|
|
if max_time is None:
|
|
|
|
max_time = float("Inf")
|
2017-11-16 17:05:54 +08:00
|
|
|
if max_step is None and max_time is None:
|
|
|
|
raise ValueError("Need a stop criteria!")
|
2017-12-08 23:41:31 +08:00
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
while step < max_step and time.time() - start_time < max_step:
|
2017-12-24 01:07:46 +08:00
|
|
|
sel_time, exp_time, back_time = self._expand()
|
2017-12-24 17:43:45 +08:00
|
|
|
self.selection_time += sel_time
|
|
|
|
self.expansion_time += exp_time
|
|
|
|
self.backpropagation_time += back_time
|
2017-12-20 16:43:42 +08:00
|
|
|
step += 1
|
2017-12-26 22:19:10 +08:00
|
|
|
if self.debug:
|
2017-12-28 01:16:24 +08:00
|
|
|
file = open("mcts_profiling.log", "a")
|
2017-12-24 01:07:46 +08:00
|
|
|
file.write("[" + str(self.role) + "]"
|
2017-12-28 01:16:24 +08:00
|
|
|
+ " sel " + '%.3f' % self.selection_time + " "
|
|
|
|
+ " sel_sta " + '%.3f' % self.state_selection_time + " "
|
|
|
|
+ " valid " + '%.3f' % self.valid_mask_time + " "
|
|
|
|
+ " sel_act " + '%.3f' % self.action_selection_time + " "
|
|
|
|
+ " hash " + '%.3f' % self.hash_time + " "
|
|
|
|
+ " step forward " + '%.3f' % self.simulate_sf_time + " "
|
|
|
|
+ " expansion " + '%.3f' % self.expansion_time + " "
|
|
|
|
+ " backprop " + '%.3f' % self.backpropagation_time + " "
|
2017-12-24 01:07:46 +08:00
|
|
|
+ "\n")
|
|
|
|
file.close()
|
2017-12-20 16:43:42 +08:00
|
|
|
|
|
|
|
def _expand(self):
|
2017-12-24 01:07:46 +08:00
|
|
|
t0 = time.time()
|
2017-12-27 21:11:40 +08:00
|
|
|
next_action = self.root.selection(self.simulator)
|
2017-12-24 01:07:46 +08:00
|
|
|
t1 = time.time()
|
2017-12-27 21:11:40 +08:00
|
|
|
prior, value = self.evaluator(next_action.next_state)
|
|
|
|
next_action.expansion(prior, self.action_num)
|
2017-12-24 01:07:46 +08:00
|
|
|
t2 = time.time()
|
2017-12-27 18:55:00 +08:00
|
|
|
if self.inverse:
|
2017-12-27 21:11:40 +08:00
|
|
|
next_action.backpropagation(-value + 0.)
|
2017-12-27 18:55:00 +08:00
|
|
|
else:
|
2017-12-27 21:11:40 +08:00
|
|
|
next_action.backpropagation(value + 0.)
|
2017-12-24 01:07:46 +08:00
|
|
|
t3 = time.time()
|
|
|
|
return t1 - t0, t2 - t1, t3 - t2
|
|
|
|
|
2017-11-21 22:19:52 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|