def run(): try: problem = BostonHousingProblem( scoring_function=Scoring.MeanAbsoluteError, normaliser=Normaliser.RobustScaler, folds=2) """ Regressor choices: - AdaBoostRegressorChromosome - DecisionTreeRegressorChromosome - GradientBoostingRegressorChromosome - KNeighborsRegressorChromosome - MLPRegressorChromosome - RandomForestRegressorChromosome - XGBRegressorChromosome """ solver = AIS(chromosome=GradientBoostingRegressorChromosome(), problem=problem, population_size=20, epochs=-1, policies=[ Policy.EnforceLimitedMutationAttempts, Policy.EnforceUniqueChromosome ], duration=None) best_chromosome = solver.run() problem.log_chromosome(best_chromosome, solver) except Exception as ex: application_logger.exception(msg="", exc_info=ex)
def run(): problem = AckleyProblem() try: solver = AIS(chromosome=AckleyChromosome(dimensions=10), problem=problem, population_size=100, epochs=100, policies=[ Policy.EnforceLimitedMutationAttempts, Policy.EnforceUniqueChromosome ], duration=None) best_chromosome = solver.run() problem.log_chromosome(best_chromosome, solver) except Exception as ex: application_logger.exception( exc_info=ex, msg="Exception occurred whilst attempting to solve {}".format( problem.name))
def run(): """ The OneMax problem represents a classical optimisation problem where the chromosome is represented as a list of 1s an 0s and the quality of the chromosome is evaluated by the sum of it's alleles. """ problem = OneMaxProblem() try: """ The Artificial Immune System is a bio inspired evolutionary method and is used here to demonstrate the application of the framework to the OneMax problem. """ solver = AIS(chromosome=OneMaxChromosome(dimensions=100), problem=problem, population_size=100, epochs=100, policies=[ Policy.EnforceLimitedMutationAttempts, Policy.EnforceUniqueChromosome ], duration=None) best_chromosome = solver.run() problem.log_chromosome(best_chromosome, solver) except Exception as ex: application_logger.exception( exc_info=ex, msg="Exception occurred whilst attempting to solve the {}".format( problem.name))
def objective_function(self, chromosome: AbstractChromosome) -> List[np.float64] or None: # by default the scores are set to None given that an exception could # occur during the learning phase scores = None try: # we prefer to farm out the learning to the number of cores assigned # in DEFAULT_NUM_JOBS with concurrent.futures.ProcessPoolExecutor(max_workers=DEFAULT_NUM_JOBS) as executor: # here we track the result of the training against the learners futures = [] for i, partition in enumerate(self.partitions): # The phenotype of the chromosome represents an instance of # a learner that implements the fit function. The learn # function is not a part of this class simply because we # do not want to serialize the entire class in order to # call the method future = executor.submit( learn, learner=chromosome.phenotype, partition=partition, evaluation_function=self.__scoring_function ) # add the futures futures.insert(i, future) # wait until the futures are complete concurrent.futures.wait(futures) # extract the output of each future into a list scores = [future.result() for future in futures] except Exception as ex: application_logger.exception( "AbstractRegressionProblem-objective_function: Failure during learning phase", exc_info=ex ) # by default uses the average of the the scores from k-fold validation chromosome.fitness = None if scores is None else np.mean(scores) # call the base class objective function super(AbstractRegressionProblem, self).objective_function(chromosome) # provides the result of cross validation allowing the caller to decide # what they want to do after the evaluation return scores
def run(self) -> AbstractChromosome: """Performs the evolutionary process by navigating the search space of the chromosome combination space Raises: NotImplementedError: Must be implemented by the derived class Returns: AbstractChromosome: The chromosome with the best fitness """ try: self.initialise() if self.duration is not None: try: register_signal() signal_ttl(self.duration) while True: self.__generation += 1 self.evolve() except TimeoutException: reset_signal() else: while self.__epochs == -1 or self.generation < self.__epochs: self.__generation += 1 self.evolve() except KeyboardInterrupt: application_logger.info(msg="Keyboard interrupt received, exiting simulation") except Exception as ex: application_logger.exception(exc_info=ex, msg="Exception occurred whilst attempting to solve the {}".format( self.problem.name)) self.sort_population() return self.population[0]