Example #1
0
 def _finetune_best(self):
     best_genome = None
     best_fitness = -100000
     for specie_key, specie in self.species.items():
         best_genome_at_specie = specie.get_fittest()
         if best_genome_at_specie.fitness > best_fitness:
             best_genome = best_genome_at_specie
             best_fitness = best_genome_at_specie.fitness
     best_genome_finetuned = self._finetune_genome(best_genome)
     self.config.parallel_evaluation = False
     evaluation_engine = EvaluationStochasticEngine()
     self.species_best_genome = evaluation_engine.evaluate(
         population={1: best_genome_finetuned})
Example #2
0
    def test_happy_path_miso(self):
        # Multiple-Input Single-Output
        self.config = create_configuration(
            filename='/classification-miso.json')
        self.config.parallel_evaluation = False
        self.config.n_processes = 1
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input, n_output=self.config.n_output)
        population = {1: genome}
        evaluation_engine = EvaluationStochasticEngine()

        population = evaluation_engine.evaluate(population=population)

        self.assertEqual(type(population.get(1).fitness), float)
Example #3
0
class EvolutionEngine:
    def __init__(self,
                 report: EvolutionReport,
                 notifier: Notifier,
                 is_cuda: bool = False):
        self.report = report
        self.notifier = notifier
        self.is_cuda = is_cuda

        self.population_engine = PopulationEngine(
            stagnation_engine=Stagnation())
        # self.speciation_engine = SpeciationEngine()
        self.speciation_engine = FixSpeciationEngine()
        self.evaluation_engine = EvaluationStochasticEngine()
        self.evolution_configuration = get_configuration()

        self.n_generations = self.evolution_configuration.n_generations

        self.population = None

        self.start_time = time.perf_counter()

    @timeit
    def run(self):
        logger.info('Started evolutionary process')
        end_condition = 'normal'
        # try:
        # initialize population
        self.population = self.population_engine.initialize_population()
        self.speciation_engine.speciate(self.population, generation=0)

        self.population = self.evaluation_engine.evaluate(
            population=self.population)

        # report
        self.report.report_new_generation(
            generation=0,
            population=self.population,
            species=self.speciation_engine.species)

        for generation in range(1, self.n_generations + 1):
            self._run_generation(generation)

            elapsed = time.perf_counter() - self.start_time
            if elapsed > TIMEOUT_SECONDS:
                end_condition = 'timeout'
                break

        if self.evolution_configuration.is_fine_tuning:
            fine_tuner = FineTuner(species=self.speciation_engine.species,
                                   config=self.evolution_configuration,
                                   is_cuda=self.is_cuda,
                                   only_best=False)
            fine_tuner.run()
            best_genomes = fine_tuner.species_best_genome
            # best_genomes = self.evaluation_engine.evaluate(population=best_genomes)
            self.report.report_fine_tuning(best_genomes)

        self.evaluation_engine.close()

        self.report.generate_final_report(end_condition=end_condition)\
                   .persist_report()
        self.report.persist_logs()
        # self.notifier.send(str(self.report.get_best_individual()))
        self._send_final_message()
        logger.info('Finished evolutionary process')

    @timeit
    def _run_generation(self, generation):
        # create new generation's population
        self.population = self.population_engine.reproduce(
            species=self.speciation_engine.species,
            pop_size=self.population_engine.pop_size,
            generation=generation)

        # evaluate
        self.population = self.evaluation_engine.evaluate(
            population=self.population)

        # create new species based on new population
        self.speciation_engine.speciate(self.population, generation=generation)

        # generation report
        self.report.report_new_generation(
            generation=generation,
            population=self.population,
            species=self.speciation_engine.species)

        # schedule parameters
        if generation == self.evolution_configuration.generation_fix_architecture:
            logger.info('Fixing Architecture')
            self.evolution_configuration.fix_architecture = True

    def _send_final_message(self):
        self.notifier.send(
            f'-----------------------------------------------------------------\n'
            f'Dataset: {self.report.dataset}. \nCorrelation id: {self.report.correlation_id}.\n'
            f'Execution id: {self.report.report.execution_id}')
        self.notifier.send(
            f'Best Fitness: {self.report.best_individual.fitness}')
        if self.report.generic_text is not None:
            self.notifier.send(str(self.report.generic_text))
        self.notifier.send(str(self.report.metrics_best))