|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer<FUNC>
org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateSimpleBoundsOptimizer<MultivariateFunction>
org.apache.commons.math3.optimization.direct.CMAESOptimizer
public class CMAESOptimizer
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.
Nested Class Summary | |
---|---|
private static class |
CMAESOptimizer.DoubleIndex
Used to sort fitness values. |
private class |
CMAESOptimizer.FitnessFunction
Normalizes fitness values to the range [0,1]. |
Field Summary | |
---|---|
private RealMatrix |
B
Coordinate system. |
private RealMatrix |
BD
B*D, stored for efficiency. |
private double[][] |
boundaries
Lower and upper boundaries of the objective variables. |
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. |
static int |
DEFAULT_CHECKFEASABLECOUNT
Default value for checkFeasableCount : 0. |
static int |
DEFAULT_DIAGONALONLY
Default value for diagonalOnly : 0. |
static boolean |
DEFAULT_ISACTIVECMA
Default value for isActiveCMA : true. |
static int |
DEFAULT_MAXITERATIONS
Default value for maxIterations : 30000. |
static RandomGenerator |
DEFAULT_RANDOMGENERATOR
Default value for random . |
static double |
DEFAULT_STOPFITNESS
Default value for stopFitness : 0.0. |
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
Individual sigma values - initial search volume. |
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. |
Fields inherited from class org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer |
---|
evaluations |
Constructor Summary | |
---|---|
CMAESOptimizer()
Default constructor, uses default parameters |
|
CMAESOptimizer(int lambda)
|
|
CMAESOptimizer(int lambda,
double[] inputSigma)
|
|
CMAESOptimizer(int lambda,
double[] inputSigma,
int maxIterations,
double stopFitness,
boolean isActiveCMA,
int diagonalOnly,
int checkFeasableCount,
RandomGenerator random,
boolean generateStatistics)
|
|
CMAESOptimizer(int lambda,
double[] inputSigma,
int maxIterations,
double stopFitness,
boolean isActiveCMA,
int diagonalOnly,
int checkFeasableCount,
RandomGenerator random,
boolean generateStatistics,
ConvergenceChecker<PointValuePair> checker)
|
Method Summary | |
---|---|
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()
Perform 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)
|
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,
RealMatrix xold)
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)
|
Methods inherited from class org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateSimpleBoundsOptimizer |
---|
getLowerBound, getUpperBound, optimize, optimize |
Methods inherited from class org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer |
---|
computeObjectiveValue, getConvergenceChecker, getEvaluations, getGoalType, getMaxEvaluations, getStartPoint |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.apache.commons.math3.optimization.BaseMultivariateOptimizer |
---|
optimize |
Methods inherited from interface org.apache.commons.math3.optimization.BaseOptimizer |
---|
getConvergenceChecker, getEvaluations, getMaxEvaluations |
Field Detail |
---|
public static final int DEFAULT_CHECKFEASABLECOUNT
checkFeasableCount
: 0.
public static final double DEFAULT_STOPFITNESS
stopFitness
: 0.0.
public static final boolean DEFAULT_ISACTIVECMA
isActiveCMA
: true.
public static final int DEFAULT_MAXITERATIONS
maxIterations
: 30000.
public static final int DEFAULT_DIAGONALONLY
diagonalOnly
: 0.
public static final RandomGenerator DEFAULT_RANDOMGENERATOR
random
.
private int lambda
private boolean isActiveCMA
private int checkFeasableCount
private double[][] boundaries
private double[] inputSigma
private int dimension
private int diagonalOnly
private boolean isMinimize
private boolean generateStatistics
private int maxIterations
private 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 RandomGenerator random
private List<Double> statisticsSigmaHistory
private List<RealMatrix> statisticsMeanHistory
private List<Double> statisticsFitnessHistory
private List<RealMatrix> statisticsDHistory
Constructor Detail |
---|
public CMAESOptimizer()
public CMAESOptimizer(int lambda)
lambda
- Population size.public CMAESOptimizer(int lambda, double[] inputSigma)
lambda
- Population size.inputSigma
- Initial search volume; sigma of offspring objective variables.public CMAESOptimizer(int lambda, double[] inputSigma, int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics)
lambda
- Population size.inputSigma
- Initial search volume; sigma of offspring objective variables.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.public CMAESOptimizer(int lambda, double[] inputSigma, int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics, ConvergenceChecker<PointValuePair> checker)
lambda
- Population size.inputSigma
- Initial search volume; sigma of offspring objective variables.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.Method Detail |
---|
public List<Double> getStatisticsSigmaHistory()
public List<RealMatrix> getStatisticsMeanHistory()
public List<Double> getStatisticsFitnessHistory()
public List<RealMatrix> getStatisticsDHistory()
protected PointValuePair doOptimize()
doOptimize
in class BaseAbstractMultivariateOptimizer<MultivariateFunction>
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, RealMatrix xold)
hsig
- Flag indicating a small correction.bestArz
- Fitness-sorted matrix of the gaussian random values of the
current offspring.xold
- xmean matrix of the previous generation.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 matrix
private 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 1.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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |