Skip to main content

Minimal python library for genetic algorithm

Project description

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

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

pyalgen-0.0.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

pyalgen-0.0.6-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pyalgen-0.0.6.tar.gz.

File metadata

  • Download URL: pyalgen-0.0.6.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyalgen-0.0.6.tar.gz
Algorithm Hash digest
SHA256 ec1965fe5eed2825e8a0696bbce250718a22c816fdb4bc42b576f13bf584e863
MD5 c683a33c262eaa11ca4fb674c385fc5b
BLAKE2b-256 29cd5e00de033f63045dbced3b99d8164addff9c5ba5900f1ca47ef556a56f61

See more details on using hashes here.

File details

Details for the file pyalgen-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyalgen-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pyalgen-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a44398d40c645e709a055872a00a79ad5fa2940b98615b505676106c3640e1b5
MD5 d1465e6208b5a59d863bb65e41c4ccc3
BLAKE2b-256 2a9e46abdadaadb82ff73dfa2fac5c4ecec482e2df49466bbe1ea88047d5fab7

See more details on using hashes here.

Supported by

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