Exemple #1
0
def test_distribution_G_1_dx_l_2(num_samples=100):
    """Test if connected planar graphs with exactly 4 nodes have the right distribution."""
    BoltzmannSamplerBase.oracle = EvaluationOracle(reference_evals)
    grammar = one_connected_graph_grammar()
    grammar.init()

    graphs_labs = [(nx.path_graph(3), 3, 2), (nx.complete_graph(3), 1, 3)]

    test_distribution_for_l_size(grammar,
                                 'G_1_dx',
                                 'x',
                                 'y',
                                 2,
                                 graphs_labs,
                                 num_samples=num_samples)
Exemple #2
0
def test_distribution_G_1_dx_dx_l_2(num_samples=100):
    """Test if connected planar graphs with exactly 4 nodes have the right distribution."""
    BoltzmannSamplerBase.oracle = EvaluationOracle(reference_evals)
    grammar = one_connected_graph_grammar()
    grammar.init()

    # All one-connected planar graphs with 4 nodes and the number of their labellings.
    # See p.15, Fig. 5.
    cycle_with_chord = nx.cycle_graph(4)
    cycle_with_chord.add_edge(0, 2)
    graphs_labs = [(nx.path_graph(4), 12, 3), (nx.star_graph(3), 4, 3),
                   (nx.lollipop_graph(3, 1), 12, 4), (nx.cycle_graph(4), 3, 4),
                   (cycle_with_chord, 6, 5), (nx.complete_graph(4), 1, 6)]

    test_distribution_for_l_size(grammar,
                                 'G_1_dx_dx',
                                 'x',
                                 'y',
                                 2,
                                 graphs_labs,
                                 num_samples=num_samples)
Exemple #3
0
def planar_graph_grammar():
    """Constructs the grammar for planar graphs.

    Returns
    -------
    grammar : DecompositionGrammar
        The grammar for sampling from G, G_dx and G_dx_dx.
    """

    # Some shortcuts to make the grammar more readable.
    Rule = pybo.AliasSampler
    G_1 = Rule('G_1')
    G_1_dx = Rule('G_1_dx')
    G_1_dx_dx = Rule('G_1_dx_dx')
    G_1_dx_dx_dx = Rule('G_1_dx_dx_dx')
    G = Rule('G')
    G_dx = Rule('G_dx')
    G_dx_dx = Rule('G_dx')
    Set = pybo.SetSampler

    grammar = pybo.DecompositionGrammar()
    grammar.rules = one_connected_graph_grammar().rules
    EarlyRejectionControl.grammar = grammar

    grammar.add_rules({
        'G':
        Set(0, G_1),
        'G_dx':
        G_1_dx * G,
        'G_dx_dx':
        G_1_dx_dx * G + G_1_dx * G_dx,
        'G_dx_dx_dx':
        G_1_dx_dx_dx * G + G_1_dx_dx * G_dx + G_1_dx_dx * G_dx +
        G_1_dx * G_dx_dx
    })
    grammar.set_builder(['G', 'G_dx', 'G_dx_dx', 'G_dx_dx_dx'],
                        PlanarGraphBuilder())

    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