Use PlayerIndex instead of String
This commit is contained in:
168
examples/atp.rs
168
examples/atp.rs
@@ -2,7 +2,27 @@ use std::collections::HashMap;
|
||||
|
||||
use time::Date;
|
||||
|
||||
use trueskill_tt::{History, BETA, MU, P_DRAW};
|
||||
use trueskill_tt::{History, PlayerIndex, BETA, MU, P_DRAW};
|
||||
|
||||
struct Players(HashMap<String, PlayerIndex>);
|
||||
|
||||
impl Players {
|
||||
fn new() -> Self {
|
||||
Self(HashMap::new())
|
||||
}
|
||||
|
||||
fn get(&mut self, name: &str) -> PlayerIndex {
|
||||
if let Some(idx) = self.0.get(name) {
|
||||
*idx
|
||||
} else {
|
||||
let idx = PlayerIndex::new(self.0.len());
|
||||
|
||||
self.0.insert(name.to_string(), idx);
|
||||
|
||||
idx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut csv = csv::Reader::open("examples/atp.csv").unwrap();
|
||||
@@ -13,17 +33,23 @@ fn main() {
|
||||
|
||||
let time_format = time::format_description::parse("[year]-[month]-[day]").unwrap();
|
||||
|
||||
let mut players = Players::new();
|
||||
|
||||
for row in csv.records() {
|
||||
if &row["double"] == "t" {
|
||||
composition.push(vec![
|
||||
vec![row["w1_id"].to_string(), row["w2_id"].to_string()],
|
||||
vec![row["l1_id"].to_string(), row["l2_id"].to_string()],
|
||||
]);
|
||||
let w1_id = players.get(&row["w1_id"]);
|
||||
let w2_id = players.get(&row["w2_id"]);
|
||||
|
||||
let l1_id = players.get(&row["l1_id"]);
|
||||
let l2_id = players.get(&row["l2_id"]);
|
||||
|
||||
composition.push(vec![vec![w1_id, w2_id], vec![l1_id, l2_id]]);
|
||||
} else {
|
||||
composition.push(vec![
|
||||
vec![row["w1_id"].to_string()],
|
||||
vec![row["l1_id"].to_string()],
|
||||
]);
|
||||
let w1_id = players.get(&row["w1_id"]);
|
||||
|
||||
let l1_id = players.get(&row["l1_id"]);
|
||||
|
||||
composition.push(vec![vec![w1_id], vec![l1_id]]);
|
||||
}
|
||||
|
||||
results.push(vec![1, 0]);
|
||||
@@ -37,6 +63,49 @@ fn main() {
|
||||
times.push(time as f64 / (60 * 60 * 24) as f64);
|
||||
}
|
||||
|
||||
/*
|
||||
let mut history = History::builder()
|
||||
.sigma(1.6)
|
||||
.gamma(0.036)
|
||||
.priors(priors)
|
||||
.build::<Time>();
|
||||
|
||||
history.add_event(teams: [[a, b], [c]], result: [1, 0], 0.0);
|
||||
|
||||
let mut history = History::builder()
|
||||
.sigma(1.6)
|
||||
.gamma(0.036)
|
||||
.priors(priors)
|
||||
.build::<NoTime>();
|
||||
|
||||
history.add_event(teams: [[a, b], [c]], result: [1, 0]);
|
||||
|
||||
history.convergence(Ctx::default());
|
||||
history.convergence(Ctx { epsilon: 1.6, ..Default::default() });
|
||||
history.convergence(Ctx { epsilon: 1.6, iterations: 10 });
|
||||
|
||||
history.convergence().run();
|
||||
|
||||
history
|
||||
.convergence()
|
||||
.epsilon(1.6)
|
||||
.run();
|
||||
|
||||
history
|
||||
.convergence()
|
||||
.epsilon(1.6)
|
||||
.iterations(10)
|
||||
.run();
|
||||
|
||||
history
|
||||
.convergence()
|
||||
.epsilon(1.6)
|
||||
.iterations(10)
|
||||
.inspect(|step, i| println!("Iteration {}: step={:?}", i, step))
|
||||
.run();
|
||||
|
||||
*/
|
||||
|
||||
let mut h = History::new(
|
||||
composition,
|
||||
results,
|
||||
@@ -53,87 +122,6 @@ fn main() {
|
||||
h.iterations = 10;
|
||||
|
||||
h.convergence();
|
||||
|
||||
/*
|
||||
composition,
|
||||
results,
|
||||
times,
|
||||
priors,
|
||||
MU,
|
||||
BETA,
|
||||
SIGMA,
|
||||
GAMMA,
|
||||
P_DRAW,
|
||||
|
||||
*/
|
||||
/*
|
||||
let mut priors = HashMap::new();
|
||||
|
||||
for k in ["aj", "bj", "cj"] {
|
||||
let player = Player::new(
|
||||
Gaussian::new(25.0, 25.0 / 3.0),
|
||||
25.0 / 6.0,
|
||||
0.15 * 25.0 / 3.0,
|
||||
N_INF,
|
||||
);
|
||||
|
||||
priors.insert(k.to_string(), player);
|
||||
}
|
||||
|
||||
let mut h1 = History::new(
|
||||
composition,
|
||||
results,
|
||||
times,
|
||||
priors,
|
||||
MU,
|
||||
BETA,
|
||||
SIGMA,
|
||||
GAMMA,
|
||||
P_DRAW,
|
||||
);
|
||||
*/
|
||||
|
||||
/*
|
||||
let columns = data
|
||||
.lines()
|
||||
.skip(1)
|
||||
.map(|line| {
|
||||
let columns = line.split(',').collect::<Vec<_>>();
|
||||
|
||||
Column {
|
||||
w1_id: columns[3],
|
||||
w2_id: columns[3],
|
||||
l1_id: columns[3],
|
||||
l2_id: columns[3],
|
||||
double: columns[3],
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
*/
|
||||
|
||||
// match_id,double,round_number,w1_id,w1_name,w2_id,w2_name,l1_id,l1_name,l2_id,l2_name,time_start,time_end,ground,tour_id,tour_name
|
||||
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
||||
|
||||
/*
|
||||
import pandas as pd
|
||||
#sudo pip3 install trueskillthroughtime
|
||||
from trueskillthroughtime import *
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
# Data
|
||||
df = pd.read_csv('input/history.csv', low_memory=False)
|
||||
|
||||
columns = zip(df.w1_id, df.w2_id, df.l1_id, df.l2_id, df.double)
|
||||
composition = [[[w1,w2],[l1,l2]] if d == 't' else [[w1],[l1]] for w1, w2, l1, l2, d in columns ]
|
||||
times = [ datetime.strptime(t, "%Y-%m-%d").timestamp()/(60*60*24) for t in df.time_start]
|
||||
|
||||
#start = time.time()
|
||||
h = History(composition = composition, times = times, sigma = 1.6, gamma = 0.036)
|
||||
h.convergence(epsilon=0.01, iterations=10)
|
||||
#end = time.time()
|
||||
#print(end-start)
|
||||
*/
|
||||
}
|
||||
|
||||
mod csv {
|
||||
|
||||
Reference in New Issue
Block a user