A python package implementing the genetic algorithm
Project description
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.
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
File details
Details for the file genetic_algorithm-1.0.0.tar.gz
.
File metadata
- Download URL: genetic_algorithm-1.0.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5542e8a02fbab92d96004bf33e167b2bbcb2a0fab3754b36e5dd1346beedb4c7 |
|
MD5 | 6c25175e46b3a937e545733b2294663f |
|
BLAKE2b-256 | 87d99a2fd9c5d53154baa867efccda351196d6c66ce34d4cc69390ee355fd052 |