示例#1
0
def binary_trees():
    print("Binary tree example.\n--------------------\n")

    def height(tree):
        if isinstance(tree, LAtomClass):
            return 1
        else:
            assert isinstance(tree, ProdClass)
            return max(height(tree._second._first), height(
                tree._second._second)) + 1

    # Define the grammar.

    # Define some shortcuts for readability.
    L = LAtomSampler
    _ = AliasSampler

    # Initialize a grammar object and set the sampling rules (in this case just one single rule).
    tree_grammar = DecompositionGrammar({
        # A binary tree is either a leaf or an inner node with two children that are binary trees.
        'T': L() + L() * _('T')**2
    })

    # Set the builder information and initialize the grammar.
    # tree_grammar.set_builder(['T'], BinaryTreeBuilder())
    tree_grammar.init()

    # Do the mathematical stuff.

    def get_x_for_size(n):
        return math.sqrt(n**2 - 1) / (2 * n)

    def eval_T(x):
        return (math.sqrt(1 - 4 * x**2) + 1) / (2 * x)

    def eval_T_dx(x):
        return (1 / (math.sqrt(1 - 4 * x**2)) + 1) / (2 * x**2)

    # TODO something is weird here ...
    target_size = 2
    # x = get_x_for_size(target_size)
    x = 0.499999999
    print(x)
    T = eval_T(x)
    print(x * T**2 / (x + x * T**2))
    T_dx = eval_T_dx(x)
    tree_oracle = EvaluationOracle({'x': x, 'T(x,y)': T, 'T_dx(x,y)': T_dx})
    # Set the newly created oracle as the active oracle.
    BoltzmannSamplerBase.oracle = tree_oracle

    print("expected size of the trees: {}\n".format(
        tree_oracle.get_expected_l_size('T', 'x', 'y')))

    print(tree_grammar.sample_iterative('T', 'x', 'y'))

    print("needed oracle entries for sampling: {}\n".format(
        tree_grammar._collect_oracle_queries('T', 'x', 'y')))

    num_samples = 10
    while True:
        try:
            trees = [
                tree_grammar.sample('T', 'x', 'y') for _ in range(num_samples)
            ]
            break
        except RecursionError:
            # print("Recursion error")
            pass
    avg_size = sum([tree.l_size for tree in trees]) / num_samples
    print("average size in {} trials: {}".format(num_samples, avg_size))
    avg_height = sum([height(tree) for tree in trees]) / num_samples
    print("average height of trees: {}".format(avg_height))

    print(2 * math.sqrt(math.pi * avg_size / 2))
示例#2
0
    BoltzmannSamplerBase.oracle = oracle
    BoltzmannSamplerBase.debug_mode = False

    grammar = dummy_sampling_grammar()
    grammar.init()
    # grammar.dummy_sampling_mode()
    # symbolic_x = 'x'
    symbolic_y = 'y'
    symbolic_x = 'x*G_1_dx(x,y)'
    # symbolic_y = 'D(x*G_1_dx(x,y),y)'
    sampled_class = 'D_dx_dx'
    grammar._precompute_evals(sampled_class, symbolic_x, symbolic_y)

    try:
        print("expected: {}\n".format(
            oracle.get_expected_l_size(sampled_class, symbolic_x, symbolic_y)))
    except PyBoltzmannError:
        pass

    # random.seed(0)
    # boltzmann_framework_random_gen.seed(0)

    l_sizes = []
    i = 0
    samples = 100
    start = timer()
    while i < samples:
        dummy = grammar.sample_iterative(sampled_class, symbolic_x, symbolic_y)
        l_sizes.append(dummy.l_size)
        i += 1
    end = timer()
def test_sampled_sizes():

    all_evaluations = [reference_evals]

    for evaluations in all_evaluations:
        oracle = EvaluationOracle(evaluations)
        BoltzmannSamplerBase.oracle = oracle
        # grammar = three_connected_graph_grammar()
        grammar = dummy_sampling_grammar()
        grammar.init()
        grammar.dummy_sampling_mode()
        grammar._precompute_evals('K', 'x*G_1_dx(x,y)', 'D(x*G_1_dx(x,y),y)')

        classes_known_dx = [
            'G_3_arrow',
            #'K_dx',
            #'K_dy',
            #'J_a',

            #'D',
            #'D_dx',
            #'S',
            #'S_dx',
            #'P',
            #'P_dx',
            #'H',
            #'H_dx',

            #'G_2_dx',
            #'G_2_dx_dx',

            #'G_1_dx_dx',
            #'G_1',
            #'G_1_dx',

            #'G',
            #'G_dx',
            #'G_dx_dx'
        ]

        symbolic_x = [
            'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)',
            'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)',
            'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)',
            'x*G_1_dx(x,y)', 'x*G_1_dx(x,y)', 'x', 'x', 'x', 'x', 'x', 'x'
        ]
        symbolic_y = [
            'D(x*G_1_dx(x,y),y)', 'D(x*G_1_dx(x,y),y)', 'D(x*G_1_dx(x,y),y)',
            'D(x*G_1_dx(x,y),y)', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y',
            'y', 'y', 'y', 'y', 'y', 'y', 'y'
        ]

        for index, label in enumerate(classes_known_dx):
            x = symbolic_x[index]
            y = symbolic_y[index]
            expected_size = oracle.get_expected_l_size(label,
                                                       symbolic_x[index],
                                                       symbolic_y[index])

            num_samples = 10000
            count = 0
            sizes = []
            rec_errors = 0
            while count < num_samples:
                try:
                    sizes.append(grammar.sample(label, x, y).l_size)
                    count += 1
                except RecursionError:
                    rec_errors += 1

            observed = sum(sizes) / len(sizes)

            print(
                "class: {} \t expected: {} \t observed: {} \t rec. errors: {} \t difference: {}"
                .format(label, expected_size, observed, rec_errors,
                        observed / expected_size - 1))

        print()