Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple teams for TrueSkill algorithm (WIP) #9

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This is a broad overview of the changes that have been made over the lifespan of this library.

## v0.25.0 - 2023-06-04

- Add Rating, RatingSystem, RatingPeriodSystem, TeamRatingSystem, and MultiTeamRatingSystem traits

## v0.24.0 - 2023-01-01

- Renamed `match_quality_teams` to `match_quality_two_teams` for consistency
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skillratings"
version = "0.24.0"
version = "0.25.0"
edition = "2021"
description = "Calculate a player's skill rating using algorithms like Elo, Glicko, Glicko-2, TrueSkill and many more."
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Alternatively, you can add the following to your `Cargo.toml` file manually:

```toml
[dependencies]
skillratings = "0.24"
skillratings = "0.25"
```

### Serde support
Expand All @@ -56,7 +56,7 @@ By editing `Cargo.toml` manually:

```toml
[dependencies]
skillratings = {version = "0.24", features = ["serde"]}
skillratings = {version = "0.25", features = ["serde"]}
```

## Usage and Examples
Expand Down
89 changes: 87 additions & 2 deletions src/dwz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use std::{collections::HashMap, error::Error, fmt::Display};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{elo::EloRating, Outcomes};
use crate::{elo::EloRating, Outcomes, Rating, RatingPeriodSystem, RatingSystem};

#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -89,6 +89,22 @@ impl Default for DWZRating {
}
}

impl Rating for DWZRating {
fn rating(&self) -> f64 {
self.rating
}
fn uncertainty(&self) -> Option<f64> {
None
}
fn new(rating: Option<f64>, _uncertainty: Option<f64>) -> Self {
Self {
rating: rating.unwrap_or(1000.0),
index: 1,
age: 26,
}
}
}

impl From<(f64, usize, usize)> for DWZRating {
fn from((r, i, a): (f64, usize, usize)) -> Self {
Self {
Expand Down Expand Up @@ -144,6 +160,44 @@ impl Display for GetFirstDWZError {

impl Error for GetFirstDWZError {}

/// Struct to calculate ratings and expected score for [`DWZRating`]
pub struct DWZ {}

impl RatingSystem for DWZ {
type RATING = DWZRating;
type CONFIG = ();

fn new(_config: Self::CONFIG) -> Self {
Self {}
}

fn rate(
&self,
player_one: &DWZRating,
player_two: &DWZRating,
outcome: &Outcomes,
) -> (DWZRating, DWZRating) {
dwz(player_one, player_two, outcome)
}

fn expected_score(&self, player_one: &DWZRating, player_two: &DWZRating) -> (f64, f64) {
expected_score(player_one, player_two)
}
}

impl RatingPeriodSystem for DWZ {
type RATING = DWZRating;
type CONFIG = ();

fn new(_config: Self::CONFIG) -> Self {
Self {}
}

fn rate(&self, player: &DWZRating, results: &[(DWZRating, Outcomes)]) -> DWZRating {
dwz_rating_period(player, results)
}
}

#[must_use]
/// Calculates new [`DWZRating`] of two players based on their old rating, index, age and outcome of the game.
///
Expand Down Expand Up @@ -903,7 +957,7 @@ mod tests {
assert_eq!(player_one, player_two);

assert_eq!(player_one, player_one.clone());
assert!(!format!("{:?}", player_one).is_empty());
assert!(!format!("{player_one:?}").is_empty());

assert_eq!(
DWZRating::from((1400.0, 20)),
Expand All @@ -921,4 +975,35 @@ mod tests {
GetFirstDWZError::NotEnoughGames.clone()
);
}

#[test]
fn test_traits() {
let player_one: DWZRating = Rating::new(Some(240.0), Some(90.0));
let player_two: DWZRating = Rating::new(Some(240.0), Some(90.0));

let rating_system: DWZ = RatingSystem::new(());

assert!((player_one.rating() - 240.0).abs() < f64::EPSILON);
assert_eq!(player_one.uncertainty(), None);

let (new_player_one, new_player_two) =
RatingSystem::rate(&rating_system, &player_one, &player_two, &Outcomes::WIN);

let (exp1, exp2) = RatingSystem::expected_score(&rating_system, &player_one, &player_two);

assert!((new_player_one.rating - 306.666_666_666_666_7).abs() < f64::EPSILON);
assert!((new_player_two.rating - 237.350_993_377_483_43).abs() < f64::EPSILON);
assert!((exp1 - 0.5).abs() < f64::EPSILON);
assert!((exp2 - 0.5).abs() < f64::EPSILON);

let player_one: DWZRating = Rating::new(Some(240.0), Some(90.0));
let player_two: DWZRating = Rating::new(Some(240.0), Some(90.0));

let rating_period: DWZ = RatingPeriodSystem::new(());

let new_player_one =
RatingPeriodSystem::rate(&rating_period, &player_one, &[(player_two, Outcomes::WIN)]);

assert!((new_player_one.rating - 306.666_666_666_666_7).abs() < f64::EPSILON);
}
}
95 changes: 92 additions & 3 deletions src/egf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::Outcomes;
use crate::{Outcomes, Rating, RatingPeriodSystem, RatingSystem};

#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -86,6 +86,20 @@ impl Default for EGFRating {
}
}

impl Rating for EGFRating {
fn rating(&self) -> f64 {
self.rating
}
fn uncertainty(&self) -> Option<f64> {
None
}
fn new(rating: Option<f64>, _uncertainty: Option<f64>) -> Self {
Self {
rating: rating.unwrap_or(0.0),
}
}
}

impl From<f64> for EGFRating {
fn from(r: f64) -> Self {
Self { rating: r }
Expand Down Expand Up @@ -124,6 +138,50 @@ impl Default for EGFConfig {
}
}

/// Struct to calculate ratings and expected score for [`EGFRating`]
pub struct EGF {
config: EGFConfig,
}

impl RatingSystem for EGF {
type RATING = EGFRating;
type CONFIG = EGFConfig;

fn new(config: Self::CONFIG) -> Self {
Self { config }
}

fn rate(
&self,
player_one: &EGFRating,
player_two: &EGFRating,
outcome: &Outcomes,
) -> (EGFRating, EGFRating) {
egf(player_one, player_two, outcome, &self.config)
}

fn expected_score(&self, player_one: &EGFRating, player_two: &EGFRating) -> (f64, f64) {
expected_score(player_one, player_two, &self.config)
}
}

impl RatingPeriodSystem for EGF {
type RATING = EGFRating;
type CONFIG = EGFConfig;

fn new(config: Self::CONFIG) -> Self {
Self { config }
}

fn rate(&self, player: &EGFRating, results: &[(EGFRating, Outcomes)]) -> EGFRating {
// Need to add a config to the results.
let new_results: Vec<(EGFRating, Outcomes, EGFConfig)> =
results.iter().map(|r| (r.0, r.1, self.config)).collect();

egf_rating_period(player, &new_results[..])
}
}

#[must_use]
/// Calculates the [`EGFRating`]s of two players based on their old ratings and the outcome of the game.
///
Expand Down Expand Up @@ -414,9 +472,40 @@ mod tests {
assert_eq!(player_one, player_one.clone());
assert!((config.handicap - config.clone().handicap).abs() < f64::EPSILON);

assert!(!format!("{:?}", player_one).is_empty());
assert!(!format!("{:?}", config).is_empty());
assert!(!format!("{player_one:?}").is_empty());
assert!(!format!("{config:?}").is_empty());

assert_eq!(player_one, EGFRating::from(0.0));
}

#[test]
fn test_traits() {
let player_one: EGFRating = Rating::new(Some(240.0), Some(90.0));
let player_two: EGFRating = Rating::new(Some(240.0), Some(90.0));

let rating_system: EGF = RatingSystem::new(EGFConfig::new());

assert!((player_one.rating() - 240.0).abs() < f64::EPSILON);
assert_eq!(player_one.uncertainty(), None);

let (new_player_one, new_player_two) =
RatingSystem::rate(&rating_system, &player_one, &player_two, &Outcomes::WIN);

let (exp1, exp2) = RatingSystem::expected_score(&rating_system, &player_one, &player_two);

assert!((new_player_one.rating - 284.457_578_792_560_87).abs() < f64::EPSILON);
assert!((new_player_two.rating - 205.842_421_207_441_73).abs() < f64::EPSILON);
assert!((exp1 - 0.5).abs() < f64::EPSILON);
assert!((exp2 - 0.5).abs() < f64::EPSILON);

let player_one: EGFRating = Rating::new(Some(240.0), Some(90.0));
let player_two: EGFRating = Rating::new(Some(240.0), Some(90.0));

let rating_period: EGF = RatingPeriodSystem::new(EGFConfig::new());

let new_player_one =
RatingPeriodSystem::rate(&rating_period, &player_one, &[(player_two, Outcomes::WIN)]);

assert!((new_player_one.rating - 284.457_578_792_560_87).abs() < f64::EPSILON);
}
}
Loading
Loading