Beispiel #1
0
def test_full_problem():
    graph = make_toy_graph()

    banned_values = set([frozenset(['h'])])
    heuristic = make_heuristic(banned_values, greed=10)

    initial_bindings = {
        'x': set(['x']),
        'y': set(['y']),
    }

    initial_expr = E.prob([E.v('y')], [E.do(E.v('x'))])

    initial_proof_state = ProofState(
        length=0,  # length of proof
        heuristic_length=0,
        bindings=initial_bindings,
        root_expr=initial_expr,
    ).normalise()

    initial_proof_state = initial_proof_state.copy(
        heuristic_length=heuristic(initial_proof_state))

    def goal_check(proof_state):
        return proof_state.heuristic_length == 0

    result = proof_search(initial_proof_state,
                          graph,
                          goal_check,
                          heuristic,
                          max_proof_length=7)
    assert result['reached_goal']
Beispiel #2
0
def test_full_problem():
    graph = make_toy_graph()

    banned_values = set([frozenset(['h'])])
    heuristic = make_heuristic(banned_values, greed=10)

    initial_bindings = {
        'x' : set(['x']),
        'y' : set(['y']),
    }

    initial_expr = E.prob([E.v('y')], [E.do(E.v('x'))])

    initial_proof_state = ProofState(
        length = 0, # length of proof
        heuristic_length = 0,
        bindings = initial_bindings,
        root_expr = initial_expr,
    ).normalise()

    initial_proof_state = initial_proof_state.copy(heuristic_length=heuristic(initial_proof_state))

    def goal_check(proof_state):
        return proof_state.heuristic_length == 0

    result = proof_search(initial_proof_state, graph, goal_check, heuristic, max_proof_length=7)
    assert result['reached_goal']
Beispiel #3
0
def main():

    if len(sys.argv) != 2:
        sys.stderr.write('usage: greediness (positive float...)\n')
        sys.exit(1)

    greed = float(sys.argv[1])

    graph = make_toy_graph()

    banned_values = set([frozenset(['h'])])
    # dial the greed parameter up high.
    # this makes the search very optimistic.
    # in general this may not find the shortest proof
    heuristic = make_heuristic(banned_values, greed)

    initial_bindings = {
        'x': frozenset(['x']),
        'y': frozenset(['y']),
    }

    initial_expr = E.prob([E.v('y')], [E.do(E.v('x'))])

    initial_proof_state = ProofState(
        length=0,  # length of proof
        heuristic_length=0,
        bindings=initial_bindings,
        root_expr=initial_expr,
        parent=None,
        comment='initial state',
    ).normalise()

    # this is a little silly
    initial_proof_state = initial_proof_state.copy(
        heuristic_length=heuristic(initial_proof_state))

    def goal_check(proof_state):
        return proof_state.heuristic_length == 0

    result = proof_search(initial_proof_state,
                          graph,
                          goal_check,
                          heuristic,
                          max_proof_length=7)
    assert result['reached_goal']
    print 'success!'

    display_proof_as_listing(result['path'])

    out_file_name = 'proof_tree.dot'
    write_proof_tree(result['path'], result['closed'], out_file_name)
Beispiel #4
0
def test_normalise_fixed_point():
    root_expr = sigma(v('x'), product([prob([v('z'), v('y')], [v('b'), v('x'), do(v('a'))]),
        prob([v('z'), v('y'), v('x')], [do(v('a'))])]))

    bindings = {'x' : 'xxx', 'z' : 'zzz', 'y' : 'yyy', 'a' : 'aaa'}

    state = ProofState(0, 0, bindings, root_expr)

    normalised_state = state.normalise()

    expected_result = sigma(v(0), product((prob((v(0), v(1), v(2)),(do(v(3)), )),
        prob((v(1), v(2)), (do(v(3)), v(0), v(4))))))

    assert normalised_state.root_expr == expected_result
Beispiel #5
0
    def gen_expansions(value, proof_state):
        for (prob_expr, inject) in E.gen_matches(E.is_prob, proof_state.root_expr):
            prob_vars = get_variable_order(prob_expr)
            prob_values = [proof_state.bindings[v] for v in prob_vars]
            if value in prob_values:
                continue
            # prob([x],[w]) -> sigma(y, product([prob([x], [y, w]), p([y], [w])]))
            i = new_variable_name(proof_state.bindings)
            v_i = E.v(i)
            alpha_left = tuple(prob_expr[1])
            alpha_right = (v_i, ) + tuple(prob_expr[2])
            alpha = E.prob(alpha_left, alpha_right)
            beta_left = (v_i, )
            beta_right = tuple(prob_expr[2])
            beta = E.prob(beta_left, beta_right)
            expr_prime = E.sigma(v_i, E.product([alpha, beta]))

            succ_length = proof_state.length + 1
            succ_heuristic = 0
            succ_bindings = dict(proof_state.bindings)
            succ_bindings[i] = value
            succ_root_expr = inject(expr_prime)

            succ_comment = 'conditioned %s on %s' % (
                pleasantly_fmt(proof_state.bindings, prob_expr),
                make_canonical_variable_name(value))

            succ_proof_state = ProofState(succ_length, succ_heuristic, succ_bindings, succ_root_expr,
                parent=proof_state, comment=succ_comment)
            yield succ_proof_state
Beispiel #6
0
def main():

    if len(sys.argv) != 2:
        sys.stderr.write('usage: greediness (positive float...)\n')
        sys.exit(1)

    greed = float(sys.argv[1])

    graph = make_toy_graph()

    banned_values = set([frozenset(['h'])])
    # dial the greed parameter up high.
    # this makes the search very optimistic.
    # in general this may not find the shortest proof
    heuristic = make_heuristic(banned_values, greed)

    initial_bindings = {
        'x' : frozenset(['x']),
        'y' : frozenset(['y']),
    }

    initial_expr = E.prob([E.v('y')], [E.do(E.v('x'))])

    initial_proof_state = ProofState(
        length = 0, # length of proof
        heuristic_length = 0,
        bindings = initial_bindings,
        root_expr = initial_expr,
        parent = None,
        comment = 'initial state',
    ).normalise()

    # this is a little silly
    initial_proof_state = initial_proof_state.copy(heuristic_length=heuristic(initial_proof_state))
    
    def goal_check(proof_state):
        return proof_state.heuristic_length == 0

    result = proof_search(initial_proof_state, graph, goal_check, heuristic, max_proof_length=7)
    assert result['reached_goal']
    print 'success!'

    display_proof_as_listing(result['path'])

    out_file_name = 'proof_tree.dot'
    write_proof_tree(result['path'], result['closed'], out_file_name)
Beispiel #7
0
def test_normalise_single_iter():
    root_expr = sigma(v('x'), product([prob([v('z'), v('y')], [v('b'), v('x'), do(v('a'))]),
        prob([v('z'), v('y'), v('x')], [do(v('a'))])]))

    bindings = {'x' : 'xxx', 'z' : 'zzz', 'y' : 'yyy', 'a' : 'aaa'}

    state = ProofState(0, 0, bindings, root_expr)

    normalised_state = state.normalise(max_iters=1)

    # first up: expression ordering (nb do(v()) comes before v() in sorted lists)
    # sigma(x, product([prob([x y z],[do(a)]), prob([y z], [(do a) b x])]))
    # so, variable order should be:
    # x y z a b
    # so, new variable names should be
    # 0 1 2 3 4
    # so, normalised state should be
    # sigma(0, product([prob([0 1 2],[do(3)]), prob([1 2], [(do 3) 4 0])]))

    expected_result = sigma(v(0), product((prob((v(0), v(1), v(2)),(do(v(3)), )),
        prob((v(1), v(2)), (do(v(3)), v(4), v(0))))))

    assert normalised_state.root_expr == expected_result
Beispiel #8
0
def gen_causal_rule_moves(rules, proof_state, graph):
    def bind(name):
        return proof_state.bindings[name]

    for rule in rules:
        for site in rule['site_gen'](proof_state.root_expr):
            prepped_args = prepare_rule_arguments(rule['unpack_target'], site)
            bound_args = bind_arguments(bind, prepped_args)
            if not rule['assumption_test'](g = graph, **bound_args):
                continue
            succ_length = proof_state.length + 1
            succ_heuristic = 0
            succ_bindings = dict(proof_state.bindings)
            succ_root_expr = rule['apply'](site)

            original_expr = site[-1]
            
            succ_comment = 'applied rule %s to %s' % (
                rule['name'],
                pleasantly_fmt(proof_state.bindings, original_expr))

            succ_proof_state = ProofState(succ_length, succ_heuristic, succ_bindings, succ_root_expr,
                parent=proof_state, comment=succ_comment)
            yield succ_proof_state