Skip to main content

Genetic Algorithm

A black-box optimization package published in pypi.

Installation

pip install genetic_algorithm

Example

The original example code can be found in test.py.

import numpy as np
import matplotlib.pyplot as plt
from genetic_algorithm import GeneticAlgorithm

Define the function to be optimized

x = np.linspace(0, 5, 1000)

ground_truth = x**3 - 2*(x**2) + 1


def func(a,b,c):
return x**a - b*(x**2) + c

Declare the fitness function as the negative RMSE of the predicted values.

def fitness(params):
return -np.sqrt(np.mean((ground_truth-func(**params))**2))

The parameter space to be searched should come as a dictionary as follows:

param_space = {"a": {'type': 'float', 'range':[0, 5]},
"b": {'type': 'float', 'range':[-1, 5]},
"c": {'type': 'int', 'range':[0, 3]}
}

Run genetic algorithm.

ga = GeneticAlgorithm(model=fitness,
                    param_space=param_space,
                    pop_size=100,
                    parent_pool_size=10,
                    keep_parent=False,
                    max_iter=100,
                    mutation_prob=0.3,
                    crossover_prob=0.7,
                    max_stop_rounds=5,
                    verbose=False)

Get the best parameters as well as the history.

result = ga.evolve()
print(result)

Visualize the difference between predicted and ground truth data:

predicted = func(**result["best params"])
plt.scatter(x, ground_truth, s=3, label="ground truth")
plt.scatter(x, predicted, s=3, c='r', label="predicted")
plt.legend(loc='upper left')
plt.show()
population size = 100 population size = 500

There is still quite some difference between the predicted ones and the ground truth. If the population size goes 100 to 500, the optimizer is then working better than before. There are other parameters such as cross-over rate and mutation rate which can also affect the optimization performance.

Metadata

Release files for genetic-algorithm 1.0.0

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

Source distribution (sdist)

Source distribution for genetic-algorithm 1.0.0
File Size Uploaded Provenance Actions
genetic_algorithm-1.0.0.tar.gz 6.9 kB Without provenance Details for genetic_algorithm-1.0.0.tar.gz

Release files / genetic_algorithm-1.0.0.tar.gz

Download URL genetic_algorithm-1.0.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5542e8a02fbab92d96004bf33e167b2bbcb2a0fab3754b36e5dd1346beedb4c7
BLAKE2b-256 checksum
How to use checksums
87d99a2fd9c5d53154baa867efccda351196d6c66ce34d4cc69390ee355fd052
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Security for genetic-algorithm 1.0.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

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