Exemple #1
0
 def test_decode_encode_uniform(self):
     for width in range(1, 5):
         for base in range(1, 5):
             space = StateSpace(width, base)
             for i in range(base**width):
                 decoded = space.decode(i)
                 encoded = space.encode(decoded)
                 self.assertEqual(i, encoded)
Exemple #2
0
 def test_encode_decode_uniform(self):
     for width in range(1, 5):
         for base in range(1, 5):
             space = StateSpace(width, base)
             for state in space:
                 encoded = space.encode(state)
                 decoded = space.decode(encoded)
                 self.assertEqual(state, decoded)
Exemple #3
0
 def test_encoding_uniform(self):
     for width in range(1, 5):
         for base in range(1, 5):
             space = StateSpace(width, base)
             counter = 0
             for state in space:
                 encoding = space.encode(state)
                 self.assertEqual(counter, encoding)
                 counter += 1
Exemple #4
0
    def test_invalid_base_type(self):
        with self.assertRaises(TypeError):
            StateSpace(5, base='a')

        with self.assertRaises(TypeError):
            StateSpace(3, base=2.5)

        with self.assertRaises(TypeError):
            StateSpace([1.0, 2.0, 3.0])
Exemple #5
0
 def test_decode_encode_nonuniform(self):
     for a in range(1, 5):
         for b in range(1, 5):
             for c in range(1, 5):
                 space = StateSpace([a, b, c])
                 for i in range(a * b * c):
                     decoded = space.decode(i)
                     encoded = space.encode(decoded)
                     self.assertEqual(i, encoded)
Exemple #6
0
 def test_encode_decode_nonuniform(self):
     for a in range(1, 5):
         for b in range(1, 5):
             for c in range(1, 5):
                 space = StateSpace([a, b, c])
                 for state in space:
                     encoded = space.encode(state)
                     decoded = space.decode(encoded)
                     self.assertEqual(state, decoded)
Exemple #7
0
 def test_encoding_nonuniform(self):
     for a in range(1, 5):
         for b in range(1, 5):
             for c in range(1, 5):
                 space = StateSpace([a, b, c])
                 counter = 0
                 for state in space:
                     encoding = space.encode(state)
                     self.assertEqual(counter, encoding)
                     counter += 1
Exemple #8
0
    def test_states_boolean(self):
        space = StateSpace(1)
        self.assertEqual([[0], [1]], list(space))

        space = StateSpace(2)
        self.assertEqual([[0, 0], [1, 0], [0, 1], [1, 1]], list(space))

        space = StateSpace(3)
        self.assertEqual([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],
                          [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]],
                         list(space))
Exemple #9
0
    def test_states_boolean_list(self):
        space = StateSpace([2])
        self.assertEqual([[0], [1]], list(space))

        space = StateSpace([2, 2])
        self.assertEqual([[0, 0], [1, 0], [0, 1], [1, 1]], list(space))

        space = StateSpace([2, 2, 2])
        self.assertEqual([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],
                          [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]],
                         list(space))
Exemple #10
0
    def test_states_nonboolean(self):
        space = StateSpace(1, base=1)
        self.assertEqual([[0]], list(space))

        space = StateSpace(1, base=3)
        self.assertEqual([[0], [1], [2]], list(space))

        space = StateSpace(2, base=1)
        self.assertEqual([[0, 0]], list(space))

        space = StateSpace(2, base=3)
        self.assertEqual([[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1],
                          [0, 2], [1, 2], [2, 2]], list(space))
Exemple #11
0
    def test_invalid_spec_value(self):
        with self.assertRaises(ValueError):
            StateSpace(0)

        with self.assertRaises(ValueError):
            StateSpace(-1)

        with self.assertRaises(ValueError):
            StateSpace([])

        with self.assertRaises(ValueError):
            StateSpace([0])

        with self.assertRaises(ValueError):
            StateSpace([-1])
Exemple #12
0
    def test_states_nonboolean_list(self):
        space = StateSpace([1])
        self.assertEqual([[0]], list(space))

        space = StateSpace([3])
        self.assertEqual([[0], [1], [2]], list(space))

        space = StateSpace([1, 2])
        self.assertEqual([[0, 0], [0, 1]], list(space))

        space = StateSpace([1, 3])
        self.assertEqual([[0, 0], [0, 1], [0, 2]], list(space))

        space = StateSpace([3, 3])
        self.assertEqual([[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1],
                          [0, 2], [1, 2], [2, 2]], list(space))
Exemple #13
0
 def test_states_count(self):
     xs = [3, 5, 2, 5, 2, 1, 4, 2]
     space = StateSpace(xs)
     count = 0
     for state in space:
         count += 1
     self.assertEqual(np.product(xs), count)
Exemple #14
0
 def test_decoding_uniform(self):
     for width in range(1, 5):
         for base in range(1, 5):
             space = StateSpace(width, base)
             states = list(space)
             decoded = list(map(space.decode, range(space.volume)))
             self.assertEqual(states, decoded)
Exemple #15
0
    def test_long_encoding(self):
        state_space = StateSpace(10)
        code = state_space.encode(np.ones(10, dtype=int))
        print(type(code))
        self.assertIsInstance(code, long)

        state_space = StateSpace(68)
        code = state_space.encode(np.ones(68, dtype=int))
        self.assertIsInstance(code, long)

        state_space = StateSpace(100)
        code = state_space.encode(np.ones(100, dtype=int))
        self.assertIsInstance(code, long)
Exemple #16
0
 def test_decoding_nonuniform(self):
     for a in range(1, 5):
         for b in range(1, 5):
             for c in range(1, 5):
                 space = StateSpace([a, b, c])
                 states = list(space)
                 decoded = list(map(space.decode, range(space.volume)))
                 self.assertEqual(states, decoded)
Exemple #17
0
    def test_check_states_uniform(self):
        state_space = StateSpace(3)
        self.assertTrue([0, 1, 1] in state_space)
        self.assertFalse([0, 0] in state_space)
        self.assertFalse([1, 2, 0] in state_space)

        self.assertFalse([0, 1, 1] not in state_space)
        self.assertTrue([0, 0] not in state_space)
        self.assertTrue([1, 2, 0] not in state_space)
Exemple #18
0
    def test_uniform_bases(self):
        spec = StateSpace(5)
        self.assertTrue(spec.is_uniform)
        self.assertEqual(5, spec.ndim)
        self.assertEqual(2, spec.base)
        self.assertEqual(32, spec.volume)

        spec = StateSpace(8, base=4)
        self.assertTrue(spec.is_uniform)
        self.assertEqual(8, spec.ndim)
        self.assertEqual(4, spec.base)
        self.assertEqual(65536, spec.volume)

        spec = StateSpace([3, 3, 3, 3])
        self.assertTrue(spec.is_uniform)
        self.assertEqual(4, spec.ndim)
        self.assertEqual(3, spec.base)
        self.assertEqual(81, spec.volume)
Exemple #19
0
    def test_check_states_varied(self):
        self.assertTrue([0, 2, 1] in StateSpace([2, 3, 2]))
        self.assertFalse([0, 1] in StateSpace([2, 2, 3]))
        self.assertFalse([1, 1, 6] in StateSpace([2, 3, 4]))

        self.assertFalse([0, 2, 1] not in StateSpace([2, 3, 2]))
        self.assertTrue([0, 1] not in StateSpace([2, 2, 3]))
        self.assertTrue([1, 1, 6] not in StateSpace([2, 3, 4]))
Exemple #20
0
    def state_space(self):
        """
        Return a :class:`neet.statespace.StateSpace` object for the network.

        .. doctest:: wtnetwork

            >>> net = WTNetwork(3)
            >>> net.state_space()
            <neet.statespace.StateSpace object at 0x...>
            >>> space = net.state_space()
            >>> list(space)
            [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]

        :returns: the network's :class:`neet.statespace.StateSpace`
        """
        return StateSpace(self.size, base=2)
Exemple #21
0
    def state_space(self):
        """
        Return a :class:`neet.statespace.StateSpace` object for the
        cellular automaton lattice.

        .. rubric:: Examples

        .. doctest:: automata

            >>> eca = RewiredECA(30, size=3)
            >>> eca.state_space()
            <neet.statespace.StateSpace object at 0x...>
            >>> space = eca.state_space()
            >>> list(space)
            [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]

        :returns: :class:`neet.statespace.StateSpace`
        """
        return StateSpace(self.__size, base=2)
Exemple #22
0
    def state_space(self, n):
        """
        Return a :class:`neet.statespace.StateSpace` object for a
        lattice of length ``n``.

        .. doctest:: automata

            >>> eca = ECA(30)
            >>> eca.state_space(3)
            <neet.statespace.StateSpace object at 0x...>
            >>> space = eca.state_space(3)
            >>> list(space)
            [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]

        :param n: the number of nodes in the lattice
        :type n: int
        :returns: :class:`neet.statespace.StateSpace`
        :raises ValueError: if ``n < 1``
        """
        return StateSpace(n, base=2)
Exemple #23
0
    def test_base_mismatch(self):
        with self.assertRaises(ValueError):
            StateSpace([2, 2, 2], base=3)

        with self.assertRaises(ValueError):
            StateSpace([3, 3, 3], base=2)

        with self.assertRaises(ValueError):
            StateSpace([2, 2, 3], base=2)

        with self.assertRaises(ValueError):
            StateSpace([2, 2, 3], base=3)

        StateSpace([2, 2, 2], base=2)
        StateSpace([3, 3, 3], base=3)
Exemple #24
0
    def __init__(self, table, names=None, reduced=False):
        """
        Construct a network from a logic truth table.

        A truth table stores a list of tuples, one for each node in order. A
        tuple of the form `(A, {C1, C2, ...})` at index `i` provides the
        activation conditions for the node of index `i`. `A` is a tuple marking
        the indices of the nodes which influence the state of node `i` via
        logic relations. `{C1, C2, ...}` is a set, each element of which is the
        collection of binary states of these influencing nodes that would
        activate node `i`, setting it to `1`. Any other collection of states of
        nodes in `A` are assumed to deactivate node `i`, setting it to `0`.

        `C1`, `C2`, etc. are sequences (`tuple` or `str`) of binary digits,
        each being the binary state of corresponding node in `A`.

        .. rubric:: Examples

        .. doctest:: logicnetwork

            >>> net = LogicNetwork([((0,), {'0'})])
            >>> net.size
            1
            >>> net.table
            [((0,), {'0'})]

        .. doctest:: logicnetwork

            >>> net = LogicNetwork([((1,), {'0', '1'}), ((0,), {'1'})])
            >>> net.size
            2
            >>> net.table == [((1,), {'0', '1'}), ((0,), {'1'})]
            True

        .. doctest:: logicnetwork

            >>> net = LogicNetwork([((1, 2), {'01', '10'}),
            ... ((0, 2), ((0, 1), '10', [1, 1])),
            ... ((0, 1), {'11'})], ['A', 'B', 'C'])
            >>> net.size
            3
            >>> net.names
            ['A', 'B', 'C']
            >>> net.table == [((1, 2), {'01', '10'}),
            ... ((0, 2), {'01', '11', '10'}), ((0, 1), {'11'})]
            True

        :param table: the logic table
        :param names: names of nodes, default None
        """
        if not isinstance(table, (list, tuple)):
            raise TypeError("table must be a list or tuple")

        self.__size = len(table)

        if names:
            if not isinstance(names, (list, tuple)):
                raise TypeError("names must be a list or tuple")
            elif len(names) != self.__size:
                raise ValueError("number of names must match network size")
            else:
                self.names = list(names)

        # Store positive truth table for human reader.
        self.table = []
        for row in table:
            # Validate incoming indices.
            if not (isinstance(row, (list, tuple)) and len(row) == 2):
                raise ValueError("Invalid table format")
            for idx in row[0]:
                if idx >= self.__size:
                    raise IndexError("mask index out of range")
            # Validate truth table of the sub net.
            if not isinstance(row[1], (list, tuple, set)):
                raise ValueError("Invalid table format")
            conditions = set()
            for condition in row[1]:
                conditions.add(''.join([str(long(s)) for s in condition]))
            self.table.append((row[0], conditions))

        if reduced:
            self.reduce_table()

        self._state_space = StateSpace(self.__size, base=2)

        # Encode truth table for faster computation.
        self._encode_table()

        self.metadata = {}
Exemple #25
0
 def state_space(self):
     return StateSpace([1, 2, 3])
Exemple #26
0
 def state_space(self):
     return StateSpace(1, base=3)
Exemple #27
0
 def state_space(self):
     return StateSpace(1)
Exemple #28
0
    def test_invalid_base_value(self):
        with self.assertRaises(ValueError):
            StateSpace(3, base=0)

        with self.assertRaises(ValueError):
            StateSpace(4, base=-1)
Exemple #29
0
 def test_nonuniform_bases(self):
     spec = StateSpace([1, 2, 3, 2, 1])
     self.assertFalse(spec.is_uniform)
     self.assertEqual([1, 2, 3, 2, 1], spec.base)
     self.assertEqual(5, spec.ndim)
     self.assertEqual(12, spec.volume)
Exemple #30
0
    def test_invalid_spec_type(self):
        with self.assertRaises(TypeError):
            StateSpace("a")

        with self.assertRaises(TypeError):
            StateSpace("abc")