Class: MCTS::Node
- Inherits:
-
Object
- Object
- MCTS::Node
- Defined in:
- lib/mcts/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#game_state ⇒ Object
readonly
Returns the value of attribute game_state.
-
#move ⇒ Object
readonly
Returns the value of attribute move.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#visits ⇒ Object
readonly
Returns the value of attribute visits.
-
#wins ⇒ Object
readonly
Returns the value of attribute wins.
Instance Method Summary collapse
- #backpropagate(won) ⇒ Object
-
#expand ⇒ Object
maybe get a maximum depth or soemthing in.
-
#initialize(game_state, move, parent) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #lost ⇒ Object
- #rollout ⇒ Object
- #root? ⇒ Boolean
- #uct_select_child ⇒ Object
- #uct_value ⇒ Object
- #untried_moves? ⇒ Boolean
- #update_won(won) ⇒ Object
- #win_percentage ⇒ Object
- #won ⇒ Object
Constructor Details
#initialize(game_state, move, parent) ⇒ Node
Returns a new instance of Node.
5 6 7 8 9 10 11 12 13 14 |
# 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 |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def children @children end |
#game_state ⇒ Object (readonly)
Returns the value of attribute game_state.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def game_state @game_state end |
#move ⇒ Object (readonly)
Returns the value of attribute move.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def move @move end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def parent @parent end |
#visits ⇒ Object (readonly)
Returns the value of attribute visits.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def visits @visits end |
#wins ⇒ Object (readonly)
Returns the value of attribute wins.
3 4 5 |
# File 'lib/mcts/node.rb', line 3 def wins @wins end |
Instance Method Details
#backpropagate(won) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# 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 ⇒ Object
maybe get a maximum depth or soemthing in
37 38 39 40 |
# File 'lib/mcts/node.rb', line 37 def move = @untried_moves.pop create_child(move) end |
#leaf? ⇒ Boolean
28 29 30 |
# File 'lib/mcts/node.rb', line 28 def leaf? @leaf end |
#lost ⇒ Object
52 53 54 |
# File 'lib/mcts/node.rb', line 52 def lost @visits += 1 end |
#rollout ⇒ Object
42 43 44 45 |
# File 'lib/mcts/node.rb', line 42 def rollout playout = Playout.new(@game_state) playout.play end |
#root? ⇒ Boolean
24 25 26 |
# File 'lib/mcts/node.rb', line 24 def root? false end |
#uct_select_child ⇒ Object
32 33 34 |
# File 'lib/mcts/node.rb', line 32 def uct_select_child children.max_by &:uct_value end |
#uct_value ⇒ Object
16 17 18 |
# 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? ⇒ Boolean
66 67 68 |
# File 'lib/mcts/node.rb', line 66 def untried_moves? !@untried_moves.empty? end |
#update_won(won) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/mcts/node.rb', line 70 def update_won(won) if won self.won else self.lost end end |
#win_percentage ⇒ Object
20 21 22 |
# File 'lib/mcts/node.rb', line 20 def win_percentage @wins/@visits end |
#won ⇒ Object
47 48 49 50 |
# File 'lib/mcts/node.rb', line 47 def won @visits += 1 @wins += 1 end |