use of org.apache.commons.math3.optim.SimpleBounds in project narchy by automenta.
the class MyCMAESOptimizerTest method testFitAccuracyDependsOnBoundary.
/**
* Cf. MATH-867
*/
@Test
public void testFitAccuracyDependsOnBoundary() {
double[] sigma1 = { 1e-1 };
MyCMAESOptimizer optimizer = new MyCMAESOptimizer(30000, 0, true, 10, 0, rng(), false, null, 5, sigma1);
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
@Override
public double value(double[] parameters) {
final double target = 11.1;
final double error = target - parameters[0];
return error * error;
}
};
final double[] start = { 1 };
// No bounds.
PointValuePair result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, SimpleBounds.unbounded(1), new InitialGuess(start));
final double resNoBound = result.getPoint()[0];
// Optimum is near the lower bound.
final double[] lower = { -20 };
final double[] upper = { 5e16 };
final double[] sigma2 = { 10 };
optimizer = new MyCMAESOptimizer(30000, 0, true, 10, 0, rng(), false, null, 5, sigma2);
result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new InitialGuess(start), new SimpleBounds(lower, upper));
final double resNearLo = result.getPoint()[0];
// Optimum is near the upper bound.
lower[0] = -5e16;
upper[0] = 20;
result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new InitialGuess(start), new SimpleBounds(lower, upper));
final double resNearHi = result.getPoint()[0];
// System.out.println("resNoBound=" + resNoBound +
// " resNearLo=" + resNearLo +
// " resNearHi=" + resNearHi);
// The two values currently differ by a substantial amount, indicating that
// the bounds definition can prevent reaching the optimum.
assertEquals(resNoBound, resNearLo, 1e-3);
assertEquals(resNoBound, resNearHi, 1e-3);
}
use of org.apache.commons.math3.optim.SimpleBounds in project narchy by automenta.
the class MyCMAESOptimizerTest method doTest.
/**
* @param func Function to optimize.
* @param startPoint Starting point.
* @param inSigma Individual input sigma.
* @param boundaries Upper / lower point limit.
* @param goal Minimization or maximization.
* @param lambda Population size used for offspring.
* @param isActive Covariance update mechanism.
* @param diagonalOnly Simplified covariance update.
* @param stopValue Termination criteria for optimization.
* @param fTol Tolerance relative error on the objective function.
* @param pointTol Tolerance for checking that the optimum is correct.
* @param maxEvaluations Maximum number of evaluations.
* @param expected Expected point / value.
*/
private void doTest(MultivariateFunction func, double[] startPoint, double[] inSigma, double[][] boundaries, GoalType goal, int lambda, boolean isActive, int diagonalOnly, double stopValue, double fTol, double pointTol, int maxEvaluations, PointValuePair expected) {
int dim = startPoint.length;
// test diagonalOnly = 0 - slow but normally fewer feval#
MyCMAESOptimizer optim = new MyCMAESOptimizer(30000, stopValue, isActive, diagonalOnly, 0, rng(), false, null, lambda, inSigma);
PointValuePair result = boundaries == null ? optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, new InitialGuess(startPoint), SimpleBounds.unbounded(dim)) : optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, new SimpleBounds(boundaries[0], boundaries[1]), new InitialGuess(startPoint));
// System.out.println("sol=" + Arrays.toString(result.getPoint()));
assertEquals(expected.getValue(), result.getValue(), fTol);
for (int i = 0; i < dim; i++) {
assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol);
}
assertTrue(optim.getIterations() > 0);
}
use of org.apache.commons.math3.optim.SimpleBounds in project narchy by automenta.
the class MyCMAESOptimizerTest method testMath864.
@Test
public void testMath864() {
final double[] sigma = { 1e-1 };
final MyCMAESOptimizer optimizer = new MyCMAESOptimizer(30000, 0, true, 10, 0, rng(), false, null, 5, sigma);
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
@Override
public double value(double[] parameters) {
final double target = 1;
final double error = target - parameters[0];
return error * error;
}
};
final double[] start = { 0 };
final double[] lower = { -1e6 };
final double[] upper = { 1.5 };
final double[] result = optimizer.optimize(new MaxEval(10000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new InitialGuess(start), new SimpleBounds(lower, upper)).getPoint();
assertTrue(result[0] <= upper[0], () -> "Out of bounds (" + result[0] + " > " + upper[0] + ")");
}
use of org.apache.commons.math3.optim.SimpleBounds in project aic-expresso by aic-sri-international.
the class OptimizationWithBOBYQA method optimize.
/**
* Main method. It computes the optimum of the function and returns a PointValuePair which contains the argopt and the optimum.
* Both can be computed directly with findOptimum and findArgopt.
*/
private PointValuePair optimize(BOBYQAOptimizer optimizer) {
final FunctionToOptimize f = new FunctionToOptimize(this.expressionToOptimize);
int dimension = numberOfVariablesInExpression();
double[] ones = new double[dimension];
for (int i = 0; i < dimension; i++) {
ones[i] = 1;
}
final PointValuePair optimum = optimizer.optimize(this.maxEval, new ObjectiveFunction(f), this.goalType, new SimpleBounds(new double[dimension], ones), this.initialGuess);
return optimum;
}
use of org.apache.commons.math3.optim.SimpleBounds in project GDSC-SMLM by aherbert.
the class PcPalmFitting method runBoundedOptimiser.
private PointValuePair runBoundedOptimiser(double[] initialSolution, double[] lowerB, double[] upperB, SumOfSquaresModelFunction function) {
// Create the functions to optimise
final ObjectiveFunction objective = new ObjectiveFunction(new SumOfSquaresMultivariateFunction(function));
final ObjectiveFunctionGradient gradient = new ObjectiveFunctionGradient(new SumOfSquaresMultivariateVectorFunction(function));
final boolean debug = false;
// Try a gradient optimiser since this will produce a deterministic solution
PointValuePair optimum = null;
boundedEvaluations = 0;
final MaxEval maxEvaluations = new MaxEval(2000);
MultivariateOptimizer opt = null;
for (int iteration = 0; iteration <= settings.fitRestarts; iteration++) {
try {
final double relativeThreshold = 1e-6;
opt = new BoundedNonLinearConjugateGradientOptimizer(BoundedNonLinearConjugateGradientOptimizer.Formula.FLETCHER_REEVES, new SimpleValueChecker(relativeThreshold, -1));
optimum = opt.optimize(maxEvaluations, gradient, objective, GoalType.MINIMIZE, new InitialGuess((optimum == null) ? initialSolution : optimum.getPointRef()), new SimpleBounds(lowerB, upperB));
if (debug) {
System.out.printf("Bounded Iter %d = %g (%d)\n", iteration, optimum.getValue(), opt.getEvaluations());
}
} catch (final RuntimeException ex) {
// No need to restart
break;
} finally {
if (opt != null) {
boundedEvaluations += opt.getEvaluations();
}
}
}
// Try a CMAES optimiser which is non-deterministic. To overcome this we perform restarts.
// CMAESOptimiser based on Matlab code:
// https://www.lri.fr/~hansen/cmaes.m
// Take the defaults from the Matlab documentation
final double stopFitness = 0;
final boolean isActiveCma = true;
final int diagonalOnly = 0;
final int checkFeasableCount = 1;
final RandomGenerator random = new RandomGeneratorAdapter(UniformRandomProviders.create());
final boolean generateStatistics = false;
final ConvergenceChecker<PointValuePair> checker = new SimpleValueChecker(1e-6, 1e-10);
// The sigma determines the search range for the variables. It should be 1/3 of the initial
// search region.
final double[] range = new double[lowerB.length];
for (int i = 0; i < lowerB.length; i++) {
range[i] = (upperB[i] - lowerB[i]) / 3;
}
final OptimizationData sigma = new CMAESOptimizer.Sigma(range);
final OptimizationData popSize = new CMAESOptimizer.PopulationSize((int) (4 + Math.floor(3 * Math.log(initialSolution.length))));
final SimpleBounds bounds = new SimpleBounds(lowerB, upperB);
opt = new CMAESOptimizer(maxEvaluations.getMaxEval(), stopFitness, isActiveCma, diagonalOnly, checkFeasableCount, random, generateStatistics, checker);
// Restart the optimiser several times and take the best answer.
for (int iteration = 0; iteration <= settings.fitRestarts; iteration++) {
try {
// Start from the initial solution
final PointValuePair constrainedSolution = opt.optimize(new InitialGuess(initialSolution), objective, GoalType.MINIMIZE, bounds, sigma, popSize, maxEvaluations);
if (debug) {
System.out.printf("CMAES Iter %d initial = %g (%d)\n", iteration, constrainedSolution.getValue(), opt.getEvaluations());
}
boundedEvaluations += opt.getEvaluations();
if (optimum == null || constrainedSolution.getValue() < optimum.getValue()) {
optimum = constrainedSolution;
}
} catch (final TooManyEvaluationsException | TooManyIterationsException ex) {
// Ignore
} finally {
boundedEvaluations += maxEvaluations.getMaxEval();
}
if (optimum == null) {
continue;
}
try {
// Also restart from the current optimum
final PointValuePair constrainedSolution = opt.optimize(new InitialGuess(optimum.getPointRef()), objective, GoalType.MINIMIZE, bounds, sigma, popSize, maxEvaluations);
if (debug) {
System.out.printf("CMAES Iter %d restart = %g (%d)\n", iteration, constrainedSolution.getValue(), opt.getEvaluations());
}
if (constrainedSolution.getValue() < optimum.getValue()) {
optimum = constrainedSolution;
}
} catch (final TooManyEvaluationsException | TooManyIterationsException ex) {
// Ignore
} finally {
boundedEvaluations += maxEvaluations.getMaxEval();
}
}
return optimum;
}
Aggregations