Skip to main content

A simple genetic algorithm library in Python.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygena-0.2.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygena-0.2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pygena-0.2.0.tar.gz.

File metadata

  • Download URL: pygena-0.2.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.7

File hashes

Hashes for pygena-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5b09c8ba29d75705c15ad5a848caa9dbf244ae7218737dbcaba2fa2a306c6372
MD5 1315f619b3b7e0fcc7664cce7280d5bf
BLAKE2b-256 06a3146dbc0a5d4fd055585a311fb7cf577893c5a7f0b52f255ba4fba0153530

See more details on using hashes here.

File details

Details for the file pygena-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pygena-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.7

File hashes

Hashes for pygena-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0604ef92661599d0cb8bbbe72f7089829602af6ac952cc741df9d5309c870f0
MD5 e736da28b6196dcd8613f1d38ced2848
BLAKE2b-256 7192a2f424334f34f39ce2f227b1d298e1a96e736f0cc151193665b02b635868

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page