Skip to content

Commit

Permalink
refactor interface to use a hash instead of paralle arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Lundquist committed Oct 10, 2012
1 parent d0c4468 commit cd4b390
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 134 deletions.
67 changes: 34 additions & 33 deletions lib/saulabs/trueskill/factor_graph.rb
@@ -1,38 +1,38 @@
# -*- encoding : utf-8 -*-
module Saulabs
module TrueSkill

class FactorGraph

# @return [Array<Array<TrueSkill::Rating>>]
attr_reader :teams

# @return [Float]
attr_reader :beta

# @return [Float]
attr_reader :beta_squared

# @return [Float]
attr_reader :draw_probability

# @return [Float]
attr_reader :epsilon

# @private
attr_reader :layers

# @return [Boolean]
attr_reader :skills_additive


# Creates a new trueskill factor graph for calculating the new skills based on the given game parameters
#
# @param [Array<Array<TrueSkill::Rating>>] teams
# @param [Array<Array<TrueSkill::Rating>>] teams
# player-ratings grouped in Arrays by teams
# @param [Array<Integer>] ranks
# @param [Array<Integer>] ranks
# team rankings, example: [2,1,3] first team in teams finished 2nd, second team 1st and third team 3rd
# @param [Hash] options
# @param [Hash] options
# the options hash to configure the factor graph constants beta, draw_probability and skills_additive
#
# @option options [Float] :beta (4.166667)
Expand All @@ -44,30 +44,31 @@ class FactorGraph
# true is valid for games like Halo etc, where skill is additive (2 players are better than 1),
# false for card games like Skat, Doppelkopf, Bridge where skills are not additive,
# two players dont make the team stronger, skills averaged)
#
#
# @example Calculating new skills of a two team game, where one team has one player and the other two
#
# require 'rubygems'
# require 'saulabs/trueskill'
#
#
# include Saulabs::TrueSkill
#
#
# # team 1 has just one player with a mean skill of 27.1, a skill-deviation of 2.13
# # and a play activity of 100 %
# team1 = [Rating.new(27.1, 2.13, 1.0)]
#
#
# # team 2 has two players
# team2 = [Rating.new(22.0, 0.98, 0.8), Rating.new(31.1, 5.33, 0.9)]
#
#
# # team 1 finished first and team 2 second
# graph = FactorGraph.new([team1, team2], [1,2])
#
#
# # update the Ratings
# graph.update_skills
#
def initialize(teams, ranks, options = {})
@teams = teams
@ranks = ranks
def initialize(ranks_teams_hash, options = {})
@teams = ranks_teams_hash.keys
@ranks = ranks_teams_hash.values

opts = {
:beta => 25/6.0,
:draw_probability => 0.1,
Expand All @@ -79,25 +80,25 @@ def initialize(teams, ranks, options = {})
@beta_squared = @beta**2
@epsilon = -Math.sqrt(2.0 * @beta_squared) * Gauss::Distribution.inv_cdf((1.0 - @draw_probability) / 2.0)
@skills_additive = opts[:skills_additive]

@prior_layer = Layers::PriorToSkills.new(self, @teams)
@layers = [
@prior_layer,
Layers::SkillsToPerformances.new(self),
Layers::PerformancesToTeamPerformances.new(self, @skills_additive),
Layers::IteratedTeamPerformances.new(self,
Layers::TeamPerformanceDifferences.new(self),
Layers::TeamDifferenceComparision.new(self, ranks)
Layers::TeamDifferenceComparision.new(self, @ranks)
)
]
end

def draw_margin
Gauss::Distribution.inv_cdf(0.5*(@draw_probability + 1)) * Math.sqrt(1 + 1) * @beta
end

# Updates the skills of the players inplace
#
#
# @return [Float] the probability of the games outcome
def update_skills
build_layers
Expand All @@ -109,9 +110,9 @@ def update_skills
end
ranking_probability
end

private

def ranking_probability
# factor_list = []
# sum_log_z, sum_log_s = 0.0
Expand All @@ -125,11 +126,11 @@ def ranking_probability
# Math.exp(sum_log_z + sum_log_s)
0.0
end

def updated_skills
@prior_layer.output
end

def build_layers
output = nil
@layers.each do |layer|
Expand All @@ -138,13 +139,13 @@ def build_layers
output = layer.output
end
end

def run_schedule
schedules = @layers.map(&:prior_schedule) + @layers.reverse.map(&:posterior_schedule)
Schedules::Sequence.new(schedules.compact).visit
end

end

end
end
12 changes: 6 additions & 6 deletions lib/saulabs/trueskill/layers/prior_to_skills.rb
Expand Up @@ -3,15 +3,15 @@ module Saulabs
module TrueSkill
# @private
module Layers

# @private
class PriorToSkills < Base

def initialize(graph, teams)
super(graph)
@teams = teams
end

def build
@teams.each do |team|
team_skills = []
Expand All @@ -23,13 +23,13 @@ def build
@output << team_skills
end
end

def prior_schedule
Schedules::Sequence.new(@factors.map { |f| Schedules::Step.new(f, 0) })
end

end

end
end
end

0 comments on commit cd4b390

Please sign in to comment.