Remove unused mut reference

This commit is contained in:
2022-12-27 22:11:04 +01:00
parent b93194f762
commit fdddf56156
4 changed files with 163 additions and 3 deletions

View File

@@ -203,7 +203,7 @@ impl Batch {
composition: Vec<Vec<Vec<Index>>>,
results: Vec<Vec<f64>>,
weights: Vec<Vec<Vec<f64>>>,
agents: &mut HashMap<Index, Agent>,
agents: &HashMap<Index, Agent>,
) {
let this_agent = composition
.iter()

159
src/gaussian2.rs Normal file
View File

@@ -0,0 +1,159 @@
use std::ops;
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Gaussian {
mu: f64,
sigma: f64,
}
impl Gaussian {
#[inline(always)]
pub const fn from_ms(mu: f64, sigma: f64) -> Self {
Self { mu, sigma }
}
#[inline(always)]
pub fn from_pt(pi: f64, tau: f64) -> Self {
Self::from_ms(tau / pi, (1.0 / pi).sqrt())
}
#[inline(always)]
pub fn mu(&self) -> f64 {
self.mu
}
#[inline(always)]
pub fn sigma(&self) -> f64 {
self.sigma
}
#[inline(always)]
pub fn pi(&self) -> f64 {
if self.sigma > 0.0 {
self.sigma.powi(-2)
} else {
f64::INFINITY
}
}
#[inline(always)]
pub fn tau(&self) -> f64 {
if self.sigma > 0.0 {
self.mu * self.pi()
} else {
f64::INFINITY
}
}
}
impl ops::Add<Gaussian> for Gaussian {
type Output = Gaussian;
fn add(self, rhs: Gaussian) -> Self::Output {
Self {
mu: self.mu + rhs.mu,
sigma: (self.sigma.powi(2) + rhs.sigma.powi(2)).sqrt(),
}
}
}
impl ops::Sub<Gaussian> for Gaussian {
type Output = Gaussian;
fn sub(self, rhs: Gaussian) -> Self::Output {
Self {
mu: self.mu - rhs.mu,
sigma: (self.sigma.powi(2) + rhs.sigma.powi(2)).sqrt(),
}
}
}
impl ops::Mul<Gaussian> for Gaussian {
type Output = Gaussian;
fn mul(self, rhs: Gaussian) -> Self::Output {
/*
if self.sigma == 0.0 || rhs.sigma == 0.0 {
let mu = self.mu / (self.sigma.powi(2) / rhs.sigma.powi(2) + 1.0)
+ rhs.mu / (rhs.sigma.powi(2) / self.sigma.powi(2) + 1.0);
let sigma = (1.0 / ((1.0 / self.sigma.powi(2)) + (1.0 / rhs.sigma.powi(2)))).sqrt();
Self::from_ms(mu, sigma)
} else {
Self::from_pt(self.pi() + rhs.pi(), self.tau() + rhs.tau())
}
*/
Self::from_pt(self.pi() + rhs.pi(), self.tau() + rhs.tau())
}
}
impl ops::Div<Gaussian> for Gaussian {
type Output = Gaussian;
fn div(self, rhs: Gaussian) -> Self::Output {
/*
let (mu, sigma) = if self.sigma == 0.0 || rhs.sigma == 0.0 {
let mu = self.mu / (1.0 - self.sigma.powi(2) / rhs.sigma.powi(2))
+ rhs.mu / (rhs.sigma.powi(2) / self.sigma.powi(2) - 1.0);
let sigma = (1.0 / ((1.0 / self.sigma.powi(2)) - (1.0 / rhs.sigma.powi(2)))).sqrt();
Self::from_ms(mu, sigma)
} else {
Self::from_pt(self.pi() - rhs.pi(), self.tau() - rhs.tau())
}
*/
Self::from_pt(self.pi() - rhs.pi(), self.tau() - rhs.tau())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
let n = Gaussian::from_ms(25.0, 25.0 / 3.0);
let m = Gaussian::from_ms(0.0, 1.0);
assert_eq!(n + m, Gaussian::from_ms(25.0, 8.393118874676116));
}
#[test]
fn test_sub() {
let n = Gaussian::from_ms(25.0, 25.0 / 3.0);
let m = Gaussian::from_ms(1.0, 1.0);
assert_eq!(n - m, Gaussian::from_ms(24.0, 8.393118874676116));
}
#[test]
fn test_mul() {
let n = Gaussian::from_ms(25.0, 25.0 / 3.0);
let m = Gaussian::from_ms(0.0, 1.0);
assert_eq!(
n * m,
Gaussian::from_ms(0.35488958990536273, 0.992876838486922)
);
}
#[test]
fn test_div() {
let n = Gaussian::from_ms(25.0, 25.0 / 3.0);
let m = Gaussian::from_ms(0.0, 1.0);
assert_eq!(
m / n,
Gaussian::from_ms(-0.3652597402597402, 1.0072787050317253)
);
assert_eq!(
n / m,
Gaussian::from_ms(-0.3652597402597402, 1.0072787050317253)
);
}
}

View File

@@ -87,7 +87,7 @@ impl Default for HistoryBuilder {
pub struct History {
size: usize,
pub batches: Vec<Batch>,
pub(crate) batches: Vec<Batch>,
agents: HashMap<Index, Agent>,
time: bool,
mu: f64,
@@ -354,7 +354,7 @@ impl History {
if self.time && self.batches.len() > k && self.batches[k].time == t {
let b = &mut self.batches[k];
b.add_events(composition, results, weights, &mut self.agents);
b.add_events(composition, results, weights, &self.agents);
for a in b.skills.keys() {
let agent = self.agents.get_mut(a).unwrap();

View File

@@ -10,6 +10,7 @@ mod approx;
pub mod batch;
mod game;
pub mod gaussian;
// mod gaussian2;
mod history;
mod message;
pub mod player;