更快的python treesearch实现

时间:2018-10-10 09:41:01

标签: python python-3.x numba monte-carlo-tree-search

我在Python中有一个treesearch实现,这只是减慢使用速度的一种方式。我怎样才能更快地运行它?我已经读过numba,但是我无法理解它的工作原理,它可以支持什么以及不可以。有人使用numba来加快树搜索的速度吗? 预先谢谢您!

    def search(self, canonicalBoard, game_copy, rootplayer_index, max_in_prediction=False, callback_counter=0):
    start_time = time.time()
    counter = 0

    s = self.game.stringRepresentation(canonicalBoard)

    if s not in self.Es:
        self.Es[s] = self.game.getGameEnded(game_copy, rootplayer_index) # 1 -> probably player index  # getGameEnded gives reward 
    if self.Es[s]!=0:
        # terminal  
        return self.Es[s]

    if callback_counter == 0 and s not in self.Ps:
        raw_pol = self.policy_net.predict(canonicalBoard)
        v = self.value_net.predict(canonicalBoard)[0][0]
        self.Ps[s] = raw_pol[0]
        valids = game_copy.roundo.get_legal_moves(game_copy.roundo.players[rootplayer_index])
        index_valids = [card.index for card in valids]
        mask = np.array(index_valids)
        mask = np.array([1 if y in mask else 0 for y in range(36)], dtype=bool)
        self.Ps[s][~mask] = 0
        self.Ps[s] /= sum(self.Ps[s])
        valids = [1 if y in index_valids else 0 for y in range(36)] # check it out
        self.Vs[s] = valids
        self.Ns[s] = 0
        return v # !!

    if callback_counter > 0:
        v = self.value_net.predict(canonicalBoard)[0][0]
        return v



    valids = self.Vs[s]
    cur_best = -float('inf')
    best_act = -1

  ....

    else:
        self.Qsa[(s,a)] = v
        self.Nsa[(s,a)] = 1

    self.Ns[s] += 1
    return v

0 个答案:

没有答案