Top Description Inners Fields Constructors Methods
org.apache.commons.math3.optim.nonlinear.scalar.noderiv

public Class CMAESOptimizer

extends MultivariateOptimizer
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .List, org.apache.commons.math3.exception.DimensionMismatchException, .NotPositiveException, .NotStrictlyPositiveException, .OutOfRangeException, .TooManyEvaluationsException, org.apache.commons.math3.linear.Array2DRowRealMatrix, .EigenDecomposition, .MatrixUtils, .RealMatrix, org.apache.commons.math3.optim.ConvergenceChecker, .OptimizationData, .PointValuePair, org.apache.commons.math3.optim.nonlinear.scalar.GoalType, .MultivariateOptimizer, org.apache.commons.math3.random.RandomGenerator, org.apache.commons.math3.util.FastMath, .MathArrays

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 N=10 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 30 N function evaluations. On difficult problems the complete optimization (a single run) is expected to take roughly between 30 N and 300 N2 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.

For more information, please refer to the following links:

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CMAESOptimizer.DoubleIndex

Used to sort fitness values.

private class
CMAESOptimizer.FitnessFunction

Normalizes fitness values to the range [0,1].

public static class
public static class
CMAESOptimizer.Sigma

Input sigma values.

private static class
CMAESOptimizer.ValuePenaltyPair

Stores the value and penalty (for repair of out of bounds point).

Field Summary

Modifier and TypeField 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 final 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 final boolean
generateStatistics

Indicates whether statistic data is collected.

private int
historySize

Size of history queue of best values.

private double[]
private final boolean
isActiveCMA

Covariance update mechanism, default is active CMA.

private boolean
isMinimize

Number of objective variables/problem dimension

private int
iterations

Hides org.apache.commons.math3.optim.BaseOptimizer.iterations.

Number of iterations already performed.
private int
lambda

Population size, offspring number.

private double
logMu2

log(mu + 0.5), stored for efficiency.

private final 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 final RandomGenerator
random

Random generator.

private double
sigma

Overall standard deviation - search volume.

private final List<RealMatrix>
statisticsDHistory

History of D matrix.

private final List<Double>
statisticsFitnessHistory

History of fitness values.

private final List<RealMatrix>
statisticsMeanHistory

History of mean matrix.

private final List<Double>
statisticsSigmaHistory

History of sigma values.

private final 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.

Constructor Summary

AccessConstructor and Description
public
CMAESOptimizer(int
Maximal number of iterations.
maxIterations
,
double
Whether to stop if objective function value is smaller than stopFitness.
stopFitness
,
boolean
Chooses the covariance matrix update method.
isActiveCMA
,
int
Number of initial iterations, where the covariance matrix remains diagonal.
diagonalOnly
,
int
Determines how often new random objective variables are generated in case they are out of bounds.
checkFeasableCount
,
RandomGenerator
Random generator.
random
,
boolean
Whether statistic data is collected.
generateStatistics
,
ConvergenceChecker<PointValuePair>
Convergence checker.
checker
)

Method Summary

Modifier and TypeMethod and Description
private void
checkParameters()

Hides org.apache.commons.math3.optim.BaseMultivariateOptimizer.checkParameters.

Checks dimensions and values of boundaries and inputSigma if defined.

private static void
copyColumn(final RealMatrix
Source matrix.
m1
,
int
Source column.
col1
,
RealMatrix
Target matrix.
m2
,
int
Target column.
col2
)

Copies a column from m1 to m2.

private static RealMatrix

Returns:

the diagonal n-by-n matrix if m is a column matrix or the column matrix representing the diagonal if m is a n-by-n matrix.
diag
(final RealMatrix
Input matrix.
m
)

private static RealMatrix

Returns:

Matrix where the elements of m and n are element-wise divided.
divide
(final RealMatrix
Input matrix 1.
m
,
final RealMatrix
Input matrix 2.
n
)

protected PointValuePair
doOptimize()

Implements abstract org.apache.commons.math3.optim.BaseOptimizer.doOptimize.

Performs the bulk of the optimization algorithm.

private static RealMatrix

Returns:

n-by-m matrix of 0 values out of diagonal, and 1 values on the diagonal.
eye
(int
Number of rows.
n
,
int
Number of columns.
m
)

public List<RealMatrix>

Returns:

History of D matrix.
getStatisticsDHistory
()

public List<Double>

Returns:

History of fitness values.
getStatisticsFitnessHistory
()

public List<RealMatrix>

Returns:

History of mean matrix.
getStatisticsMeanHistory
()

public List<Double>

Returns:

History of sigma values.
getStatisticsSigmaHistory
()

private void
initializeCMA(double[]
Initial guess for the arguments of the fitness function.
guess
)

Initialization of the dynamic search parameters

private static int[]

Returns:

the inverse of the mapping defined by indices.
inverse
(final int[]
Input index array.
indices
)

private static RealMatrix

Returns:

Matrix representing the element-wise logarithm of m.
log
(final RealMatrix
Input matrix
m
)

private static double

Returns:

the maximum of the matrix element values.
max
(final RealMatrix
Input matrix.
m
)

private static double

Returns:

the maximum of the array values.
max
(final double[]
Input array.
m
)

private static double

Returns:

the minimum of the matrix element values.
min
(final RealMatrix
Input matrix.
m
)

private static double

Returns:

the minimum of the array values.
min
(final double[]
Input array.
m
)

private static RealMatrix

Returns:

n-by-m matrix filled with 1.
ones
(int
Number of rows.
n
,
int
Number of columns.
m
)

public PointValuePair

Returns:

a point/value pair that satisfies the convergence criteria.
optimize
(OptimizationData...
Optimization data. In addition to those documented in MultivariateOptimizer, this method will register the following data:
optData
)

Overrides org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer.optimize.

Stores data and performs the optimization.

protected void
parseOptimizationData(OptimizationData...
Optimization data. The following data will be looked for:
optData
)

Overrides org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer.parseOptimizationData.

Scans the list of (required and optional) optimization data that characterize the problem.

private static void
push(double[]
History queue.
vals
,
double
Current best fitness value.
val
)

Pushes the current best fitness value in a history queue.

private double[]

Returns:

an array of Gaussian random numbers.
randn
(int
Length of random array.
size
)

private RealMatrix

Returns:

a 2-dimensional matrix of Gaussian random numbers.
randn1
(int
Number of rows.
size
,
int
Population size.
popSize
)

private static RealMatrix

Returns:

a matrix which replicates the input matrix in both directions.
repmat
(final RealMatrix
Input matrix.
mat
,
int
Number of row replicates.
n
,
int
Number of column replicates.
m
)

private static int[]

Returns:

the indices in inverse order (last is first).
reverse
(final int[]
Input index array.
indices
)

private static RealMatrix

Returns:

Matrix representing the selected columns.
selectColumns
(final RealMatrix
Input matrix.
m
,
final int[]
Columns to select.
cols
)

private static RealMatrix

Returns:

a sequence as column matrix.
sequence
(double
Start value.
start
,
double
End value.
end
,
double
Step size.
step
)

private int[]

Returns:

a sorted array of indices pointing into doubles.
sortedIndices
(final double[]
Array of values to be sorted.
doubles
)

Sorts fitness values.

private static RealMatrix

Returns:

Matrix representing the element-wise square root of m.
sqrt
(final RealMatrix
Input matrix.
m
)

private static RealMatrix

Returns:

Matrix representing the element-wise square of m.
square
(final RealMatrix
Input matrix.
m
)

private static RealMatrix

Returns:

Row matrix representing the sums of the rows.
sumRows
(final RealMatrix
Input matrix.
m
)

private static RealMatrix

Returns:

the matrix where the elements of m and n are element-wise multiplied.
times
(final RealMatrix
Input matrix 1.
m
,
final RealMatrix
Input matrix 2.
n
)

private static RealMatrix

Returns:

Upper triangular part of matrix.
triu
(final RealMatrix
Input matrix.
m
,
int
Diagonal position.
k
)

private void
updateBD(double
Negative covariance factor.
negccov
)

Update B and D from C.

private void
updateCovariance(boolean
Flag indicating a small correction.
hsig
,
final RealMatrix
Fitness-sorted matrix of the argument vectors producing the current offspring.
bestArx
,
final RealMatrix
Unsorted matrix containing the gaussian random values of the current offspring.
arz
,
final int[]
Indices indicating the fitness-order of the current offspring.
arindex
,
final RealMatrix
xmean matrix of the previous generation.
xold
)

Update of the covariance matrix C.

private void
updateCovarianceDiagonalOnly(boolean
Flag indicating a small correction.
hsig
,
final RealMatrix
Fitness-sorted matrix of the gaussian random values of the current offspring.
bestArz
)

Update of the covariance matrix C for diagonalOnly > 0

private boolean

Returns:

hsig flag indicating a small correction.
updateEvolutionPaths
(RealMatrix
Weighted row matrix of the gaussian random numbers generating the current offspring.
zmean
,
RealMatrix
xmean matrix of the previous generation.
xold
)

Update of the evolution paths ps and pc.

private double

Returns:

a double equal to maximum value minus minimum value.
valueRange
(final CMAESOptimizer.ValuePenaltyPair[]
Array of valuePenaltyPairs to get range from.
vpPairs
)

Get range of values.

private static RealMatrix

Returns:

n-by-m matrix of zero values.
zeros
(int
Number of rows.
n
,
int
Number of columns.
m
)

Inherited from org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer:
computeObjectiveValuegetGoalType