GeneticAlPy
Genetic Algorithms with Python
Yet another package for lightweight applications of GA in Python.
This package provides utilities for implementation of Genetic Algorithm (Holland 1962) for multivariate, multimodal optimization problems. Check out an introduction from a biological perspective in my paper, Nayak & Saha (2022)
By default, the binary representation is chosen as the genotype. The package provides roulette wheel, stochastic universal sampling and rank selection for choosing parents at each generation, uniform crossover, and bit-flip mutation. Elitist and liberal strategies may also be employed optionally.
Quickstart
Install the package with pip
pip install geneticalpy
Imports
from geneticalpy import genetical, examples
import numpy as np
import matplotlib.pyplot as plt
Define a fitness function from your cost function
cost = examples.ackley # replace this with your objective
def fitness(x): # to be maximized, by definition
return 1/(1 + cost(x)) # this also depends loosely on your objective
Set initial GA hyperparameters
decimals = 4
n_var = 2
var_ranges = np.array([[-10,]*n_var,[10,]*n_var])
n_bits_segment = len(format(int(max(var_ranges[1] - var_ranges[0])*10**decimals), 'b'))
n_bits_chromosome = n_bits_segment * n_var
offset = -var_ranges[0]
init_popsize = 400
Initialize GA
PopGen = genetical.PopGenetics(
fitness_func = fitness,
n_var = n_var,
decimal_acc = decimals,
n_bits_chromosome = n_bits_chromosome,
)
pop_bin = PopGen.initialize_population(
popsize = init_popsize,
var_ranges = var_ranges,
return_genotype = True,
offset = offset,
)
Run evolution
evol_rec = PopGen.evolve(
pop_bin,
n_gen = 50,
n_pairs = 800,
elitist = True,
n_elites = 3,
liberal = False,
n_runts = 0,
switch_selection = 5,
prob_mut = 0.02,
prune = True,
pruning_cutoff = 800,
verbose = True,
n_workers = 1,
)
Look at the results
print(f"Best solution is {evol_rec['fittest_individual']} with fitness {evol_rec['best_overall_fitness']}.")
plt.figure(figsize = (5,3))
gens = np.arange(len(evol_rec['best_fitness_per_generation']))+1
plt.plot(gens, evol_rec['best_fitness_per_generation'], color = 'green', label = 'Best')
plt.plot(gens, evol_rec['mean_fitness_per_generation'], color = 'blue', label = 'Mean')
plt.plot(gens, evol_rec['median_fitness_per_generation'], color = 'indigo', label = 'Median')
plt.legend()
plt.xlim(gens[0], gens[-1])
plt.xlabel('Generations')
plt.ylabel('Fitness')
plt.show()
Tutorial
Check out the notebook at tutorial/tutorial.ipynb for a lightning tutorial of GeneticAlPy!
Get in touch
Drop me an email at parth3e8@gmail.com in case of any questions or to request more functionality!
Metadata
Release files for geneticalpy 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| geneticalpy-0.1.1.tar.gz | 24.1 kB | With provenance | Details for geneticalpy-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| geneticalpy-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for geneticalpy-0.1.1-py3-none-any.whl |
Total release size: 47.2 kB
Release files / geneticalpy-0.1.1.tar.gz
| Download URL | geneticalpy-0.1.1.tar.gz |
|---|---|
| Size | 24.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3a23ba0bd7e6b20c2c1f73fe6cde5c1acc4de197d9b839efb01cee8b6deea91
|
|
BLAKE2b-256 checksum How to use checksums |
6b5c1d4e0422d42b6803b85db90aaf36764f9415118cdd4b84c738facee21a8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 1, 2026.
Transparency logRelease files / geneticalpy-0.1.1-py3-none-any.whl
| Download URL | geneticalpy-0.1.1-py3-none-any.whl |
|---|---|
| Size | 23.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f63479b4cb1be41e021b65063d51107ee4e90da19a49c7b93b05ab4f085110f2
|
|
BLAKE2b-256 checksum How to use checksums |
57da62a3c40f7b637506e3b39ec662e4647aa44d579fa734e752dbb70575b8df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 1, 2026.
Transparency logSecurity for geneticalpy 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for geneticalpy-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.