pub fn sample_joint(&self) -> Option<Vec<usize>> {
        let node_order = self.forest_topo_sort()?;
        let mut inboxes = self.make_inboxes();
        self.propagate_messages(node_order.iter().rev(), &mut inboxes);
        // now sample in topological order
        // the "messages" in this phase are conditioned potentials
        let mut assignments = Vec::with_capacity(self.n_nodes());
        let mut visited = Vec::with_capacity(self.n_nodes());
        for _ in 0..self.n_nodes() {
            assignments.push(0usize);
            visited.push(false);
        }
        for &node in node_order.iter() {
            visited[node] = true;
            // sample node
            // get potentials
            // compute a message as `fixed_v1` for each unvisited edge
            let conditional = inboxes[node]
                .iter()
                .fold(self.node_potential(node).clone(), |p, msg| {
                    p.update_potentials(&msg.1)
                });
            let sample_val = sample(&conditional.to_distribution());
            assignments[node] = sample_val;
            for &neighbor in self.neighbors(node).iter() {
                if !visited[neighbor] {
                    inboxes[neighbor].push((
                        node,
                        self.edge_potential(node, neighbor)
                            .unwrap()
                            .fixed_v1(sample_val),
                    ));
                }
            }
        }
        Some(assignments)
    }
/// Sample from the joint distribution of all nodes in the graph
/// Requires the graph to be a forest
Sample from the joint distribution of all nodes in the graph
Requires the graph to be a forest
Sample from the joint distribution of all nodes in the graph Requires the graph to be a forest
pub fn sample_joint(&self) -> Option<Vec<usize>> {
        let node_order = self.forest_topo_sort()?;
        let mut inboxes = self.make_inboxes();
        self.propagate_messages(node_order.iter().rev(), &mut inboxes);
        // now sample in topological order
        // the "messages" in this phase are conditioned potentials
        let mut assignments = Vec::with_capacity(self.n_nodes());
        let mut visited = Vec::with_capacity(self.n_nodes());
        for _ in 0..self.n_nodes() {
            assignments.push(0usize);
            visited.push(false);
        }
        for &node in node_order.iter() {
            visited[node] = true;
            // sample node
            // get potentials
            // compute a message as `fixed_v1` for each unvisited edge
            let conditional = inboxes[node]
                .iter()
                .fold(self.node_potential(node).clone(), |p, msg| {
                    p.update_potentials(&msg.1)
                });
            let sample_val = sample(&conditional.to_distribution());
            assignments[node] = sample_val;
            for &neighbor in self.neighbors(node).iter() {
                if !visited[neighbor] {
                    inboxes[neighbor].push((
                        node,
                        self.edge_potential(node, neighbor)
                            .unwrap()
                            .fixed_v1(sample_val),
                    ));
                }
            }
        }
        Some(assignments)
    }
pub fn sample_joint ( & self ) -> Option < Vec < usize > > { let node_order = self . forest_topo_sort ( ) ? ; let mut inboxes = self . make_inboxes ( ) ; self . propagate_messages ( node_order . iter ( ) . rev ( ) , & mut inboxes ) ; // now sample in topological order // the "messages" in this phase are conditioned potentials let mut assignments = Vec :: with_capacity ( self . n_nodes ( ) ) ; let mut visited = Vec :: with_capacity ( self . n_nodes ( ) ) ; for _ in 0 .. self . n_nodes ( ) { assignments . push ( 0usize ) ; visited . push ( false ) ; } for & node in node_order . iter ( ) { visited [ node ] = true ; // sample node // get potentials // compute a message as `fixed_v1` for each unvisited edge let conditional = inboxes [ node ] . iter ( ) . fold ( self . node_potential ( node ) . clone ( ) , | p , msg | { p . update_potentials ( & msg . 1 ) } ) ; let sample_val = sample ( & conditional . to_distribution ( ) ) ; assignments [ node ] = sample_val ; for & neighbor in self . neighbors ( node ) . iter ( ) { if ! visited [ neighbor ] { inboxes [ neighbor ] . push ( ( node , self . edge_potential ( node , neighbor ) . unwrap ( ) . fixed_v1 ( sample_val ) , ) ) ; } } } Some ( assignments ) }
Sample from the joint distribution of all nodes in the graph
Requires the graph to be a forest
Sample from the joint distribution of all nodes in the graph Requires the graph to be a forest
&self