コード例 #1
0
ファイル: test_neural_network.py プロジェクト: nicrox89/LEAP
def test_decode2():
    """If we receive a genome that is too long, throw an exception."""
    n_inputs = 10
    n_outputs = 5

    dec = neural_network.SimpleNeuralNetworkDecoder((n_inputs, n_outputs))
    correct_length = (n_inputs + 1)*n_outputs  # +1 because there is an extra bias weight
    genome = np.random.uniform(0, 1, correct_length + 1)

    with pytest.raises(ValueError):
        nn = dec.decode(genome)
コード例 #2
0
ファイル: test_neural_network.py プロジェクト: nicrox89/LEAP
def test_decode3():
    """If we receive a genome that is too short, throw an exception."""
    n_inputs = 10
    n_outputs = 5

    # Adding one more "input" to the weights shape to serve as the bias weight
    dec = neural_network.SimpleNeuralNetworkDecoder((n_inputs, n_outputs))
    correct_length = (n_inputs + 1)*n_outputs   # +1 because there is an extra bias weight
    genome = np.random.uniform(0, 1, correct_length - 1)

    with pytest.raises(ValueError):
        nn = dec.decode(genome)
コード例 #3
0
ファイル: test_neural_network.py プロジェクト: nicrox89/LEAP
def test_decode1():
    """If we're expecting a single layer, create a single weight matrix from
    the genome."""
    n_inputs = 10
    n_outputs = 5

    # Adding one more "input" to the weights shape to serve as the bias weight
    dec = neural_network.SimpleNeuralNetworkDecoder((n_inputs, n_outputs))

    genome = list(range(0, n_outputs))*(n_inputs + 1)
    expected = np.array([ [ 0, 1, 2, 3, 4 ] ] * (n_inputs + 1)) # +1 because there is an extra bias weight
    nn = dec.decode(genome)
    matrix = nn.weight_matrices[0]
    assert((expected == matrix).all())
コード例 #4
0
def neural_representation(environment, num_hidden_nodes):
    """Return a neural network representation suitable for learning a
    controller for this environment."""
    num_inputs = int(np.prod(environment.observation_space.shape))
    num_actions = environment.action_space.n

    # Decode genomes into a feed-forward neural network,
    # but also wrap an argmax around the networks so their
    # output is a single integer
    decoder = executable.WrapperDecoder(
        wrapped_decoder=neural_network.SimpleNeuralNetworkDecoder(
            shape=(num_inputs, num_hidden_nodes, num_actions)),
        decorator=executable.ArgmaxExecutable)

    # Initialized genomes are random real-valued vectors.
    initialize = create_real_vector(bounds=([[-1, 1]] *
                                            decoder.wrapped_decoder.length))

    return Representation(decoder, initialize)
コード例 #5
0
ファイル: test_neural_network.py プロジェクト: nicrox89/LEAP
def test_decode4():
    """If we're expecting three layers, create three weight matrices out of 
    the genome."""
    n_inputs = 4
    n_hidden1 = 3
    n_hidden2 = 2
    n_outputs = 2

    shape = [ n_inputs, n_hidden1, n_hidden2, n_outputs ]
    dec = neural_network.SimpleNeuralNetworkDecoder(shape)

    genome = list(range(0, 29))
    expected = [ np.reshape(np.arange(0, 15), (5, 3)),
                 np.reshape(np.arange(15, 23), (4, 2)),
                 np.reshape(np.arange(23, 29), (3, 2)) ]
    
    nn = dec.decode(genome)
    result = nn.weight_matrices
    assert(len(expected) == len(result))
    print(expected)
    print(result)
    for e, r in zip(expected, result):
        assert((e == r).any())
コード例 #6
0
    if os.environ.get(test_env_var, False) == 'True':
        generations = 2
    else:
        generations = 1000

    # Load the OpenAI Gym simulation
    environment = gym.make('CartPole-v0')

    # Representation
    num_inputs = 4
    num_actions = environment.action_space.n
    # Decode genomes into a feed-forward neural network,
    # but also wrap an argmax around the networks so their
    # output is a single integer
    decoder = executable.WrapperDecoder(
        wrapped_decoder=neural_network.SimpleNeuralNetworkDecoder(
            shape=(num_inputs, num_hidden_nodes, num_actions)),
        decorator=executable.ArgmaxExecutable)

    with open('./genomes.csv', 'w') as genomes_file:

        ea = generational_ea(
            max_generations=generations,
            pop_size=pop_size,
            # Solve a problem that executes agents in the
            # environment and obtains fitness from it
            problem=problems.EnvironmentProblem(runs_per_fitness_eval,
                                                simulation_steps,
                                                environment,
                                                'reward',
                                                gui=gui),
            representation=Representation(initialize=create_real_vector(