class MCTS::Root

Public Class Methods

new(game_state) click to toggle source
Calls superclass method MCTS::Node::new
# File lib/mcts/root.rb, line 3
def initialize(game_state)
  super game_state, nil, nil
end

Public Instance Methods

best_child() click to toggle source
# File lib/mcts/root.rb, line 11
def best_child
  children.max_by &:win_percentage
end
best_move() click to toggle source
# File lib/mcts/root.rb, line 15
def best_move
  best_child.move
end
explore_tree() click to toggle source
# File lib/mcts/root.rb, line 19
def explore_tree
  selected_node = select
  playout_node =  if selected_node.leaf?
                    selected_node
                  else
                    selected_node.expand
                  end
  won = playout_node.rollout
  playout_node.backpropagate(won)
end
root?() click to toggle source
# File lib/mcts/root.rb, line 7
def root?
  true
end
update_won(won) click to toggle source
# File lib/mcts/root.rb, line 30
def update_won(won)
  # logic reversed as the node accumulates its children and has no move
  # of its own
  if won
    self.lost
  else
    self.won
  end
end

Private Instance Methods

select() click to toggle source
# File lib/mcts/root.rb, line 41
def select
  node = self
  until node.untried_moves? || node.leaf? do
    node = node.uct_select_child
  end
  node
end