Refactor some code
This commit is contained in:
@@ -107,9 +107,9 @@ fn main() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
let mut h = History::new(
|
let mut h = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
times,
|
×,
|
||||||
HashMap::new(),
|
HashMap::new(),
|
||||||
MU,
|
MU,
|
||||||
1.6,
|
1.6,
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ impl Agent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn receive(&self, elapsed: f64) -> Gaussian {
|
pub fn receive(&self, elapsed: f64) -> Gaussian {
|
||||||
if self.message != N_INF {
|
if self.message != N_INF {
|
||||||
self.message.forget(self.player.gamma, elapsed)
|
self.message.forget(self.player.gamma, elapsed)
|
||||||
@@ -191,12 +192,14 @@ impl Batch {
|
|||||||
self.iteration(from, agents);
|
self.iteration(from, agents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn posterior(&self, agent: &PlayerIndex) -> Gaussian {
|
pub fn posterior(&self, agent: &PlayerIndex) -> Gaussian {
|
||||||
let skill = &self.skills[agent];
|
let skill = &self.skills[agent];
|
||||||
|
|
||||||
skill.likelihood * skill.backward * skill.forward
|
skill.likelihood * skill.backward * skill.forward
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn posteriors(&self) -> HashMap<PlayerIndex, Gaussian> {
|
pub fn posteriors(&self) -> HashMap<PlayerIndex, Gaussian> {
|
||||||
self.skills
|
self.skills
|
||||||
.keys()
|
.keys()
|
||||||
@@ -204,6 +207,7 @@ impl Batch {
|
|||||||
.collect::<HashMap<_, _>>()
|
.collect::<HashMap<_, _>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn within_prior(&self, item: &Item, agents: &mut HashMap<PlayerIndex, Agent>) -> Player {
|
fn within_prior(&self, item: &Item, agents: &mut HashMap<PlayerIndex, Agent>) -> Player {
|
||||||
let r = &agents[&item.index].player;
|
let r = &agents[&item.index].player;
|
||||||
let g = self.posterior(&item.index) / item.likelihood;
|
let g = self.posterior(&item.index) / item.likelihood;
|
||||||
@@ -278,12 +282,14 @@ impl Batch {
|
|||||||
i
|
i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn forward_prior_out(&self, agent: &PlayerIndex) -> Gaussian {
|
pub fn forward_prior_out(&self, agent: &PlayerIndex) -> Gaussian {
|
||||||
let skill = &self.skills[agent];
|
let skill = &self.skills[agent];
|
||||||
|
|
||||||
skill.forward * skill.likelihood
|
skill.forward * skill.likelihood
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn backward_prior_out(
|
pub fn backward_prior_out(
|
||||||
&self,
|
&self,
|
||||||
agent: &PlayerIndex,
|
agent: &PlayerIndex,
|
||||||
@@ -295,6 +301,7 @@ impl Batch {
|
|||||||
n.forget(agents[agent].player.gamma, skill.elapsed)
|
n.forget(agents[agent].player.gamma, skill.elapsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn new_backward_info(&mut self, agents: &mut HashMap<PlayerIndex, Agent>) {
|
pub fn new_backward_info(&mut self, agents: &mut HashMap<PlayerIndex, Agent>) {
|
||||||
for (agent, skill) in self.skills.iter_mut() {
|
for (agent, skill) in self.skills.iter_mut() {
|
||||||
skill.backward = agents[agent].message;
|
skill.backward = agents[agent].message;
|
||||||
@@ -303,6 +310,7 @@ impl Batch {
|
|||||||
self.iteration(0, agents);
|
self.iteration(0, agents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn new_forward_info(&mut self, agents: &mut HashMap<PlayerIndex, Agent>) {
|
pub fn new_forward_info(&mut self, agents: &mut HashMap<PlayerIndex, Agent>) {
|
||||||
for (agent, skill) in self.skills.iter_mut() {
|
for (agent, skill) in self.skills.iter_mut() {
|
||||||
skill.forward = agents[agent].receive(skill.elapsed);
|
skill.forward = agents[agent].receive(skill.elapsed);
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ impl Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let r = &self.result;
|
let r = &self.result;
|
||||||
let o = utils::sortperm(r, true);
|
let o = utils::sort_perm(r);
|
||||||
|
|
||||||
let t = (0..self.teams.len())
|
let t = (0..self.teams.len())
|
||||||
.map(|e| TeamVariable {
|
.map(|e| TeamVariable {
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ pub struct History {
|
|||||||
size: usize,
|
size: usize,
|
||||||
batches: Vec<Batch>,
|
batches: Vec<Batch>,
|
||||||
agents: HashMap<PlayerIndex, Agent>,
|
agents: HashMap<PlayerIndex, Agent>,
|
||||||
mu: f64,
|
// mu: f64,
|
||||||
sigma: f64,
|
// sigma: f64,
|
||||||
gamma: f64,
|
// beta: f64,
|
||||||
|
// gamma: f64,
|
||||||
p_draw: f64,
|
p_draw: f64,
|
||||||
time: bool,
|
time: bool,
|
||||||
pub epsilon: f64,
|
pub epsilon: f64,
|
||||||
@@ -18,9 +19,9 @@ pub struct History {
|
|||||||
|
|
||||||
impl History {
|
impl History {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
composition: Vec<Vec<Vec<PlayerIndex>>>,
|
composition: &[Vec<Vec<PlayerIndex>>],
|
||||||
results: Vec<Vec<u16>>,
|
results: &[Vec<u16>],
|
||||||
times: Vec<f64>,
|
times: &[f64],
|
||||||
priors: HashMap<PlayerIndex, Player>,
|
priors: HashMap<PlayerIndex, Player>,
|
||||||
mu: f64,
|
mu: f64,
|
||||||
sigma: f64,
|
sigma: f64,
|
||||||
@@ -57,9 +58,6 @@ impl History {
|
|||||||
size: composition.len(),
|
size: composition.len(),
|
||||||
batches: Vec::new(),
|
batches: Vec::new(),
|
||||||
agents,
|
agents,
|
||||||
mu,
|
|
||||||
sigma,
|
|
||||||
gamma,
|
|
||||||
p_draw,
|
p_draw,
|
||||||
time: !times.is_empty(),
|
time: !times.is_empty(),
|
||||||
epsilon: 1e-6,
|
epsilon: 1e-6,
|
||||||
@@ -74,12 +72,12 @@ impl History {
|
|||||||
|
|
||||||
fn trueskill(
|
fn trueskill(
|
||||||
&mut self,
|
&mut self,
|
||||||
composition: Vec<Vec<Vec<PlayerIndex>>>,
|
composition: &[Vec<Vec<PlayerIndex>>],
|
||||||
results: Vec<Vec<u16>>,
|
results: &[Vec<u16>],
|
||||||
times: Vec<f64>,
|
times: &[f64],
|
||||||
) {
|
) {
|
||||||
let o = if self.time {
|
let o = if self.time {
|
||||||
utils::sort_time(×, false)
|
utils::sort_time(times)
|
||||||
} else {
|
} else {
|
||||||
(0..composition.len()).collect::<Vec<_>>()
|
(0..composition.len()).collect::<Vec<_>>()
|
||||||
};
|
};
|
||||||
@@ -88,13 +86,13 @@ impl History {
|
|||||||
|
|
||||||
while i < self.size {
|
while i < self.size {
|
||||||
let mut j = i + 1;
|
let mut j = i + 1;
|
||||||
let t = if self.time {
|
let time = if self.time {
|
||||||
times[o[i]]
|
times[o[i]]
|
||||||
} else {
|
} else {
|
||||||
i as f64 + 1.0
|
i as f64 + 1.0
|
||||||
};
|
};
|
||||||
|
|
||||||
while self.time && j < self.size && times[o[j]] == t {
|
while self.time && j < self.size && times[o[j]] == time {
|
||||||
j += 1;
|
j += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,20 +102,17 @@ impl History {
|
|||||||
|
|
||||||
let results = (i..j).map(|e| results[o[e]].clone()).collect::<Vec<_>>();
|
let results = (i..j).map(|e| results[o[e]].clone()).collect::<Vec<_>>();
|
||||||
|
|
||||||
let b = Batch::new(
|
let b = Batch::new(composition, results, time, &mut self.agents, self.p_draw);
|
||||||
composition,
|
|
||||||
results,
|
|
||||||
t as f64,
|
|
||||||
&mut self.agents,
|
|
||||||
self.p_draw,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.batches.push(b.clone());
|
self.batches.push(b);
|
||||||
|
|
||||||
|
let idx = self.batches.len() - 1;
|
||||||
|
let b = &mut self.batches[idx];
|
||||||
|
|
||||||
for a in b.skills.keys() {
|
for a in b.skills.keys() {
|
||||||
let agent = self.agents.get_mut(a).unwrap();
|
let agent = self.agents.get_mut(a).unwrap();
|
||||||
|
|
||||||
agent.last_time = if self.time { t as f64 } else { f64::INFINITY };
|
agent.last_time = if self.time { time } else { f64::INFINITY };
|
||||||
agent.message = b.forward_prior_out(a);
|
agent.message = b.forward_prior_out(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,9 +280,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut h = History::new(
|
let mut h = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
vec![1.0, 2.0, 3.0],
|
&[1.0, 2.0, 3.0],
|
||||||
priors,
|
priors,
|
||||||
MU,
|
MU,
|
||||||
BETA,
|
BETA,
|
||||||
@@ -348,9 +343,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut h1 = History::new(
|
let mut h1 = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
times,
|
×,
|
||||||
priors,
|
priors,
|
||||||
MU,
|
MU,
|
||||||
BETA,
|
BETA,
|
||||||
@@ -425,9 +420,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut h2 = History::new(
|
let mut h2 = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
times,
|
×,
|
||||||
priors,
|
priors,
|
||||||
MU,
|
MU,
|
||||||
BETA,
|
BETA,
|
||||||
@@ -509,9 +504,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut h = History::new(
|
let mut h = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
times,
|
×,
|
||||||
priors,
|
priors,
|
||||||
MU,
|
MU,
|
||||||
BETA,
|
BETA,
|
||||||
@@ -566,9 +561,9 @@ mod tests {
|
|||||||
let results = vec![vec![1, 0], vec![0, 1], vec![1, 0]];
|
let results = vec![vec![1, 0], vec![0, 1], vec![1, 0]];
|
||||||
|
|
||||||
let mut h = History::new(
|
let mut h = History::new(
|
||||||
composition,
|
&composition,
|
||||||
results,
|
&results,
|
||||||
Vec::new(),
|
&[],
|
||||||
HashMap::new(),
|
HashMap::new(),
|
||||||
25.0,
|
25.0,
|
||||||
25.0 / 3.0,
|
25.0 / 3.0,
|
||||||
|
|||||||
20
src/utils.rs
20
src/utils.rs
@@ -144,26 +144,20 @@ pub(crate) fn compute_margin(p_draw: f64, sd: f64) -> f64 {
|
|||||||
ppf(0.5 - p_draw / 2.0, 0.0, sd).abs()
|
ppf(0.5 - p_draw / 2.0, 0.0, sd).abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn sortperm<T: Ord>(xs: &[T], reverse: bool) -> Vec<usize> {
|
#[inline]
|
||||||
|
pub(crate) fn sort_perm(xs: &[u16]) -> Vec<usize> {
|
||||||
let mut x = xs.iter().enumerate().collect::<Vec<_>>();
|
let mut x = xs.iter().enumerate().collect::<Vec<_>>();
|
||||||
|
|
||||||
if reverse {
|
|
||||||
x.sort_unstable_by_key(|(_, x)| Reverse(*x));
|
x.sort_unstable_by_key(|(_, x)| Reverse(*x));
|
||||||
} else {
|
|
||||||
x.sort_unstable_by_key(|(_, x)| *x);
|
|
||||||
}
|
|
||||||
|
|
||||||
x.into_iter().map(|(i, _)| i).collect()
|
x.into_iter().map(|(i, _)| i).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn sort_time(xs: &[f64], reverse: bool) -> Vec<usize> {
|
#[inline]
|
||||||
|
pub(crate) fn sort_time(xs: &[f64]) -> Vec<usize> {
|
||||||
let mut x = xs.iter().enumerate().collect::<Vec<_>>();
|
let mut x = xs.iter().enumerate().collect::<Vec<_>>();
|
||||||
|
|
||||||
if reverse {
|
|
||||||
x.sort_unstable_by(|(_, a), (_, b)| b.partial_cmp(a).unwrap());
|
|
||||||
} else {
|
|
||||||
x.sort_unstable_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
|
x.sort_unstable_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
|
||||||
}
|
|
||||||
|
|
||||||
x.into_iter().map(|(i, _)| i).collect()
|
x.into_iter().map(|(i, _)| i).collect()
|
||||||
}
|
}
|
||||||
@@ -244,13 +238,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sortperm() {
|
fn test_sortperm() {
|
||||||
assert_eq!(sortperm(&[0, 1, 2, 0], true), vec![2, 1, 0, 3]);
|
assert_eq!(sort_perm(&[0, 1, 2, 0]), vec![2, 1, 0, 3]);
|
||||||
assert_eq!(sortperm(&[1, 1, 1], false), vec![0, 1, 2]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sort_time() {
|
fn test_sort_time() {
|
||||||
assert_eq!(sort_time(&[0.0, 1.0, 2.0, 0.0], true), vec![2, 1, 0, 3]);
|
assert_eq!(sort_time(&[1.0, 1.0, 1.0]), vec![0, 1, 2]);
|
||||||
assert_eq!(sort_time(&[1.0, 1.0, 1.0], false), vec![0, 1, 2]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user