python框架an_GAFT:Python 实现的遗传算法框架

GAFT

A Genetic Algorithm Framework in pyThon

68747470733a2f2f7472617669732d63692e6f72672f5079744c61622f676166742e7376673f6272616e63683d6d6173746572

68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f5079744c61622f676166742f6d61737465722e737667

68747470733a2f2f6c616e6473636170652e696f2f6769746875622f5079744c61622f676166742f6d61737465722f6c616e6473636170652e7376673f7374796c653d666c6174

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f707974686f6e2d332e352d677265656e2e737667

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f707970692d76302e352e372d626c75652e737667

68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f676166742f62616467652f3f76657273696f6e3d6c6174657374

Introduction

GAFT is a general Python Framework for genetic algorithm computation. It provides built-in genetic operators for target 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.

Python Support

GAFT requires Python version 3.x (Python 2.x is not supported).

Installation

Via pip: pip install gaft

From source: python setup.py install

If you want GAFT to run in MPI env, please install mpi4py explicitly:

pip install mpi4py

See INSTALL.md for more installation details.

Test

Run unit test:

python setup.py test

Quick start

1. Importing

from gaft import GAEngine

from gaft.components import BinaryIndividual, Population

from gaft.operators import RouletteWheelSelection, UniformCrossover, FlipBitMutation

# Analysis plugin base class.

from gaft.plugin_interfaces.analysis import OnTheFlyAnalysis

2. Define population

indv_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001)

population = Population(indv_template=indv_template, size=50)

population.init() # Initialize population with individuals.

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=[FitnessStore])

5. Define and register fitness function

@engine.fitness_register

def fitness(indv):

x, = indv.solution

return x + 10*sin(5*x) + 7*cos(4*x)

or if you want to minimize it, you can add a minimization decorator on it

@engine.fitness_register

@engine.minimize

def fitness(indv):

x, = indv.solution

return x + 10*sin(5*x) + 7*cos(4*x)

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

@engine.analysis_register

class ConsoleOutput(OnTheFlyAnalysis):

master_only = True

interval = 1

def register_step(self, g, population, engine):

best_indv = population.best_indv(engine.fitness)

msg = 'Generation: {}, best fitness: {:.3f}'.format(g, engine.fmax)

engine.logger.info(msg)

7. Run

if '__main__' == __name__:

engine.run(ng=100)

8. Evolution curve

envolution_curve.png

9. Optimization animation

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

surface_animation.gif

See example 02 for a two-dimension search for the global maximum of function f(x, y) = 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(Chinese Simplified)

TODO

✅ Parallelization

✅ Add more built-in genetic operators with different algorithms

🏃 Add C++ backend(See GASol)

Obtain a copy

The GAFT framework is distributed under the GPLv3 license and can be obtained from the GAFT git repository or PyPI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值