A Genetic Algorithm Framework in pyThon
gaft is a Python Framework for genetic algorithm computation. It provide built-in genetic operators for genetic algorithm optimization and plugin interfaces for users to define your own genetic operators and on-the-fly analysis for algorithm testing.
gaft is now accelerated using MPI parallelization interfaces. You can run it on your cluster in parallel with MPI environment.
Via pip:
pip install gaft
From source:
python setup.py install
from gaft import GAEngine
from gaft.components import GAIndividual, GAPopulation
from gaft.operators import RouletteWheelSelection, UniformCrossover, FlipBitMutation
# Analysis plugin base class.
from gaft.plugin_interfaces.analysis import OnTheFlyAnalysis
indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001)
population = GAPopulation(indv_template=indv_template, size=50)
# Use built-in operators here.
selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitMutation(pm=0.1)
engine = GAEngine(population=population, selection=selection,
crossover=crossover, mutation=mutation,
analysis=[FitnessStoreAnalysis])
@engine.fitness_register
def fitness(indv):
x, = indv.variants
return x + 10*sin(5*x) + 7*cos(4*x)
@engine.analysis_register
class ConsoleOutputAnalysis(OnTheFlyAnalysis):
master_only = True
interval = 1
def register_step(self, ng, population, engine):
best_indv = population.best_indv(engine.fitness)
msg = 'Generation: {}, best fitness: {:.3f}'.format(ng, engine.fitness(best_indv))
engine.logger.info(msg)
if '__main__' == __name__:
engine.run(ng=100)
See example 01 for a one-dimension search for the global maximum of function f(x) = x + 10sin(5x) + 7cos(4x)
See example 02 for a two-dimension search for the global maximum of function f(x) = y*sin(2*pi*x) + x*cos(2*pi*y)
You can define your own genetic operators for GAFT and run your algorithm test.
The plugin interfaces are defined in /gaft/plugin_interfaces/, you can extend the interface class and define your own analysis class or genetic operator class. The built-in operators and built-in on-the-fly analysis can be treated as an official example for plugins development.
- ✅ Parallelization
- 🏃 Add more built-in genetic operators with different algorithms
- 🏃 Add C++ backend