Replace "CMAESOptimizer.Sigma" with "Sigma".

This commit is contained in:
Gilles Sadowski 2022-01-17 02:37:20 +01:00
parent 336811dff5
commit d81b5e921e
2 changed files with 35 additions and 62 deletions

View File

@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.List;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
@ -35,6 +34,7 @@ import org.apache.commons.math4.legacy.optim.OptimizationData;
import org.apache.commons.math4.legacy.optim.PointValuePair;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.PopulationSize;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.Sigma;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.MultivariateOptimizer;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.statistics.distribution.ContinuousDistribution;
@ -78,14 +78,6 @@ import org.apache.commons.math4.core.jdkmath.JdkMath;
* <li><a href="http://en.wikipedia.org/wiki/CMA-ES">Wikipedia</a></li>
* </ul>
*
* <p>
* The {@link PopulationSize number of offsprings} is the primary strategy
* parameter. In the absence of better clues, a good default could be an integer
* close to {@code 4 + 3 ln(n)}, where {@code n} is the number of optimized
* parameters. Increasing the population size improves global search properties
* at the expense of speed (which in general decreases at most linearly with
* increasing population size).
*
* @since 3.0
*/
public class CMAESOptimizer
@ -278,44 +270,6 @@ public class CMAESOptimizer
return statisticsDHistory;
}
/**
* Input sigma values.
* They define the initial coordinate-wise standard deviations for
* sampling new search points around the initial guess.
* It is suggested to set them to the estimated distance from the
* initial to the desired optimum.
* Small values induce the search to be more local (and very small
* values are more likely to find a local optimum close to the initial
* guess).
* Too small values might however lead to early termination.
*/
public static class Sigma implements OptimizationData {
/** Sigma values. */
private final double[] sigma;
/**
* @param s Sigma values.
* @throws NotPositiveException if any of the array entries is smaller
* than zero.
*/
public Sigma(double[] s) {
for (int i = 0; i < s.length; i++) {
if (s[i] < 0) {
throw new NotPositiveException(s[i]);
}
}
sigma = s.clone();
}
/**
* @return the sigma values.
*/
public double[] getSigma() {
return sigma.clone();
}
}
/**
* {@inheritDoc}
*
@ -323,14 +277,32 @@ public class CMAESOptimizer
* {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
* MultivariateOptimizer}, this method will register the following data:
* <ul>
* <li>{@link Sigma}</li>
* <li>{@link PopulationSize}</li>
* <li>
* {@link Sigma} values define the initial coordinate-wise standard
* deviations for sampling new search points around the initial guess.
* It is suggested to set them to the estimated distance from the
* initial to the desired optimum.
* Small values induce the search to be more local (and very small
* values are more likely to find a local optimum close to the initial
* guess).
* Too small values might however lead to early termination.
* </li>
* <li>
* {@link PopulationSize} is the number of offsprings and the primary
* strategy parameter.
* In the absence of better clues, a good default could be an integer
* close to {@code 4 + 3 ln(n)}, where {@code n} is the number of
* optimized parameters.
* Increasing the population size improves global search properties at
* the expense of speed (which in general decreases at most linearly
* with increasing population size).
* </li>
* </ul>
* @return {@inheritDoc}
* @throws TooManyEvaluationsException if the maximal number of
* evaluations is exceeded.
* @throws DimensionMismatchException if the initial guess, target, and weight
* arguments have inconsistent dimensions.
* @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded.
* @throws DimensionMismatchException if the initial guess, target, and
* weight arguments have inconsistent dimensions.
*/
@Override
public PointValuePair optimize(OptimizationData... optData) {

View File

@ -18,7 +18,7 @@ package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
@ -28,6 +28,7 @@ import org.apache.commons.math4.legacy.optim.PointValuePair;
import org.apache.commons.math4.legacy.optim.SimpleBounds;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.PopulationSize;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.Sigma;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.apache.commons.rng.simple.RandomSource;
@ -81,7 +82,7 @@ public class CMAESOptimizerTest {
1e-13, 1e-6, 100000, expected);
}
@Test(expected = NotPositiveException.class)
@Test(expected = NotStrictlyPositiveException.class)
public void testInputSigmaNegative() {
final int dim = 12;
double[] startPoint = OptimTestUtils.point(dim, 0.5);
@ -175,7 +176,7 @@ public class CMAESOptimizerTest {
SimpleBounds.unbounded(1),
GoalType.MINIMIZE,
new PopulationSize(5),
new CMAESOptimizer.Sigma(sigma),
new Sigma(sigma),
new InitialGuess(start)).getPoint();
Assert.assertEquals(0, result[0], 1e-7);
}
@ -396,7 +397,7 @@ public class CMAESOptimizerTest {
new ObjectiveFunction(fitnessFunction),
GoalType.MINIMIZE,
new PopulationSize(5),
new CMAESOptimizer.Sigma(sigma),
new Sigma(sigma),
new InitialGuess(start),
new SimpleBounds(lower, upper)).getPoint();
Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
@ -428,7 +429,7 @@ public class CMAESOptimizerTest {
GoalType.MINIMIZE,
SimpleBounds.unbounded(1),
new PopulationSize(5),
new CMAESOptimizer.Sigma(new double[] { 1e-1 }),
new Sigma(new double[] { 1e-1 }),
new InitialGuess(start));
final double resNoBound = result.getPoint()[0];
@ -440,7 +441,7 @@ public class CMAESOptimizerTest {
new ObjectiveFunction(fitnessFunction),
GoalType.MINIMIZE,
new PopulationSize(5),
new CMAESOptimizer.Sigma(sigma),
new Sigma(sigma),
new InitialGuess(start),
new SimpleBounds(lower, upper));
final double resNearLo = result.getPoint()[0];
@ -452,7 +453,7 @@ public class CMAESOptimizerTest {
new ObjectiveFunction(fitnessFunction),
GoalType.MINIMIZE,
new PopulationSize(5),
new CMAESOptimizer.Sigma(sigma),
new Sigma(sigma),
new InitialGuess(start),
new SimpleBounds(lower, upper));
final double resNearHi = result.getPoint()[0];
@ -505,7 +506,7 @@ public class CMAESOptimizerTest {
goal,
new InitialGuess(startPoint),
SimpleBounds.unbounded(dim),
new CMAESOptimizer.Sigma(inSigma),
new Sigma(inSigma),
new PopulationSize(lambda)) :
optim.optimize(new MaxEval(maxEvaluations),
new ObjectiveFunction(func),
@ -513,7 +514,7 @@ public class CMAESOptimizerTest {
new SimpleBounds(boundaries[0],
boundaries[1]),
new InitialGuess(startPoint),
new CMAESOptimizer.Sigma(inSigma),
new Sigma(inSigma),
new PopulationSize(lambda));
Assert.assertEquals(expected.getValue(), result.getValue(), fTol);