class MCTS::Node
Attributes
children[R]
game_state[R]
move[R]
parent[R]
visits[R]
wins[R]
Public Class Methods
new(game_state, move, parent)
click to toggle source
# File lib/mcts/node.rb, line 5 def initialize(game_state, move, parent) @parent = parent @game_state = game_state @move = move @wins = 0.0 @visits = 0 @children = [] @untried_moves = game_state.all_valid_moves @leaf = game_state.finished? || @untried_moves.empty? end
Public Instance Methods
backpropagate(won)
click to toggle source
# File lib/mcts/node.rb, line 56 def backpropagate(won) node = self node.update_won won until node.root? do won = !won # switching players perspective node = node.parent node.update_won(won) end end
expand()
click to toggle source
maybe get a maximum depth or soemthing in
# File lib/mcts/node.rb, line 37 def expand move = @untried_moves.pop create_child(move) end
leaf?()
click to toggle source
# File lib/mcts/node.rb, line 28 def leaf? @leaf end
lost()
click to toggle source
# File lib/mcts/node.rb, line 52 def lost @visits += 1 end
rollout()
click to toggle source
# File lib/mcts/node.rb, line 42 def rollout playout = Playout.new(@game_state) playout.play end
root?()
click to toggle source
# File lib/mcts/node.rb, line 24 def root? false end
uct_select_child()
click to toggle source
# File lib/mcts/node.rb, line 32 def uct_select_child children.max_by &:uct_value end
uct_value()
click to toggle source
# File lib/mcts/node.rb, line 16 def uct_value win_percentage + UCT_BIAS_FACTOR * Math.sqrt(Math.log(parent.visits)/@visits) end
untried_moves?()
click to toggle source
# File lib/mcts/node.rb, line 66 def untried_moves? !@untried_moves.empty? end
update_won(won)
click to toggle source
# File lib/mcts/node.rb, line 70 def update_won(won) if won self.won else self.lost end end
win_percentage()
click to toggle source
# File lib/mcts/node.rb, line 20 def win_percentage @wins/@visits end
won()
click to toggle source
# File lib/mcts/node.rb, line 47 def won @visits += 1 @wins += 1 end
Private Instance Methods
create_child(move)
click to toggle source
# File lib/mcts/node.rb, line 80 def create_child(move) game_state = @game_state.dup game_state.set_move(move) child = Node.new game_state, move, self @children << child child end