Package: eao
Evolutionary Algorithm Optimization (EAO)Package for optimization using Evolutionary Algorithms (EA), providing base classes for individuals and an evaluation module.Version History0.4Removed necessity to manually assignloss_attribute0.5Added extensive logging capabilities; will be cleaned up a little in later versions0.6Fixed critical bug that affects optimization with more than one parent0.7Made logging much cleaner; added plus-selectionDocumentationeao(Evolutionary Algorithm Optimization)This library provides a function minimizer using a simple modular evolutionary algorithm scheme that can be adapted to all kinds of target domains.
The algorithm performs optimization steps (generations) on a population of solutions by iteratively creating offspring through recombination and mutation, evaluation the offspring using a customizable fitness function and selecting new parents for the next generation, either throughplusselection (best individuals from offspring plus previous parents) orcommaselection (best individuals from offspring only).
Additionally,eaohas useful logging capabilities, that allow to fully retrace each optimization run for further analysis.In its core,eaorequires the user to extend 2 classes:Individual: Class representing the target domain, e.g. real numbers, vectors, bit strings or more complex objectsEvaluator: Class containing the evaluation method, which takes an object ofIndividualand assigns it aloss value(or inverse fitness value) according to which it is rankedThe following sections will explain how to implement these classes and how to use them with the optimizer.
As an example, we will implement an optimization problem on real vectors.IndividualIn a first step, we will create a subclass ofIndividualcalledRealVectorthat will represent vectors of float values with fixed size. I'll be usingnumpyarrays as the actual internal data structure and write a wrapper that implements the methods required for individuals.
For a minimal implementation we will need the following methods:copy(): This method returns a (deep) copy of the object. This is necessary because objects are copied between populations internally, and this method allows us to manually copy internal class attributes that require manual deep copying on their own, such asnumpyarrays.random(): This is a static method (@classmethod) and returns a random instance of our data typemutate(*kwargs): This method performsmutationin-place, i.e. it introduces a little variation to the individual. For real vectors, we will add some random noise to one of the elements. Additional parameters can be passed to this function, which can be set via the configuration dict, more on this later.cross(other, *kwargs): This performscrossover(recombination) in-place, taking another individual as input. For our example, we will performone-point crossover, meaning we'll sample a cutting point and take one part from either parent. Just asmutate, this method can take custom parameters. Also, if you don't want to use crossover, you can skip this method.After implementing these methods for our example application, theRealVectorclass looks like this:importnumpyasnpfromeaoimportIndividual,Evaluator,OptimizerclassRealVector(Individual):def__init__(self,vec):self.vec=vecdefcopy(self):returnRealVector(self.vec.copy())@classmethoddefrandom(cls):returncls(np.random.uniform(-1,1,size=16))defmutate(self):index=np.random.choice(16)self.vec[index]+=np.random.normal(loc=0,scale=0.1)defcross(self,other):index=np.random.choice(17)self.vec[ix:]=other.vec[ix:]As you can see we'll be working with RealVectors of size 16, and random instances will have elements uniformly distributed between -1 and 1.
When implementingrandom, make sure the initial individuals are not unreasonable, e.g. much too large or too small - this will make it difficult to reach the solution by small mutations.EvaluatorTheEvaluatorclass contains the loss function that we'll have to specify for our optimization problem.
We only need to implement a single method:classRealVectorEvaluator(Evaluator):defeval(self,rv):even_indices=rv[::2]odd_indices=rv[1::2]returnnp.sum((even_indices-1)**2)+np.sum((odd_indices+1)**2)For our example, we'll be be adding all even indices' distance to 1 and all odd indices' distance to -1, so that our global optimum is a vector of alternating 1 and -1. This nonsensical loss function is purely for demonstration purposes.ConfigurationComing soon!