A framework for genetic algorithms in Python
Project description
Genyal: The cheerful genetic algorithm framework
This work is licensed under a Creative Commons Attribution 4.0 International License
Genyal is a genetic algorithm framework aimed to be as simple to use as it can be. Evolution is natural, so using evolutionary algorithms should feel natural as well.
One of the main benefits of evolutionary programming is making it simple to solve complicated problems, and that's something that most implementations of this kind of algorithms seems to forget. You'll find either, very simple implementations of genetic algorithms from scratch, or full fledged libraries that are, by no means, something that someone with little experience in the field of evolutionary programming can easily use; nothing in between. Genyal is that missing "in between", by providing a simple interface so users new to the topic can learn and use it without need of being experts, and flexible enough so that people can adapt it to more sophisticated scenarios.
Usage
Just like using it, installing Genyal should be a piece of cake (great, now I want cake). To do so, you can get it directly from PyPi or, with pip:
pip install genyal
Example: Guessing a word
Let's check a very simple example. A program to guess a given word.
First, we need a way to create the individuals of the population, in this case, each individual will represent a word, and each gene of the individual will be a letter (lowercase). For this we can implement a generator function like:
import random
import string
from genyal.genotype import GeneFactory
from genyal.engine import GenyalEngine
def random_char():
return random.choice(string.ascii_lowercase)
gene_factory = GeneFactory[str]()
gene_factory.generator = random_char
# This is how the engine will select the fittest individuals
def fitness_fun(word: list[str]) -> float:
return sum([word[i] == "owo"[i] for i in range(0, 3)])
# This is the condition to stop the evolution
def target(genyal_engine: GenyalEngine) -> bool:
return "".join(genyal_engine.fittest.genes) == "owo"
engine = GenyalEngine(fitness_function=fitness_fun, terminating_function=target)
# We create an initial population of 16 words (Individuals) of 3 characters (genes)
# using our previously defined gene factory.
engine.create_population(16, 3, gene_factory)
engine.evolve()
print(f"Found solution in {engine.generation} generations")
print(
f"Fittest individual: \"{''.join(engine.fittest.genes)}\" with fitness: "
f"{engine.fittest.fitness}")
If you run the code it will print something like:
Found solution in 543 generations
Fittest individual: "owo" with fitness: 3
You can find the explanation of this code, along with other examples, at the project's wiki
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file genyal-0.2.1.tar.gz
.
File metadata
- Download URL: genyal-0.2.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0dcf1f07528d1f35c39374167ef1ee43e8f42853988bd28bf9b9f111af95a91 |
|
MD5 | 1dd0ed7c53b71616dc1739f5e87c4606 |
|
BLAKE2b-256 | c5e64563c86962fb489c69722ecfab8573319ea244eab00f4df790c2b4bdea0a |
File details
Details for the file genyal-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: genyal-0.2.1-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a65743b589523dd2b54c2a756d30bdfa7b5adc7e5328845f74857c6abcdd1b3 |
|
MD5 | df754fe98932f1636a2a434cb4f93f28 |
|
BLAKE2b-256 | 8636e2772c509fe8a5fea70343f737154ba6f095239e5b5deb02eb48d837c26a |