org.apache.commons.math3.optim.nonlinear.scalar.noderiv
public class CMAESOptimizer extends MultivariateOptimizer
An implementation of the active Covariance Matrix Adaptation Evolution Strategy (CMA-ES) for non-linear, non-convex, non-smooth, global function minimization. The CMA-Evolution Strategy (CMA-ES) is a reliable stochastic optimization method which should be applied if derivative-based methods, e.g. quasi-Newton BFGS or conjugate gradient, fail due to a rugged search landscape (e.g. noise, local optima, outlier, etc.) of the objective function. Like a quasi-Newton method, the CMA-ES learns and applies a variable metric on the underlying search space. Unlike a quasi-Newton method, the CMA-ES neither estimates nor uses gradients, making it considerably more reliable in terms of finding a good, or even close to optimal, solution.
In general, on smooth objective functions the CMA-ES is roughly ten times slower than BFGS (counting objective function evaluations, no gradients provided). For up to variables also the derivative-free simplex direct search method (Nelder and Mead) can be faster, but it is far less reliable than CMA-ES.
The CMA-ES is particularly well suited for non-separable and/or badly conditioned problems. To observe the advantage of CMA compared to a conventional evolution strategy, it will usually take about function evaluations. On difficult problems the complete optimization (a single run) is expected to take roughly between and function evaluations.
This implementation is translated and adapted from the Matlab version
of the CMA-ES algorithm as implemented in module cmaes.m
version 3.51.
Modifier and Type | Class and Description |
---|---|
private static class |
CMAESOptimizer.DoubleIndex
Used to sort fitness values.
|
private class |
CMAESOptimizer.FitnessFunction
Normalizes fitness values to the range [0,1].
|
static class |
CMAESOptimizer.PopulationSize
Population size.
|
static class |
CMAESOptimizer.Sigma
Input sigma values.
|
Modifier and Type | Field and Description |
---|---|
private RealMatrix |
B
Coordinate system.
|
private RealMatrix |
BD
B*D, stored for efficiency.
|
private RealMatrix |
C
Covariance matrix.
|
private double |
cc
Cumulation constant.
|
private double |
ccov1
Learning rate for rank-one update.
|
private double |
ccov1Sep
Learning rate for rank-one update - diagonalOnly
|
private double |
ccovmu
Learning rate for rank-mu update'
|
private double |
ccovmuSep
Learning rate for rank-mu update - diagonalOnly
|
private int |
checkFeasableCount
Determines how often a new random offspring is generated in case it is
not feasible / beyond the defined limits, default is 0.
|
private double |
chiN
Expectation of ||N(0,I)|| == norm(randn(N,1)).
|
private double |
cs
Cumulation constant for step-size.
|
private RealMatrix |
D
Scaling.
|
private double |
damps
Damping for step-size.
|
private RealMatrix |
diagC
Diagonal of C, used for diagonalOnly.
|
private RealMatrix |
diagD
Diagonal of sqrt(D), stored for efficiency.
|
private int |
diagonalOnly
Defines the number of initial iterations, where the covariance matrix
remains diagonal and the algorithm has internally linear time complexity.
|
private int |
dimension
Number of objective variables/problem dimension
|
private double[] |
fitnessHistory
History queue of best values.
|
private boolean |
generateStatistics
Indicates whether statistic data is collected.
|
private int |
historySize
Size of history queue of best values.
|
private double[] |
inputSigma |
private boolean |
isActiveCMA
Covariance update mechanism, default is active CMA.
|
private boolean |
isMinimize
Number of objective variables/problem dimension
|
private int |
iterations
Number of iterations already performed.
|
private int |
lambda
Population size, offspring number.
|
private double |
logMu2
log(mu + 0.5), stored for efficiency.
|
private int |
maxIterations
Maximal number of iterations allowed.
|
private int |
mu
Number of parents/points for recombination.
|
private double |
mueff
Variance-effectiveness of sum w_i x_i.
|
private double |
normps
Norm of ps, stored for efficiency.
|
private RealMatrix |
pc
Evolution path.
|
private RealMatrix |
ps
Evolution path for sigma.
|
private RandomGenerator |
random
Random generator.
|
private double |
sigma
Overall standard deviation - search volume.
|
private List<RealMatrix> |
statisticsDHistory
History of D matrix.
|
private List<Double> |
statisticsFitnessHistory
History of fitness values.
|
private List<RealMatrix> |
statisticsMeanHistory
History of mean matrix.
|
private List<Double> |
statisticsSigmaHistory
History of sigma values.
|
private double |
stopFitness
Limit for fitness value.
|
private double |
stopTolFun
Stop if fun-changes smaller stopTolFun.
|
private double |
stopTolHistFun
Stop if back fun-changes smaller stopTolHistFun.
|
private double |
stopTolUpX
Stop if x-changes larger stopTolUpX.
|
private double |
stopTolX
Stop if x-change smaller stopTolX.
|
private RealMatrix |
weights
Array for weighted recombination.
|
private RealMatrix |
xmean
Objective variables.
|
evaluations
Constructor and Description |
---|
CMAESOptimizer(int maxIterations,
double stopFitness,
boolean isActiveCMA,
int diagonalOnly,
int checkFeasableCount,
RandomGenerator random,
boolean generateStatistics,
ConvergenceChecker<PointValuePair> checker) |
Modifier and Type | Method and Description |
---|---|
private void |
checkParameters()
Checks dimensions and values of boundaries and inputSigma if defined.
|
private static void |
copyColumn(RealMatrix m1,
int col1,
RealMatrix m2,
int col2)
Copies a column from m1 to m2.
|
private static RealMatrix |
diag(RealMatrix m) |
private static RealMatrix |
divide(RealMatrix m,
RealMatrix n) |
protected PointValuePair |
doOptimize()
Performs the bulk of the optimization algorithm.
|
private static RealMatrix |
eye(int n,
int m) |
List<RealMatrix> |
getStatisticsDHistory() |
List<Double> |
getStatisticsFitnessHistory() |
List<RealMatrix> |
getStatisticsMeanHistory() |
List<Double> |
getStatisticsSigmaHistory() |
private void |
initializeCMA(double[] guess)
Initialization of the dynamic search parameters
|
private static int[] |
inverse(int[] indices) |
private static RealMatrix |
log(RealMatrix m) |
private static double |
max(double[] m) |
private static double |
max(RealMatrix m) |
private static double |
min(double[] m) |
private static double |
min(RealMatrix m) |
private static RealMatrix |
ones(int n,
int m) |
PointValuePair |
optimize(OptimizationData... optData)
Stores data and performs the optimization.
|
private void |
parseOptimizationData(OptimizationData... optData)
Scans the list of (required and optional) optimization data that
characterize the problem.
|
private static void |
push(double[] vals,
double val)
Pushes the current best fitness value in a history queue.
|
private double[] |
randn(int size) |
private RealMatrix |
randn1(int size,
int popSize) |
private static RealMatrix |
repmat(RealMatrix mat,
int n,
int m) |
private static int[] |
reverse(int[] indices) |
private static RealMatrix |
selectColumns(RealMatrix m,
int[] cols) |
private static RealMatrix |
sequence(double start,
double end,
double step) |
private int[] |
sortedIndices(double[] doubles)
Sorts fitness values.
|
private static RealMatrix |
sqrt(RealMatrix m) |
private static RealMatrix |
square(RealMatrix m) |
private static RealMatrix |
sumRows(RealMatrix m) |
private static RealMatrix |
times(RealMatrix m,
RealMatrix n) |
private static RealMatrix |
triu(RealMatrix m,
int k) |
private void |
updateBD(double negccov)
Update B and D from C.
|
private void |
updateCovariance(boolean hsig,
RealMatrix bestArx,
RealMatrix arz,
int[] arindex,
RealMatrix xold)
Update of the covariance matrix C.
|
private void |
updateCovarianceDiagonalOnly(boolean hsig,
RealMatrix bestArz)
Update of the covariance matrix C for diagonalOnly > 0
|
private boolean |
updateEvolutionPaths(RealMatrix zmean,
RealMatrix xold)
Update of the evolution paths ps and pc.
|
private static RealMatrix |
zeros(int n,
int m) |
computeObjectiveValue, getGoalType
getLowerBound, getStartPoint, getUpperBound
getConvergenceChecker, getEvaluations, getIterations, getMaxEvaluations, getMaxIterations, incrementEvaluationCount, incrementIterationCount
private int lambda
private final boolean isActiveCMA
private final int checkFeasableCount
private double[] inputSigma
CMAESOptimizer.Sigma
private int dimension
private int diagonalOnly
private boolean isMinimize
private final boolean generateStatistics
private final int maxIterations
private final double stopFitness
private double stopTolUpX
private double stopTolX
private double stopTolFun
private double stopTolHistFun
private int mu
private double logMu2
private RealMatrix weights
private double mueff
private double sigma
private double cc
private double cs
private double damps
private double ccov1
private double ccovmu
private double chiN
private double ccov1Sep
private double ccovmuSep
private RealMatrix xmean
private RealMatrix pc
private RealMatrix ps
private double normps
private RealMatrix B
private RealMatrix D
private RealMatrix BD
private RealMatrix diagD
private RealMatrix C
private RealMatrix diagC
private int iterations
private double[] fitnessHistory
private int historySize
private final RandomGenerator random
private final List<RealMatrix> statisticsMeanHistory
private final List<Double> statisticsFitnessHistory
private final List<RealMatrix> statisticsDHistory
public CMAESOptimizer(int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics, ConvergenceChecker<PointValuePair> checker)
maxIterations
- Maximal number of iterations.stopFitness
- Whether to stop if objective function value is smaller than
stopFitness
.isActiveCMA
- Chooses the covariance matrix update method.diagonalOnly
- Number of initial iterations, where the covariance matrix
remains diagonal.checkFeasableCount
- Determines how often new random objective variables are
generated in case they are out of bounds.random
- Random generator.generateStatistics
- Whether statistic data is collected.checker
- Convergence checker.public List<Double> getStatisticsSigmaHistory()
public List<RealMatrix> getStatisticsMeanHistory()
public List<Double> getStatisticsFitnessHistory()
public List<RealMatrix> getStatisticsDHistory()
public PointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException
optimize
in class MultivariateOptimizer
optData
- Optimization data. The following data will be looked for:
TooManyEvaluationsException
- if the maximal number of
evaluations is exceeded.DimensionMismatchException
- if the initial guess, target, and weight
arguments have inconsistent dimensions.protected PointValuePair doOptimize()
doOptimize
in class BaseOptimizer<PointValuePair>
private void parseOptimizationData(OptimizationData... optData)
optData
- Optimization data. The following data will be looked for:
private void checkParameters()
private void initializeCMA(double[] guess)
guess
- Initial guess for the arguments of the fitness function.private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold)
zmean
- Weighted row matrix of the gaussian random numbers generating
the current offspring.xold
- xmean matrix of the previous generation.private void updateCovarianceDiagonalOnly(boolean hsig, RealMatrix bestArz)
hsig
- Flag indicating a small correction.bestArz
- Fitness-sorted matrix of the gaussian random values of the
current offspring.private void updateCovariance(boolean hsig, RealMatrix bestArx, RealMatrix arz, int[] arindex, RealMatrix xold)
hsig
- Flag indicating a small correction.bestArx
- Fitness-sorted matrix of the argument vectors producing the
current offspring.arz
- Unsorted matrix containing the gaussian random values of the
current offspring.arindex
- Indices indicating the fitness-order of the current offspring.xold
- xmean matrix of the previous generation.private void updateBD(double negccov)
negccov
- Negative covariance factor.private static void push(double[] vals, double val)
vals
- History queue.val
- Current best fitness value.private int[] sortedIndices(double[] doubles)
doubles
- Array of values to be sorted.private static RealMatrix log(RealMatrix m)
m
- Input matrixprivate static RealMatrix sqrt(RealMatrix m)
m
- Input matrix.private static RealMatrix square(RealMatrix m)
m
- Input matrix.private static RealMatrix times(RealMatrix m, RealMatrix n)
m
- Input matrix 1.n
- Input matrix 2.private static RealMatrix divide(RealMatrix m, RealMatrix n)
m
- Input matrix 1.n
- Input matrix 2.private static RealMatrix selectColumns(RealMatrix m, int[] cols)
m
- Input matrix.cols
- Columns to select.private static RealMatrix triu(RealMatrix m, int k)
m
- Input matrix.k
- Diagonal position.private static RealMatrix sumRows(RealMatrix m)
m
- Input matrix.private static RealMatrix diag(RealMatrix m)
m
- Input matrix.private static void copyColumn(RealMatrix m1, int col1, RealMatrix m2, int col2)
m1
- Source matrix.col1
- Source column.m2
- Target matrix.col2
- Target column.private static RealMatrix ones(int n, int m)
n
- Number of rows.m
- Number of columns.private static RealMatrix eye(int n, int m)
n
- Number of rows.m
- Number of columns.private static RealMatrix zeros(int n, int m)
n
- Number of rows.m
- Number of columns.private static RealMatrix repmat(RealMatrix mat, int n, int m)
mat
- Input matrix.n
- Number of row replicates.m
- Number of column replicates.private static RealMatrix sequence(double start, double end, double step)
start
- Start value.end
- End value.step
- Step size.private static double max(RealMatrix m)
m
- Input matrix.private static double min(RealMatrix m)
m
- Input matrix.private static double max(double[] m)
m
- Input array.private static double min(double[] m)
m
- Input array.private static int[] inverse(int[] indices)
indices
- Input index array.private static int[] reverse(int[] indices)
indices
- Input index array.private double[] randn(int size)
size
- Length of random array.private RealMatrix randn1(int size, int popSize)
size
- Number of rows.popSize
- Population size.Copyright (c) 2003-2013 Apache Software Foundation