コード例 #1
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
コード例 #2
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])
コード例 #3
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])
コード例 #4
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])
コード例 #5
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])