use crate::constellation::Constellation;
use crate::NNVFloat;
use ndarray::{Array1, Dimension, Ix2};
use ordered_float::OrderedFloat;
use rand::{thread_rng, Rng};
use std::collections::BinaryHeap;
use std::convert::TryInto;
#[derive(Debug)]
struct Belt<'a, T: NNVFloat, D: Dimension> {
constellation: &'a mut Constellation<T, D>,
frontier: BinaryHeap<(OrderedFloat<T>, usize)>, leaves: Vec<(OrderedFloat<T>, usize)>,
}
impl<'a, T: NNVFloat, D: Dimension> Belt<'a, T, D> {
pub fn new(constellation: &'a mut Constellation<T, D>) -> Self {
let root_id = constellation.get_root_id();
Self {
constellation,
frontier: BinaryHeap::from(vec![(OrderedFloat::one(), root_id)]),
leaves: Vec::new(),
}
}
}
impl<'a, T: crate::NNVFloat> Belt<'a, T, Ix2> {
pub fn expand<R: Rng>(&mut self, rng: &mut R, stability_eps: T) -> bool {
if let Some((_weight, next)) = self.frontier.pop() {
let children = self.constellation.get_node_child_ids(next, stability_eps);
for idx in children {
let cdf = self
.constellation
.get_node_cdf(idx, 10, 10, rng, stability_eps);
let bounds = self.constellation.get_node_output_bounds(idx);
let f = bounds.0.abs().max(bounds.1.abs()); let entry = (std::convert::From::from(f * cdf), idx);
if self.constellation.is_node_leaf(idx, stability_eps) {
self.leaves.push(entry);
} else {
self.frontier.push(entry);
}
}
true
} else {
false
}
}
pub fn importance_sample(&mut self, n_samples: T, stability_eps: T) -> T {
let mut rng = thread_rng();
let total_weight: T = self
.frontier
.iter()
.map(|(weight, _idx)| weight)
.sum::<OrderedFloat<T>>()
.0;
self.frontier
.clone()
.iter()
.chain(self.leaves.clone().iter())
.map(|(weight, idx)| {
let star_prob = weight.0 / total_weight;
let n_local_samples = ((star_prob * n_samples).into() as u64).try_into().unwrap();
let local_samples: Vec<Array1<T>> = self
.constellation
.sample_gaussian_node_output(*idx, &mut rng, n_local_samples, 10, stability_eps)
.into_iter()
.collect();
let local_sum: T = local_samples.iter().map(|x| x[[0]]).sum();
let local_mean: T = local_sum / (local_samples.len() as f64).into();
local_mean * weight.0
})
.sum::<T>()
/ total_weight
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::test_util::*;
use proptest::*;
proptest! {
#[test]
fn test_perform_importance_sample(mut constellation in constellation(2, 1, 2, 2)) {
let mut belt = Belt::new(&mut constellation);
belt.importance_sample(1.0, 1e-4);
}
#[test]
fn test_multiple_beltloops(mut constellation in constellation(2, 2, 2, 2)) {
}
}
}