Example #1
0
        public static void AssertChessRating(TwoPlayerEloCalculator calculator,
                                             double player1BeforeRating,
                                             double player2BeforeRating,
                                             PairwiseComparison player1Result,
                                             double player1AfterRating,
                                             double player2AfterRating)
        {
            var player1 = new Player(1);
            var player2 = new Player(2);

            var teams = Teams.Concat(
                new Team(player1, new EloRating(player1BeforeRating)),
                new Team(player2, new EloRating(player2BeforeRating)));

            var chessGameInfo = new GameInfo(1200, 0, 200, 0, 0);

            var result = calculator.CalculateNewRatings(chessGameInfo, teams,
                (player1Result == PairwiseComparison.Win) ? new[] { 1, 2 } :
                (player1Result == PairwiseComparison.Lose) ? new[] { 2, 1 } :
                new[] { 1, 1 });


            Assert.AreEqual(player1AfterRating, result[player1].Mean, ErrorTolerance);
            Assert.AreEqual(player2AfterRating, result[player2].Mean, ErrorTolerance);
        }
Example #2
0
        private static double GetScoreFromComparison(PairwiseComparison comparison)
        {
            switch (comparison)
            {
            case PairwiseComparison.Win:
                return(1);

            case PairwiseComparison.Draw:
                return(0.5);

            case PairwiseComparison.Lose:
                return(0);

            default:
                throw new NotSupportedException();
            }
        }
        private void UpdateDuels <TPlayer>(GameInfo gameInfo,
                                           IDictionary <TPlayer, IDictionary <TPlayer, double> > duels,
                                           TPlayer player1, Rating player1Rating,
                                           TPlayer player2, Rating player2Rating,
                                           PairwiseComparison weakToStrongComparison)
        {
            var duelOutcomes = _TwoPlayerEloCalc.CalculateNewRatings(gameInfo,
                                                                     Teams.Concat(
                                                                         new Team <TPlayer>(player1, player1Rating),
                                                                         new Team <TPlayer>(player2, player2Rating)),
                                                                     (weakToStrongComparison == PairwiseComparison.Win) ? new int[] { 1, 2 }
                                                                      : (weakToStrongComparison == PairwiseComparison.Lose) ? new int[] { 2, 1 }
                                                                      : new int[] { 1, 1 });


            UpdateDuelInfo(duels, player1, player1Rating, duelOutcomes[player1], player2);
            UpdateDuelInfo(duels, player2, player2Rating, duelOutcomes[player2], player1);
        }
        public void PairwiseComparison_Constructor_NullAlternatives_ThrowsArgumentNullException()
        {
            //Arrange
            Exception exception = null;

            //Act
            try
            {
                var comparison = new PairwiseComparison((List <AlternativeNode>)null);
            }
            catch (Exception e)
            {
                exception = e;
            }

            //Assert
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(ArgumentNullException));
            Assert.AreEqual("alternatives", ((ArgumentNullException)exception).ParamName);
        }
Example #5
0
        public PairwiseParameterConstraint(PairwiseParameter first, PairwiseComparison op, PairwiseParameter second) : base(first)
        {
            this.op     = op;
            this.second = second;

            // only allow between the same strong-ish-types
            if (!this.First.IsCompatibleWith(second))
            {
                throw new ArgumentException(string.Format("{0} != {1}", this.First.PairwiseValueType, this.second.PairwiseValueType));
            }

            bool isrelative     = !(op == PairwiseComparison.Equal || op == PairwiseComparison.NotEqual);
            PairwiseValueType t = this.First.PairwiseValueType;

            if (isrelative)
            {
                if (t == PairwiseValueType.Enum || t == PairwiseValueType.ComplexObject)
                {
                    throw new NotSupportedException("Not supported: " + op + " on " + t + "; might be possible, though!");
                }
            }
        }
Example #6
0
        public static void AssertChessRating(TwoPlayerEloCalculator calculator,
                                             double player1BeforeRating, double player2BeforeRating,
                                             PairwiseComparison player1Result, double player1AfterRating,
                                             double player2AfterRating)
        {
            var player1 = new Player(1);
            var player2 = new Player(2);

            var teams = Teams.Concat(
                new Team(player1, new EloRating(player1BeforeRating)),
                new Team(player2, new EloRating(player2BeforeRating)));

            var chessGameInfo = new GameInfo(1200, 0, 200, 0, 0);

            var result = calculator.CalculateNewRatings(chessGameInfo, teams,
                                                        (player1Result == PairwiseComparison.Win) ? new[] { 1, 2 } :
                                                        (player1Result == PairwiseComparison.Lose) ? new[] { 2, 1 } :
                                                        new[] { 1, 1 });

            Assert.AreEqual(player1AfterRating, result[player1].Mean, ErrorTolerance);
            Assert.AreEqual(player2AfterRating, result[player2].Mean, ErrorTolerance);
        }
        public void PairwiseComparison_PriorityIndexerGetter_NullNode_ThrowsArgumentNullException()
        {
            //Arrange
            var       node1      = new CriterionNode("Criterion1");
            var       node2      = new CriterionNode("Criterion2");
            var       comparison = new PairwiseComparison(new CriterionNode[] { node1, node2 });
            Exception exception  = null;

            //Act
            try
            {
                var priority = comparison[null];
            }
            catch (Exception e)
            {
                exception = e;
            }

            //Assert
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(ArgumentNullException));
            Assert.AreEqual("node", ((ArgumentNullException)exception).ParamName);
        }
        public void PairwiseComparison_PriorityIndexerGetter_InvalidNode_ThrowsKeyNotFoundException()
        {
            //Arrange
            var       node1      = new CriterionNode("Criterion1");
            var       node2      = new CriterionNode("Criterion2");
            var       nodeWrong  = new CriterionNode("Criterion3");
            var       comparison = new PairwiseComparison(new CriterionNode[] { node1, node2 });
            Exception exception  = null;

            //Act
            try
            {
                var priority = comparison[nodeWrong];
            }
            catch (Exception e)
            {
                exception = e;
            }

            //Assert
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(KeyNotFoundException));
            Assert.AreEqual("Parameter node is not found in the nodes of the PairwiseComparison object.", exception.Message);
        }
        public void PairwiseComparison_ImportanceIndexerSetter_InvalidValue_ThrowsArgumentOutOfRangeException()
        {
            //Arrange
            var       node1      = new CriterionNode("Criterion1");
            var       node2      = new CriterionNode("Criterion2");
            var       comparison = new PairwiseComparison(new CriterionNode[] { node1, node2 });
            Exception exception1 = null;
            Exception exception2 = null;

            //Act
            try
            {
                comparison[node1, node2] = 1M / 10M;
            }
            catch (Exception e)
            {
                exception1 = e;
            }

            try
            {
                comparison[node1, node2] = 10;
            }
            catch (Exception e)
            {
                exception2 = e;
            }

            //Assert
            Assert.IsNotNull(exception1);
            Assert.IsNotNull(exception2);
            Assert.IsInstanceOfType(exception1, typeof(ArgumentOutOfRangeException));
            Assert.IsInstanceOfType(exception2, typeof(ArgumentOutOfRangeException));
            Assert.AreEqual("value", ((ArgumentOutOfRangeException)exception1).ParamName);
            Assert.AreEqual("value", ((ArgumentOutOfRangeException)exception2).ParamName);
        }
Example #10
0
        private static void UpdatePlayerRatings <TPlayer>(GameInfo gameInfo,
                                                          IDictionary <TPlayer, Rating> newPlayerRatings,
                                                          IDictionary <TPlayer, Rating> selfTeam,
                                                          IDictionary <TPlayer, Rating> otherTeam,
                                                          PairwiseComparison selfToOtherTeamComparison)
        {
            double drawMargin  = DrawMargin.GetDrawMarginFromDrawProbability(gameInfo.DrawProbability, gameInfo.Beta);
            double betaSquared = Square(gameInfo.Beta);
            double tauSquared  = Square(gameInfo.DynamicsFactor);

            int totalPlayers = selfTeam.Count() + otherTeam.Count();

            double selfMeanSum      = selfTeam.Values.Sum(r => r.Mean);
            double otherTeamMeanSum = otherTeam.Values.Sum(r => r.Mean);

            double c = Math.Sqrt(selfTeam.Values.Sum(r => Square(r.StandardDeviation))
                                 +
                                 otherTeam.Values.Sum(r => Square(r.StandardDeviation))
                                 +
                                 totalPlayers * betaSquared);

            double winningMean = selfMeanSum;
            double losingMean  = otherTeamMeanSum;

            switch (selfToOtherTeamComparison)
            {
            case PairwiseComparison.Win:
            case PairwiseComparison.Draw:
                // NOP
                break;

            case PairwiseComparison.Lose:
                winningMean = otherTeamMeanSum;
                losingMean  = selfMeanSum;
                break;
            }

            double meanDelta = winningMean - losingMean;

            double v;
            double w;
            double rankMultiplier;

            if (selfToOtherTeamComparison != PairwiseComparison.Draw)
            {
                // non-draw case
                v = TruncatedGaussianCorrectionFunctions.VExceedsMargin(meanDelta, drawMargin, c);
                w = TruncatedGaussianCorrectionFunctions.WExceedsMargin(meanDelta, drawMargin, c);
                rankMultiplier = (int)selfToOtherTeamComparison;
            }
            else
            {
                // assume draw
                v = TruncatedGaussianCorrectionFunctions.VWithinMargin(meanDelta, drawMargin, c);
                w = TruncatedGaussianCorrectionFunctions.WWithinMargin(meanDelta, drawMargin, c);
                rankMultiplier = 1;
            }

            foreach (var teamPlayerRatingPair in selfTeam)
            {
                Rating previousPlayerRating = teamPlayerRatingPair.Value;

                double meanMultiplier   = (Square(previousPlayerRating.StandardDeviation) + tauSquared) / c;
                double stdDevMultiplier = (Square(previousPlayerRating.StandardDeviation) + tauSquared) / Square(c);

                double playerMeanDelta = (rankMultiplier * meanMultiplier * v);
                double newMean         = previousPlayerRating.Mean + playerMeanDelta;

                double newStdDev =
                    Math.Sqrt((Square(previousPlayerRating.StandardDeviation) + tauSquared) * (1 - w * stdDevMultiplier));

                newPlayerRatings[teamPlayerRatingPair.Key] = new Rating(newMean, newStdDev);
            }
        }
        public void PairwiseComparison_ImportanceIndexerSetterGetter_InvalidNode_ThrowsKeyNotFoundException()
        {
            //Arrange
            var       node1      = new CriterionNode("Criterion1");
            var       node2      = new CriterionNode("Criterion2");
            var       nodeWrong  = new CriterionNode("Criterion3");
            var       comparison = new PairwiseComparison(new CriterionNode[] { node1, node2 });
            Exception exception1 = null;
            Exception exception2 = null;
            Exception exception3 = null;
            Exception exception4 = null;

            //Act
            try
            {
                comparison[node1, nodeWrong] = 1M;
            }
            catch (Exception e)
            {
                exception1 = e;
            }

            try
            {
                comparison[nodeWrong, node2] = 1M;
            }
            catch (Exception e)
            {
                exception2 = e;
            }

            try
            {
                var importance = comparison[node1, nodeWrong];
            }
            catch (Exception e)
            {
                exception3 = e;
            }

            try
            {
                var importance = comparison[nodeWrong, node2];
            }
            catch (Exception e)
            {
                exception4 = e;
            }

            //Assert
            Assert.IsNotNull(exception1);
            Assert.IsNotNull(exception2);
            Assert.IsNotNull(exception3);
            Assert.IsNotNull(exception4);
            Assert.IsInstanceOfType(exception1, typeof(KeyNotFoundException));
            Assert.IsInstanceOfType(exception2, typeof(KeyNotFoundException));
            Assert.IsInstanceOfType(exception3, typeof(KeyNotFoundException));
            Assert.IsInstanceOfType(exception4, typeof(KeyNotFoundException));
            Assert.AreEqual("Parameter node2 is not found in the nodes of the PairwiseComparison object.", exception1.Message);
            Assert.AreEqual("Parameter node1 is not found in the nodes of the PairwiseComparison object.", exception2.Message);
            Assert.AreEqual("Parameter node2 is not found in the nodes of the PairwiseComparison object.", exception3.Message);
            Assert.AreEqual("Parameter node1 is not found in the nodes of the PairwiseComparison object.", exception4.Message);
        }
        public void PairwiseComparison_ImportanceIndexerSetterAndGetter_NullNode_ThrowsArgumentNullException()
        {
            //Arrange
            var       node1      = new CriterionNode("Criterion1");
            var       node2      = new CriterionNode("Criterion2");
            var       comparison = new PairwiseComparison(new CriterionNode[] { node1, node2 });
            Exception exception1 = null;
            Exception exception2 = null;
            Exception exception3 = null;
            Exception exception4 = null;

            //Act
            try
            {
                comparison[node1, null] = 1M;
            }
            catch (Exception e)
            {
                exception1 = e;
            }

            try
            {
                comparison[null, node2] = 1M;
            }
            catch (Exception e)
            {
                exception2 = e;
            }

            try
            {
                var importance = comparison[node1, null];
            }
            catch (Exception e)
            {
                exception3 = e;
            }

            try
            {
                var importance = comparison[null, node2];
            }
            catch (Exception e)
            {
                exception4 = e;
            }

            //Assert
            Assert.IsNotNull(exception1);
            Assert.IsNotNull(exception2);
            Assert.IsNotNull(exception3);
            Assert.IsNotNull(exception4);
            Assert.IsInstanceOfType(exception1, typeof(ArgumentNullException));
            Assert.IsInstanceOfType(exception2, typeof(ArgumentNullException));
            Assert.IsInstanceOfType(exception3, typeof(ArgumentNullException));
            Assert.IsInstanceOfType(exception4, typeof(ArgumentNullException));
            Assert.AreEqual("node2", ((ArgumentNullException)exception1).ParamName);
            Assert.AreEqual("node1", ((ArgumentNullException)exception2).ParamName);
            Assert.AreEqual("node2", ((ArgumentNullException)exception3).ParamName);
            Assert.AreEqual("node1", ((ArgumentNullException)exception4).ParamName);
        }
Example #13
0
        protected virtual EloRating CalculateNewRating(GameInfo gameInfo, double selfRating, double opponentRating, PairwiseComparison selfToOpponentComparison)
        {
            double expectedProbability = GetPlayerWinProbability(gameInfo, selfRating, opponentRating);
            double actualProbability = GetScoreFromComparison(selfToOpponentComparison);
            double k = _KFactor.GetValueForRating(selfRating);
            double ratingChange = k * (actualProbability - expectedProbability);
            double newRating = selfRating + ratingChange;

            return new EloRating(newRating);
        }