Genetic algorithms for n-dimensional function maximization.
Project description
fmga
fmga (function maximization through genetic algorithms) is a package that takes a genetic algorithm approach to maximization problem of non-convex objective functions in multiple dimensions.
The objective function doesn't have to be differentiable, or even continuous in the specified domain!
The idea is to sample an evolving population of points converging to the function maximum over many iterations.
The population of n-dimensional points undergoes random mutations - and is selected through elitism along with breeding with selection weights inversely proportional to fitness and diversity ranks.
Installation
Install with pip:
pip install fmga
Import within the python script with:
import fmga
Execution
Given a function on multiple variables, say:
def f(x, y, z):
return x - math.sin(y) * z
Pass this function as the objective_function argument to the Population constructor (lambdas work too!). Note that this is the first argument to the constructor, so both of the following will work:
population = fmga.Population(f, population_size=60)
population = fmga.Population(population_size=60, objective_function=f)
If you wish to define custom boundaries, create a dictionary of keys, and store a tuple of elements:
boundaries = {}
boundaries[0] = (-50, 50)
boundaries[1] = (20, 200)
...
and pass this to the boundaries argument to the Population constructor:
population = fmga.Population(f, population_size=60, boundaries=boundaries)
Note that the default range for missing dimensions is (0, 100).
The population can be set to breed and iterate by using the .converge() method.
population.converge(iterations=20)
To perform only one iteration of breeding and mutating, do:
population.iterate()
Access population mean fitness and mean L1 diversity stats through the .mean_fitness and .mean_diversity attributes:
print(population.mean_fitness, population.mean_diversity)
The .best_estimate() method returns the point closest to the function point of maxima in the population, as a Point object.
best_point = population.best_estimate()
Every Point object has the coordinates attribute, a numpy array signifying the coordinates of point.
print(best_point.coordinates)
To find the value of the function at this point, use:
print(best_point.fitness)
Population Class Methods
The Population constructor takes the following arguments, in order:
objective_function The function to maximize!
population_size (default = 60) Number of points in the population.
boundaries (default = (0, 100) for every dimension) Must be a dictionary, with keys (representing dimensions) varying from 0 to
number of dimensions - 1. For every key, the tuple of 2 elements represents the domain where the points are spread along that dimension.
elite_fraction (default = 0.1) Fraction of the population's points to be kept as elite during breeding. Must be between 0 and 1, inclusive.
mutation_probability (default = 0.05) How likely is is for a single point to mutate - this probability is the same for all points in the population.
Must be between 0 and 1, inclusive.
mutation_range (default = 5) The range of the mutation when it does occur. Note that the point will never mutate out of the domain defined!
verbose (default = 2) How much output to be displayed when iterating population after population. Must take values 0, 1 or 2 with 2 representing the most output, and 0 representing none.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fmga-1.0.0.tar.gz
.
File metadata
- Download URL: fmga-1.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4a290917a94634f85360810d8eef4a3d1fdb683ad43f8a40e7431a1f1e63d13 |
|
MD5 | 6fc6a30c4e3fe5be81ec9d940e84dfdb |
|
BLAKE2b-256 | 2e99c8ae04c38b8eb216a7b4d5567e915da67a997c45fe6311e354ceef48215a |
File details
Details for the file fmga-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: fmga-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6afe2921485c4e136036d7c4c3d9b7ecdb2156ee746ad1d11f78820cd1fb96a3 |
|
MD5 | 37c6dcbeef0ead9d03aaeeb584ddd954 |
|
BLAKE2b-256 | 9c7dfdfeb96109a1a049675d027cede98ff2a2d058337483e4e9a092ad3066e8 |