Exemplo n.º 1
0
 def print_tree(self, node):
     # print(node)
     t = var()
     t = run(1, t, Decision_Node_Test(node, t))
     if not t:
         self.print_answer_node(node)
         return
     print('Test for node', node, t)
     l = var()
     l = run(1, l, Tree_Node_LChild(node, l))
     if l:
         print(node, '-L->', l)
         self.print_tree(l[0])
     r = var()
     r = run(1, r, Tree_Node_RChild(node, r))
     if r:
         print(node, '-R->', r)
         self.print_tree(r[0])
Exemplo n.º 2
0
 def dot_add_subtree(self, dot, node):
     t = var()
     t = run(1, t, Decision_Node_Test(node, t))
     if not t:
         self.dot_add_answernode(dot, node)
         return
     #dotnode = self.quote_node(node)
     print('t', t)
     t = t[0]
     # dot.node(node, str(t))
     self.dot_node(dot, node, t)
     l = var()
     l = run(1, l, Tree_Node_LChild(node, l))[0]
     if l:
         print('l', l)
         # dot.edge(dotnode, l)
         self.dot_edge(dot, node, l)
         self.dot_add_subtree(dot, l)
     r = var()
     r = run(1, r, Tree_Node_RChild(node, r))[0]
     if r:
         # dot.edge(dotnode, r)
         self.dot_edge(dot, node, r)
         self.dot_add_subtree(dot, r)
Exemplo n.º 3
0
def count_binding_set(args, goals, fact_rels):
    # print(friends.facts)
    bs = run(0, args, *goals)
    # print('binding set', bs)
    return len(bs)
Exemplo n.º 4
0
from mykanren import run, eq, var, vars, conde
from mykanren import Relation, fact, facts

from numsys.number_relations import RegisteringRelation, ge, lt  #, neg

parent = Relation()

facts(parent, ("Homer", "Bart"), ("Homer", "Lisa"), ("Abe", "Homer"))

x = var()

ans = run(2, x, eq(x, 5))


def grandparent(x, z):
    y = var()
    return conde((parent(x, y), parent(y, z)))


# print(ans)

# ================================================================================

from mykanren.assoccomm import eq_assoccomm as eq
from mykanren.assoccomm import commutative, associative

# Define some dummy Operationss
add = 'add'
mul = 'mul'
# Declare that these ops are commutative using the facts system
fact(commutative, mul)
Exemplo n.º 5
0
    def learn_tree_basic(self):
        relations = self.rels
        target = self.target
        modes = self.modes
        # tree = {} # dict of relations of a tree
        treeid = self._new_tree_id(target)
        self.rootid = self._new_node_id(treeid)

        # for ex in self.examples:
        #     fact(Decision_Node_Example, self.rootid, ex)

        # pos = list(self.pos_rels[self.target](*self.target_args)({}))
        pos = run(0, *self.target_args, self.pos_goal)
        # print('pos',pos)
        # fact(Decision_Node_Positive, self.rootid, pos)

        # neg = list(self.neg_rels[self.target](*self.target_args)({}))
        neg = run(0, *self.target_args, self.neg_goal)
        # print('neg',neg)
        # fact(Decision_Node_Negative, self.rootid, neg)

        score = self.gini_score(len(pos), len(neg))
        print('root score', score)

        expanding = [] # sorted by splitting score
        #heapq.heappush(expanding, (score, self.rootid))

        # def find_best_test():
        #     test = find_best_test()
        #     print('best test', test)
        #     pass

        def expand_node(node, typed_vars, examples, score):
            print('expanding node', node)
            print('current vars', typed_vars)
            best_score = 2
            bests = []
            test = None
            lex = None
            rex = None
            for rel, arg_types in modes:
                if rel not in relations:
                    continue
                rel = relations[rel]
                print('try relation', rel)
                print('mode', arg_types)
                argss = self._get_args(arg_types, typed_vars)
                argss = list(argss)
                print('argss', argss)
                for args in argss:
                    test = (rel, args)
                    print('args', args)
                    #node_ex_ss = self.get_node_ex_subs(node)
                    #s,lex,rex = self.score_test(test, node_ex_ss)
                    s,lex,rex = self.try_test(node, test, examples)
                    print('score', s)
                    choice = (s, node, arg_types, typed_vars, test, lex, rex)
                    if s == 0:
                        continue
                    elif s < best_score:
                        print('update bests')
                        best_score = s
                        bests = [choice]
                    elif s == best_score:
                        print('appending choice')
                        bests.append(choice)
                    print(bests)
            if not bests or bests[0][0] >= score:
                print('no valuable test')
                #set as leaf node
                self.make_answer_node(node, examples)
                return
            best = random.sample(bests, 1)[0]
            print('best case when expanding node', node, best)
            heapq.heappush(expanding, best)
            return
Exemplo n.º 6
0
 def dot_add_answernode(self, dot, node):
     ans = var()
     ans = run(1, ans, Decision_Answer_Node(node, ans))[0]
     # dot.node(self.quote_node(node), "%.3f: %s"%ans)
     self.dot_node(dot, node, "%.3f: %s" % ans)
Exemplo n.º 7
0
 def print_answer_node(self, node):
     ans = var()
     ans = run(1, ans, Decision_Answer_Node(node, ans))
     print(node, 'Answer:', ans)
Exemplo n.º 8
0
    def learn_tree_basic(self):
        relations = self.rels
        target = self.target
        modes = self.modes
        # tree = {} # dict of relations of a tree
        treeid = self._new_tree_id(target)
        self.rootid = self._new_node_id(treeid)

        # for ex in self.examples:
        #     fact(Decision_Node_Example, self.rootid, ex)

        # pos = list(self.pos_rels[self.target](*self.target_args)({}))
        pos = run(0, *self.target_args, self.pos_goal)
        # print('pos',pos)
        # fact(Decision_Node_Positive, self.rootid, pos)

        # neg = list(self.neg_rels[self.target](*self.target_args)({}))
        neg = run(0, *self.target_args, self.neg_goal)
        # print('neg',neg)
        # fact(Decision_Node_Negative, self.rootid, neg)

        score = self.gini_score(len(pos), len(neg))
        print('root score', score)

        expanding = []  # sorted by splitting score

        #heapq.heappush(expanding, (score, self.rootid))

        # def find_best_test():
        #     test = find_best_test()
        #     print('best test', test)
        #     pass

        def expand_node(node, typed_vars, examples, score):
            print('expanding node', node)
            print('current vars', typed_vars)
            best_score = 2
            bests = []
            test = None
            lex = None
            rex = None
            for rel, arg_types in modes:
                if rel not in relations:
                    continue
                rel = relations[rel]
                print('try relation', rel)
                print('mode', arg_types)
                argss = self._get_args(arg_types, typed_vars)
                argss = list(argss)
                print('argss', argss)
                for args in argss:
                    test = (rel, args)
                    print('args', args)
                    #node_ex_ss = self.get_node_ex_subs(node)
                    #s,lex,rex = self.score_test(test, node_ex_ss)
                    s, lex, rex = self.try_test(node, test, examples)
                    print('score', s)
                    choice = (s, node, arg_types, typed_vars, test, examples,
                              lex, rex)
                    # if s == 0:
                    #     continue
                    # elif s < best_score:
                    if s < best_score:
                        print('update bests')
                        best_score = s
                        bests = [choice]
                    elif s == best_score:
                        print('appending choice')
                        bests.append(choice)
                    print(bests)
            # if not bests or bests[0][0] >= score:
            #     print('no valuable test')
            #     #set as leaf node
            #     self.make_answer_node(node, examples)
            #     return
            best = random.sample(bests, 1)[0]
            print('best case when expanding node', node, best)
            heapq.heappush(expanding, best)

            return

        # test: (rel, args: var instances)
        # arg_types: type for each arg var
        def split_node(node, arg_types, typed_vars, test, examples, lex, rex):
            # Decision_Node_Test(node, test)
            fact(Decision_Node_Test, node, test)
            rel, args = test

            # typed_vars = defaultdict(list)
            # typed_vars.update(typed_vars) #make a shallow copy
            typed_vars = typed_vars.copy()  #make a shallow copy
            print('type_vars copy:', typed_vars)

            def _add_var(typename, var):
                # if typename not in typed_vars:
                #     typed_vars[typename] = [var]
                typed_vars[typename] = typed_vars[typename] + [
                    var
                ]  #build new list

            for i, arg in enumerate(args):
                guide, typename = arg_types[i]
                print(i, arg, guide, typename)
                if guide == '-':
                    _add_var(typename, arg)

            l = self._new_node_id(treeid)
            fact(Tree_Node_LChild, node, l)
            lex, (lpos, lneg) = lex
            # fact(Decision_Node_Example, l, lex)
            # fact(Decision_Node_Positive, l, lpos)
            # fact(Decision_Node_Negative, l, lneg)
            # for ex in map(partial(reify, self.target_args), lex):
            #     # print('ex', ex)
            #     fact(Decision_Node_Example, l, *ex)

            expand_node(l, typed_vars, lex, score)
            self.nodes.append(l)

            r = self._new_node_id(treeid)
            fact(Tree_Node_RChild, node, r)
            rex, (rpos, rneg) = rex
            # fact(Decision_Node_Example, r, rex)
            # fact(Decision_Node_Positive, r, rpos)
            # fact(Decision_Node_Negative, r, rneg)
            # for ex in map(partial(reify, self.target_args), rex):
            #     fact(Decision_Node_Example, r, *ex)

            expand_node(r, typed_vars, rex, score)
            self.nodes.append(r)

            self.num_splits += 1

        expand_node(self.rootid, self.target_typed_vars, self.examples, score)

        c = 0
        while expanding and self.num_splits < self.split_times:
            # _, node, test, lex, rex = heapq.heappop(expanding)
            # print(node, test, lex, rex)
            score, *expand = heapq.heappop(expanding)
            print('expanding', *expand)
            # split_node(node, test, lex, rex)
            split_node(*expand)
            self.print_tree(self.rootid)
            # c+=1
            # if c == 5:
            #     break

        # make answer nodes at leaves, which are what left in the expanding queue
        while expanding:
            score, *expand = heapq.heappop(expanding)
            node, arg_types, typed_vars, test, examples, lex, rex = expand
            self.make_answer_node(node, examples)

        return treeid