Reference

Python library for creating genetic algorithms.

https://github.com/lkozelnicky/GeneticAlgos

GeneticAlgos is a simple and powerful Python library for creating genetic algorithms to solve complex optimization problems. GeneticAlgos is built on NumPy and it is under active development.

Features:
  • Uses smart defaults for genetic algorithms parameters

  • A simple-to-use API to modify genetic algorithms parameters.

  • Lightweight and just one dependency - Numpy.

  • Excellent test coverage.

  • Tested on Python 3.7, 3.8 and 3.9

class geneticalgos.ga.GeneticAlgo(fitness_function: Callable[[numpy.ndarray], float], gene_intervals: numpy.ndarray, objective_goal: str = 'maximize', chromosome_type: str = 'float', population_size: int = 100)

A user-created GeneticAlgo object.

Object instantiated by the GeneticAlgo simulate genetic algorithm evolution.

Parameters
  • fitness_function (callable) – user defined objective function which calculate suitability of the given chromosome.

  • gene_intervals (numpy.ndarray) – 2-dimensional numpy.ndarray specifies chromosome length and value range for each gene.

  • objective_goal (str) – optional - whether maximum (maximize) or minimum (minimize) is considered best value for fitness function. Supported values are minimize, maximize. The default value is maximize.

  • chromosome_type (str) – optional - chromosome encoding. Supported values are int, float. The default value is float.

  • population_size (int) – optional - population size. The default value is 100.

Variables

selection_k_max – max tournament size for tournament selection method. Hard coded value is 20.

property crossover_prob: float

crossover probability.

Crossover probability specifies how likely is that crossover is applied to selected parents. Must be float between <0-1>. If 0 then crossover is never applied. If 1 then crossover is applied to every selected parent pair. The default value is 0.9, meaning there is 90% chance that crossover is applied to the given parent pair.

Getter

Returns current crossover probability

Setter

Sets new crossover probability

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not within allowed range <0-1>

property crossover_type: str

Crossover method.

Specifies which crossover method is going to be used in simulate(). Supported values are “random”, “one_point”, “two_points”, and “uniform”. The default value is “random”.

Getter

Returns current crossover method

Setter

Sets new crossover method

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not supported crossover method

property mutation_prob: float

Mutation probability for particular gene.

Mutation is applied to each gene with mutation probability. Must be float between <0-1>. If 0 then mutation is not applied at all. If 1 then mutation is applied to every gene in chromosome. The default value is 0.2, meaning meaning there is 20% chance that gene is going to mutate.

Getter

Returns current mutation probability

Setter

Sets new mutation probability

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not within allowed range <0-1>

property n_elite: int

Configure elitism.

n_elite - specifies how many chromosome are carried over to the next generation without any gene change. This method guarantees that the solution quality obtained by the genetic algorithms will not decrease from one generation to the next.

If 0 there is no elitism. Upper limit is: population_size - (n_pairs * 2). The default value is 2.

Getter

Returns current elite count

Setter

Sets new elite count

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not within allowed range

property n_pairs: int

Number of parent pairs selected for reproduction.

specifies the number of pairs selected to reproduce (crossover, mutation). If 5 it means that 5 * 2 = 10 chromosomes are going to be selected for reproduction. If 0 there is no chromosome selected for reproduction. Upper limit is: (population_size - n_elite) / 2. The default value is population_size / 3.

Getter

Returns current number of pairs for selection

Setter

Sets new number of pairs for selection

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not within allowed range

property new_pop_type: str

Method for creating new population.

Creates new population when all parents finish their reproduction process (crossover, mutation). Supported methods are “random”, “always_offsprings” and “tournament”. The default value is “random”.

Getter

Returns current method for creating new population

Setter

Sets new method for creating new population

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not supported method for creating new population

property selection_k: int

Tournaments size for tournament selection method.

Maximum tournament size is hard coded in selection_k_max and it is 20.

Getter

Returns current tournament size

Setter

Sets new tournament size

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not within allowed range

property selection_type: str

Selection method.

Specifies which selection method is going to be used in simulate(). Supported values are “random”, “roulette_fitness”, “roulette_rank” and “tournament”. The default value is “random”.

Getter

Returns current selection method.

Setter

Sets new selection method.

Raises
  • TypeError – if new value has incorrect type

  • ValueError – if new value is not supported selection method

simulate(n_iterations: int = 100) None

Simulate genetic algorithms evolution.

Workflow for simulating genetic evolution is - select parents, crossover, mutation and then create new population. Repeating this loop n_iterations times. Every time is method executed it starts genetic evolution from beginning.

Parameters

n_iterations (int) – number of generation cycles

Raises