Package: leap-ec
LEAP: Evolutionary Algorithms in PythonWritten by Dr. Jeffrey K. Bassett, Dr. Mark Coletti, and Dr. Eric ScottLEAP is a general purpose Evolutionary Computation package that combines
readable and easy-to-use syntax for search and optimization algorithms with
powerful distribution and visualization features.LEAP's signature is its operator pipeline, which uses a simple list of
functional operators to concisely express a metaheuristic algorithm's
configuration as high-level code.  Adding metrics, visualization, or
special features (like distribution, coevolution, or island migrations)
is often as simple as adding operators into the pipeline.Using LEAPGet the stable version of LEAP from the Python package index withpipinstallleap_ecSimple ExampleThe easiest way to use an evolutionary algorithm in LEAP is to use theleap_ec.simplepackage, which contains simple interfaces for pre-built
algorithms:fromleap_ec.simpleimportea_solvedeff(x):"""A real-valued function to optimized."""returnsum(x)**2ea_solve(f,bounds=[(-5.12,5.12)for_inrange(5)],maximize=True)Genetic Algorithm ExampleThe next-easiest way to use LEAP is to configure a custom algorithm via one
of the metaheuristic functions in theleap_ec.algorithmspackage.  These
interfaces offer you a flexible way to customize the various operators,
representations, and other components that go into a modern evolutionary
algorithm.Metaheuristics are usually defined by three main objects: aProblem, aRepresentation, and a pipeline (list) ofOperators.Here's an example that applies a genetic algorithm variant to solve theMaxOnesoptimization problem.  It uses bitflip mutation, uniform crossover,
and binary tournament_selection selection:Python code for simple GAfromleap_ec.algorithmimportgenerational_eafromleap_ecimportops,decoder,probe,representationfromleap_ec.binary_repimportinitializersfromleap_ec.binary_repimportproblemsfromleap_ec.binary_rep.opsimportmutate_bitflippop_size=5final_pop=generational_ea(max_generations=10,pop_size=pop_size,# Solve a MaxOnes Boolean optimization problemproblem=problems.MaxOnes(),representation=representation.Representation(# Genotype and phenotype are the same for this taskdecoder=decoder.IdentityDecoder(),# Initial genomes are random binary sequencesinitialize=initializers.create_binary_sequence(length=10)),# The operator pipelinepipeline=[# Select parents via tournament_selection selectionops.tournament_selection,ops.clone,# Copy them (just to be safe)# Basic mutation with a 1/L mutation ratemutate_bitflip(expected_num_mutations=1),# Crossover with a 40% chance of swapping each geneops.uniform_crossover(p_swap=0.4),ops.evaluate,# Evaluate fitness# Collect offspring into a new populationops.pool(size=pop_size),probe.BestSoFarProbe()# Print the BSF])Low-level ExampleHowever, it may sometimes be necessary to have access to low-level details of
an EA implementation, in which case the programmer can arbitrarily connect
individual components of the EA workflow for maximum tailorability.   For
example:Low-level example python codefromtoolzimportpipefromleap_ec.individualimportIndividualfromleap_ec.decoderimportIdentityDecoderfromleap_ec.contextimportcontextimportleap_ec.opsasopsfromleap_ec.binary_rep.problemsimportMaxOnesfromleap_ec.binary_rep.initializersimportcreate_binary_sequencefromleap_ec.binary_rep.opsimportmutate_bitflipfromleap_ecimportutil# create initial rand population of 5 individualsparents=Individual.create_population(5,initialize=create_binary_sequence(4),decoder=IdentityDecoder(),problem=MaxOnes())# Evaluate initial populationparents=Individual.evaluate_population(parents)# print initial, random populationutil.print_population(parents,generation=0)# generation_counter is an optional convenience for generation trackinggeneration_counter=util.inc_generation(context=context)whilegeneration_counter.generation()<6:offspring=pipe(parents,ops.tournament_selection,ops.clone,mutate_bitflip(expected_num_mutations=1),ops.uniform_crossover(p_swap=0.2),ops.evaluate,ops.pool(size=len(parents)))# accumulate offspringparents=offspringgeneration_counter()# increment to the next generationutil.print_population(parents,context['leap']['generation'])More ExamplesA number of LEAP demo applications are found in the theexample/directory of the github repository:gitclonehttps://github.com/AureumChaos/LEAP.git
pythonLEAP/examples/advanced/island_models.pyDemo of LEAP running a 3-population island model on a real-valued optimization problem.DocumentationThe stable version of LEAP's full documentation is over atReadTheDocs.If you want to build a fresh set of docs for yourself, you can do so after runningmake setup:make docThis will create HTML documentation in thedocs/build/html/directory.  It might take a while the first time,
since building the docs involves generating some plots and executing some example algorithms.Installing from SourceTo install a source distribution of LEAP, clone the repo:git clone https://github.com/AureumChaos/LEAP.gitAnd use the Makefile to install the package:makesetupRun the Test SuiteLEAP ships with a two-partpytestharness, divided into fast and slow tests.  You can run them withmaketest-fastandmaketest-slowrespectively.