Minimal python library for genetic algorithm
Project description
pyalgen - A minimal library for genetic algorithm in python
Install
pip3 install pyalgen
How to use
- Import the package
import pyalgen # import pyalgen
from pyalgen import TestFunctions as tf # test functions to optimize
- 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
- Select type of selection, crossover and mutation strategies
selection = pyalgen.Selection.tournament
crossover = pyalgen.Crossover.onepoint
mutation = pyalgen.Mutation.randompoint
- Instantiate Genetic Algorithm object with defined variables
ga = pyalgen.GeneticAlgorithm(population, tf.matyas, selection, crossover, mutation)
- Run the algorithm
iterations, objective, pop = ga.forward(iterations=200)
# iterations is the number of generations to run for
print(f'min_value: {objective.min()}, solution: {pop[objective.argmin()]}, generation: {iterations}')
# print the minimum objective and the chromosome in population which
# given minimum objective
- Check the result
100%|██████████████████████████████████████████████████████████| 1000/1000 [00:03<00:00, 262.55it/s]
min_value: 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=1000)
if iterations == 1000:
print(f'min_value: {objective.min()}, \
solution: {pop[objective.argmin()]}, generation: {iterations}')
else:
print(f'min_value: {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
1%|▋ | 11/1000 [00:00<00:05, 171.30it/s]
min_value: 0, solution: [11 4 1 2], generation: 12
One of our chromosome 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.4.tar.gz
(5.3 kB
view details)
Built Distribution
File details
Details for the file pyalgen-0.0.4.tar.gz
.
File metadata
- Download URL: pyalgen-0.0.4.tar.gz
- Upload date:
- Size: 5.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95196c73e45481d89969e4fb9919a82c4580b66e1e25ba63af0209937e16ae29 |
|
MD5 | 3f4214cf809a71e7834ccec6768ad2f1 |
|
BLAKE2b-256 | 0c46b7a721530d8c2bd227def0471f31216b630b968e1a9e764375364cdeadb8 |
File details
Details for the file pyalgen-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pyalgen-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97a4000f360c555119b2b8214a7079c936ebf0777ec2ff68f6f894e67fb48c43 |
|
MD5 | 30945770486a687adadceef9c1d5625b |
|
BLAKE2b-256 | c959ca1b92c3055cb01ccdd7154cbc2e39501c6024057a01da5bda794233433e |