Exemple #1
0
 def get_bbn_potential(self, node):
     """
     Gets the potential associated with the specified BBN node.
     :param node: BBN node.
     :return: Potential.
     """
     clique = node.metadata['parent.clique']
     return PotentialUtil.normalize(
         PotentialUtil.marginalize_for(self, clique, [node]))
Exemple #2
0
 def start(self):
     """
     Starts the evidence distribution.
     """
     self.start_clique.mark()
     for sep_set_id in self.join_tree.get_neighbors(self.start_clique.id):
         sep_set = self.join_tree.get_node(sep_set_id)
         for clique_id in self.join_tree.get_neighbors(sep_set_id):
             y = self.join_tree.get_node(clique_id)
             if not y.is_marked():
                 PotentialUtil.pass_single_message(self.join_tree,
                                                   self.start_clique,
                                                   sep_set, y)
                 self.__walk__(self.start_clique, sep_set, y)
    def start(self):
        """
        Starts the evidence distribution.
        """
        self.start_clique.mark()
        sepsets, cliques = self.__get_neighboring_cliques__(
            self.join_tree, self.start_clique)

        for clique in cliques:
            clique[1].mark()

        for s, c in zip(sepsets, cliques):
            PotentialUtil.pass_single_message(self.join_tree,
                                              self.start_clique, s[1], c[1])
            self.__walk__(self.start_clique, s[1], c[1])
 def __walk__(self, x, s, y):
     """
     Walks away from the specified x clique.
     :param x: Clique.
     :param s: Separation-set.
     :param y: Clique.
     """
     y.mark()
     for sep_set_id in self.join_tree.get_neighbors(y.id):
         sep_set = self.join_tree.get_node(sep_set_id)
         for clique_id in self.join_tree.get_neighbors(sep_set_id):
             clique = self.join_tree.get_node(clique_id)
             if not clique.is_marked():
                 self.__walk__(y, sep_set, clique)
     PotentialUtil.pass_single_message(self.join_tree, y, s, x)
Exemple #5
0
 def init(bbn):
     """
     Initializes the BBN potentials.
     :param bbn: BBN graph.
     """
     for node in bbn.get_nodes():
         parents = [bbn.get_node(parent_id) for parent_id in bbn.get_parents_ordered(node.id)]
         potential = PotentialUtil.get_potential(node, parents)
         node.potential = potential
Exemple #6
0
 def reinit(jt):
     """
     Reinitialize potentials of BBN nodes in join tree.
     :param jt: Join tree.
     :return: None.
     """
     for node, parents in jt.get_bbn_node_and_parents().items():
         potential = PotentialUtil.get_potential(node, parents)
         node.potential = potential
    def __walk__(self, x, s, y):
        """
        Walks away from the specified node y.

        :param x: Clique.
        :param s: Separation-set.
        :param y: Clique.
        """
        sepsets, cliques = self.__get_neighboring_cliques__(self.join_tree, y)

        s_arr = []
        c_arr = []
        for sep, cli in zip(sepsets, cliques):
            if not cli[1].is_marked():
                cli[1].mark()
                s_arr.append(sep)
                c_arr.append(cli)

        for sep, cli in zip(s_arr, c_arr):
            PotentialUtil.pass_single_message(self.join_tree, y, sep[1],
                                              cli[1])
            self.__walk__(y, sep[1], cli[1])
Exemple #8
0
    def initialize(join_tree):
        """
        Starts the initialization.

        :param join_tree: Join tree.
        :return: Join tree.
        """
        for clique in join_tree.get_cliques():
            potential = PotentialUtil.get_potential_from_nodes(clique.nodes)
            join_tree.add_potential(clique, potential)

        for sep_set in join_tree.get_sep_sets():
            potential = PotentialUtil.get_potential_from_nodes(sep_set.nodes)
            join_tree.add_potential(sep_set, potential)

        nodes = join_tree.get_bbn_nodes()
        for node in nodes:
            clique = Initializer.get_clique(node, join_tree)
            # print('{} mapped to clique {}'.format(node.variable.name, clique))
            p1 = join_tree.potentials[clique.id]
            p2 = node.potential
            # print(p1)
            # print('>>>>>')
            # print(p2)
            # print('----')
            PotentialUtil.multiply(p1, p2)
            # print(p1)
            # print('****')

        for node in nodes:
            for value in node.variable.values:
                clique = node.metadata['parent.clique']
                clique_potential = join_tree.potentials[clique.id]
                node_potential = join_tree.get_evidence(node, value)
                PotentialUtil.multiply(clique_potential, node_potential)
                # print(clique)
                # print(clique_potential)
        return join_tree