Beispiel #1
0
    def create_population(self, pop_size, genius=None):
        population = []
        
        for i in range(0, pop_size):
            creature = DNA()
            creature.generate_DNA()
            population.append(creature)

        if genius != None:
            population = population[:-1] + [genius]            

        return population
Beispiel #2
0
    def run(self, pcr_model_path="hybrid_pcr_model.ml", record_path=None, warm_up=True, stagnant_period=None):
        population = []
        if warm_up:
            best_creature = self.create_genius()
        else:
            best_creature = DNA()
            best_creature.generate_DNA()
        best_creature.score = -1000000
        cgeneration = 0

        if stagnant_period == None:
            stagnant_period = self.max_generation
        # Increase mutation chance if the fitness score not improved over generations
        # Reach the mutation limit in half of the stagnant period
        mutation_initial_value = self.mutation_chance
        mutation_step = (self.mutation_limit - mutation_initial_value) / stagnant_period * 2
        
        for noGeneration in range(0, self.max_generation):
            population = self.breed_population(population=population, genius=best_creature)
            print(f"-------- Generation={noGeneration} / {self.max_generation} Population={len(population)} --------")
                
            for loc, creature in enumerate(population):
                self.eval_fitness_score(creature=creature, pcr_model=self.load_model(pcr_model_path))
                print(f"Creature {loc} scores {creature.score:.2f} fitness points")

            # Rank the creature fitness scores
            population.sort(key=self.getScore, reverse=True)

            # Kill the bottom half of the population
            population = population[:self.pop_size // 2]

            if population[0].score > best_creature.score:
                best_creature = population[0].copy()
                self.mutation_chance = mutation_initial_value
                cgeneration = 0
                print(f"*** Generation {noGeneration} new genius scores {best_creature.score} fitness points\n\n")
            else:
                cgeneration += 1
                if self.mutation_chance < self.mutation_limit:
                    self.mutation_chance += mutation_step     
                print(f"-------- Stagnant Period = {cgeneration} / {stagnant_period}")           

            if cgeneration >= stagnant_period:
                print(f"WARNING: Population quality has reach its peak. Algorithm ends.\n")
                break

        print("==============================================================")
        print(f"The best creature score is {best_creature.score:.2f} fitness points")
        self.export_creature(creature=best_creature,
                             filepath=record_path[:-4] + f"score{int(best_creature.score)}" + record_path[-4:],
                             pcr_model_path=pcr_model_path,)