Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.goochjs</groupId>
<artifactId>goochjs-glicko2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<packaging>bundle</packaging>
<description>Glicko-2 rating system</description>
<name>glicko2</name>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/goochjs/glicko2/RatingCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public RatingCalculator(
*/
public void updateRatings(RatingPeriodResults results) {
for ( Rating player : results.getParticipants() ) {
if ( results.getResults(player).size() > 0 ) {
calculateNewRating(player, results.getResults(player));
List<Result> playerResults = results.getResults(player);

if ( !playerResults.isEmpty() ) {
calculateNewRating(player, playerResults);
} else {
// if a player does not compete during the rating period, then only Step 6 applies.
// the player's rating and volatility parameters remain the same but deviation increases
Expand Down
57 changes: 36 additions & 21 deletions src/main/java/org/goochjs/glicko2/RatingPeriodResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package org.goochjs.glicko2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -17,7 +20,7 @@
* @author Jeremy Gooch
*/
public class RatingPeriodResults {
private List<Result> results = new ArrayList<Result>();
private Map<Rating, List<Result>> results = new HashMap<Rating, List<Result>>();
private Set<Rating> participants = new HashSet<Rating>();


Expand All @@ -44,9 +47,7 @@ public RatingPeriodResults(Set<Rating> participants) {
* @param loser
*/
public void addResult(Rating winner, Rating loser) {
Result result = new Result(winner, loser);

results.add(result);
this.addResult(new Result(winner, loser));
}


Expand All @@ -57,28 +58,45 @@ public void addResult(Rating winner, Rating loser) {
* @param player2
*/
public void addDraw(Rating player1, Rating player2) {
Result result = new Result(player1, player2, true);

results.add(result);
this.addResult(new Result(player1, player2, true));
}




private void addResult(Result result) {
List<Result> playerResults = results.get(result.getWinner());

if (playerResults == null) {
playerResults = new ArrayList<Result>();
results.put(result.getWinner(), playerResults);
}

playerResults.add(result);

playerResults = results.get(result.getLoser());

if (playerResults == null) {
playerResults = new ArrayList<Result>();
results.put(result.getLoser(), playerResults);
}

playerResults.add(result);
}


/**
* Get a list of the results for a given player.
*
* @param player
* @return List of results
*/
public List<Result> getResults(Rating player) {
List<Result> filteredResults = new ArrayList<Result>();

for ( Result result : results ) {
if ( result.participated(player) ) {
filteredResults.add(result);
}
List<Result> playerResults = results.get(player);

if (playerResults != null) {
return playerResults;
}
return filteredResults;

return Collections.emptyList();
}


Expand All @@ -89,10 +107,7 @@ public List<Result> getResults(Rating player) {
*/
public Set<Rating> getParticipants() {
// Run through the results and make sure all players have been pushed into the participants set.
for ( Result result : results ) {
participants.add(result.getWinner());
participants.add(result.getLoser());
}
participants.addAll(results.keySet());

return participants;
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/goochjs/glicko2/TestGlicko2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package org.goochjs.glicko2;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -79,4 +82,31 @@ private void printResults(String text) {
System.out.println(player4);
System.out.println(player5);
}

@Test
public void scaleTest() {
int playerCount = 100000;
List<Rating> players = new ArrayList(playerCount);

results = new RatingPeriodResults();

for (int i = 0; i < playerCount; i++) {
Rating player = new Rating("player" + i, ratingSystem);
player.setRating(1000.0 + i % 1000);
players.add(player);
}

long start = System.currentTimeMillis();

for (int i = 0; i < playerCount; i++) {
results.addResult(players.get(i), players.get((i + 1) % playerCount));
results.addResult(players.get(i), players.get((i + 2) % playerCount));
}

ratingSystem.updateRatings(results);

long end = System.currentTimeMillis();

System.out.println("Execution time: " + (end - start));
}
}