More rustifying

This commit is contained in:
2022-12-27 22:30:20 +01:00
parent 9b6cb9e7eb
commit 8e25826f91

View File

@@ -110,7 +110,7 @@ pub struct Batch {
impl Batch { impl Batch {
pub fn new( pub fn new(
compositions: Vec<Vec<Vec<Index>>>, composition: Vec<Vec<Vec<Index>>>,
results: Vec<Vec<f64>>, results: Vec<Vec<f64>>,
weights: Vec<Vec<Vec<f64>>>, weights: Vec<Vec<Vec<f64>>>,
time: i64, time: i64,
@@ -118,15 +118,15 @@ impl Batch {
agents: &HashMap<Index, Agent>, agents: &HashMap<Index, Agent>,
) -> Self { ) -> Self {
assert!( assert!(
results.is_empty() || results.len() == compositions.len(), results.is_empty() || results.len() == composition.len(),
"TODO: Add a comment here" "TODO: Add a comment here"
); );
assert!( assert!(
weights.is_empty() || weights.len() == compositions.len(), weights.is_empty() || weights.len() == composition.len(),
"TODO: Add a comment here" "TODO: Add a comment here"
); );
let this_agent = compositions let this_agent = composition
.iter() .iter()
.flatten() .flatten()
.flatten() .flatten()
@@ -148,13 +148,13 @@ impl Batch {
}) })
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
let events = compositions let events = composition
.iter() .iter()
.enumerate() // (0..composition.len()) .enumerate()
.map(|(e, composition)| { .map(|(e, event)| {
let teams = composition let teams = event
.iter() .iter()
.enumerate() // (0..composition.len()) .enumerate()
.map(|(t, team)| { .map(|(t, team)| {
let items = team let items = team
.iter() .iter()
@@ -167,7 +167,7 @@ impl Batch {
Team { Team {
items, items,
output: if results.is_empty() { output: if results.is_empty() {
(composition.len() - (t + 1)) as f64 (event.len() - (t + 1)) as f64
} else { } else {
results[e][t] results[e][t]
}, },
@@ -231,14 +231,15 @@ impl Batch {
} }
} }
let from = self.events.len(); let events = composition.iter().enumerate().map(|(e, event)| {
let teams = event
for e in 0..composition.len() { .iter()
let teams = (0..composition[e].len()) .enumerate()
.map(|t| { .map(|(t, team)| {
let items = (0..composition[e][t].len()) let items = team
.map(|a| Item { .iter()
agent: composition[e][t][a], .map(|&agent| Item {
agent,
likelihood: N_INF, likelihood: N_INF,
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -246,7 +247,7 @@ impl Batch {
Team { Team {
items, items,
output: if results.is_empty() { output: if results.is_empty() {
(composition[e].len() - (t + 1)) as f64 (event.len() - (t + 1)) as f64
} else { } else {
results[e][t] results[e][t]
}, },
@@ -254,7 +255,7 @@ impl Batch {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let event = Event { Event {
teams, teams,
evidence: 0.0, evidence: 0.0,
weights: if weights.is_empty() { weights: if weights.is_empty() {
@@ -262,10 +263,12 @@ impl Batch {
} else { } else {
weights[e].clone() weights[e].clone()
}, },
};
self.events.push(event);
} }
});
let from = self.events.len();
self.events.extend(events);
self.iteration(from, agents); self.iteration(from, agents);
} }
@@ -615,7 +618,7 @@ mod tests {
], ],
vec![vec![1.0, 0.0], vec![0.0, 1.0], vec![1.0, 0.0]], vec![vec![1.0, 0.0], vec![0.0, 1.0], vec![1.0, 0.0]],
vec![], vec![],
&mut agents, &agents,
); );
assert_eq!(batch.events.len(), 6); assert_eq!(batch.events.len(), 6);