Skip to main content

pyga

Python implementation of Genetic Algorithm

Installation

pip3 install pygenal

Usage

this test is able to find global maximum of 6th order polynom with two variables in 500ms with precision approximately 10-4 to 10-5 % (depending on hardware)

#!/usr/bin/env python3

# import required classes
from pygenal.ga import Population, Individual, Gene, geneTypes, Duration
import numpy as np
import time


# create population of your own, override fitness method to suit your needs.
# your system evaluation goes here, for sake of demonstration, use polynom
class Polynom(Population):
    def fitness(self, individual):
        x = individual.get("x").value
        y = individual.get("y").value
        color = individual.get("color").value

        # test against function with global maximum:
        # max{x + 3 x^2 - x^4 - y^6 + y/3 - y^3 4 - y - 5} ≈ 3.35864
        # at (x, y) ≈ (1.30084, -1.27413)
        # + favorize chocolate over shorter colors, just for demonstration
        return (x + 3*(x**2) - x**4 - y**6 + y/3.0 - (y**3)*4 - y - 5) + len(color)/10


# introduce non-number options such as color, good for selections
colors = ["brown", "green", "grey", "blue", "chocolate"]


if __name__ == '__main__':

    # spawn individual
    i1 = Individual()

    # construct "DNA"
    i1 += Gene("x", geneTypes.REAL, np.random.randint(-10, 10), min=-10, max=10)
    i1 += Gene("y", geneTypes.REAL, np.random.randint(-10, 10), min=-10, max=10)
    i1 += Gene("color", geneTypes.VALUE, np.random.choice(colors), availableOptions=colors, dominant=True)

    # create tribe with size of 100 individuals based on your first Individual
    population = Polynom(
            species=i1,
            size=100,
            chanceOfMutationStart=.5,
            chanceOfMutationStop=0.0001
        )

    tStart = time.time()

    # start evolution until:
    #   you didn't pass 1000 generations OR
    #   fittest didn't change for 200 generations OR
    #   2.5s didn't passes yet
    population.evolve(
            allowCrossover=True,
            generations=100,
            verbose=True,
            timeout=Duration(seconds=0, miliseconds=500),
        )
    print(f"Evolved in {time.time()-tStart}s, precision: {(4.258642115713602 - population.fittest.score)*100}%")
    print(f"Fittest: {population.fittest}, genes: {repr(population.fittest)}")

Release files for pygenal 1.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygenal 1.0.7
File Size Uploaded
pygenal-1.0.7.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygenal 1.0.7
File Interpreter ABI Platform
pygenal-1.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / pygenal-1.0.7.tar.gz

Download URL pygenal-1.0.7.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
114b3c28acff00b44d0923d5037c0bcff0213e59892173af119a4df55675adbc
BLAKE2b-256 checksum
How to use checksums
d629ffa70b55d65c9593a3f2fb03c634d1b41c4388dcaf29985a6af21d4680b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

Release files / pygenal-1.0.7-py3-none-any.whl

Download URL pygenal-1.0.7-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3365a068dc512cd3fe43404ab4dbfb20d52f723e400225ebc8759709e11a1a17
BLAKE2b-256 checksum
How to use checksums
e82b7aa1a5ee6e013beec6219cd655a65d84cc5bfffa39ada8f6080186176d58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

Release history Release notifications | RSS feed

This release

1.0.7 This release

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page