mloptimizer.genetic#

Submodules#

Package Contents#

Classes#

DeapOptimizer

GeneticAlgorithmRunner

IndividualUtils

class mloptimizer.genetic.DeapOptimizer(hyperparam_space: mloptimizer.hyperparams.HyperparameterSpace = None, use_parallel=False, seed=None)[source]#
init_individual(pcls)[source]#

Method to create an individual

Parameters:

pcls (class) – class of the individual

Returns:

ind – individual

Return type:

individual

individual2dict(individual)[source]#

Method to convert an individual to a dictionary of hyperparams

Parameters:

individual (individual) – individual to convert

Returns:

individual_dict – dictionary of hyperparams

Return type:

dict

setup()[source]#

Method to set the parameters for the optimization.

class mloptimizer.genetic.GeneticAlgorithmRunner(deap_optimizer: mloptimizer.genetic.DeapOptimizer, tracker: mloptimizer.aux.Tracker, seed, evaluator)[source]#
simple_run(population_size: int, n_generations: int, cxpb: float = 0.5, mutation_prob: float = 0.5, n_elites: int = 10, tournsize: int = 3, indpb: float = 0.05)[source]#

Method to run the genetic algorithm. This uses the deap eaSimple method. It cannot be used to track what happens in each generation.

Parameters:
  • population_size (int) – size of the population

  • n_generations (int) – number of generations

  • cxpb (float) – crossover probability

  • mutation_prob (float) – mutation probability

  • n_elites (int) – number of elites

  • tournsize (int) – size of the tournament

  • indpb (float) – probability of a gene to be mutated

Returns:

  • population (list) – final population

  • logbook (~deap.tools.Logbook) – logbook

  • hof (~deap.tools.HallOfFame) – hall of fame

run(population_size: int, n_generations: int, cxpb: float = 0.5, mutation_prob: float = 0.5, n_elites: int = 10, tournsize: int = 3, indpb: float = 0.05, checkpoint: str = None) object[source]#

Method to run the genetic algorithm. This uses the custom_ea_simple method. It allows to track what happens in each generation.

Parameters:
  • population_size (int) – size of the population

  • n_generations (int) – number of generations

  • cxpb (float) – crossover probability

  • mutation_prob (float) – mutation probability

  • n_elites (int) – number of elites

  • tournsize (int) – size of the tournament

  • indpb (float) – probability of a gene to be mutated

  • checkpoint (str) – path to the checkpoint file

Returns:

  • population (list) – final population

  • logbook (~deap.tools.Logbook) – logbook

  • hof (~deap.tools.HallOfFame) – hall of fame

custom_ea_simple(population: list, toolbox: deap.base.Toolbox, cxpb: float = 0.5, mutpb: float = 0.5, start_gen: int = 0, ngen: int = 4, checkpoint_path: str = None, stats: deap.tools.Statistics = None, halloffame: deap.tools.HallOfFame = None, verbose: bool = True, checkpoint_flag: bool = True)[source]#

This algorithm reproduces the simplest evolutionary algorithm as presented in chapter 7 of [Back2000].

The code is close to the ~deap.algorithms.eaSimple method, but it has been modified to track the progress of the optimization and to save the population and the logbook in each generation. More info can be found on deap documentation

Parameters:
  • population (list) – A list of individuals.

  • toolbox (Toolbox) – A toolbox that contains the evolution operators.

  • cxpb (float) – The probability of mating two individuals.

  • mutpb (float) – The probability of mutating an individual.

  • start_gen (int) – The starting generation number. Used in case of checkpoint.

  • ngen (int) – The number of generations.

  • checkpoint_path (str) – The path to the checkpoint file.

  • stats (Statistics) – A ~deap.tools.Statistics object that is updated inplace, optional.

  • halloffame (HallOfFame) – A ~deap.tools.HallOfFame object that contains the best individuals, optional.

  • verbose (bool) – Whether or not to log the statistics.

  • checkpoint_flag (bool) – Whether or not to save the checkpoint.

Returns:

  • population (list) – The final population.

  • logbook (~deap.tools.Logbook) – A logbook containing the statistics of the evolution.

  • halloffame (~deap.tools.HallOfFame) – A hall of fame object that contains the best individuals.

References

[Back2000]

Back, Fogel and Michalewicz, “Evolutionary Computation 1 : Basic Algorithms and Operators”, 2000.

population_2_df()[source]#

Method to convert the population to a pandas dataframe

Returns:

df – dataframe with the population

Return type:

pandas dataframe

class mloptimizer.genetic.IndividualUtils(hyperparam_space: mloptimizer.hyperparams.HyperparameterSpace = None, estimator_class=None, mlopt_seed=None)[source]#
get_clf(individual)[source]#
individual2dict(individual)[source]#

Method to convert an individual to a dictionary of hyperparams

Parameters:

individual (individual) – individual to convert

Returns:

individual_dict – dictionary of hyperparams

Return type:

dict