geneticoptimizer

Optimization using Genetic Algorithms (GA)

The geneticoptimizer module provides optimizers and wrappers for GA.

As for now, I didn’t try a convincing Genetic Algorithm library. This is why it is built-in here. Feel free to propose a copyleft library on GitHub!

Created on Thu Jun 10 21:20:47 2021.

@author: HugoFara

class leggedsnake.geneticoptimizer.GeneticOptimization(dna, fitness, prob=0.07, **kwargs)

Bases: object

__init__(dna, fitness, prob=0.07, **kwargs) None
Parameters:
  • dna (list) –

  • fitness (callable) –

  • prob (float or tuple[float]) –

  • kwargs (dict) –

    Other useful parameters for the optimization.

    max_popint, default=11

    Maximum number of individuals. The default is 11.

    max_genetic_distfloat, default=.7

    Maximum genetic distance, before individuals cannot reproduce (separated species). The default is .7.

    startnstopbool, default=False

    Ability to close program without loosing population. If True, we verify at initialization the existence of a data file. Population is saved every int(250 / max_pop) iterations. The default is False.

    fitness_argstuple

    Keyword arguments to send to the fitness function. The default is None (no argument sent).

    verboseint

    Level of verbosity. 0: no verbose, do not print anything. 1: show a progress bar. 2: complete report for each turn. The default is 1.

birth(par1, par2)

Return a new individual with par1 and par2 as parents (two sequences).

Child are generated by a uniform crossover followed by a random “resetting” mutation of each gene. The resetting is a normal law.

Initial positions come from one of the two parents randomly.

Parameters:
  • par1 (list[float, tuple of float, tuple of tuple of float]) – Dna of first parent.

  • par2 (list[float, tuple of float, tuple of tuple of float]) – Dna of second parent.

Returns:

child – Dna of the child.

Return type:

list[float, tuple of float, tuple of tuple of float]

evaluate_individual(dna, fitness_args)

Simple evaluation for a single individual.

Parameters:
  • dna (list[float, tuple of float, tuple of tuple of float]) – List of the individuals’ DNAs

  • fitness_args (tuple) – Additional arguments to pass to the fitness function. Usually the initial positions of the joints.

Returns:

Score then initial coordinates.

Return type:

tuple

See also

evaluate_population

counterpart for an entire population.

evaluate_population(fitness_args, verbose=True, processes=1)

Evaluate the whole population, attribute scores.

Parameters:
  • fitness_args (tuple) – Additional arguments to pass to the fitness function. Usually the initial positions of the joints.

  • verbose (bool, default=True) – To display information about population evaluation.

  • processes (int, default=1) – Number of processes involved for a multiprocessor evaluation.

See also

evaluate_individual

same function but on a single DNA.

make_children(parents, max_genetic_dist=inf)
reduce_population()

Reduce the population down to max_pop.

Returns:

new_population – At most self.max_pop individuals, sorted by score.

Return type:

list of dna

run(iters, processes=1)

Optimization by genetic algorithm (GA).

Parameters:
  • iters (int) – Number of iterations.

  • processes (int, default=1) – Number of processes that will evaluate the linkages.

Returns:

List of 3-tuples: best score, best dimensions and initial positions. The list is sorted by score order.

Return type:

list[float, tuple[float], tuple[tuple[float]]]

select_parents(verbose=True)

Selection 1/4 of the population as parents.

leggedsnake.geneticoptimizer.kwargs_switcher(arg_name, kwargs, default=None)

Simple function to return the good element from a kwargs dict.

leggedsnake.geneticoptimizer.load_population(file_path)

Return a population from a given file.

leggedsnake.geneticoptimizer.save_population(file_path, population, verbose=False, data_descriptors=None)

Save the population to a json file.

Parameters:
  • file_path (str) – Path of the file to write to.

  • population (list) – Sequence of dna

  • verbose (bool) – Enable or not verbosity (outputs success).

  • data_descriptors (dict) – Any additional value you want to save for the current generation.