alphazero_separate_net.py
def search(self, n_mcts, c, env, mcts_env):
        """
        Perform the MCTS search from the root
        """
        if self.root is None:
            self.root = State(
                self.root_index, r=0.0, terminal=False,
                parent_action=None, na=self.na,
                bootstrap_last_state_value=self.bootstrap_last_state_value,
                model=self.model)  # initialize new root
        else:
            self.root.parent_action = None # continue from current root
        if self.root.terminal:
            raise(ValueError("Can't do tree search from a terminal state"))

        is_atari = is_atari_game(env)
        if is_atari:
            snapshot = copy_atari_state(env) # for Atari: snapshot the root at the beginning
        
        for i in range(n_mcts):     
            state = self.root  # reset to root for new trace
            if not is_atari:
                mcts_env = copy.deepcopy(env) # copy original Env to rollout from
            else:
                restore_atari_state(mcts_env, snapshot)
            
            while not state.terminal: 
                action = state.select(c=c)
                s1, r, t, _ = mcts_env.step(action.index)
                if hasattr(action, 'child_state'):
                    state = action.child_state   # select
                    continue
                else:
                    state = action.add_child_state(s1, r, t, self.model) # expand
                    break

            # Back-up 
            r = state.V
            while state.parent_action is not None:   # loop back-up until root is reached
                r = state.r + self.gamma * r
                action = state.parent_action
                action.update(r)
                state = action.parent_state
                state.update()

        Perform the MCTS search from the root
        
Perform the MCTS search from the root
Perform the MCTS search from the root
def search(self, n_mcts, c, env, mcts_env):
        if self.root is None:
            self.root = State(
                self.root_index, r=0.0, terminal=False,
                parent_action=None, na=self.na,
                bootstrap_last_state_value=self.bootstrap_last_state_value,
                model=self.model)  
        else:
            self.root.parent_action = None 
        if self.root.terminal:
            raise(ValueError("Can't do tree search from a terminal state"))
        is_atari = is_atari_game(env)
        if is_atari:
            snapshot = copy_atari_state(env) 
        for i in range(n_mcts):     
            state = self.root  
            if not is_atari:
                mcts_env = copy.deepcopy(env) 
            else:
                restore_atari_state(mcts_env, snapshot)
            while not state.terminal: 
                action = state.select(c=c)
                s1, r, t, _ = mcts_env.step(action.index)
                if hasattr(action, 'child_state'):
                    state = action.child_state   
                    continue
                else:
                    state = action.add_child_state(s1, r, t, self.model) 
                    break
            r = state.V
            while state.parent_action is not None:   
                r = state.r + self.gamma * r
                action = state.parent_action
                action.update(r)
                state = action.parent_state
                state.update()
def search ( self , n_mcts , c , env , mcts_env ) : if self . root is None : self . root = State ( self . root_index , r = 0.0 , terminal = False , parent_action = None , na = self . na , bootstrap_last_state_value = self . bootstrap_last_state_value , model = self . model ) else : self . root . parent_action = None if self . root . terminal : raise ( ValueError ( "Can't do tree search from a terminal state" ) ) is_atari = is_atari_game ( env ) if is_atari : snapshot = copy_atari_state ( env ) for i in range ( n_mcts ) : state = self . root if not is_atari : mcts_env = copy . deepcopy ( env ) else : restore_atari_state ( mcts_env , snapshot ) while not state . terminal : action = state . select ( c = c ) s1 , r , t , _ = mcts_env . step ( action . index ) if hasattr ( action , 'child_state' ) : state = action . child_state continue else : state = action . add_child_state ( s1 , r , t , self . model ) break r = state . V while state . parent_action is not None : r = state . r + self . gamma * r action = state . parent_action action . update ( r ) state = action . parent_state state . update ( )
Perform the MCTS search from the root
Perform the MCTS search from the root
"""
        Perform the MCTS search from the root
        """ # initialize new root # continue from current root # for Atari: snapshot the root at the beginning # reset to root for new trace # copy original Env to rollout from # select # expand # Back-up  # loop back-up until root is reached
self
n_mcts
c
env
mcts_env