def manual_constructs(self): p1 = c_const("p1") p2 = c_const("p2") p3 = c_const("p3") parent = c_pred("parent", 2) grandparent = c_pred("grandparent", 2) f1 = parent(p1, p2) f2 = parent(p2, p3) v1 = c_var("X") v2 = c_var("Y") v3 = c_var("Z") cl = grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3) assert isinstance(p1, Constant) assert isinstance(p2, Constant) assert isinstance(p3, Constant) assert isinstance(parent, Predicate) assert isinstance(grandparent, Predicate) assert isinstance(v1, Variable) assert isinstance(v2, Variable) assert isinstance(v2, Variable) assert isinstance(cl, Clause) assert isinstance(f1, Atom) assert isinstance(f2, Atom)
def graph_connectivity(self): v1 = c_const("v1") v2 = c_const("v2") v3 = c_const("v3") v4 = c_const("v4") edge = c_pred("edge", 2) path = c_pred("path", 2) f1 = edge(v1, v2) f2 = edge(v1, v3) f3 = edge(v2, v4) X = c_var("X") Y = c_var("Y") Z = c_var("Z") cl1 = path(X, Y) <= edge(X, Y) cl2 = path(X, Y) <= path(X, Z) & edge(Z, Y) solver = MuZ() solver.assert_fact(f1) solver.assert_fact(f2) solver.assert_fact(f3) solver.assert_rule(cl1) solver.assert_rule(cl2) # assert solver.has_solution(path(v1, v2)) # assert solver.has_solution(path(v1, v4)) # assert not solver.has_solution(path(v3, v4)) # # assert len(solver.one_solution(path(v1, X))) == 1 # assert len(solver.one_solution(path(X, v4))) == 1 # assert len(solver.one_solution(path(X, Y))) == 2 # # assert len(solver.all_solutions(path(v1, X))) == 3 # assert len(solver.all_solutions(path(X, Y))) == 4 assert solver.has_solution(path(v1, v2)) assert solver.has_solution(path(v1, v4)) assert not solver.has_solution(path(v3, v4)) assert len(solver.query(path(v1, X), max_solutions=1)[0]) == 1 assert len(solver.query(path(X, v4), max_solutions=1)[0]) == 1 assert len(solver.query(path(X, Y), max_solutions=1)[0]) == 2 assert len(solver.query(path(v1, X))) == 3 assert len(solver.query(path(X, Y))) == 4
head = Atom(not_number, [A]) body = Atom(is_number, [A]) clause10 = Clause(head, Body(Not(body))) head = Atom( mk_uppercase, [Structure(s, [Pair(H1, Ta), Pair(H2, Tb)]), Structure(s, [Ta, Tb])]) body = Atom(convert_case, [H2, H1]) clause11 = Clause(head, Body(body)) head = Atom( mk_lowercase, [Structure(s, [Pair(H1, Ta), Pair(H2, Tb)]), Structure(s, [Ta, Tb])]) body = Atom(convert_case, [H1, H2]) clause12 = Clause(head, Body(body)) Az = c_const("\"A\"") Bz = c_const("\"B\"") Cz = c_const("\"C\"") Dz = c_const("\"D\"") Ez = c_const("\"E\"") Fz = c_const("\"F\"") Gz = c_const("\"G\"") Hz = c_const("\"H\"") Iz = c_const("\"I\"") Jz = c_const("\"J\"") Kz = c_const("\"K\"") Lz = c_const("\"L\"") Mz = c_const("\"M\"") Nz = c_const("\"N\"") Oz = c_const("\"O\"") Pz = c_const("\"P\"")
def learn_text(): """ We describe piece of text spanning multiple lines: "node A red <newline> node B green <newline> node C blue <newline>" using the next\2, linestart\2, lineend\2, tokenlength\2 predicates """ token = c_type("token") num = c_type("num") next = c_pred("next", 2, ("token", "token")) linestart = c_pred("linestart", 2, ("token", "token")) lineend = c_pred("lineend", 2, ("token", "token")) tokenlength = c_pred("tokenlength", 2, ("token", "num")) n1 = c_const("n1", num) n3 = c_const("n3", num) n4 = c_const("n4", num) n5 = c_const("n5", num) node1 = c_const("node1", token) node2 = c_const("node2", token) node3 = c_const("node3", token) red = c_const("red", token) green = c_const("green", token) blue = c_const("blue", token) a_c = c_const("a_c", token) b_c = c_const("b_c", token) c_c = c_const("c_c", token) start = c_const("c_START", token) end = c_const("c_END", token) bk = Knowledge(next(start, node1), next(node1, a_c), next(a_c, red), next(red, node2), next(node2, green), next(green, b_c), next(b_c, node3), next(node3, c_c), next(c_c, blue), next(blue, end), tokenlength(node1, n4), tokenlength(node2, n4), tokenlength(node3, n4), tokenlength(a_c, n1), tokenlength(b_c, n1), tokenlength(c_c, n1), tokenlength(red, n3), tokenlength(green, n5), tokenlength(blue, n4), linestart(node1, node1), linestart(a_c, node1), linestart(red, node1), linestart(node2, node2), linestart(b_c, node2), linestart(green, node2), linestart(node3, node3), linestart(c_c, node3), linestart(blue, node3), lineend(node1, a_c), lineend(a_c, red), lineend(node2, red), lineend(b_c, green), lineend(node3, blue), lineend(c_c, blue), lineend(red, red), lineend(green, green), lineend(blue, blue)) solver = SWIProlog() eval_fn1 = Coverage(return_upperbound=True) learner = Aleph(solver, eval_fn1, max_body_literals=3, do_print=False) # 1. Consider the hypothesis: f1(word) :- word is the second word on a line if True: f1 = c_pred("f1", 1, [token]) neg = {f1(x) for x in [node1, node2, node3, blue, green, red]} pos = {f1(x) for x in [a_c, b_c, c_c]} task = Task(positive_examples=pos, negative_examples=neg) res = learner.learn(task, bk, None) print(res) # 2. Consider the hypothesis: f2(word) :- word is the first word on a line if True: f2 = c_pred("f2", 1, [token]) neg = {f1(x) for x in [a_c, b_c, c_c, blue, green, red]} pos = {f1(x) for x in [node1, node2, node3]} task2 = Task(positive_examples=pos, negative_examples=neg) res = learner.learn(task2, bk, None) print(res) # 3. Assume we have learned the predicate node(X) before (A, B and C and nodes). # We want to learn f3(Node,X) :- X is the next token after Node if True: node = c_pred("node", 1, [token]) color = c_pred("color", 1, [token]) nodecolor = c_pred("nodecolor", 2, [token, token]) a = c_var("A", token) b = c_var("B", token) bk_old = bk.get_all() bk = Knowledge(*bk_old, node(a_c), node(b_c), node(c_c), node(a_c), node(b_c), node(c_c), color(red), color(green), color(blue)) pos = { nodecolor(a_c, red), nodecolor(b_c, green), nodecolor(c_c, blue) } neg = set() neg = { nodecolor(node1, red), nodecolor(node2, red), nodecolor(node3, red), nodecolor(node1, blue), nodecolor(node2, blue), nodecolor(node2, blue), nodecolor(node1, green), nodecolor(node2, green), nodecolor(node3, green), nodecolor(a_c, green), nodecolor(a_c, blue), nodecolor(b_c, blue), nodecolor(b_c, red), nodecolor(c_c, red), nodecolor(c_c, green) } task3 = Task(positive_examples=pos, negative_examples=neg) # prog = learner.learn(task3,bk,None,initial_clause=Body(node(a),color(b))) result = learner.learn(task3, bk, None, initial_clause=Body(node(a), color(b)), minimum_freq=3) print(result)
def learn_with_constants(): """ Consider a row of blocks [ block1 block2 block3 block4 block5 block6 ] The order of this row is expressed using follows(X,Y) The color of a block is expressed using color(X,Color) Goal: learn a function f that says: a block is positive when it is followed by a red block pos(X) :- next(X,Y), color(Y,red) """ block = c_type("block") col = c_type("col") block1 = c_const("block1", domain=block) # blue -> positive block2 = c_const("block2", domain=block) # red block3 = c_const("block3", domain=block) # green -> positive block4 = c_const("block4", domain=block) # red -> positive block5 = c_const("block5", domain=block) # red block6 = c_const("block6", domain=block) # green block7 = c_const("block7", domain=block) # blue block8 = c_const("block8", domain=block) # blue red = c_const("red", domain="col") green = c_const("green", domain="col") blue = c_const("blue", domain="col") follows = c_pred("follows", 2, domains=[block, block]) color = c_pred("color", 2, domains=[block, col]) # Predicate to learn: f = c_pred("f", 1, domains=[block]) bk = Knowledge(follows(block1, block2), follows(block2, block3), follows(block3, block4), follows(block4, block5), follows(block5, block6), follows(block6, block7), follows(block7, block8), color(block1, blue), color(block2, red), color(block3, green), color(block4, red), color(block5, red), color(block6, green), color(block7, blue), color(block8, blue)) pos = {f(x) for x in [block1, block3, block4]} neg = {f(x) for x in [block2, block5, block6, block7, block8]} task = Task(positive_examples=pos, negative_examples=neg) solver = SWIProlog() # EvalFn must return an upper bound on quality to prune search space. eval_fn1 = Coverage(return_upperbound=True) eval_fn2 = Compression(return_upperbound=True) eval_fn3 = Accuracy(return_upperbound=True) learners = [ Aleph(solver, eval_fn, max_body_literals=4, do_print=False) for eval_fn in [eval_fn1, eval_fn3] ] for learner in learners: res = learner.learn(task, bk, None, minimum_freq=1) print(res)
def simple_grandparent(self): p1 = c_const("p1") p2 = c_const("p2") p3 = c_const("p3") parent = c_pred("parent", 2) grandparent = c_pred("grandparent", 2) f1 = parent(p1, p2) f2 = parent(p2, p3) v1 = c_var("X") v2 = c_var("Y") v3 = c_var("Z") cl = (grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3)) solver = MuZ() solver.assert_fact(f1) solver.assert_fact(f2) solver.assert_rule(cl) # assert solver.has_solution(parent(v1, v2)) # assert not solver.has_solution(parent(v1, v1)) # assert len(solver.all_solutions(parent(v1, v2))) == 2 # assert len(solver.all_solutions(parent(p1, v1))) == 1 # assert solver.has_solution(parent(p1, p2)) # assert not solver.has_solution(parent(p2, p1)) # assert len(solver.one_solution(parent(p1, v1))) == 1 # # assert solver.has_solution(grandparent(v1, v2)) # assert solver.has_solution(grandparent(p1, v1)) # assert len(solver.one_solution(grandparent(p1, v1))) == 1 # assert solver.has_solution(grandparent(p1, p3)) # assert not solver.has_solution(grandparent(p2, v1)) # assert len(solver.one_solution(grandparent(p1, v1))) == 1 # ans = solver.one_solution(grandparent(p1, v1)) # assert ans[v1] == p3 # ans = solver.one_solution(grandparent(v1, v2)) # assert ans[v1] == p1 and ans[v2] == p3 # # assert solver.has_solution(cl) # ans = solver.one_solution(cl) # assert ans[v1] == p1 and ans[v3] == p3 # assert len(solver.all_solutions(cl)) == 1 assert solver.has_solution(parent(v1, v2)) assert not solver.has_solution(parent(v1, v1)) assert len(solver.query(parent(v1, v2))) == 2 assert len(solver.query(parent(p1, v1))) == 1 assert solver.has_solution(parent(p1, p2)) assert not solver.has_solution(parent(p2, p1)) assert len(solver.query(parent(p1, v1), max_solutions=1)) == 1 assert solver.has_solution(grandparent(v1, v2)) assert solver.has_solution(grandparent(p1, v1)) assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1 assert solver.has_solution(grandparent(p1, p3)) assert not solver.has_solution(grandparent(p2, v1)) assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1 ans = solver.query(grandparent(p1, v1), max_solutions=1)[0] assert ans[v1] == p3 ans = solver.query(grandparent(v1, v2), max_solutions=1)[0] assert ans[v1] == p1 and ans[v2] == p3 assert solver.has_solution(*cl.get_literals()) ans = solver.query(*cl.get_literals(), max_solutions=1)[0] assert ans[v1] == p1 and ans[v3] == p3 assert len(solver.query(*cl.get_literals())) == 1