Skip to main content

Genetic Algorithm Library

Project description

Holland

Genetic Algorithm Library for Python

Computer programs that "evolve" in ways that resemble natural selection can solve complex problems even their creators do not fully understand

PyPI Build Coverage Documentation Status License: MIT

Description

Holland is a simple, flexible package for implementing the Genetic Algorithm in Python. The program is designed to act on an arbitrary evaluation function with arbitrary encoding of individuals within a population, both of which are provided by the user.

Installing

Holland is available via the Python Package Index (PyPI) and can be installed with:

pip install holland

Usage

Full Documentation

Hello World!

from holland import Evolver
from holland.library import get_uniform_crossover_function
from holland.utils import bound_value
import random


# Define a fitness function
def fitness_function(genome):
    message = genome["message"]
    target = "Hello World!"
    score = 0
    for i in range(len(message)):
        score += abs(ord(target[i]) - ord(message[i]))
    return score

def mutation_function(value):
    mutated_value = ord(value) * random.random() * 2
    return chr(bound_value(mutated_value, minimum=32, maximum=126, to_int=True))

# Define genome parameters for individuals
genome_params = {
    "message": {
        "type": "[str]",
        "size": len("Hello World!"),
        "initial_distribution": lambda: chr(random.randint(32, 126)),
        "crossover_function": get_uniform_crossover_function(),
        "mutation_function": mutation_function,
        "mutation_rate": 0.15
    }
}

# Define how to select individuals for reproduction
selection_strategy = {"pool": {"top": 10}}

# Run Evolution
evolver = Evolver(
    fitness_function,
    genome_params,
    selection_strategy,
    should_maximize_fitness=False
)
final_population = evolver.evolve(stop_conditions={"target_fitness": 0})

With sample run:

Generation: 0; Top Score: 201:     N~flx.JGcu-*

Generation: 1; Top Score: 98:       Xljlw);mj]f

Generation: 2; Top Score: 64:       =c}kk SmsYf

Generation: 3; Top Score: 37:       Kcjlk$Vms]f

Generation: 4; Top Score: 24:       Cdjkn Smshf

Generation: 5; Top Score: 16:       Idjln Vmshf

Generation: 6; Top Score: 14:       Idjln Voshf

Generation: 7; Top Score: 11:       Hdjln Vmslf

Generation: 8; Top Score: 9:         Hdjln Voslf

Generation: 9; Top Score: 8:         Hdjln Vosle

Generation: 10; Top Score: 7:       Hdmln Vosle

Generation: 11; Top Score: 6:       Hdlln Vosle

Generation: 12; Top Score: 5:       Hdllo Vosle

Generation: 13; Top Score: 4:       Hdllo Vosle!

Generation: 14; Top Score: 3:       Hello Vosle!

Generation: 15; Top Score: 2:       Hello Wosle!

Generation: 16; Top Score: 2:       Hello Wosle!

Generation: 17; Top Score: 1:       Hello Worle!

Generation: 18; Top Score: 1:       Hello Worle!

Generation: 19; Top Score: 1:       Hello Worle!

Generation: 20; Top Score: 0:       Hello World!

Best Genome:

{
    'message': ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
}

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

holland-0.0.2.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

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

holland-0.0.2-py3-none-any.whl (44.2 kB view details)

Uploaded Python 3

File details

Details for the file holland-0.0.2.tar.gz.

File metadata

  • Download URL: holland-0.0.2.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for holland-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b043e4c11801cb6dd27795abe225ddf40bd378c761d663d63bfbc73e2f7d4942
MD5 54506acdfe530a167cd28535ef33f556
BLAKE2b-256 c52ac8e43a1b4a269857ad3b042c3ae71312c3a07cf775df71d5c77275b2277a

See more details on using hashes here.

File details

Details for the file holland-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: holland-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 44.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for holland-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5d162a58f247a403ac022e8a6098713d6787ee48cb56f9ce358075e2c46bfd63
MD5 eab79f94d6c3513db5215e4165880460
BLAKE2b-256 089b904dee2038b3cf7f1cef38e448ac48b736e944a3fa6c22f2f78252f9e959

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