Package: geneal
GeneAlgenealis a python library implementing genetic algorithms (GAs). It has functionality for both binary and continuous GA,
as well as specific use case applications such as a solver for theTravelling Salesman Problem.Installation$ pip install genealUsagegenealprovides 2 main GA solver classes,BinaryGenAlgSolverandContinuousGenAlgSolverfor binary and continuous problem formulations respectively. The library is set up in such a way that all problems
have to be a maximization, and hence, the fitness functions need to be adjusted accordingly.Binary GAThe binary GA class can be customized by the input variables provided on its initialization.
The minimum required arguments to provide at the initialization are the number of genes on each individual's
chromosome, and the fitness function to be maximized.An example is shown below.fromgeneal.genetic_algorithmsimportBinaryGenAlgSolverfromgeneal.applications.fitness_functions.binaryimportfitness_functions_binarysolver=BinaryGenAlgSolver(n_genes=3,# number of variables defining the problemfitness_function=fitness_functions_binary(1),# fitness function to be maximized)solver.solve()The above call will perform the optimization with the default parameters, which will most likely have to be adjusted for
each individual problem at hand. The algorithm parameters can be customized as shown below.fromgeneal.genetic_algorithmsimportBinaryGenAlgSolverfromgeneal.applications.fitness_functions.binaryimportfitness_functions_binarysolver=BinaryGenAlgSolver(n_genes=3,fitness_function=fitness_functions_binary(1),n_bits=1,# number of bits describing each gene (variable)pop_size=10,# population size (number of individuals)max_gen=500,# maximum number of generationsmutation_rate=0.05,# mutation rate to apply to the populationselection_rate=0.5,# percentage of the population to select for matingselection_strategy="roulette_wheel",# strategy to use for selection. see below for more detailsfitness_tolerance=(1E-4,50)# Loop will be exited if the best fitness value does not change more than# 1E-4 for 50 generations)solver.solve()Continuous GAFollowing a similar pattern as the binary GA solver, the parameters of the continuous GA can be adjusted at initialization.
As a bare minimum, the number of variables and the fitness function to maximize must be provided, as shown below.fromgeneal.genetic_algorithmsimportContinuousGenAlgSolverfromgeneal.applications.fitness_functions.continuousimportfitness_functions_continuoussolver=ContinuousGenAlgSolver(n_genes=4,# number of variables defining the problemfitness_function=fitness_functions_continuous(3),# fitness function to be maximized)solver.solve()In order to customize the continuous GA solver further, more arguments can be passed at initialization.fromgeneal.genetic_algorithmsimportContinuousGenAlgSolverfromgeneal.applications.fitness_functions.continuousimportfitness_functions_continuoussolver=ContinuousGenAlgSolver(n_genes=4,fitness_function=fitness_functions_continuous(3),pop_size=10,# population size (number of individuals)max_gen=200,# maximum number of generationsmutation_rate=0.1,# mutation rate to apply to the populationselection_rate=0.6,# percentage of the population to select for matingselection_strategy="roulette_wheel",# strategy to use for selection. see below for more detailsfitness_tolerance=(1E-5,20)# Loop will be exited if the best fitness value does not change more than# 1E-5 for 20 generations)solver.solve()A notable difference to the binary GA solver is the fact that we can customize the input space of the problem
by defining if the problem is of typeintorfloat, and defining an overall minimum and maximum values for each
variable (or for all at once).fromgeneal.genetic_algorithmsimportContinuousGenAlgSolverfromgeneal.applications.fitness_functions.continuousimportfitness_functions_continuoussolver=ContinuousGenAlgSolver(n_genes=4,fitness_function=fitness_functions_continuous(3),pop_size=10,max_gen=200,mutation_rate=0.1,selection_rate=0.6,selection_strategy="roulette_wheel",variables_type=float,# Defines the possible values as float numbersvariables_limits=(-10,10)# Defines the limits of all variables between -10 and 10.# Alternatively one can pass an array of tuples defining the limits# for each variable: [(-10, 10), (0, 5), (0, 5), (-20, 20)])solver.solve()selection strategyIt is possible to choose the selection strategy that the algorithm will use to select the parents that will
be used for generating new offsprings for the next generation. The options are:roulette_wheelrandomtwo_by_twotournamentroulette wheelThis selection strategy orders the individuals in the selection pool by probability, with the fittest individuals
having higher odds of being selected.randomThis selection procedure selects randomly individuals from the selection pool, following in essence a similar procedure
as the roulette wheel, but with the same probabilities for each individual.two_by_twoThis strategy groups the individuals in the mating pool 2 by 2, from top to bottom.tournamentThis strategy will select 3 individuals candidates for each parent position, which are then sorted by their fitness
and from which the fittest one is selected.Specific ApplicationsIn the real world, there's usually the need to adapt a genetic algorithm implementation to each individual problem.
Thus,genealoffers the user a level of customization that aims to be both versatile and relatively simple. For that,
one just has to create a class which inherits from theBinaryGenAlgSolverorContinuousGenAlgSolverbase classes, and on which some overriding methods are defined. This allows the user to control the main steps of a GA:fitness functionpopulation initializationmating between individuals (creation of offsprings)mutation of the populationA boilerplate template of such a class is shown below:fromgeneal.genetic_algorithmsimportContinuousGenAlgSolver,BinaryGenAlgSolverclassTemplateChildClass(ContinuousGenAlgSolver,BinaryGenAlgSolver):def__init__(self,*args,**kwargs):BinaryGenAlgSolver.__init__(self,*args,**kwargs)ContinuousGenAlgSolver.__init__(self,*args,**kwargs)deffitness_function(self,chromosome):"""Implements the logic that calculates the fitnessmeasure of an individual.:param chromosome: chromosome of genes representing an individual:return: the fitness of the individual"""passdefinitialize_population(self):"""Initializes the population of the problem:param pop_size: number of individuals in the population:param n_genes: number of genes representing the problem. In case of the binarysolver, it represents the number of genes times the number of bits per gene:return: a numpy array with a randomized initialized population"""passdefcreate_offspring(self,first_parent,sec_parent,crossover_pt,offspring_number):"""Creates an offspring from 2 parents. It uses the crossover point(s)to determine how to perform the crossover:param first_parent: first parent's chromosome:param sec_parent: second parent's chromosome:param crossover_pt: point(s) at which to perform the crossover:param offspring_number: whether it's the first or second offspring from a pair of parents.Important if there's different logic to be applied to each case.:return: the resulting offspring."""passdefmutate_population(self,population,n_mutations):"""Mutates the population according to a given user defined rule.:param population: the population at a given iteration:param n_mutations: number of mutations to be performed. This number iscalculated according to mutation_rate, but can be adjusted as needed inside this function:return: the mutated population"""passTravelling Salesman ProblemOne of the possible applications of genetic algorithms is to theTravelling Salesman Problem. This problem is NP hard,
as the number of possible solutions grows with the factorial of the number of variables, and therefore, genetic algorithms
are a good fit for approximating solutions to these problems.As this particular problem has its own sets of constraints, a specific class adapted to this problem is provided
inTravellingSalesmanProblemSolver, which can be used out of the box to virtually
all problems of this kind. As a minimum input, this class requires the user to provide anetworkxundirected graph containing all the nodes
representing the problem and with each node connected to every other node by edges with the respective
associated cost (weight). To build this graph, one can use thecreate_graphmethod, as shown below:importturffromgeneal.applications.tsp.helpersimportcreate_graphfromgeneal.applications.tsp.examples.world_capitalsimportworld_capitals_dictG=create_graph(world_capitals_dict,# a python dictionary containing the nodes as keysturf.distance,# function to use to calculate distance between nodeslon=lambdax:x["CapitalLongitude"],# lambda function on how to retrieve the longitudelat=lambdax:x["CapitalLatitude"],# lambda function on how to retrieve the latitude)After having a built graph, one can pass it directly toTravellingSalesmanProblemSolver, and the number of genes
will be automatically retrieved from the number of nodes in the graph.fromgeneal.applications.tsp.travelling_salesman_problemimportTravellingSalesmanProblemSolverfromgeneal.applications.tsp.examples.world_capitals.graphimportGtsp_solver=TravellingSalesmanProblemSolver(graph=G)Again, one can control the solver parameters as before, by providing them on the initialization:fromgeneal.applications.tsp.travelling_salesman_problemimportTravellingSalesmanProblemSolverfromgeneal.applications.tsp.examples.world_capitals.graphimportGtsp_solver=TravellingSalesmanProblemSolver(graph=G,pop_size=10,# population size (number of individuals)max_gen=500,# maximum number of generationsmutation_rate=0.05,# mutation rate to apply to the populationselection_rate=0.5,# percentage of the population to select for matingselection_strategy="tournament",# strategy to use for selection.mutation_strategy="random_inversion"# strategy to use for mutation. see below for more details.)specific TSP parametersBeing a particular use case for genetic algorithms, the Travelling Salesman Problem has also specific settings that
allow the user to control the convergence for each use case. One of such parameters ismutation_strategy, which
can be one of the following:2-opt,random_swap,random_inversion,random_gene_nearest_neighbour,worst_gene_random,worst_gene_nearest_neighbour,select_any_mutation,Below some brief explanation is given on each of the options:2-optIt performs a 2-opt mutation on a tour. It selects randomly 2 edges from the tour and combines the resulting sub tours
by swapping them. More details can be foundhere.random swapA random swap mutation will choose randomly 2 genes from a given tour and swap them around. This is the
kind of mutation that most closely resembles a typical genetic algorithm mutation.random inversionA random inversion swap will choose a consecutive subset from the tour and reverse it. The size of the subset is also
chosen randomly.random gene nearest neighbourThis is a knowledge-based mutation, where a randomly selected gene is shifted next to its closest neighbour.
More details can be found in point4.2.7.worst gene random mutationThis is again a knowledge-based mutation, where a randomly selected gene is swapped with the gene contributing
most to the cost of the tour. More details can be found in point4.2.3.worst gene random mutationThis is again a knowledge-based mutation, where gene contributing most to the total cost of the tour
is moved next to one of its neighbours. More details can be found in point4.2.4