Exemplo n.º 1
0
def load_top_individuals(population_size, network_width, network_hidden_layers,
                         observation_space, action_space, environment_name,
                         total_population_counter):
    initial_population = []
    for i in range(NUM_SELECTED_FOR_REPRODUCTION):
        action_predictor_model = create_model(network_width,
                                              network_hidden_layers,
                                              observation_space, action_space)

        dir_path = os.path.realpath(".")
        fn = dir_path + "/" + "" + versionName + "" + str(i) + "-weights.h5"
        print("filepath ", fn)
        if os.path.isfile(fn):
            print("loading weights")
            action_predictor_model.load_weights("" + versionName + "" +
                                                str(i) + "-weights.h5")
        else:
            print("File ", "" + versionName + "" + str(i) + "-weights.h5",
                  " does not exis. Retraining... ")

        indiv = Individual(generationID=0,
                           indivID=total_population_counter,
                           network=action_predictor_model)
        total_population_counter += 1
        initial_population.append(indiv)
    return initial_population, total_population_counter
Exemplo n.º 2
0
def initialize_population(population_size,network_width,network_hidden_layers, observation_space, action_space, environment_name,total_population_counter):
    initial_population = []
    for i in range (population_size):
        action_predictor_model = create_model(network_width,network_hidden_layers, observation_space, action_space)
        indiv = Individual(generationID=0, indivID=total_population_counter , network = action_predictor_model)
        total_population_counter += 1
        initial_population.append(indiv)
    return initial_population, total_population_counter
Exemplo n.º 3
0
def populate_next_generation(generationID, top_individuals, population_size,
                             network_width, network_hidden_layers,
                             observation_space, action_space,
                             total_population_counter):
    newPop = top_individuals
    num_selected = len(top_individuals)
    for i in range(population_size - len(top_individuals)):
        newModel = create_model(network_width, network_hidden_layers,
                                observation_space, action_space)
        model1 = top_individuals[0].network
        model2 = top_individuals[1].network
        sz = len(newModel.layers)
        #if largeNoise:
        #    print("Setting Large Noise!")
        for k in range(sz):
            w = newModel.layers[k].get_weights()
            w1 = model1.layers[k].get_weights()
            w2 = model2.layers[k].get_weights()

            if np.alen(w) > 0:
                #print("k==>",k)
                #w[0][0] = combine_weights(w[0][0],w1[0][0],w2[0][0])
                for j in range(np.alen(w[0])):
                    y = w[0][j]
                    y1 = w1[0][j]
                    y2 = w2[0][j]
                    for l in range(np.alen(y)):
                        z = y[l]
                        #chrose randomly from top_individuals
                        parentID = randint(0, num_selected - 1)
                        parent = top_individuals[parentID]
                        parentNetwork = parent.network
                        z1 = parentNetwork.layers[k].get_weights()[0][j][l]
                        z = z1 + 0.0
                        y[l] = z
                    w[0][j] = y

            newModel.layers[k].set_weights(w)
        top_individuals.append(
            Individual(generationID, total_population_counter, newModel))
        total_population_counter += 1
    return top_individuals, total_population_counter
Exemplo n.º 4
0
def populate_next_generation(generationID, top_individuals, population_size,
                             network_width, network_hidden_layers,
                             observation_space, action_space,
                             total_population_counter):
    newPop = top_individuals
    for i in range(population_size - len(top_individuals)):
        newModel = create_model(network_width, network_hidden_layers,
                                observation_space, action_space)
        model1 = top_individuals[0].network
        model2 = top_individuals[1].network
        sz = len(newModel.layers)
        #if largeNoise:
        #    print("Setting Large Noise!")
        for k in range(sz):
            w = newModel.layers[k].get_weights()
            w1 = model1.layers[k].get_weights()
            w2 = model2.layers[k].get_weights()

            if np.alen(w) > 0:
                #print("k==>",k)
                #w[0][0] = combine_weights(w[0][0],w1[0][0],w2[0][0])
                for j in range(np.alen(w[0])):
                    y = w[0][j]
                    y1 = w1[0][j]
                    y2 = w2[0][j]
                    for l in range(np.alen(y)):
                        z = y[l]
                        z1 = y1[l]
                        z2 = y2[l]
                        if np.random.rand(1) > 0.5:
                            z = z1 + 0.0
                        else:
                            z = z2 + 0.0
                        y[l] = z
                    w[0][j] = y

            newModel.layers[k].set_weights(w)
        top_individuals.append(
            Individual(generationID, total_population_counter, newModel))
        total_population_counter += 1
    return top_individuals, total_population_counter