pyuncertainnumber.opt.ga

Classes

GA

Genetic Algorithm class

Module Contents

class pyuncertainnumber.opt.ga.GA(f: callable, task: str, dimension: int, varbound)

Genetic Algorithm class

Parameters:
  • f (callable) – the target function to be optimised, should have a single argument

  • task (str) – either ‘minimisation’ or ‘maximisation’

  • dimension (int) – the dimension of the design space, i.e. the number of parameters

  • varbound (np.ndarry) – the bounds for the design space, e.g. ‘np.array([[-2, 10]])’

Example

>>> import numpy as np
>>> from pyuncertainnumber.opt.ga import GA
>>> def black_box_function(x):
...     return np.exp(-(x - 2)**2) + np.exp(-(x - 6)**2 / 10) + 1 / (x**2 + 1)
>>> ga = GA(f=black_box_function, task='maximisation', dimension=1, varbound=np.array([[-2, 10]]))
>>> ga.run()  # the progress bar will be shown as side effect
>>> print(ga.optimal)  # get the optimal parameters and target value

Implementation

The range of the design space is defined by varbound, which is a 2D numpy array with shape (n, 2), where n is the number of parameters. This is a different signature compared to the Bayesian Optimisation class, which uses a dictionary for bounds. For consistency, it is recommended to use the class EpistemicDomain.to_varbound() to automatically take care of the format of the bounds.

example:
>>> from pyuncertainnumber import pba, EpistemicDomain
>>> ed = EpistemicDomain(pba.I(-5, 5), pba.I(-5, 5))
>>> ga = GA(
...     f=foo,
...     task='maximisation',
...     dimension=2,
...     varbound=ed.to_GA_varBounds()  # the trick
... )

See also

pyuncertainnumber.propagation.epistemic_uncertainty.helper.EpistemicDomain: the utility tool for setting up the epistemic domain.

f
task
dimension
varbound
setup()

Objective direction setup

get_results()

display the results of the optimization

run(algorithm_param=None, **kwargs)

run the genetic algorithm

Parameters:
  • algorithm_param (dict) – the parameters for the genetic algorithm; if None, the default parameters will be used.

  • convergence_curve (Boolean) – whether to return the convergence curve, default is True

  • progress_bar (Boolean) – whether to show the progress bar, default is True

property optimal
property optimal_xc

return the design variable that gives the optimal function value

property optimal_target

return the optimal target value