Skip to main content

geneticpython

A simple and friendly Python framework for genetic-based algorithms (strongly supports tree-encoding)

  • Supported algorithms: Genetic Algorithm (GAEngine), NSGA-ii (NSGAIIEngine).
  • An example on ZDT1 problem:

alt tag

Installation

This package requires python 3.6 or later.

pip install geneticpython

Getting started

We can quickly design a genetic algorithm in the following steps:

  1. define a individual template with specific encoding
from geneticpython.models import BinaryIndividual
indv_temp = BinaryIndividual(length=100)
  1. define population based on created individual template. This population can uniformly initialize a population or you can define your own by passing init_population argument function
from geneticpython import Population
population = Population(indv_temp, pop_size=100)
  1. define some core operators in genetic algorithm
from geneticpython.core.operators import RouletteWheelSelection, UniformCrossover, \
                                        FlipBitMutation, RouletteWheelReplacement
selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitMutation(pm=0.1)
# this function decides which individuals will be survived
replacement = RouletteWheelReplacement()
  1. create an engine and register the defined population and operators
from geneticpython import GAEngine
engine = GAEngine(population, selection=selection,
                  selection_size=100,
                  crossover=crossover,
                  mutation=mutation,
                  replacement=replacement)
  1. register fitness function which gets an individual and returns its fitness value
@engine.maximize_objective
def fitness(indv):
    return fitness_of_indv
  1. run engine
engine.create_seed(seed)
history = engine.run(generations=1000)
  1. get results and plot history
ans = engine.get_best_indv()
print(ans)
plot_single_objective_history({'geneticpython': history})

You can find more examples here

Issues

This project is in development, if you find any issues, please create an issue here.

TODO

  • Create extensive documentation and docs and comments in source-code
  • Implement other algorithms: PSO, DE, MOED/A, MOPSO, MODE,...
  • Implement other operators: PMX crossover, ...
  • Create unit tests.

Contributing

The goal of this project is to be able to build a simple and novice-friendly library yet functional enough to experiment with research projects. It is spontaneous and non-profit and also flawed.

We appreciate all contributions. If you are interested in contributing this project (including functional implementation or standard examples), please check Contribution page.

If you plan to contribute new features, utility functions, fix bugs, or extensions to the core, please first open an issue and discuss the feature with us.

Contributors

Ngoc Bui (ngocjr7)

Acknowledgements

Special thanks to https://github.com/PytLab/gaft for getting me started a great API design.

This repository includes some parts of the following repos:

Release files for geneticpython 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geneticpython 0.0.3
File Size Uploaded
geneticpython-0.0.3.tar.gz 35.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for geneticpython 0.0.3
File Interpreter ABI Platform
geneticpython-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size:99.9 kB

Release files / geneticpython-0.0.3.tar.gz

Download URL geneticpython-0.0.3.tar.gz
Size 35.9 kB
Tags Source
SHA-256 checksum
How to use checksums
92ad6d888ff4d27ebebb29e296635ee2633645d66fe874c73109da4324bca835
BLAKE2b-256 checksum
How to use checksums
07527ca6aac39a0226c0600eb865543112ef28384bf7ba4a259e0a33ba4e0cc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / geneticpython-0.0.3-py3-none-any.whl

Download URL geneticpython-0.0.3-py3-none-any.whl
Size 63.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c51b957d8c9e0275ffa8e1bff64bd42daa3f0be634db1ce3d21680ad12793dca
BLAKE2b-256 checksum
How to use checksums
3937399964789064ee35d3d96e7efbefc9c809534c85dd677e623bd18fa7e0d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page