You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2011/09/23 15:23:06 UTC
svn commit: r1174740 -
/labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java
Author: simonetripodi
Date: Fri Sep 23 13:23:06 2011
New Revision: 1174740
URL: http://svn.apache.org/viewvc?rev=1174740&view=rev
Log:
first checkin of RankingCalculator class
Added:
labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java (with props)
Added: labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java
URL: http://svn.apache.org/viewvc/labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java?rev=1174740&view=auto
==============================================================================
--- labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java (added)
+++ labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java Fri Sep 23 13:23:06 2011
@@ -0,0 +1,115 @@
+package org.apache.openelo;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static java.lang.Math.pow;
+
+/**
+ *
+ */
+public final class RankingCalculator
+{
+
+ private static final double DEFAULT_POW_BASE = 10;
+
+ private static final double DEFAULT_DIVISOR = 400;
+
+ private static final int DEFAULT_K_FACTOR = 32;
+
+ private final int kFactor;
+
+ /**
+ *
+ */
+ public RankingCalculator()
+ {
+ this( DEFAULT_K_FACTOR );
+ }
+
+ /**
+ *
+ *
+ * @param kFactor
+ */
+ public RankingCalculator( int kFactor )
+ {
+ this.kFactor = kFactor;
+ }
+
+ /**
+ *
+ *
+ * @param <I>
+ * @param <N>
+ * @param playerA
+ * @param playerB
+ * @param gameResult
+ */
+ public final <I> void updateRating( Player<I> playerA, Player<I> playerB, GameResult gameResult )
+ {
+ double qA = calculateQFactor( playerA.getRanking() );
+ double qB = calculateQFactor( playerB.getRanking() );
+
+ double eA = calculateEFactor( qA, qB );
+ double eB = calculateEFactor( qB, qA );
+
+ double sA;
+ double sB;
+ switch ( gameResult )
+ {
+ case WIN:
+ sA = 1;
+ sB = 0;
+ break;
+
+ case LOSE:
+ sA = 0;
+ sB = 1;
+ break;
+
+ case DRAW:
+ sA = 0.5;
+ sB = 0.5;
+ break;
+
+ default: // shouldn't happen
+ throw new IllegalArgumentException( "Input GameResult not accepted" );
+ }
+
+ updateRanking( playerA, kFactor, sA, eA );
+ updateRanking( playerB, kFactor, sB, eB );
+ }
+
+ private static double calculateQFactor( double ranking )
+ {
+ return pow( DEFAULT_POW_BASE, ranking / DEFAULT_DIVISOR);
+ }
+
+ private static double calculateEFactor( double qA, double qB )
+ {
+ return qA / (qA + qB);
+ }
+
+ private static <I> void updateRanking( Player<I> player, double kFactor, double sFactor, double eFactor )
+ {
+ player.updateRanking( player.getRanking() + ( kFactor * ( sFactor - eFactor ) ) );
+ }
+
+}
Propchange: labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: labs/openelo/src/main/java/org/apache/openelo/RankingCalculator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org