Added basic test for rate.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
Implementation of TrueSkill™ in Rust.
|
||||
|
||||
## See
|
||||
|
||||
* [mafredri/go-trueskill](https://github.com/mafredri/go-trueskill)
|
||||
* [JesseBuesking/trueskill](https://github.com/JesseBuesking/trueskill)
|
||||
* [sublee/trueskill](https://github.com/sublee/trueskill)
|
||||
* [moserware/Skills](https://github.com/moserware/Skills)
|
||||
|
||||
70
src/lib.rs
70
src/lib.rs
@@ -27,11 +27,58 @@ const DRAW_PROBABILITY: f64 = 0.10;
|
||||
/// A basis to check reliability of the result.
|
||||
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 {
|
||||
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
|
||||
.iter()
|
||||
.flat_map(|group| group.iter())
|
||||
@@ -171,7 +218,7 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
|
||||
factor.update_mean(&mut variable_arena);
|
||||
}
|
||||
|
||||
println!("after:");
|
||||
let mut ratings = Vec::new();
|
||||
|
||||
for i in 0..size {
|
||||
let value = variable_arena
|
||||
@@ -179,11 +226,13 @@ pub fn rate(rating_groups: &[&[Gaussian]]) {
|
||||
.map(|variable| variable.get_value())
|
||||
.unwrap();
|
||||
|
||||
let mu = value.mu();
|
||||
let sigma = value.sigma();
|
||||
|
||||
println!("* player={}, mu={}, sigma={}", i, mu, sigma);
|
||||
ratings.push(Rating {
|
||||
mu: value.mu(),
|
||||
sigma: value.sigma(),
|
||||
});
|
||||
}
|
||||
|
||||
ratings
|
||||
}
|
||||
|
||||
pub fn quality(rating_groups: &[&[Gaussian]]) -> f64 {
|
||||
@@ -262,8 +311,13 @@ mod tests {
|
||||
let chris = 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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user