Package: generic-algorithm-light
Genetic Algorithm ClassNow aspackage:last versionPython implementation of a genetic algorithm to solve optimization problems withncontrol variables.DescriptionAgenetic algorithm(GA) is a search heuristic part of a broader family of algorithms calledevolutionary algorithms(EAs). EAs are population-based metaheuristics that integrate mechanisms inspired by biological evolution such as reproduction, mutation, selection. The GA algorithm is used particularly in optimization problems where calculating gradients of an objective function is problematic or not possible.Steps in GAInitialization: initialize a population ofindividualsor candidate solutions to the problem. This initialization can be done by means of random sampling. Each individual is defined by an encoding which we callgenes.Selection: calculate the best candidates based on a definedfitnessfunction we want to optimize. We select the bestjparentswhich will be combined. The parameterjis arbitrary.Crossover: we combine the genes of the parents to produce anoffspring. These aresnew individuals in our population.Mutation: addrandomnessto the generated offspring. We can add e.g. a Gausian noise to one of the genes of the offspring for each individual.Replacement: select thelfittest individuals of the population to evaluate on the next epoch.We repeat these evolution steps for certain amount of epochs or until an exit condition is met.GA implementationDependenciesNumpy>= 1.18HyperparametersIndividuals:lower_boundupper_boundnumber_of_genes: dimension of the search space. In this implementation it indicates the shape of the array that represents each individual.Note: The number of genes of each individual and the fitness function must be congruentPopulation:n_parents:jparents.offspring_size: thesnew individuals from combiningjparents.mutation_mean,mutation_sd: mean and standard deviation of the Gaussian noise added during the mutation step.size: maximum size of the population orlfittest individuals to survive for the next epoch.Evolution:epochs: number of times we repet each evolution step.ExampleAn example fitness function could be something like this:deffitness(x,y):returnx*(x-1)*np.cos(2*x-1)*np.sin(2*x-1)*(y-2)We can limit our search to evaluate individualswithin the domainwith theind_parametersdictionary. Likewise, we control the population parameters with thepop_parameters.# example.pyimportnumpyasnpfromga.evolutionimportEvolution# Define a fitness functiondeffitness(x,y):returnx*(x-1)*np.cos(2*x-1)*np.sin(2*x-1)*(y-2)# Define parameter for each individualind_parameters={'lower_bound':-2,'upper_bound':2,'number_of_genes':2}# Define parameter for the entire populationpop_parameters={'n_parents':6,'offspring_size':(2,ind_parameters['number_of_genes']),'mutation_mean':0.25,'mutation_sd':0.5,'size':10}defexample():# Instantiate an evolutionevo=Evolution(pop_parameters,ind_parameters,fitness)# Repeat evolution step 200 epochsepochs=10000# Record fitness historyhistory=[]x_history=[]y_history=[]for_inrange(epochs):print('Epoch{}/{}, Progress:{}%\r'.format(_+1,epochs,np.round(((_+1)/epochs)*100,2)),end="")evo.step()history.append(evo._best_score)x_history.append(evo._best_individual[0][0])y_history.append(evo._best_individual[0][1])print('\nResults:')print('Best individual:',evo.solution.best_individual)print('Fitness value of best individual:',evo.solution.best_score)example()The results are really close to the global optimum within this domain and the best individual does not change after 50 epochs.# Output
Epoch  200/200, Progress 100.0%
Results:
Best individual: [-1.52637873, -2.        ]
Fitness value of best individual: 7.4697265870418414ReferencesAn Extensible Evolutionary Algorithm Example in PythonGenetic Algorithm Implementation in Python