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: 11
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.5.tar.gz
(5.6 kB
view details)
Built Distribution
File details
Details for the file pyalgen-0.0.5.tar.gz
.
File metadata
- Download URL: pyalgen-0.0.5.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e5de85e4d1ad36f69fc5d80262cf5bc2d426759ee8dd439be6434b2de5062c9 |
|
MD5 | e389212ee9bdc52fa9ddefa507e6dd4f |
|
BLAKE2b-256 | 1b19766979f98f5d864d6de18014cbe9975c8e79fed21514176ae98682d20ce5 |
File details
Details for the file pyalgen-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: pyalgen-0.0.5-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f8685d9c27606e58c97318fd929280efebb442cbdb076b38e01a7229d2c1893 |
|
MD5 | c77c3362bab49ae52f9710e5659f0417 |
|
BLAKE2b-256 | 17d612460691f3f756644b12db671dcd00e3166ed3efad87d88d756fc631e73e |