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.1.0.tar.gz (5.3 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.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pygena-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ef99691ed07f70d3e2b3325ccf7f7b0caeedaa9568b879e683225a6112cbdc1a
MD5 8ee5dff11fe0d8bbcea8ca85727f3a18
BLAKE2b-256 8ee51a8723da41ef6015612c44028aa4154a5eb24d8e2cc21a68fe0e601e6706

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygena-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6d25950af09ce11fe9ab262166950e766d71b89ea7f3535ce3e5bf8168e69b4
MD5 d589c20afba5be57cbc826d94f102000
BLAKE2b-256 c49ea39590976ac6a05251a32f54afd8b861ea38d5ec83b4d362d41e756f24bc

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