def test_distribution_K_l_1(num_samples=100): """Unrooted binary trees (class K) with one black node.""" BoltzmannSamplerBase.oracle = EvaluationOracle(reference_evals) grammar = binary_tree_grammar() grammar.init() symbolic_x = 'x*G_1_dx(x,y)' symbolic_y = 'D(x*G_1_dx(x,y),y)' sampled_class = 'K' grammar._precompute_evals(sampled_class, symbolic_x, symbolic_y) # There are only 2 possibilities. graphs_labs = [ (nx.path_graph(2), 1, 4), (nx.path_graph(3), 2, 5), # TODO here something looks wrong when you run it. ] test_distribution_for_l_size( grammar, sampled_class, symbolic_x, symbolic_y, 1, # l-size graphs_labs, num_samples)
def test_distribution_K_l_2(num_samples=100): BoltzmannSamplerBase.oracle = EvaluationOracle(reference_evals) grammar = binary_tree_grammar() grammar.init() symbolic_x = 'x*G_1_dx(x,y)' symbolic_y = 'D(x*G_1_dx(x,y),y)' sampled_class = 'K' grammar._precompute_evals(sampled_class, symbolic_x, symbolic_y) star_path_1 = nx.star_graph(3) star_path_1.add_edge(1, 4) star_path_2 = nx.star_graph(3) star_path_2.add_edge(1, 4) star_path_2.add_edge(4, 5) other = nx.Graph(star_path_1) other.add_edge(4, 5) other.add_edge(4, 6) # The first factor is due to the position of the leaves and the second are the labellings. graphs_labs_u_size = [(nx.path_graph(3), 1 * 2, 5), (nx.path_graph(4), 4 * 2, 6), (nx.path_graph(5), 4 * 2, 7), (star_path_1, 2 * 2, 7), (star_path_2, 4 * 2, 8), (other, 1 * 2, 9)] test_distribution_for_l_size( grammar, sampled_class, symbolic_x, symbolic_y, 2, # l-size graphs_labs_u_size, num_samples)
def test_distribution_R_b_l_1(num_samples=100): BoltzmannSamplerBase.oracle = EvaluationOracle(planar_graph_evals_n100) grammar = binary_tree_grammar() grammar.init() symbolic_x = 'x*G_1_dx(x,y)' symbolic_y = 'D(x*G_1_dx(x,y),y)' sampled_class = 'R_b' grammar._precompute_evals(sampled_class, symbolic_x, symbolic_y) # In this case the labs column does not correspond to actual labelings # but to distinction because of the root. graphs_labs_u_size = [ (nx.path_graph(1), 1, 2), (nx.path_graph(2), 2, 3), (nx.path_graph(3), 1, 4), ] test_distribution_for_l_size( grammar, sampled_class, symbolic_x, symbolic_y, 1, # l-size graphs_labs_u_size, num_samples)
def binary_tree_oldtest_V2(): binary_tree_test_oracle = EvaluationOracle({ 'x': 0.0475080992953792, 'y': 1.0, # 'R_b_as(x,y)': 1, # 'R_w_as(x,y)': 1, 'R_w(x,y)': 1, 'R_b(x,y)': 1, # 'R_b_head(x,y)': 0.000001, # 'R_w_head(x,y)': 0.9 }) BoltzmannSamplerBase.oracle = binary_tree_test_oracle # BoltzmannSampler.oracle = EvaluationOracle(planar_graph_evals_n100) grammar = binary_tree_grammar() grammar.init() symbolic_x = 'x' symbolic_y = 'y' # [print(query) for query in sorted(binary_tree_grammar.collect_oracle_queries('K_dy', symbolic_x, symbolic_y))] tree = grammar.sample('K_dy', symbolic_x, symbolic_y) # print(tree) print(tree.base_class_object().get_attribute('numblacknodes'), end="\t") print(tree.base_class_object().get_attribute('numwhitenodes'), end="\t") print(tree.base_class_object().get_attribute('numtotal'), end="\t") return tree.base_class_object()
def binary_tree_oldtest(): binary_tree_test_oracle = EvaluationOracle({ 'x': 0.0475080992953792, 'y': 1.0, # 'R_b_as(x,y)': 1, # 'R_w_as(x,y)': 1, 'R_w(x,y)': 1, 'R_b(x,y)': 1, # 'R_b_head(x,y)': 0.000001, # 'R_w_head(x,y)': 0.9 }) # BoltzmannSampler.oracle = binary_tree_test_oracle BoltzmannSamplerBase.oracle = EvaluationOracle(planar_graph_evals_n100) grammar = binary_tree_grammar() grammar.init() # symbolic_x = 'x' symbolic_x = 'x*G_1_dx(x,y)' # symbolic_y = 'y' symbolic_y = 'D(x*G_1_dx(x,y),y)' print("Needed oracle entries:") [ print(query) for query in sorted( grammar._collect_oracle_queries('K_dy', symbolic_x, symbolic_y)) ] tree = grammar.sample('R_b', symbolic_x, symbolic_y) print("Black nodes: {}".format(tree.black_nodes_count)) print("White nodes: {}".format(tree.white_nodes_count)) print("Total nodes: {}".format(tree.black_nodes_count + tree.white_nodes_count)) print("Total leaves: {}".format(tree.leaves_count)) return tree
def irreducible_dissection_grammar(): """Builds the dissection grammar. Must still be initialized with init(). Returns ------- DecompositionGrammar The grammar for sampling from J_a and J_a_dx. """ # Some shorthands to keep the grammar readable. L = pybo.LAtomSampler Rule = pybo.AliasSampler K = Rule('K') K_dx = Rule('K_dx') K_dx_dx = Rule('K_dx_dx') I = Rule('I') I_dx = Rule('I_dx') I_dx_dx = Rule('I_dx_dx') J = Rule('J') J_dx = Rule('J_dx') J_dx_dx = Rule('J_dx_dx') Bij = pybo.BijectionSampler Rej = pybo.RejectionSampler grammar = pybo.DecompositionGrammar() # This grammar depends on the binary tree grammar so we add it. grammar.rules = binary_tree_grammar().rules EarlyRejectionControl.grammar = grammar grammar.add_rules({ # Non-derived dissections (standard, rooted, admissible). 'I': Bij(K, closure), # We drop the 3*L*U factor here. # This bijection does not preserve l-size/u-size. 'J': Bij(I, add_random_root_edge), 'J_a': Rej(J, is_admissible), # Derived dissections. # The result is not a derived class, the bijection does not preserve l-size. 'I_dx': Bij(K_dx, closure), # We drop the factor 3*U. # This bijection does not preserve l-size/u-size. 'J_dx': Bij(I + L() * I_dx, add_random_root_edge), 'J_a_dx': Rej(J_dx, is_admissible), # Bi-derived dissections. # Does not preserve l-size, result is not a derived class. 'I_dx_dx': Bij(K_dx_dx, closure), # We dropped a factor. 'J_dx_dx': Bij(2 * I_dx + L() * I_dx_dx, add_random_root_edge), 'J_a_dx_dx': Rej(J_dx_dx, is_admissible) }) return grammar
def ___sample_combinatorial_class(name, comb_class, symbolic_x, symbolic_y, size, exact=True, derived=0): start_sampling = timer() number_trials = 0 BoltzmannSamplerBase.oracle = EvaluationOracle.get_best_oracle_for_size( size, planar_graph_evals) grammar = None if name is "binary_tree": grammar = binary_tree_grammar() elif name is "three_connected": grammar = three_connected_graph_grammar() elif name is "two_connected": grammar = two_connected_graph_grammar() elif name is "one_connected": grammar = one_connected_graph_grammar() elif name is "planar_graph": grammar = planar_graph_grammar() else: raise Exception("No such graph class") assert (grammar is not None) grammar.init() node_num = 0 if exact: while node_num != size: number_trials += 1 graph = grammar.sample_iterative(comb_class, symbolic_x, symbolic_y) if derived == 0 and name is not "planar_graph" and name is not "one_connected": node_num = graph.number_of_nodes else: node_num = graph.l_size + derived else: graph = grammar.sample_iterative(comb_class, symbolic_x, symbolic_y) if derived == 0 and name is not "planar_graph" and name is not "one_connected": node_num = graph.number_of_nodes else: node_num = graph.l_size + derived if derived == 0 and name is not "planar_graph" and name is not "one_connected": edge_num = graph.number_of_edges else: edge_num = graph.u_size end_sampling = timer() time_needed = end_sampling - start_sampling data = (node_num, edge_num, number_trials, time_needed) ___save_graph_in_file(graph, name) return data, graph