示例#1
0
 def test_transition_multiple(self):
     sm = StateMachineNetwork(
         [State(1), State(2)],
         [Transition(1, 2), Transition(2, 1)])
     self.assertEqual(len(sm.transitions), 2)
     self.assertEqual(len(sm.transitions[1]), 1)
     self.assertEqual(len(sm.transitions[2]), 1)
示例#2
0
    def test_execution_true_transition(self):
        sm = StateMachineNetwork([State(1), State(2)], [Transition(1, 2)])
        sm.states[1].set_biases([1, 2])
        sm.states[1].set_weights([[1, 2], [3, 4]])
        sm.transitions[1][0].add_condition(Condition(0, operator.eq, 5))

        next_state, output = sm.activate(1, [5, 10])
        self.assertEqual(next_state, 2)
        self.assertEqual(output, [26, 57])
示例#3
0
    def test_execution_multiple_transitions_all_false(self):
        sm = StateMachineNetwork(
            [State(1), State(2), State(3)],
            [Transition(1, 2), Transition(1, 2)])
        sm.states[1].set_biases([1, 2])
        sm.states[1].set_weights([[1, 2], [3, 4]])
        sm.transitions[1][0].add_condition(Condition(0, operator.eq, 4))
        sm.transitions[1][1].add_condition(Condition(1, operator.gt, 10))

        next_state, output = sm.activate(1, [5, 10])
        self.assertEqual(1, next_state)
        self.assertEqual(output, [26, 57])
示例#4
0
    def test_execution_no_transitions(self):
        sm = StateMachineNetwork([State(1)], [])
        sm.states[1].set_biases([1, 2])
        sm.states[1].set_weights([[1, 2], [3, 4]])

        next_state, output = sm.activate(1, [5, 10])
        self.assertEqual(next_state, 1)
        self.assertEqual(output, [26, 57])
示例#5
0
def compare_state_outcomes(genome, sensor_input_file):
    # This function evaluates the difference of the states by running it on a randomly generated set
    # input values and reporting the distance between the output vectors of the states.

    # Load the randomly generated sensor inputs
    with open(sensor_input_file, 'r') as csv_file:
        csv_reader = csv.reader(csv_file, quoting=csv.QUOTE_NONNUMERIC)

        sensor_vectors = [row for row in csv_reader]

    # Evaluate the sensor inputs on each genome.
    state_differences = dict()

    activations = ActivationFunctionSet()
    aggregations = AggregationFunctionSet()
    for enc_state1 in itervalues(genome.states):

        state1 = State(1, enc_state1.weights, enc_state1.biases,
                       aggregations.get(enc_state1.aggregation),
                       activations.get(enc_state1.activation))

        for enc_state2 in itervalues(genome.states):

            key_pair = (enc_state1.key, enc_state2.key)
            if key_pair[0] != key_pair[1] and key_pair not in state_differences \
                    and key_pair[::-1] not in state_differences:

                state2 = State(2, enc_state2.weights, enc_state2.biases,
                               aggregations.get(enc_state2.aggregation),
                               activations.get(enc_state2.activation))

                output_pairs = [(state1.activate(inputs),
                                 state2.activate(inputs))
                                for inputs in sensor_vectors]
                distances = [
                    np.average(
                        np.abs(
                            np.array(output_pair[0]) -
                            np.array(output_pair[1])))
                    for output_pair in output_pairs
                ]

                state_differences[key_pair] = np.average(distances), np.std(
                    distances)

    return state_differences
示例#6
0
 def test_2_in_1_out(self):
     state = State(42)
     state.set_biases([0])
     state.set_weights([[2, 3]])
     self.assertListEqual(state.activate([5, 10]), [40])
示例#7
0
 def test_init(self):
     state = State(42)
     self.assertEqual(state.id, 42)
     self.assertIsNone(state.biases)
     self.assertIsNone(state.weights)
示例#8
0
 def test_add_state(self):
     sm = StateMachineNetwork([State(1)], [])
     self.assertEqual(len(sm.states), 1)
     self.assertEqual(sm.states[1].id, 1)
示例#9
0
 def test_transition_multiple_begin_state(self):
     sm = StateMachineNetwork(
         [State(1), State(2), State(3)],
         [Transition(1, 2), Transition(1, 3)])
     self.assertEqual(len(sm.transitions), 3)
     self.assertEqual(len(sm.transitions[1]), 2)
示例#10
0
 def test_invalid_transition(self):
     self.assertRaises(ValueError, StateMachineNetwork, [State(1)],
                       [Transition(4, 2)])
示例#11
0
 def test_1_in_1_out(self):
     state = State(42)
     state.set_biases([1])
     state.set_weights([[2]])
     self.assertListEqual(state.activate([5]), [11])
示例#12
0
 def test_not_without_weights(self):
     state = State(42)
     state.set_biases([2, 3])
     self.assertRaises(AssertionError, state.activate, [5, 10])
示例#13
0
 def test_2_in_3_out(self):
     state = State(42)
     state.set_biases([0, 1, 2])
     state.set_weights([[2, 3], [4, 5], [1, 2]])
     self.assertListEqual(state.activate([5, 10]), [40, 71, 27])
示例#14
0
 def test_3_in_2_out(self):
     state = State(42)
     state.set_biases([0, 1])
     state.set_weights([[2, 3, 1], [4, 5, 1]])
     self.assertListEqual(state.activate([5, 10, 20]), [60, 91])
示例#15
0
 def test_invalid_state(self):
     sm = StateMachineNetwork([State(1)], [])
     self.assertRaises(KeyError, sm.activate, 3, [5, 10])
示例#16
0
 def test_to_little_weight_length(self):
     state = State(42)
     state.set_biases([2, 3])
     state.set_weights([[2], [2]])
     self.assertRaises(AssertionError, state.activate, [5, 2])
示例#17
0
 def test_to_many_weight_length(self):
     state = State(42)
     state.set_biases([2, 2])
     state.set_weights([[2, 3, 3], [3, 4, 5]])
     self.assertRaises(AssertionError, state.activate, [5, 10])
示例#18
0
    def test_add_state_twice(self):
        with self.assertRaises(ValueError) as context:
            StateMachineNetwork([State(1), State(1)], [])

        self.assertTrue('State included twice' in str(context.exception))
示例#19
0
 def test_too_short_bias_length(self):
     state = State(42)
     state.set_biases([2])
     state.set_weights([[2, 3], [4, 5]])
     self.assertRaises(AssertionError, state.activate, [5, 10])