using System.Collections.Generic; using System.Linq; using Moserware.Numerics; using Moserware.Skills.FactorGraphs; using Moserware.Skills.TrueSkill.Factors; namespace Moserware.Skills.TrueSkill.Layers { // We intentionally have no Posterior schedule since the only purpose here is to internal class PlayerPriorValuesToSkillsLayer : TrueSkillFactorGraphLayer , GaussianPriorFactor, KeyedVariable> { private readonly IEnumerable> _Teams; public PlayerPriorValuesToSkillsLayer(TrueSkillFactorGraph parentGraph, IEnumerable> teams) : base(parentGraph) { _Teams = teams; } public override void BuildLayer() { foreach (var currentTeam in _Teams) { var currentTeamSkills = new List>(); foreach (var currentTeamPlayer in currentTeam) { KeyedVariable playerSkill = CreateSkillOutputVariable(currentTeamPlayer.Key); AddLayerFactor(CreatePriorFactor(currentTeamPlayer.Key, currentTeamPlayer.Value, playerSkill)); currentTeamSkills.Add(playerSkill); } OutputVariablesGroups.Add(currentTeamSkills); } } public override Schedule CreatePriorSchedule() { return ScheduleSequence( from prior in LocalFactors select new ScheduleStep("Prior to Skill Step", prior, 0), "All priors"); } private GaussianPriorFactor CreatePriorFactor(TPlayer player, Rating priorRating, Variable skillsVariable) { return new GaussianPriorFactor(priorRating.Mean, Square(priorRating.StandardDeviation) + Square(ParentFactorGraph.GameInfo.DynamicsFactor), skillsVariable); } private KeyedVariable CreateSkillOutputVariable(TPlayer key) { return ParentFactorGraph.VariableFactory.CreateKeyedVariable(key, "{0}'s skill", key); } } }