Remove unnecessary allocations
This commit is contained in:
50
src/batch.rs
50
src/batch.rs
@@ -1,4 +1,4 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
agent::Agent, game::Game, gaussian::Gaussian, player::Player, tuple_gt, tuple_max, Index, N_INF,
|
||||
@@ -116,29 +116,34 @@ impl Batch {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_events(
|
||||
pub fn add_events(
|
||||
&mut self,
|
||||
composition: Vec<Vec<Vec<Index>>>,
|
||||
results: Vec<Vec<f64>>,
|
||||
weights: Vec<Vec<Vec<f64>>>,
|
||||
agents: &HashMap<Index, Agent>,
|
||||
) {
|
||||
let this_agent = composition
|
||||
.iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.cloned()
|
||||
.collect::<HashSet<_>>();
|
||||
let mut unique = Vec::with_capacity(10);
|
||||
|
||||
let this_agent = composition.iter().flatten().flatten().filter(|idx| {
|
||||
if !unique.contains(idx) {
|
||||
unique.push(*idx);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
});
|
||||
|
||||
for idx in this_agent {
|
||||
let elapsed = compute_elapsed(agents[&idx].last_time, self.time);
|
||||
|
||||
if let Some(skill) = self.skills.get_mut(&idx) {
|
||||
if let Some(skill) = self.skills.get_mut(idx) {
|
||||
skill.elapsed = elapsed;
|
||||
skill.forward = agents[&idx].receive(elapsed);
|
||||
} else {
|
||||
self.skills.insert(
|
||||
idx,
|
||||
*idx,
|
||||
Skill {
|
||||
forward: agents[&idx].receive(elapsed),
|
||||
elapsed,
|
||||
@@ -172,14 +177,19 @@ impl Batch {
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let weights = if weights.is_empty() {
|
||||
teams
|
||||
.iter()
|
||||
.map(|team| vec![1.0; team.items.len()])
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
weights[e].clone()
|
||||
};
|
||||
|
||||
Event {
|
||||
teams,
|
||||
evidence: 0.0,
|
||||
weights: if weights.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
weights[e].clone()
|
||||
},
|
||||
weights,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -202,7 +212,7 @@ impl Batch {
|
||||
let teams = event.within_priors(false, false, &self.skills, agents);
|
||||
let result = event.outputs();
|
||||
|
||||
let g = Game::new(teams, result, event.weights.clone(), self.p_draw);
|
||||
let g = Game::new(teams, &result, &event.weights, self.p_draw);
|
||||
|
||||
for (t, team) in event.teams.iter_mut().enumerate() {
|
||||
for (i, item) in team.items.iter_mut().enumerate() {
|
||||
@@ -291,8 +301,8 @@ impl Batch {
|
||||
.map(|(_, event)| {
|
||||
Game::new(
|
||||
event.within_priors(online, forward, &self.skills, agents),
|
||||
event.outputs(),
|
||||
event.weights.clone(),
|
||||
&event.outputs(),
|
||||
&event.weights,
|
||||
self.p_draw,
|
||||
)
|
||||
.evidence
|
||||
@@ -316,8 +326,8 @@ impl Batch {
|
||||
.map(|(_, event)| {
|
||||
Game::new(
|
||||
event.within_priors(online, forward, &self.skills, agents),
|
||||
event.outputs(),
|
||||
event.weights.clone(),
|
||||
&event.outputs(),
|
||||
&event.weights,
|
||||
self.p_draw,
|
||||
)
|
||||
.evidence
|
||||
|
||||
Reference in New Issue
Block a user