Example #1
0
 def tournamentSelection(self, pop):
     tournament = p.Population(journey_manager=self.journey_manager, population_size=self.tournament_size, initialise=False)
     for i in range(0, self.tournament_size):
         randomId = int(random.random() * pop.size_of_population())
         tournament.save_journey(i, pop.get_journey(randomId))
     fittest = tournament.get_fittest()
     return fittest
Example #2
0
    def test_network_env_integration(self):
        """
        Verifies that Environment constructors are working
        """
        simulator = environment.Environment()
        observation = simulator.reset()

        pop = population.Population(10, 1, 2)

        for i, model in enumerate(pop.members):
            print("Member ", i)
            observation = simulator.env.reset()
            for t in range(1000):
                output = model.run(observation)
                action = model.get_category(output)
                random_action = simulator.env.action_space.sample()
                observation, reward, done, info = simulator.step(action)
                # print("Observation: ", observation)
                # print("Reward: ", reward)
                # print("Network Output: ", output)
                # print("Network Action: ", action)
                # print("Random Action: ", random_action)

                if done:
                    print("Episode finished after {} timesteps".format(t + 1))
                    break
        simulator.close()
Example #3
0
def test_crossover():
    """
    Verifies that the population crossover function works
    """
    parent_a = net.Network(
        1,
        2,
        [11, 9, 7, 5, 3],
        ['relu', 'relu', 'relu', 'softmax', 'softmax'],
    )
    parent_b = net.Network(
        1,
        2,
        [10, 8, 6],
        ['relu', 'softmax', 'softmax'],
    )
    print("---------------------------------------")
    print("---------------------------------------")
    # for i in parent_a.model.get_weights():
    #     print(i)
    #     print("--------------")
    # print(parent_a.model.layers)

    crosser = pop.Population(0, 1, 2)
    child_a, child_b = crosser.cross(parent_a, parent_b)
    print("Child A:\n", child_a)
    print("---------------------------------------")
    print("Child B:\n", child_b)
Example #4
0
    def evolvePopulation(self, pop):
        #print(f'size matters {pop.size_of_population()}')
        newPopulation = p.Population(journey_manager=self.journey_manager, population_size=pop.size_of_population(), initialise=False)
        elitismOffset = 0
        if self.elitism:
            newPopulation.save_journey(0, pop.get_fittest())
            elitismOffset = 1

        for i in range(elitismOffset, newPopulation.size_of_population()):
            parent1 = self.tournamentSelection(pop)
            parent2 = self.tournamentSelection(pop)
            child = self.crossover(parent1, parent2)
            newPopulation.save_journey(i, child)

        for i in range(elitismOffset, newPopulation.size_of_population()):
            self.mutate(newPopulation.get_journey(i))

        return newPopulation
Example #5
0
 def test_crossover(self):
     parent_a = network.Network(
         1,
         2,
         [11, 9, 7, 5, 3],
         ['relu', 'relu', 'relu', 'softmax', 'softmax'],
     )
     parent_b = network.Network(
         1,
         2,
         [10, 8, 6],
         ['relu', 'softmax', 'softmax'],
     )
     print("---------------------------------------")
     print("---------------------------------------")
     # for i in parent_a.model.get_weights():
     #     print(i)
     #     print("--------------")
     # print(parent_a.model.layers)
     pop = population.Population(0, 1, 2)
     child_a, child_b = pop.cross(parent_a, parent_b)
     print("Child A:\n", child_a)
     print("---------------------------------------")
     print("Child B:\n", child_b)
Example #6
0
 def test_load_pop_from_file(self):
     pop = population.Population(0, 1, 1)
     pop.load_from_file("./autogen_dir_9/gen-1.txt")
Example #7
0
 def test_population_creation(self):
     """
     Verifies that Population constructors are working
     """
     pop = population.Population(5, 1, 2)
     print("Population Creation is Online...")
Example #8
0
 def test_evolution(self):
     env = gamemaster.GameMaster('MountainCarContinuous-v0')
     pop = population.Population(20, 1, 1, evaluator=env)
     pop.step_gen()
Example #9
0
    parent_selection_portion = 0.5
    Noffsprings = 2 * pop_size
    mutation_p = 0.1

    scores = np.zeros(shape=(number_of_generations))
    # Getting data
    cities_data = data.data()
    cities_data.read_csv(path_to_datafile)
    representation = cities_data.get_representation(start=0, N=subset_sizes[1])
    subset_data = cities_data.get_subset(subset_sizes[1])
    # ip.embed()
    # Population
    cur_population = population.Population(
        Genotype=genotype.Genotype,
        representation=representation,
        evaluator=fit,
        population_size=pop_size,
        parent_selection_portion=parent_selection_portion,
        number_of_offsprings=Noffsprings,
        mutation_probability=mutation_p)

    # -  Evaluate
    res = cur_population.evaluate(df=subset_data, genotype_set="population")
    scores[0] = np.sum(res) / len(res)
    # -  Loop
    for generation in range(number_of_generations):
        # ip.embed()
        # -  Parent Selection
        cur_population.parent_selection()
        # -  Recombine - Crossover
        cur_population.recombination()
        # -  Mutate
Example #10
0
    journey_manager.add_journey(journey5)
    journey6 = jy.Journey(start_time=start_time)
    journey_manager.add_journey(journey6)
    journey7 = jy.Journey(start_time=start_time)
    journey_manager.add_journey(journey7)
    journey8 = jy.Journey(start_time=start_time)
    journey_manager.add_journey(journey8)
    journey9 = jy.Journey(start_time=start_time)
    journey_manager.add_journey(journey9)
    journey10 = jy.Journey(start_time=start_time)
    journey_manager.add_journey(journey10)

    available_stops = ['a', 'b', 'c', 'd', 'e']

    pop = p.Population(journey_manager=journey_manager,
                       available_stops=available_stops,
                       population_size=20,
                       initialise=True)

    print(f'fittest')
    fittest_allocation = pop.get_fittest()
    print(
        f"Initial fitness: {fittest_allocation.get_fitness()} stops {fittest_allocation.journey_allocation}"
    )

    ga = gen.GeneticAlgorithm(journey_manager)
    pop = ga.evolvePopulation(pop)
    for i in range(0, 100):
        pop = ga.evolvePopulation(pop)

    # Print final results
    print("Finished")