Skip to main content

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 and ranking selection with selection weights inversely proportional to fitness and diversity ranks.

fmga now supports multiprocessing through pathos too!

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 .maximize() method (lambdas work too!).

best_point = fmga.maximize(f, population_size=60, dimensions=3)

The maximize() method creates a Population of Point objects, calls the .converge() method on the Population object, and finally, returns a Point object representing the n-dimensional point with best fitness through the .best_estimate() method.

print(best_point, best_point.fitness)

By default, the multiprocessing argument defaults to False, so to enable multiprocessing, set this argument to True, and pass the number of processes to be spawned as the processes argument.

best_point = fmga.maximize(f, multiprocessing=True, processes=4)

Note that, when multiprocessing is enabled on Windows systems, you must put a guard over the entry point of your script. See here for a how-to.

fmga also supports a variable number of dimensions to optimise over, passed as the dimensions argument, which defaults to the number of arguments of the objective function passed.

If you wish to interact with the Population object directly, you can. Both of the following work:

population = fmga.Population(f, population_size=60, dimensions=3)
population = fmga.Population(population_size=60, objective_function=f, dimensions=3)

If you wish to define custom boundaries, create a list of tuples, for each dimension. Default boundaries are (0, 100). (This is different than in versions 1.x)

boundaries = [(0, 2.5), (0, 10)]

and pass this as the boundaries argument to the Population constructor or the maximise() method:

population = fmga.Population(f, population_size=60, boundaries=boundaries)
best_point = fmga.maximize(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. To find the value of the function at this point, use the fitness attribute.

print(best_point.coordinates)
print(best_point.fitness)

Population Class Methods

The Population constructor takes the following arguments, in order:
objective_function The function to maximize!
dimensions (default = number of arguments of objective_function) The dimensionality of the points and the number of variables to maximize over.

From versions 2.8.0 and onward, the PopulationParameters class handles the parameters below. The interface is the same as previous versions, however, so you can pass these arguments to the Population constructor as before.

population_size (default = 60) Number of points in the population.
boundaries (default = (0, 100) for every dimension) Must be an iterable of tuples. The tuple indicates 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!
multiprocessing (default = False) Whether multiprocessing is enabled
processes (default = multiprocessing.cpu_count()) Number of processes to spawn if multiprocessing is enabled.

The maximize() method takes all of the above, an iterations argument, defaulting to 15, signifying the number of iterations that the underlying population undergoes, as well as a verbose argument (default = 0, was 2 for versions <= 2.4.0) denoting how much console output to be displayed after each iteration (Must take values 0, 1 or 2 with 2 representing the most output, and 0 representing none.)

The converge() and iterate() methods also take the iterations and verbose arguments.

The minimize() method is a wrapper over the maximize() method - replacing the objective function by its negative, and maximizing this new objective function.

The unpack() method accepts two arguments, a tuple of values and a list of shapes. If the length of the list of shapes is greater than one, it returns a list of numpy arrays of shape according to the list, by reshaping the tuple in-order. Otherwise it returns just a numpy array of the passed shape, formed by reshaping the tuple.
This is useful when working with a large number of arguments! Example:

def f(*args):
    x, y, z = fmga.unpack(args, (1, (2, 2), 4))

    # x.shape == (1,)
    # y.shape == (2, 2)
    # z.shape == (4,)

    return x - y[0][0] + z[2]

Dependencies

  • numpy
  • pathos (>= 0.2.2.1)

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

fmga-2.8.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

fmga-2.8.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file fmga-2.8.0.tar.gz.

File metadata

  • Download URL: fmga-2.8.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.5.2

File hashes

Hashes for fmga-2.8.0.tar.gz
Algorithm Hash digest
SHA256 6075704858d14a80a35086371e154925f280b0e4a06be03c9d9c961b666a43e5
MD5 1d476132c7b7f329da7bcb0cecb66612
BLAKE2b-256 ef3e18d76e739d77263854934f99ebca7004e94ec8e919811d136710228cd83c

See more details on using hashes here.

File details

Details for the file fmga-2.8.0-py3-none-any.whl.

File metadata

  • Download URL: fmga-2.8.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.5.2

File hashes

Hashes for fmga-2.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb67fcd3f4ef6420d1a1df90e6d6828eb070fd5b6415f0f95fa6d1c259d7b87c
MD5 dbacc20d7c989e85fc88cd0694eaaa12
BLAKE2b-256 49193bb4da5c30358a9436942bbcef5b38df549e9866fd30fb9e87916d3741d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page