Genetic algorithms for 2-dimensional function maximization.
Project description
## fmga
**fmga** (**f**unction **m**aximization through **g**enetic **a**lgorithms) is a package that takes a genetic algorithm approach to maximization problem of non-convex objective functions in two dimensions.
The idea is to sample an evolving population of points converging to the function maximum over many iterations.
Given a function on two variables x and y, say:
```python
def f(x, y):
return x - math.sin(y) + 28
```
Pass this function as the *objective_function* argument to the **population2D** constructor (lambdas work too!):
```python
population = population2D(objective_function=f, population_size=60)
```
The population can be set to breed and iterate by using the **.converge()** method.
```python
population.converge(iterations=20)
```
To perform only one iteration of breeding and mutating, do:
```python
population.iterate()
```
Access population mean fitness and mean L1 diversity stats through the _.mean_fitness_ and _.mean_diversity_ attributes:
```python
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 **Point2D** object.
```python
best_point = population.best_estimate()
```
Every **Point2D** object has attributes 'x' and 'y', signifying the coordinates of the maxima point.
```python
print(best_point.x, " ", best_point.y)
```
To find the value of the function at this point, use:
```python
print(best_point.fitness)
```
The objective function doesn't have to be differentiable, or even continuous in the specified domain!
The population of 2-dimensional points undergoes random mutations - and is selected through elitism along with breeding with selection weights inversely proportional to fitness and diversity ranks.
## population2D Class Methods
The population2D constructor takes the following arguments:
**population_size** (default = 60) Number of points in the population.
**objective_function** The function to maximize!
**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.
**x_min, x_max, y_min, y_max** (default = 0, 100, 0, and 100 respectively) The domain where the points are spread.
**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 - 2 representing the most output, and 0 representing none.
**fmga** (**f**unction **m**aximization through **g**enetic **a**lgorithms) is a package that takes a genetic algorithm approach to maximization problem of non-convex objective functions in two dimensions.
The idea is to sample an evolving population of points converging to the function maximum over many iterations.
Given a function on two variables x and y, say:
```python
def f(x, y):
return x - math.sin(y) + 28
```
Pass this function as the *objective_function* argument to the **population2D** constructor (lambdas work too!):
```python
population = population2D(objective_function=f, population_size=60)
```
The population can be set to breed and iterate by using the **.converge()** method.
```python
population.converge(iterations=20)
```
To perform only one iteration of breeding and mutating, do:
```python
population.iterate()
```
Access population mean fitness and mean L1 diversity stats through the _.mean_fitness_ and _.mean_diversity_ attributes:
```python
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 **Point2D** object.
```python
best_point = population.best_estimate()
```
Every **Point2D** object has attributes 'x' and 'y', signifying the coordinates of the maxima point.
```python
print(best_point.x, " ", best_point.y)
```
To find the value of the function at this point, use:
```python
print(best_point.fitness)
```
The objective function doesn't have to be differentiable, or even continuous in the specified domain!
The population of 2-dimensional points undergoes random mutations - and is selected through elitism along with breeding with selection weights inversely proportional to fitness and diversity ranks.
## population2D Class Methods
The population2D constructor takes the following arguments:
**population_size** (default = 60) Number of points in the population.
**objective_function** The function to maximize!
**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.
**x_min, x_max, y_min, y_max** (default = 0, 100, 0, and 100 respectively) The domain where the points are spread.
**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 - 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
fmga-0.1.7.tar.gz
(4.8 kB
view details)
Built Distribution
fmga-0.1.7-py3-none-any.whl
(7.2 kB
view details)
File details
Details for the file fmga-0.1.7.tar.gz
.
File metadata
- Download URL: fmga-0.1.7.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 767398a6c65a5d8166b5a9424c33b19f1e840ad574b0b58552a33151188ad868 |
|
MD5 | 7ee4322d96a193a04ad1c5563b721587 |
|
BLAKE2b-256 | a8141c014e41bff97b31c4ec5f86d1df0c12e4a1cfbe7cc49b37e9a9cf0d1548 |
File details
Details for the file fmga-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: fmga-0.1.7-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41bbaa6f55a9c3c878e83c80b0b1e2e57f8007d39763eb0b97fa656ff8b2c013 |
|
MD5 | 261e43773bc55c043bc9e286f684c799 |
|
BLAKE2b-256 | 2106ac82165f367601c16cbc6d0dda7fbbf73948f2f448b94a657f1e736f859f |