Class: WholeHistoryRating::Base

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

Defined Under Namespace

Classes: UnstableRatingException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
# File 'lib/whole_history_rating/base.rb', line 11

def initialize(config = {})
  @config = config
  @config[:w2] ||= 300.0  # elo^2
  @games = []
  @players = {}
end

Instance Attribute Details

#gamesObject

Returns the value of attribute games.



9
10
11
# File 'lib/whole_history_rating/base.rb', line 9

def games
  @games
end

#playersObject

Returns the value of attribute players.



9
10
11
# File 'lib/whole_history_rating/base.rb', line 9

def players
  @players
end

Instance Method Details

#add_game(game) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/whole_history_rating/base.rb', line 65

def add_game(game)
  game.white_player.add_game(game)
  game.black_player.add_game(game)
  if game.bpd.nil?
    puts "Bad game: #{options.inspect} -> #{game.inspect}"
  end
  @games << game
  game
end

#create_game(black, white, winner, time_step, handicap, extras = {}) ⇒ Object



60
61
62
63
# File 'lib/whole_history_rating/base.rb', line 60

def create_game(black, white, winner, time_step, handicap, extras = {})
  game = setup_game(black, white, winner, time_step, handicap, extras)
  add_game(game)
end

#iterate(count) ⇒ Object



75
76
77
78
79
80
# File 'lib/whole_history_rating/base.rb', line 75

def iterate(count)
  count.times { run_one_iteration }
  players.each do |name,player|
    player.update_uncertainty
  end     
end

#log_likelihoodObject



27
28
29
30
31
32
33
34
35
# File 'lib/whole_history_rating/base.rb', line 27

def log_likelihood
  score = 0.0
  @players.values.each do |p|
    unless p.days.empty?
      score += p.log_likelihood
    end
  end
  score
end

#player_by_name(name) ⇒ Object



37
38
39
# File 'lib/whole_history_rating/base.rb', line 37

def player_by_name(name)
  players[name] || players[name] = Player.new(name, @config)
end


18
19
20
21
22
23
24
25
# File 'lib/whole_history_rating/base.rb', line 18

def print_ordered_ratings
  players = @players.values.select {|p| p.days.count > 0}
  players.sort_by { |p| p.days.last.gamma }.each_with_index do |p,idx|
    if p.days.count > 0
      puts "#{p.name} => #{p.days.map(&:elo)}"
    end
  end
end

#ratings_for_player(name) ⇒ Object



41
42
43
44
# File 'lib/whole_history_rating/base.rb', line 41

def ratings_for_player(name)
  player = player_by_name(name)
  player.days.map {|d| [d.day, d.elo.round, (d.uncertainty*100).round]}
end

#run_one_iterationObject



82
83
84
85
86
# File 'lib/whole_history_rating/base.rb', line 82

def run_one_iteration
  players.each do |name,player|
    player.run_one_newton_iteration
  end
end

#setup_game(black, white, winner, time_step, handicap, extras = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/whole_history_rating/base.rb', line 46

def setup_game(black, white, winner, time_step, handicap, extras = {})
      
  # Avoid self-played games (no info)
  if black == white
    raise "Invalid game (black player == white player)"
    return nil
  end

  white_player = player_by_name(white)
  black_player = player_by_name(black)
  game = Game.new(black_player, white_player, winner, time_step, handicap, extras)
  game
end