def test_triangulate_no_tie_break(self, huang_darwiche_moralized): # Now lets see what happens if # we dont enforce the tie-breakers... # It seems the triangulated graph is # different adding edges from d to c # and b to c # Will be interesting to see whether # inference will still be correct. triangulate(huang_darwiche_moralized) nodes = {node.name: node for node in huang_darwiche_moralized.nodes} assert set(nodes["f_a"].neighbours) == set( [nodes["f_b"], nodes["f_c"]]) assert set(nodes["f_b"].neighbours) == set( [nodes["f_a"], nodes["f_d"], nodes["f_c"]]) assert set(nodes["f_c"].neighbours) == set([ nodes["f_a"], nodes["f_e"], nodes["f_g"], nodes["f_b"], nodes["f_d"] ]) assert set(nodes["f_d"].neighbours) == set( [nodes["f_b"], nodes["f_f"], nodes["f_e"], nodes["f_c"]]) assert set(nodes["f_e"].neighbours) == set([ nodes["f_c"], nodes["f_f"], nodes["f_h"], nodes["f_d"], nodes["f_g"] ]) assert set(nodes["f_f"].neighbours) == set( [nodes["f_d"], nodes["f_e"]]) assert set(nodes["f_g"].neighbours) == set( [nodes["f_c"], nodes["f_h"], nodes["f_e"]]) assert set(nodes["f_h"].neighbours) == set( [nodes["f_e"], nodes["f_g"]])
def test_insert_duplicate_clique(huang_darwiche_moralized): cliques, _ = triangulate(huang_darwiche_moralized, priority_func) forest = set() for clique in cliques: jt_node = JoinTreeCliqueNode(clique) clique.node = jt_node tree = JoinTree([jt_node]) forest.add(tree) s = SepSet(cliques[0], cliques[0]) assert s.insertable(forest) is False s_copy = copy.deepcopy(s) s.insert(forest) assert len(s.X.node.neighbours) > len(s_copy.X.node.neighbours)
def test_triangulate(self, huang_darwiche_moralized): # Because of ties in the priority q we will # override the priority function here to # insert tie breakers to ensure the same # elimination ordering as Darwich Huang. def priority_func_override(node): introduced_arcs = 0 cluster = [node] + node.neighbours for node_a, node_b in combinations(cluster, 2): if node_a not in node_b.neighbours: assert node_b not in node_a.neighbours introduced_arcs += 1 introduced_arcs_dict = { "f_h": [introduced_arcs, 0], "f_g": [introduced_arcs, 1], "f_c": [introduced_arcs, 2], "f_b": [introduced_arcs, 3], "f_d": [introduced_arcs, 4], "f_e": [introduced_arcs, 5], "others": [introduced_arcs, 10], } if node.name in introduced_arcs_dict: return introduced_arcs_dict[node.name] return introduced_arcs_dict["others"] cliques, elimination_ordering = triangulate(huang_darwiche_moralized, priority_func_override) nodes = {node.name: node for node in huang_darwiche_moralized.nodes} assert len(cliques) == 6 assert cliques[0].nodes == set( [nodes["f_e"], nodes["f_g"], nodes["f_h"]]) assert cliques[1].nodes == set( [nodes["f_c"], nodes["f_e"], nodes["f_g"]]) assert cliques[2].nodes == set( [nodes["f_d"], nodes["f_e"], nodes["f_f"]]) assert cliques[3].nodes == set( [nodes["f_a"], nodes["f_c"], nodes["f_e"]]) assert cliques[4].nodes == set( [nodes["f_a"], nodes["f_b"], nodes["f_d"]]) assert cliques[5].nodes == set( [nodes["f_a"], nodes["f_d"], nodes["f_e"]]) assert elimination_ordering == [ "f_h", "f_g", "f_f", "f_c", "f_b", "f_d", "f_e", "f_a", ] # Now lets ensure the triangulated graph is # the same as Darwiche Huang fig. 2 pg. 13 nodes = {node.name: node for node in huang_darwiche_moralized.nodes} assert set(nodes["f_a"].neighbours) == set( [nodes["f_b"], nodes["f_c"], nodes["f_d"], nodes["f_e"]]) assert set(nodes["f_b"].neighbours) == set( [nodes["f_a"], nodes["f_d"]]) assert set(nodes["f_c"].neighbours) == set( [nodes["f_a"], nodes["f_e"], nodes["f_g"]]) assert set(nodes["f_d"].neighbours) == set( [nodes["f_b"], nodes["f_f"], nodes["f_e"], nodes["f_a"]]) assert set(nodes["f_e"].neighbours) == set([ nodes["f_c"], nodes["f_f"], nodes["f_h"], nodes["f_d"], nodes["f_g"], nodes["f_a"], ]) assert set(nodes["f_f"].neighbours) == set( [nodes["f_d"], nodes["f_e"]]) assert set(nodes["f_g"].neighbours) == set( [nodes["f_c"], nodes["f_h"], nodes["f_e"]]) assert set(nodes["f_h"].neighbours) == set( [nodes["f_e"], nodes["f_g"]])