… _api-design:
API Design¶
API design principles
Modules¶
Short introduction to modules:
meta: Metaclassesmixin: Mixin Classesbase: The base classes, esp. for creating GAspopulation: populations to implement classical GAschromosome: some type of chromosomesmultipopulation: multi-population GAsindividual: individuals for GA or other EAes,de,ep,gpa...: Evolutionary algorithmspso: Swarm intelligent algorithmsmisc/ba,fa..: Swarm intelligent algorithms, Bionic intelligent algorithmdeco: Decoratorsutils: Helperserrors: Exceptionslocal_search/*: Local search algorithmslearn/*: Machine learning implemented by GAs
Metaclasses¶
The metaclass System is defined to simulate abstract algebraic systems, which are instantiated as a set containing a set of elements, as well as operators and functions on them.
MetaContainer is the super-metaclass of System for creating container classes.
ParamType --> MetaContainer --> SystemParamType --> MetaArray
MetaArray is a compact version of MetaContainer
Mixin Classes¶
Metaclasses define what the algorithm is, while mixin classes specify what the algorithm does.
The inheritance of metaclasses:
IterativeMixin ---> CollectiveMixin
| |
| |
v v
FitnessMixin ---> PopulationMixin
If you want to create novel algorithm different from GAs, it is recommended to inherit from the mixin classes. The base classes are designed for GA-like algorithms. It is not coercive. One can override the methods in the base classes.
Iterative models/algorithms¶
An iterative model is a mixin class implementing iterative algorithms, mainly repeating to call transition method.
Following is the core (pseudo-)code of the class.
class IterativeMixin:
def evolve(self):
self.init()
looping by k:
self.transition(k)
Users could override transition to implement own iteration algorithms.
Fitness models¶
Fitness Models (FitnessMixin) are iterative models with fitness. The iteration in such models would be influenced by fitness.
Indiviudal is a fitness model in GA.
Population models¶
Population Models (PopulationMixin) are collective algorithms with fitness.
Basic Classes¶
Individuals¶
An individual is defined as a container of chromosomes.
The individual classes are inherited from BaseIndividual. For instance, BinaryIndividual is a subclass encoded by several binary chromosomes.
See Examples section for a simple example of binary individuals — Knapsack Problem.
Chromosomes¶
The chromosomes as an array of genes, could be regarded as the unit of genetic operations. The subclass used most frequently isBinaryChromosome.
A chromosome is equivalent to an individual with only one chromosome, mathematically.
Populations¶
The population classes are inherited from BasePopulation. StandardPopulation is the standard population for GA. It is recommended to use HOFPopulation in most cases.
A population is a container of individuals, in original meaning. But It is possible to be a container of chromosomes in the view of algebra.
Multi-populations¶
It is useful for multi-populations GA. It is regarded as a container of populations.
Environment¶
It is designed to be the context for evolution of individuals. The aim of the class is not for numerical computing, instead for “the Skeuomorphic Design”.
Methods¶
About fitness¶
Calculating the fintess is the most frequent task in the algorithms.
_fitness(): the basic method to calculate the fitness of the individual. The methods whose names are started with_always do the most dirty task.fitness: decorated byproperty, get the fitness of the individual/population (from the cache first then call -_fitnessif there is a cache). The fitness of a population is the maximal fitness of the individuals in the population.best/mean/std_fitness: the maximum/mean/std of the fitnesses of the individuals in a population.get_all_fitness: get the all fitnesses
About elements(individuals in a population or populations in a multi-population/community)¶
best_element: wrapped byproperty, equiv. toget_best_elementget_best_element: get the element with maximal fitnessget_best_elements: get some elementssort/argsort: sort the elements by fitnesssolution: decoding the best element
Basic operation¶
copy: copy an objectrandom: class method to generate an object randomlydecode: decode an individual to the real solution
Genetic operation¶
clone: chromosome-levelmutate: chromosome-levelcross: chromosome-levelselect: population-levelmate: population-level, cross the elements, invalid in solution-level.migrate: population-level, crossover of the populationsdual: chromosome-levelreplicate: copy then mutatelocal_search: population-level
Some operators defined in solution-level, i.e. only valid for individuals/populations (or chromosomes as the individuals), such as _fitness, some could be defined under solution-level, i.e. valid for chromosomes in an individual, such as dual, cross, mutate.
List-like operation¶
append, extend, pop, remove to operate the list of the elements
Side-effect and pure¶
Here side-effect means the method will change the fitness of the individual or the population. It could be ignored if you do not use the cache (to get the fitness).
Others¶
set_*/get_*: set/get methods, for instance,get_allthe attributes of all individuals in a population.set: set attributes/methods for a classsave/load: serialization/deserialization for populations/individuals
Arguments/Parameters/Attributions¶
n_*: number of …, such asn_chromosomesrepresents the number of the chromosomes in an individual.*_prob: probability*_rate: e.g.learning_rate
params/alias¶
params is a dictionary of parameters for the algorithm, it could be inherited from super class by meta-programming.
alias is a dictionary for alias to attributes.