use crate::num::One;
use crate::starsets::Constellation;
use crate::starsets::StarSet;
use crate::NNVFloat;
use ndarray::{Array1, Dimension, Ix2};
use ordered_float::OrderedFloat;
use rand::{thread_rng, Rng};
use std::collections::BinaryHeap;
#[derive(Debug)]
struct Belt<'a, D: Dimension> {
constellation: &'a mut Constellation<D>,
frontier: BinaryHeap<(OrderedFloat<NNVFloat>, usize)>, leaves: Vec<(OrderedFloat<NNVFloat>, usize)>,
}
impl<'a, D: Dimension> Belt<'a, D> {
pub fn new(constellation: &'a mut Constellation<D>) -> Self {
let root_id = constellation.get_root_id();
Self {
constellation,
frontier: BinaryHeap::from(vec![(OrderedFloat::one(), root_id)]),
leaves: Vec::new(),
}
}
}
impl<'a> Belt<'a, Ix2> {
pub fn expand<R: Rng>(&mut self, rng: &mut R, stability_eps: NNVFloat) -> 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: NNVFloat, stability_eps: NNVFloat) -> NNVFloat {
let mut rng = thread_rng();
let total_weight: NNVFloat = self
.frontier
.iter()
.map(|(weight, _idx)| weight)
.sum::<OrderedFloat<NNVFloat>>()
.0;
self.frontier
.clone()
.iter()
.chain(self.leaves.clone().iter())
.map(|(weight, idx)| {
let star_prob = weight.0 / total_weight;
let n_local_samples: usize = (star_prob * n_samples) as usize;
let local_samples: Vec<Array1<NNVFloat>> = self
.constellation
.sample_gaussian_node_output(*idx, &mut rng, n_local_samples, 10, stability_eps)
.into_iter()
.collect();
let local_sum: NNVFloat = local_samples.iter().map(|x| x[[0]]).sum();
let local_mean: NNVFloat = local_sum / (local_samples.len() as NNVFloat);
local_mean * weight.0
})
.sum::<NNVFloat>()
/ 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)) {
}
}
}