Beispiel #1
0
    def test_eqns(self):
        p = MyProp(positive_feedback_rate=0.0, sigmoid_p=1.5)

        def query(g, features, k=10):
            activations_in = dict((f, 1.0) for f in features)
            activations_out = p.propagate(g, activations_in, num_iterations=10)
            tups = [(node, a) for (node, a) in activations_out.items()
                    if isinstance(node, Equation)]
            return nlargest(k, tups, itemgetter(1))

        def see(activations_d):
            for node, a in sorted(activations_d.items(), key=itemgetter(1)):
                print(f'{node!s:20s} {a:0.3f}')

        g = nx.Graph()

        # Make slipnet: a bipartite graph of Equations and features
        for a in range(1, 11):
            for b in range(1, 11):
                if b >= a:
                    continue
                for operator in [plus, minus, times]:
                    e = Equation((a, b), operator, operator.call(a, b))
                    g.add_node(e)
                    for f in e.features():
                        g.add_edge(f, e, weight=1.0)

        tups = query(g, [4, 5, Before(4), Before(5)], k=3)
        self.assertCountEqual(['5 + 4 = 9', '5 x 4 = 20', '5 - 4 = 1'],
                              [str(eqn) for (eqn, a) in tups])
Beispiel #2
0
 def move(self, operator: Operator, operands: List[int]) -> 'SolnState':
     avs = copy(self.avails)
     for operand in operands:
         avs.remove(operand)
         #TODO ValueError
     result = operator.call(*operands)
     expr = operator.name.join(str(n) for n in operands)
     return SolnState([result] + avs, last_move=f'{expr}={result}')
Beispiel #3
0
 def make_slipnet(self):
     slipnet = UTSlipnet(
         Equation((a, b), operator, operator.call(a, b))
         for a in range(1, 11) for b in range(1, 11)
         for operator in [plus, times, minus] if a >= b)
     slipnet.add_layer2_nodes(
         Equation((a, 1), operator, operator.call(a, 1))
         for a in range(10, 102) for operator in [plus, minus])
     slipnet.add_layer2_nodes(
         Equation((a, 2), plus, a + 2) for a in range(0, 102, 2))
     '''
     slipnet.add_layer2_nodes([
         Equation((4, 5, 6), plus, 15)
     ])
     slipnet.add_layer2_nodes([40, 50, 60])
     slipnet.add_edge(Leading(4), 40, weight=1.0)
     slipnet.add_edge(Leading(5), 50, weight=1.0)
     '''
     return slipnet
Beispiel #4
0
    def top(self,
            d: Dict[Hashable, float],
            type: Type,
            k: Union[int, None] = None) -> List[NodeA]:
        nas = [
            NodeA(node, a) for (node, a) in d.items()
            if isinstance(node, type)
        ]
        if k is None:
            return sorted(nas, key=attrgetter('a'), reverse=True)
        else:
            return nlargest(k, nas, key=attrgetter('a'))


slipnet = Slipnet(
    Equation((a, b), operator, operator.call(a, b)) for a in range(1, 11)
    for b in range(1, 11) for operator in [plus, times, minus] if a >= b)
slipnet.add_layer2_nodes([Equation((4, 5, 6), plus, 15)])
slipnet.add_layer2_nodes(
    Equation((a, 1), operator, operator.call(a, 1)) for a in range(10, 102)
    for operator in [plus, minus])
slipnet.add_layer2_nodes(
    Equation((a, 2), plus, a + 2) for a in range(0, 102, 2))

numble = Numble([4, 5, 6], 15)
ss0 = SolnState([4, 5, 6])
ss1 = ss0.move(plus, [4, 5])

sc0 = SolnCanvas.init([4, 5, 6])
sc1 = sc0.move(plus, [4, 5])
sc2 = sc1.move(plus, [9, 6])
Beispiel #5
0
        tups = [(node, a) for (node, a) in activations_out.items()
                if isinstance(node, Equation)]
        return nlargest(k, tups, itemgetter(1))

    def see(activations_d):
        for node, a in sorted(activations_d.items(), key=itemgetter(1)):
            print(f'{node!s:20s} {a:0.3f}')

    g = nx.Graph()

    for a in range(1, 11):
        for b in range(1, 11):
            if b >= a:
                continue
            for operator in [plus, minus, times]:
                e = Equation((a, b), operator, operator.call(a, b))
                g.add_node(e)
                for f in e.features():
                    g.add_edge(f, e, weight=1.0)

    #e1 = Equation((2, 3), plus, plus.call(2, 3))
    #print(e1)
#    g.add_node(e1)
#    for f in e1.features():
#        g.add_edge(f, e1, weight=1.0)

#    a0 = dict((f, 1.0) for f in [4, 5, Before(4), Before(5)])
#    #a0 = dict((f, 1.0) for f in [7, 6, Before(7), Before(6)])
#    see(a0)
#    print()
#
Beispiel #6
0
def make_dict(classes):
    return dict(zip(map(call('__name__'), classes, classes)))