Exemplo n.º 1
0
    def test_pons_asinorum(self):
        g0 = Graph.with_features(
            Equation.make_table(range(1, 20), range(1, 20),
                                [plus, minus, times]))
        g1 = Graph.augment(PrefixedGraph(1, g0), PrefixedGraph(2, g0))
        #        print('QQ')
        #        qq = list((x1, x1.with_prefix(2))
        #            for x1 in g1.query(WithPrefix(1, OfClass(After))))
        #        print(type(qq), type(qq[0]), len(qq[0]))
        #        pts(qq)
        g = g1.add_edges(
            EnumEdges(
                Hops.from_pairs(*(
                    (p_after, PrefixedNode(2, Before(p_after.unprefixed().x)))
                    for p_after in g1.query(WithPrefix(1, OfClass(After)))))))
        #pts(g.query(WithPrefix(1, OfClass(After))))
        #pts(g.query(WithPrefix(2, OfClass(Before))))
        #pts(g.successors_of(PrefixedNode(1, After(10))))
        #print()
        #pts(g.successors_of(PrefixedNode(1, After(63))))

        self.assertTrue(
            g.find_hop(PrefixedNode(1, After(10)), PrefixedNode(2,
                                                                Before(10))))
        self.assertFalse(
            g.find_hop(PrefixedNode(1, After(63)), PrefixedNode(2,
                                                                Before(63))))
Exemplo n.º 2
0
    def test_features_graph(self):
        e = Equation.make([3, 4], plus)
        g = Graph.with_features([e])
        #print(type(g.edges))
        #pr(g.edges)
        self.assertTrue(g.has_node(NumOperands(2)))
        self.assertTrue(g.has_node(2))

        self.assertEqual(g.hop_weight(e, plus), 1.0)
        self.assertEqual(g.hop_weight(plus, e), 1.0)
        self.assertEqual(g.hop_weight(e, 2), 0.0)
Exemplo n.º 3
0
    def test_features(self):
        @dataclass(frozen=True)
        class Parity(Feature):
            name: str

        Even = Parity('Even')
        Odd = Parity('Odd')

        @dataclass(frozen=True)
        class BaseNode:
            n: int

            def features_of(self):
                if self.n & 1:
                    yield Odd
                else:
                    yield Even
                yield self.n

        g = Graph.with_features([BaseNode(1), BaseNode(2), BaseNode(3)])

        self.assertCountEqual(g.query(OfClass(Parity)), [Odd, Even])
        self.assertCountEqual(g.hops_to_node(BaseNode(1)), [
            Hop(Odd, BaseNode(1), 1.0),
            Hop(1, BaseNode(1), 1.0),
            Hop(BaseNode, BaseNode(1), 1.0)
        ])
        self.assertCountEqual(g.hops_from_node(Odd), [
            Hop(Odd, BaseNode(1), 1.0),
            Hop(Odd, BaseNode(3), 1.0),
            Hop(Odd, Parity, 1.0)
        ])
        self.assertEqual(g.find_hop(BaseNode(2), Even),
                         Hop(BaseNode(2), Even, 1.0))
        self.assertEqual(g.find_hop(Even, Odd), None)

        # Now add mutual inhibition
        g = g.add_edges(MutualInhibition((Feature, int)))

        self.assertEqual(g.find_hop(Even, Odd), Hop(Even, Odd, -0.2))
        self.assertEqual(g.find_hop(Odd, Even), Hop(Odd, Even, -0.2))
        self.assertEqual(g.find_hop(1, 2), Hop(1, 2, -0.2))
Exemplo n.º 4
0
 def test_equation_table(self):
     g = Graph.with_features(
         Equation.make_table(range(1, 11), range(1, 11),
                             [plus, minus, times])).add_edges(
                                 MutualInhibition(Feature))
     self.assertEqual(g.hop_weight(Before(2), Before(3)), -0.2)
Exemplo n.º 5
0
    NewType, Type, ClassVar, Sequence, Callable, Hashable, Collection, \
    Sequence, Literal

import matplotlib.pyplot as plt  # type: ignore[import]
import numpy as np  # type: ignore[import]

from FARGModel import FARGModel, SeqCanvas, SeqState, CellRef, Want, Consume, \
    Before, After
from FMTypes import epsilon, Elem, Elems, Value, Addr, FMPred
from Graph2 import Graph, MutualInhibition
from Equation import Equation, Operator, plus, times, minus, EqnConsume
from util import pts, pl, pr

eqn_graph = Graph.with_features(
    EqnConsume.make(eqn)
        for eqn in Equation.make_table(
            range(1, 11), range(1, 11), [plus, minus, times]
        )
) #.add_edges(MutualInhibition((Feature, Equation, int), weight=-0.02))

class Numbo(FARGModel):

    def fill_slipnet(self):
        #self.slipnet = eqn_graph
        self.slipnet.base_graph = \
            Graph.augment(self.slipnet.base_graph, eqn_graph)

def r4_5_6__15(*args, **kwargs):
    global fm, ca, wa
    fm = Numbo(*args, **kwargs)
    ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)]))
    wa = fm.build(Want(15, CellRef(ca, 0)))
Exemplo n.º 6
0
from Equation import Equation, Operator, plus, times, minus
from Equation import IncreaseOrDecrease, Increase, Decrease, NumOperands, \
    Before, After, MaxBefore, MinBefore
from Graph2 import Graph, Node, Hop, Hops, Nodes, Edges, EnumNodes, EnumEdges, \
    OfClass, MutualInhibition, Feature, features_of, GraphPropagatorOutgoing, \
    PrefixedGraph, PrefixedNode, WithPrefix
from Slipnet2 import Slipnet, NodeA, TyrrellPropagator, \
    default_tyrrell_propagator
from FMTypes import epsilon
from util import as_iter, as_dict, union, pts, pr


eqn_graph = Graph.with_features(
    Equation.make_table(
        range(1, 11), range(1, 11), [plus, minus, times]
    )
) #.add_edges(MutualInhibition((Feature, Operator, Equation, int), weight=-5.0))
p = TyrrellPropagator(
    max_total=10.0,
    noise=0.0,
    positive_feedback_rate=0.0, #1.5,  # higher -> initial features matter more
    sigmoid_p=1.2,  #1.5 higher -> sharper distinctions, more salience
    num_iterations=10,
    alpha=0.95,
    tyrrell_alpha=0.05,  # 0.2
    tyrrell_beta=0.1
)
p1a = TyrrellPropagator(  # Makes 6+4=10 just barely win
    max_total=10.0,
    noise=0.0,
Exemplo n.º 7
0
        # .add_edges() from one PrefixedGraph to the other

        g2 = g.add_edges(
            EnumEdges(
                Hops.from_pairs((PrefixedNode(1, 'a'), PrefixedNode(2, 'a')))))
        self.assertEqual(
            g2.find_hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'a')),
            Hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'a'), 1.0))

        self.assertCountEqual(g2.query(OfClass(str)), [
            PrefixedNode(1, 'a'),
            PrefixedNode(1, 'b'),
            PrefixedNode(1, 'o'),
            PrefixedNode(2, 'a'),
            PrefixedNode(2, 'b'),
            PrefixedNode(2, 'o')
        ])
        self.assertCountEqual(
            g2.query(WithPrefix(2, OfClass(str))),
            [PrefixedNode(2, 'a'),
             PrefixedNode(2, 'b'),
             PrefixedNode(2, 'o')])
        self.assertCountEqual(g0.query(WithPrefix(2, OfClass(str))), [])


if __name__ == '__main__':
    # TODO mv this someplace where make_equations() is defined
    g = Graph.with_features(make_equations(1, 20, {plus, minus, times})) \
             .add_edges(MutualInhibition(Feature))