Search in sources :

Example 16 with MaxEval

use of org.apache.commons.math3.optim.MaxEval in project narchy by automenta.

the class Optimize method solve.

public void solve(int dim, ObjectiveFunction func, double[] mid, double[] min, double[] max, double[] inc, int maxIterations) {
    if (dim == 1) {
        // use a solver capable of 1 dim
        new SimplexOptimizer(1e-10, 1e-30).optimize(new MaxEval(maxIterations), func, GoalType.MAXIMIZE, new InitialGuess(mid), // new NelderMeadSimplex(inc)
        new MultiDirectionalSimplex(inc));
    } else {
        int popSize = // 4 + 3 ln(n)
        (int) Math.ceil(4 + 3 * Math.log(tweaks.size()));
        double[] sigma = MathArrays.scale(1f, inc);
        MyCMAESOptimizer m = new MyCMAESOptimizer(maxIterations, Double.NaN, true, 0, 1, new MersenneTwister(System.nanoTime()), true, null, popSize, sigma);
        m.optimize(func, GoalType.MAXIMIZE, new MaxEval(maxIterations), new SimpleBounds(min, max), new InitialGuess(mid));
        m.print(System.out);
    // final int numIterpolationPoints = 3 * dim; //2 * dim + 1 + 1;
    // new BOBYQAOptimizer(numIterpolationPoints,
    // dim * 2.0,
    // 1.0E-4D /* ? */).optimize(
    // MaxEval.unlimited(), //new MaxEval(maxIterations),
    // new MaxIter(maxIterations),
    // func,
    // GoalType.MAXIMIZE,
    // new SimpleBounds(min, max),
    // new InitialGuess(mid));
    }
}
Also used : MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) MersenneTwister(org.apache.commons.math3.random.MersenneTwister)

Example 17 with MaxEval

use of org.apache.commons.math3.optim.MaxEval 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);
}
Also used : MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PointValuePair(org.apache.commons.math3.optim.PointValuePair) Test(org.junit.jupiter.api.Test)

Example 18 with MaxEval

use of org.apache.commons.math3.optim.MaxEval 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);
}
Also used : MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 19 with MaxEval

use of org.apache.commons.math3.optim.MaxEval 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] + ")");
}
Also used : MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Test(org.junit.jupiter.api.Test)

Example 20 with MaxEval

use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.

the class Ling method optimizeNonGaussianity.

private void optimizeNonGaussianity(final int rowIndex, final TetradMatrix dataSetTetradMatrix, final double[][] W, List<Mapping> allMappings) {
    final List<Mapping> mappings = mappingsForRow(rowIndex, allMappings);
    MultivariateFunction function = new MultivariateFunction() {

        public double value(double[] values) {
            for (int i = 0; i < values.length; i++) {
                Mapping mapping = mappings.get(i);
                W[mapping.getI()][mapping.getJ()] = values[i];
            }
            double v = ngFullData(rowIndex, dataSetTetradMatrix, W);
            if (Double.isNaN(v))
                return 10000;
            return -(v);
        }
    };
    {
        double[] values = new double[mappings.size()];
        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            values[k] = W[mapping.getI()][mapping.getJ()];
        }
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
        values = pair.getPoint();
        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            W[mapping.getI()][mapping.getJ()] = values[k];
        }
    }
}
Also used : MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Aggregations

MaxEval (org.apache.commons.math3.optim.MaxEval)47 InitialGuess (org.apache.commons.math3.optim.InitialGuess)39 PointValuePair (org.apache.commons.math3.optim.PointValuePair)39 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)39 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)19 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)16 PowellOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer)15 SimpleBounds (org.apache.commons.math3.optim.SimpleBounds)14 UnivariateObjectiveFunction (org.apache.commons.math3.optim.univariate.UnivariateObjectiveFunction)12 MultivariateFunction (org.apache.commons.math3.analysis.MultivariateFunction)11 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)10 OptimizationData (org.apache.commons.math3.optim.OptimizationData)10 SimpleValueChecker (org.apache.commons.math3.optim.SimpleValueChecker)10 CMAESOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer)10 UnivariatePointValuePair (org.apache.commons.math3.optim.univariate.UnivariatePointValuePair)10 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)8 ObjectiveFunctionGradient (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient)6 NelderMeadSimplex (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex)6 BrentOptimizer (org.apache.commons.math3.optim.univariate.BrentOptimizer)6 SearchInterval (org.apache.commons.math3.optim.univariate.SearchInterval)6