Added basic test for rate.

This commit is contained in:
2018-10-24 16:12:25 +02:00
parent fc43a60929
commit 2757e6f0f2
2 changed files with 64 additions and 8 deletions

View File

@@ -3,6 +3,8 @@
Implementation of TrueSkill™ in Rust. Implementation of TrueSkill™ in Rust.
## See ## See
* [mafredri/go-trueskill](https://github.com/mafredri/go-trueskill) * [mafredri/go-trueskill](https://github.com/mafredri/go-trueskill)
* [JesseBuesking/trueskill](https://github.com/JesseBuesking/trueskill) * [JesseBuesking/trueskill](https://github.com/JesseBuesking/trueskill)
* [sublee/trueskill](https://github.com/sublee/trueskill) * [sublee/trueskill](https://github.com/sublee/trueskill)
* [moserware/Skills](https://github.com/moserware/Skills)

View File

@@ -27,11 +27,58 @@ const DRAW_PROBABILITY: f64 = 0.10;
/// A basis to check reliability of the result. /// A basis to check reliability of the result.
const DELTA: f64 = 0.0001; const DELTA: f64 = 0.0001;
pub trait Rateable {
fn mu(&self) -> f64;
fn sigma(&self) -> f64;
}
#[derive(Debug, PartialEq)]
pub struct Rating {
pub mu: f64,
pub sigma: f64,
}
impl Rating {
pub fn new(mu: f64, sigma: f64) -> Rating {
Rating { mu, sigma }
}
}
impl Rateable for Rating {
fn mu(&self) -> f64 {
self.mu
}
fn sigma(&self) -> f64 {
self.sigma
}
}
impl Rateable for Gaussian {
fn mu(&self) -> f64 {
self.mu()
}
fn sigma(&self) -> f64 {
self.sigma()
}
}
fn draw_margin(p: f64, beta: f64, total_players: f64) -> f64 { fn draw_margin(p: f64, beta: f64, total_players: f64) -> f64 {
math::icdf((p + 1.0) / 2.0) * total_players.sqrt() * beta math::icdf((p + 1.0) / 2.0) * total_players.sqrt() * beta
} }
pub fn rate(rating_groups: &[&[Gaussian]]) { /*
pub fn calculate_ratings<R>(team_players: &[R], team_ranks: &[u16]) -> Vec<Rating>
where
R: Rateable,
{
*/
pub fn rate<R>(rating_groups: &[&[R]]) -> Vec<Rating>
where
R: Rateable,
{
let flatten_ratings = rating_groups let flatten_ratings = rating_groups
.iter() .iter()
.flat_map(|group| group.iter()) .flat_map(|group| group.iter())
@@ -171,7 +218,7 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
factor.update_mean(&mut variable_arena); factor.update_mean(&mut variable_arena);
} }
println!("after:"); let mut ratings = Vec::new();
for i in 0..size { for i in 0..size {
let value = variable_arena let value = variable_arena
@@ -179,11 +226,13 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
.map(|variable| variable.get_value()) .map(|variable| variable.get_value())
.unwrap(); .unwrap();
let mu = value.mu(); ratings.push(Rating {
let sigma = value.sigma(); mu: value.mu(),
sigma: value.sigma(),
println!("* player={}, mu={}, sigma={}", i, mu, sigma); });
} }
ratings
} }
pub fn quality(rating_groups: &[&[Gaussian]]) -> f64 { pub fn quality(rating_groups: &[&[Gaussian]]) -> f64 {
@@ -262,8 +311,13 @@ mod tests {
let chris = Gaussian::from_mu_sigma(MU, SIGMA); let chris = Gaussian::from_mu_sigma(MU, SIGMA);
let darren = Gaussian::from_mu_sigma(MU, SIGMA); let darren = Gaussian::from_mu_sigma(MU, SIGMA);
rate(&[&[alice], &[bob], &[chris], &[darren]]); let new_ratings = rate(&[&[alice], &[bob], &[chris], &[darren]]);
assert_eq!(true, false); assert_eq!(new_ratings, vec![
Rating::new(33.20778932559525, 6.347937214998893),
Rating::new(27.401497882797486, 5.787057812482782),
Rating::new(22.598576351652632, 5.7871159419307645),
Rating::new(16.79337409436942, 6.348053083319977),
]);
} }
} }