pygena
A simple, flexible genetic algorithm library for Python. Easily define your own chromosomes, fitness functions, and evolutionary strategies to solve optimization and search problems.
Features
- Minimal, intuitive API
- Customizable chromosomes and fitness functions
- Population management and evolution
Installation
pip install pygena
Or clone and install from source:
git clone https://github.com/atasoglu/pygena.git
cd pygena
pip install .
Usage
Here's a minimal example of evolving a list of numbers to sum to a target:
from pygena import Chromosome, Population
import random
target = 100
chromosome_size = 10
population_size = 10
mutation_rate = 0.05
iterations = 100
def random_int(gene=None):
return random.randint(-100, 100)
def random_list():
return [random_int() for _ in range(chromosome_size)]
def fitness_fn(chromosome):
diff = abs(sum(chromosome.genes) - target)
return 1 / (diff + 1e-5)
random.seed(42)
population = Population(
chromosomes=[Chromosome(random_list()) for _ in range(population_size)],
mutation_rate=mutation_rate,
mutation_fn=random_int,
)
for i, local_best, global_best in population.run(iterations, fitness_fn):
print(f"Iteration {i}: {global_best.genes} (sum: {sum(global_best.genes)})")
if sum(global_best.genes) == target:
print(f"Target reached in {i} iterations.")
break
For more examples, see the examples/ directory.
You can simply run an example script: python3 -m pygena.examples.text. Run with the --help flag to see the full list of arguments.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to help improve pygena.
License
MIT License. See LICENSE.
Metadata
Release files for pygena 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pygena-0.2.0.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pygena-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.3 kB
Release files / pygena-0.2.0.tar.gz
| Download URL | pygena-0.2.0.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b09c8ba29d75705c15ad5a848caa9dbf244ae7218737dbcaba2fa2a306c6372
|
|
BLAKE2b-256 checksum How to use checksums |
06a3146dbc0a5d4fd055585a311fb7cf577893c5a7f0b52f255ba4fba0153530
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.7
|
Release files / pygena-0.2.0-py3-none-any.whl
| Download URL | pygena-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0604ef92661599d0cb8bbbe72f7089829602af6ac952cc741df9d5309c870f0
|
|
BLAKE2b-256 checksum How to use checksums |
7192a2f424334f34f39ce2f227b1d298e1a96e736f0cc151193665b02b635868
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.7
|