Remove unused code

This commit is contained in:
2022-06-23 07:48:44 +02:00
parent ba2f8cffdc
commit d52895f804

View File

@@ -5,7 +5,7 @@ use crate::{
batch::{self, Batch}, batch::{self, Batch},
gaussian::Gaussian, gaussian::Gaussian,
player::Player, player::Player,
sort_time, tuple_gt, tuple_max, BETA, EPSILON, GAMMA, ITERATIONS, MU, P_DRAW, SIGMA, sort_time, tuple_gt, tuple_max, BETA, GAMMA, MU, P_DRAW, SIGMA,
}; };
#[derive(Clone)] #[derive(Clone)]
@@ -17,8 +17,6 @@ pub struct HistoryBuilder {
gamma: f64, gamma: f64,
p_draw: f64, p_draw: f64,
online: bool, online: bool,
epsilon: f64,
iterations: usize,
} }
impl HistoryBuilder { impl HistoryBuilder {
@@ -57,16 +55,6 @@ impl HistoryBuilder {
self self
} }
pub fn epsilon(mut self, epsilon: f64) -> Self {
self.epsilon = epsilon;
self
}
pub fn iterations(mut self, iterations: usize) -> Self {
self.iterations = iterations;
self
}
pub fn build(self) -> History { pub fn build(self) -> History {
History { History {
size: 0, size: 0,
@@ -79,8 +67,6 @@ impl HistoryBuilder {
gamma: self.gamma, gamma: self.gamma,
p_draw: self.p_draw, p_draw: self.p_draw,
online: self.online, online: self.online,
epsilon: self.epsilon,
iterations: self.iterations,
} }
} }
} }
@@ -95,8 +81,6 @@ impl Default for HistoryBuilder {
gamma: GAMMA, gamma: GAMMA,
p_draw: P_DRAW, p_draw: P_DRAW,
online: false, online: false,
epsilon: EPSILON,
iterations: ITERATIONS,
} }
} }
} }
@@ -112,8 +96,6 @@ pub struct History {
gamma: f64, gamma: f64,
p_draw: f64, p_draw: f64,
online: bool, online: bool,
epsilon: f64,
iterations: usize,
} }
impl Default for History { impl Default for History {
@@ -129,8 +111,6 @@ impl Default for History {
gamma: GAMMA, gamma: GAMMA,
p_draw: P_DRAW, p_draw: P_DRAW,
online: false, online: false,
epsilon: EPSILON,
iterations: ITERATIONS,
} }
} }
} }
@@ -140,163 +120,6 @@ impl History {
HistoryBuilder::default() HistoryBuilder::default()
} }
pub fn new(
composition: Vec<Vec<Vec<&str>>>,
results: Vec<Vec<f64>>,
times: Vec<u64>,
weights: Vec<Vec<Vec<f64>>>,
priors: HashMap<String, Player>,
mu: f64,
sigma: f64,
beta: f64,
gamma: f64,
p_draw: f64,
online: bool,
) -> Self {
assert!(
results.is_empty() || results.len() == composition.len(),
"(length(results) > 0) & (length(composition) != length(results))"
);
assert!(
times.is_empty() || times.len() == composition.len(),
"length(times) > 0) & (length(composition) != length(times))"
);
assert!(
weights.is_empty() || weights.len() == composition.len(),
"(length(weights) > 0) & (length(composition) != length(weights))"
);
let this_agent = composition
.iter()
.flatten()
.flatten()
.cloned()
.collect::<HashSet<_>>();
let agents = this_agent
.into_iter()
.map(|agent| {
let player = priors
.get(agent)
.cloned()
.unwrap_or_else(|| Player::new(Gaussian::new(mu, sigma), beta, gamma));
(
agent.to_string(),
Agent {
player,
..Default::default()
},
)
})
.collect::<HashMap<_, _>>();
let mut this = Self {
size: composition.len(),
batches: Vec::new(),
agents,
time: !times.is_empty(),
mu,
sigma,
beta,
gamma,
p_draw,
online,
epsilon: 0.0,
iterations: 10,
};
this.trueskill(composition, results, times, weights, online);
this
}
fn trueskill(
&mut self,
composition: Vec<Vec<Vec<&str>>>,
results: Vec<Vec<f64>>,
times: Vec<u64>,
weights: Vec<Vec<Vec<f64>>>,
online: bool,
) {
let o = if self.time {
sort_time(&times, false)
} else {
(0..composition.len()).collect::<Vec<_>>()
};
let mut i = 0;
let mut last = 0.0;
while i < self.size {
let mut j = i + 1;
let t = if self.time { times[o[i]] } else { i as u64 + 1 };
while self.time && j < self.size && times[o[j]] == t {
j += 1;
}
let composition = (i..j)
.map(|e| composition[o[e]].clone())
.collect::<Vec<_>>();
let results = if results.is_empty() {
Vec::new()
} else {
(i..j).map(|e| results[o[e]].clone()).collect::<Vec<_>>()
};
let weights = if weights.is_empty() {
Vec::new()
} else {
(i..j).map(|e| weights[o[e]].clone()).collect::<Vec<_>>()
};
let b = Batch::new(
composition,
results,
weights,
t,
self.p_draw,
&mut self.agents,
);
let idx = self.batches.len();
self.batches.push(b);
if online {
let new = 100.0 * (i as f64 / self.size as f64);
if new != last {
println!("{:.02}%", new);
last = new;
}
for skill in self.batches[idx].skills.values_mut() {
skill.online = skill.forward;
}
self.convergence(self.iterations, self.epsilon, false);
}
let b = &mut self.batches[idx];
for a in b.skills.keys() {
let agent = self.agents.get_mut(a).unwrap();
agent.last_time = if self.time { t } else { u64::MAX };
agent.message = b.forward_prior_out(a);
}
i = j;
}
if online {
println!("100.00%");
}
}
fn iteration(&mut self) -> (f64, f64) { fn iteration(&mut self) -> (f64, f64) {
let mut step = (0.0, 0.0); let mut step = (0.0, 0.0);
@@ -588,7 +411,7 @@ impl History {
mod tests { mod tests {
use approx::assert_ulps_eq; use approx::assert_ulps_eq;
use crate::{Game, Gaussian, Player, BETA, EPSILON, GAMMA, ITERATIONS, MU, P_DRAW, SIGMA}; use crate::{Game, Gaussian, Player, EPSILON, ITERATIONS, P_DRAW};
use super::*; use super::*;