Giter VIP home page Giter VIP logo

gaft's Introduction

GAFT

A Genetic Algorithm Framework in pyThon

Build Status Code Health platform versions

Introduction

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.

Installation:

  1. Via pip:

    pip install gaft
    
  2. From source:

    python setup.py install
    

Example:

1. Importing

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

2. Define population

indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001)
population = GAPopulation(indv_template=indv_template, size=50)

3. Create genetic operators

# Use built-in operators here.
selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitMutation(pm=0.1)

4. Create genetic algorithm engine to run optimization

engine = GAEngine(population=population, selection=selection,
                  crossover=crossover, mutation=mutation,
                  analysis=[FitnessStoreAnalysis])

5. Define and register fitness function

@engine.fitness_register
def fitness(indv):
    x, = indv.variants
    return x + 10*sin(5*x) + 7*cos(4*x)

6. Define and register an on-the-fly analysis (optional)

@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)

7. Run

if '__main__' == __name__:
    engine.run(ng=100)

8. Evolution curve

https://github.com/PytLab/gaft/blob/master/examples/ex01/envolution_curve.png

9. Optimization animation

https://github.com/PytLab/gaft/blob/master/examples/ex01/animation.gif

See example 01 for a one-dimension search for the global maximum of function f(x) = x + 10sin(5x) + 7cos(4x)

Global maximum search for binary function

https://github.com/PytLab/gaft/blob/master/examples/ex02/surface_animation.gif

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)

Plugins

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.

Blogs

TODO

  1. ✅ Parallelization
  2. 🏃 Add more built-in genetic operators with different algorithms
  3. 🏃 Add C++ backend

gaft's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.