Class: WholeHistoryRating::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/whole_history_rating/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(black, white, winner, time_step, handicap, extras) ⇒ Game

Returns a new instance of Game.



5
6
7
8
9
10
11
12
13
# File 'lib/whole_history_rating/game.rb', line 5

def initialize(black, white, winner, time_step, handicap, extras)
  @day = time_step
  @white_player = white
  @black_player = black
  @winner = winner
  @extras = extras
  @handicap = handicap || 0
  @handicap_proc = handicap.is_a?(Proc) ? handicap : nil
end

Instance Attribute Details

#black_playerObject

Returns the value of attribute black_player.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def black_player
  @black_player
end

#bpdObject

Returns the value of attribute bpd.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def bpd
  @bpd
end

#dayObject

Returns the value of attribute day.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def day
  @day
end

#extrasObject

Returns the value of attribute extras.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def extras
  @extras
end

#handicapObject

Returns the value of attribute handicap.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def handicap
  @handicap
end

#white_playerObject

Returns the value of attribute white_player.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def white_player
  @white_player
end

#winnerObject

Returns the value of attribute winner.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def winner
  @winner
end

#wpdObject

Returns the value of attribute wpd.



3
4
5
# File 'lib/whole_history_rating/game.rb', line 3

def wpd
  @wpd
end

Instance Method Details

#black_win_probabilityObject



62
63
64
# File 'lib/whole_history_rating/game.rb', line 62

def black_win_probability
  bpd.gamma/(bpd.gamma + opponents_adjusted_gamma(black_player))
end

#inspectObject



49
50
51
# File 'lib/whole_history_rating/game.rb', line 49

def inspect
  "#{self}: W:#{white_player.name}(r=#{wpd ? wpd.r : '?'}) B:#{black_player.name}(r=#{bpd ? bpd.r : '?'}) winner = #{winner}, komi = #{@komi}, handicap = #{@handicap}"
end

#opponent(player) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/whole_history_rating/game.rb', line 33

def opponent(player)
  if player == white_player
    black_player
  elsif player == black_player
    white_player
  end
end

#opponents_adjusted_gamma(player) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/whole_history_rating/game.rb', line 15

def opponents_adjusted_gamma(player)
  black_advantage = @handicap_proc ? @handicap_proc.call(self) : @handicap   
  #puts "black_advantage = #{black_advantage}"
  
  if player == white_player
    opponent_elo = bpd.elo + black_advantage
  elsif player == black_player
    opponent_elo = wpd.elo - black_advantage
  else
    raise "No opponent for #{player.inspect}, since they're not in this game: #{self.inspect}."
  end
  rval = 10**(opponent_elo/400.0)
  if rval == 0 || rval.infinite? || rval.nan?
    raise WHR::UnstableRatingException, "bad adjusted gamma: #{inspect}"
  end
  rval
end

#prediction_scoreObject



41
42
43
44
45
46
47
# File 'lib/whole_history_rating/game.rb', line 41

def prediction_score
  if white_win_probability == 0.5
    0.5
  else
    ((winner == "W" && white_win_probability > 0.5) || (winner == "B" && white_win_probability < 0.5)) ? 1.0 : 0.0
  end
end

#white_win_probabilityObject

This is the Bradley-Terry Model



58
59
60
# File 'lib/whole_history_rating/game.rb', line 58

def white_win_probability
  wpd.gamma/(wpd.gamma + opponents_adjusted_gamma(white_player))
end