Skip to main content

pyalgen - A minimal library for genetic algorithm in python

Install

pip3 install pyalgen

How to use

  1. Import the package
import pyalgen # import pyalgen
from pyalgen import TestFunctions as tf # test functions to optimize
  1. Define population
pop = pyalgen.Population(low=-10, high=10, dtype='float', dist='uniform')
# the variable in the population is of type float and the 
# values are taken from a uniform distribution in (low, high)
population = pop(pop_size=1000, variables=2)
# variables is the number of variables to optimize. We are optimizing the 
# matyas function which has two variables
# search space ranges from -10 to 10
# pop_size is the population size of each generation


Matyas function

  1. Select type of selection, crossover and mutation strategies
selection = pyalgen.Selection.tournament
crossover = pyalgen.Crossover.onepoint
mutation = pyalgen.Mutation.randompoint
  1. Instantiate Genetic Algorithm object with defined variables
ga = pyalgen.GeneticAlgorithm(population, tf.matyas, selection, crossover, mutation)
  1. Run the algorithm
iterations, objective, pop = ga.forward(iterations=200)
# iterations is the number of generations to run for
print(f'objective: {objective.min()}, solution: {pop[objective.argmin()]}, generation: {iterations}')
# print the minimum objective and the chromosome in population which 
# given minimum objective 
  1. Check the result
100%|██████████████████████████████████████████████████████████| 1000/1000 [00:03<00:00, 262.55it/s]
objective: 7.719286052427051e-07, solution: [-0.00447918 -0.00410235], generation: 1000
# global minimum of matyas is at f(0, 0) = 0
# our algorithm gives minimum, f(-0.004, -0.004) = 7.7e-07
# which is pretty close 

Results can be improved by tweaking the parameters

Testing the algorithm on custom function

Let's solve the equation,

Complete code
import pyalgen

pop = pyalgen.Population(1, 30, unique=True, dtype='int')
population = pop(1000, 4) # here we generate intergers in range[1, 30)
# for population

selection = pyalgen.Selection.tournament
crossover = pyalgen.Crossover.onepoint
mutation = pyalgen.Mutation.randompoint

def obj(a, b, c, d): # objective function
    return a + 2*b + 3*c + 4*d - 30 

ga = pyalgen.GeneticAlgorithm(population, obj, selection, crossover, mutation)


iterations, objective, pop = ga.forward(iterations=50)

if iterations == 1000:
    print(f'objective: {objective.min()}, \
        solution: {pop[objective.argmin()]}, generation: {iterations}')   
else:
    print(f'objective: {objective[objective == 0][0]},\
         solution: {pop[objective == 0][0]}, generation: {iterations}')   

GeneticAlgorithm breaks the computation, if any of the chromosome reached out objective, i.e, 0

Result

 40%|████████████████████████▊                                     | 20/50 [00:00<00:00, 509.30it/s]
objective: 0, solution: [4 6 2 2], generation: 20

The algorithm reached a solution during generation: 11 Solution: a = 11, b = 4, c = 1, d = 2

Release files for pyalgen 0.0.6

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

Source distribution (sdist)

Source distribution for pyalgen 0.0.6
File Size Uploaded
pyalgen-0.0.6.tar.gz 5.6 kB Details

Built distribution (wheel)

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

Total release size: 11.8 kB

Release files / pyalgen-0.0.6.tar.gz

Download URL pyalgen-0.0.6.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ec1965fe5eed2825e8a0696bbce250718a22c816fdb4bc42b576f13bf584e863
BLAKE2b-256 checksum
How to use checksums
29cd5e00de033f63045dbced3b99d8164addff9c5ba5900f1ca47ef556a56f61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release files / pyalgen-0.0.6-py3-none-any.whl

Download URL pyalgen-0.0.6-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a44398d40c645e709a055872a00a79ad5fa2940b98615b505676106c3640e1b5
BLAKE2b-256 checksum
How to use checksums
2a9e46abdadaadb82ff73dfa2fac5c4ecec482e2df49466bbe1ea88047d5fab7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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