Class: MCTS::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/mcts/node.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/mcts/node.rb', line 3

def children
  @children
end

#game_stateObject (readonly)

Returns the value of attribute game_state.



3
4
5
# File 'lib/mcts/node.rb', line 3

def game_state
  @game_state
end

#moveObject (readonly)

Returns the value of attribute move.



3
4
5
# File 'lib/mcts/node.rb', line 3

def move
  @move
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/mcts/node.rb', line 3

def parent
  @parent
end

#visitsObject (readonly)

Returns the value of attribute visits.



3
4
5
# File 'lib/mcts/node.rb', line 3

def visits
  @visits
end

#winsObject (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

#expandObject

maybe get a maximum depth or soemthing in



37
38
39
40
# File 'lib/mcts/node.rb', line 37

def expand
  move = @untried_moves.pop
  create_child(move)
end

#leaf?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mcts/node.rb', line 28

def leaf?
  @leaf
end

#lostObject



52
53
54
# File 'lib/mcts/node.rb', line 52

def lost
  @visits += 1
end

#rolloutObject



42
43
44
45
# File 'lib/mcts/node.rb', line 42

def rollout
  playout = Playout.new(@game_state)
  playout.play
end

#root?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/mcts/node.rb', line 24

def root?
  false
end

#uct_select_childObject



32
33
34
# File 'lib/mcts/node.rb', line 32

def uct_select_child
  children.max_by &:uct_value
end

#uct_valueObject



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

Returns:

  • (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_percentageObject



20
21
22
# File 'lib/mcts/node.rb', line 20

def win_percentage
  @wins/@visits
end

#wonObject



47
48
49
50
# File 'lib/mcts/node.rb', line 47

def won
  @visits += 1
  @wins += 1
end