You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/02/25 22:49:35 UTC
[07/18] [math] Remove deprecated optimization package.
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/CMAESOptimizerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/CMAESOptimizerTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/CMAESOptimizerTest.java
deleted file mode 100644
index f8587ee..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/CMAESOptimizerTest.java
+++ /dev/null
@@ -1,761 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.math4.optimization.direct;
-
-import java.util.Arrays;
-import java.util.Random;
-
-import org.apache.commons.math4.Retry;
-import org.apache.commons.math4.RetryRunner;
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.exception.DimensionMismatchException;
-import org.apache.commons.math4.exception.NotPositiveException;
-import org.apache.commons.math4.exception.NumberIsTooLargeException;
-import org.apache.commons.math4.exception.NumberIsTooSmallException;
-import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.InitialGuess;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.SimpleBounds;
-import org.apache.commons.math4.optimization.direct.CMAESOptimizer;
-import org.apache.commons.math4.random.MersenneTwister;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-/**
- * Test for {@link CMAESOptimizer}.
- */
-@Deprecated
-@RunWith(RetryRunner.class)
-public class CMAESOptimizerTest {
-
- static final int DIM = 13;
- static final int LAMBDA = 4 + (int)(3.*FastMath.log(DIM));
-
- @Test(expected = NumberIsTooLargeException.class)
- public void testInitOutofbounds1() {
- double[] startPoint = point(DIM,3);
- double[] insigma = point(DIM, 0.3);
- double[][] boundaries = boundaries(DIM,-1,2);
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
- @Test(expected = NumberIsTooSmallException.class)
- public void testInitOutofbounds2() {
- double[] startPoint = point(DIM, -2);
- double[] insigma = point(DIM, 0.3);
- double[][] boundaries = boundaries(DIM,-1,2);
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test(expected = DimensionMismatchException.class)
- public void testBoundariesDimensionMismatch() {
- double[] startPoint = point(DIM,0.5);
- double[] insigma = point(DIM, 0.3);
- double[][] boundaries = boundaries(DIM+1,-1,2);
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test(expected = NotPositiveException.class)
- public void testInputSigmaNegative() {
- double[] startPoint = point(DIM,0.5);
- double[] insigma = point(DIM,-0.5);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test(expected = OutOfRangeException.class)
- public void testInputSigmaOutOfRange() {
- double[] startPoint = point(DIM,0.5);
- double[] insigma = point(DIM, 1.1);
- double[][] boundaries = boundaries(DIM,-0.5,0.5);
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test(expected = DimensionMismatchException.class)
- public void testInputSigmaDimensionMismatch() {
- double[] startPoint = point(DIM,0.5);
- double[] insigma = point(DIM + 1, 0.5);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- @Retry(3)
- public void testRosen() {
- double[] startPoint = point(DIM,0.1);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- @Retry(3)
- public void testMaximize() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),1.0);
- doTest(new MinusElli(), startPoint, insigma, boundaries,
- GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13,
- 2e-10, 5e-6, 100000, expected);
- doTest(new MinusElli(), startPoint, insigma, boundaries,
- GoalType.MAXIMIZE, LAMBDA, false, 0, 1.0-1e-13,
- 2e-10, 5e-6, 100000, expected);
- boundaries = boundaries(DIM,-0.3,0.3);
- startPoint = point(DIM,0.1);
- doTest(new MinusElli(), startPoint, insigma, boundaries,
- GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13,
- 2e-10, 5e-6, 100000, expected);
- }
-
- @Test
- public void testEllipse() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Elli(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new Elli(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testElliRotated() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new ElliRotated(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new ElliRotated(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testCigar() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Cigar(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 200000, expected);
- doTest(new Cigar(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testCigarWithBoundaries() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = boundaries(DIM, -1e100, Double.POSITIVE_INFINITY);
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Cigar(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 200000, expected);
- doTest(new Cigar(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testTwoAxes() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new TwoAxes(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 200000, expected);
- doTest(new TwoAxes(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13,
- 1e-8, 1e-3, 200000, expected);
- }
-
- @Test
- public void testCigTab() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.3);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new CigTab(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 5e-5, 100000, expected);
- doTest(new CigTab(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 5e-5, 100000, expected);
- }
-
- @Test
- public void testSphere() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Sphere(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new Sphere(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testTablet() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Tablet(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new Tablet(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testDiffPow() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new DiffPow(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 10, true, 0, 1e-13,
- 1e-8, 1e-1, 100000, expected);
- doTest(new DiffPow(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 10, false, 0, 1e-13,
- 1e-8, 2e-1, 100000, expected);
- }
-
- @Test
- public void testSsDiffPow() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new SsDiffPow(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 10, true, 0, 1e-13,
- 1e-4, 1e-1, 200000, expected);
- doTest(new SsDiffPow(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 10, false, 0, 1e-13,
- 1e-4, 1e-1, 200000, expected);
- }
-
- @Test
- public void testAckley() {
- double[] startPoint = point(DIM,1.0);
- double[] insigma = point(DIM,1.0);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Ackley(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
- 1e-9, 1e-5, 100000, expected);
- doTest(new Ackley(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13,
- 1e-9, 1e-5, 100000, expected);
- }
-
- @Test
- public void testRastrigin() {
- double[] startPoint = point(DIM,0.1);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,0.0),0.0);
- doTest(new Rastrigin(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), true, 0, 1e-13,
- 1e-13, 1e-6, 200000, expected);
- doTest(new Rastrigin(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), false, 0, 1e-13,
- 1e-13, 1e-6, 200000, expected);
- }
-
- @Test
- public void testConstrainedRosen() {
- double[] startPoint = point(DIM, 0.1);
- double[] insigma = point(DIM, 0.1);
- double[][] boundaries = boundaries(DIM, -1, 2);
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13,
- 1e-13, 1e-6, 100000, expected);
- }
-
- @Test
- public void testDiagonalRosen() {
- double[] startPoint = point(DIM,0.1);
- double[] insigma = point(DIM,0.1);
- double[][] boundaries = null;
- PointValuePair expected =
- new PointValuePair(point(DIM,1.0),0.0);
- doTest(new Rosen(), startPoint, insigma, boundaries,
- GoalType.MINIMIZE, LAMBDA, false, 1, 1e-13,
- 1e-10, 1e-4, 1000000, expected);
- }
-
- @Test
- public void testMath864() {
- final CMAESOptimizer optimizer = new CMAESOptimizer();
- final MultivariateFunction fitnessFunction = new MultivariateFunction() {
- 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(10000, fitnessFunction, GoalType.MINIMIZE,
- start, lower, upper).getPoint();
- Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
- result[0] <= upper[0]);
- }
-
- /**
- * Cf. MATH-867
- */
- @Test
- public void testFitAccuracyDependsOnBoundary() {
- final CMAESOptimizer optimizer = new CMAESOptimizer();
- final MultivariateFunction fitnessFunction = new MultivariateFunction() {
- 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(100000, fitnessFunction, GoalType.MINIMIZE,
- start);
- final double resNoBound = result.getPoint()[0];
-
- // Optimum is near the lower bound.
- final double[] lower = { -20 };
- final double[] upper = { 5e16 };
- result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE,
- start, lower, upper);
- final double resNearLo = result.getPoint()[0];
-
- // Optimum is near the upper bound.
- lower[0] = -5e16;
- upper[0] = 20;
- result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE,
- start, 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.
- Assert.assertEquals(resNoBound, resNearLo, 1e-3);
- Assert.assertEquals(resNoBound, resNearHi, 1e-3);
- }
-
- /**
- * @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#
- CMAESOptimizer optim = new CMAESOptimizer(30000, stopValue, isActive, diagonalOnly,
- 0, new MersenneTwister(), false, null);
- final double[] lB = boundaries == null ? null : boundaries[0];
- final double[] uB = boundaries == null ? null : boundaries[1];
- PointValuePair result = boundaries == null ?
- optim.optimize(maxEvaluations, func, goal,
- new InitialGuess(startPoint),
- new CMAESOptimizer.Sigma(inSigma),
- new CMAESOptimizer.PopulationSize(lambda)) :
- optim.optimize(maxEvaluations, func, goal,
- new InitialGuess(startPoint),
- new SimpleBounds(lB, uB),
- new CMAESOptimizer.Sigma(inSigma),
- new CMAESOptimizer.PopulationSize(lambda));
- // System.out.println("sol=" + Arrays.toString(result.getPoint()));
- Assert.assertEquals(expected.getValue(), result.getValue(), fTol);
- for (int i = 0; i < dim; i++) {
- Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol);
- }
- }
-
- private static double[] point(int n, double value) {
- double[] ds = new double[n];
- Arrays.fill(ds, value);
- return ds;
- }
-
- private static double[][] boundaries(int dim,
- double lower, double upper) {
- double[][] boundaries = new double[2][dim];
- for (int i = 0; i < dim; i++)
- boundaries[0][i] = lower;
- for (int i = 0; i < dim; i++)
- boundaries[1][i] = upper;
- return boundaries;
- }
-
- private static class Sphere implements MultivariateFunction {
-
- public double value(double[] x) {
- double f = 0;
- for (int i = 0; i < x.length; ++i)
- f += x[i] * x[i];
- return f;
- }
- }
-
- private static class Cigar implements MultivariateFunction {
- private double factor;
-
- Cigar() {
- this(1e3);
- }
-
- Cigar(double axisratio) {
- factor = axisratio * axisratio;
- }
-
- public double value(double[] x) {
- double f = x[0] * x[0];
- for (int i = 1; i < x.length; ++i)
- f += factor * x[i] * x[i];
- return f;
- }
- }
-
- private static class Tablet implements MultivariateFunction {
- private double factor;
-
- Tablet() {
- this(1e3);
- }
-
- Tablet(double axisratio) {
- factor = axisratio * axisratio;
- }
-
- public double value(double[] x) {
- double f = factor * x[0] * x[0];
- for (int i = 1; i < x.length; ++i)
- f += x[i] * x[i];
- return f;
- }
- }
-
- private static class CigTab implements MultivariateFunction {
- private double factor;
-
- CigTab() {
- this(1e4);
- }
-
- CigTab(double axisratio) {
- factor = axisratio;
- }
-
- public double value(double[] x) {
- int end = x.length - 1;
- double f = x[0] * x[0] / factor + factor * x[end] * x[end];
- for (int i = 1; i < end; ++i)
- f += x[i] * x[i];
- return f;
- }
- }
-
- private static class TwoAxes implements MultivariateFunction {
-
- private double factor;
-
- TwoAxes() {
- this(1e6);
- }
-
- TwoAxes(double axisratio) {
- factor = axisratio * axisratio;
- }
-
- public double value(double[] x) {
- double f = 0;
- for (int i = 0; i < x.length; ++i)
- f += (i < x.length / 2 ? factor : 1) * x[i] * x[i];
- return f;
- }
- }
-
- private static class ElliRotated implements MultivariateFunction {
- private Basis B = new Basis();
- private double factor;
-
- ElliRotated() {
- this(1e3);
- }
-
- ElliRotated(double axisratio) {
- factor = axisratio * axisratio;
- }
-
- public double value(double[] x) {
- double f = 0;
- x = B.Rotate(x);
- for (int i = 0; i < x.length; ++i)
- f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
- return f;
- }
- }
-
- private static class Elli implements MultivariateFunction {
-
- private double factor;
-
- Elli() {
- this(1e3);
- }
-
- Elli(double axisratio) {
- factor = axisratio * axisratio;
- }
-
- public double value(double[] x) {
- double f = 0;
- for (int i = 0; i < x.length; ++i)
- f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
- return f;
- }
- }
-
- private static class MinusElli implements MultivariateFunction {
-
- public double value(double[] x) {
- return 1.0-(new Elli().value(x));
- }
- }
-
- private static class DiffPow implements MultivariateFunction {
-
- public double value(double[] x) {
- double f = 0;
- for (int i = 0; i < x.length; ++i)
- f += FastMath.pow(FastMath.abs(x[i]), 2. + 10 * (double) i
- / (x.length - 1.));
- return f;
- }
- }
-
- private static class SsDiffPow implements MultivariateFunction {
-
- public double value(double[] x) {
- double f = FastMath.pow(new DiffPow().value(x), 0.25);
- return f;
- }
- }
-
- private static class Rosen implements MultivariateFunction {
-
- public double value(double[] x) {
- double f = 0;
- for (int i = 0; i < x.length - 1; ++i)
- f += 1e2 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1])
- + (x[i] - 1.) * (x[i] - 1.);
- return f;
- }
- }
-
- private static class Ackley implements MultivariateFunction {
- private double axisratio;
-
- Ackley(double axra) {
- axisratio = axra;
- }
-
- public Ackley() {
- this(1);
- }
-
- public double value(double[] x) {
- double f = 0;
- double res2 = 0;
- double fac = 0;
- for (int i = 0; i < x.length; ++i) {
- fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
- f += fac * fac * x[i] * x[i];
- res2 += FastMath.cos(2. * FastMath.PI * fac * x[i]);
- }
- f = (20. - 20. * FastMath.exp(-0.2 * FastMath.sqrt(f / x.length))
- + FastMath.exp(1.) - FastMath.exp(res2 / x.length));
- return f;
- }
- }
-
- private static class Rastrigin implements MultivariateFunction {
-
- private double axisratio;
- private double amplitude;
-
- Rastrigin() {
- this(1, 10);
- }
-
- Rastrigin(double axisratio, double amplitude) {
- this.axisratio = axisratio;
- this.amplitude = amplitude;
- }
-
- public double value(double[] x) {
- double f = 0;
- double fac;
- for (int i = 0; i < x.length; ++i) {
- fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
- if (i == 0 && x[i] < 0)
- fac *= 1.;
- f += fac * fac * x[i] * x[i] + amplitude
- * (1. - FastMath.cos(2. * FastMath.PI * fac * x[i]));
- }
- return f;
- }
- }
-
- private static class Basis {
- double[][] basis;
- Random rand = new Random(2); // use not always the same basis
-
- double[] Rotate(double[] x) {
- GenBasis(x.length);
- double[] y = new double[x.length];
- for (int i = 0; i < x.length; ++i) {
- y[i] = 0;
- for (int j = 0; j < x.length; ++j)
- y[i] += basis[i][j] * x[j];
- }
- return y;
- }
-
- void GenBasis(int DIM) {
- if (basis != null ? basis.length == DIM : false)
- return;
-
- double sp;
- int i, j, k;
-
- /* generate orthogonal basis */
- basis = new double[DIM][DIM];
- for (i = 0; i < DIM; ++i) {
- /* sample components gaussian */
- for (j = 0; j < DIM; ++j)
- basis[i][j] = rand.nextGaussian();
- /* substract projection of previous vectors */
- for (j = i - 1; j >= 0; --j) {
- for (sp = 0., k = 0; k < DIM; ++k)
- sp += basis[i][k] * basis[j][k]; /* scalar product */
- for (k = 0; k < DIM; ++k)
- basis[i][k] -= sp * basis[j][k]; /* substract */
- }
- /* normalize */
- for (sp = 0., k = 0; k < DIM; ++k)
- sp += basis[i][k] * basis[i][k]; /* squared norm */
- for (k = 0; k < DIM; ++k)
- basis[i][k] /= FastMath.sqrt(sp);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionMappingAdapterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionMappingAdapterTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionMappingAdapterTest.java
deleted file mode 100644
index 76d9139..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionMappingAdapterTest.java
+++ /dev/null
@@ -1,194 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.math4.optimization.direct;
-
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.direct.MultivariateFunctionMappingAdapter;
-import org.apache.commons.math4.optimization.direct.NelderMeadSimplex;
-import org.apache.commons.math4.optimization.direct.SimplexOptimizer;
-import org.junit.Assert;
-import org.junit.Test;
-
-@Deprecated
-public class MultivariateFunctionMappingAdapterTest {
-
- @Test
- public void testStartSimplexInsideRange() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
- final MultivariateFunctionMappingAdapter wrapped =
- new MultivariateFunctionMappingAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper());
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
- wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
- }));
-
- final PointValuePair optimum
- = optimizer.optimize(300, wrapped, GoalType.MINIMIZE,
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }));
- final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
-
- }
-
- @Test
- public void testOptimumOutsideRange() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
- final MultivariateFunctionMappingAdapter wrapped =
- new MultivariateFunctionMappingAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper());
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
- wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
- }));
-
- final PointValuePair optimum
- = optimizer.optimize(100, wrapped, GoalType.MINIMIZE,
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }));
- final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
-
- }
-
- @Test
- public void testUnbounded() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0,
- Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
- Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
- final MultivariateFunctionMappingAdapter wrapped =
- new MultivariateFunctionMappingAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper());
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
- wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
- }));
-
- final PointValuePair optimum
- = optimizer.optimize(300, wrapped, GoalType.MINIMIZE,
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }));
- final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
-
- }
-
- @Test
- public void testHalfBounded() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
- 1.0, Double.POSITIVE_INFINITY,
- Double.NEGATIVE_INFINITY, 3.0);
- final MultivariateFunctionMappingAdapter wrapped =
- new MultivariateFunctionMappingAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper());
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-13, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
- wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
- }));
-
- final PointValuePair optimum
- = optimizer.optimize(200, wrapped, GoalType.MINIMIZE,
- wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }));
- final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 1e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 1e-7);
-
- }
-
- private static class BiQuadratic implements MultivariateFunction {
-
- private final double xOptimum;
- private final double yOptimum;
-
- private final double xMin;
- private final double xMax;
- private final double yMin;
- private final double yMax;
-
- public BiQuadratic(final double xOptimum, final double yOptimum,
- final double xMin, final double xMax,
- final double yMin, final double yMax) {
- this.xOptimum = xOptimum;
- this.yOptimum = yOptimum;
- this.xMin = xMin;
- this.xMax = xMax;
- this.yMin = yMin;
- this.yMax = yMax;
- }
-
- public double value(double[] point) {
-
- // the function should never be called with out of range points
- Assert.assertTrue(point[0] >= xMin);
- Assert.assertTrue(point[0] <= xMax);
- Assert.assertTrue(point[1] >= yMin);
- Assert.assertTrue(point[1] <= yMax);
-
- final double dx = point[0] - xOptimum;
- final double dy = point[1] - yOptimum;
- return dx * dx + dy * dy;
-
- }
-
- public double[] getLower() {
- return new double[] { xMin, yMin };
- }
-
- public double[] getUpper() {
- return new double[] { xMax, yMax };
- }
-
- public double getBoundedXOptimum() {
- return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum);
- }
-
- public double getBoundedYOptimum() {
- return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum);
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionPenaltyAdapterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionPenaltyAdapterTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionPenaltyAdapterTest.java
deleted file mode 100644
index 0080bca..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/MultivariateFunctionPenaltyAdapterTest.java
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.math4.optimization.direct;
-
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.SimplePointChecker;
-import org.apache.commons.math4.optimization.direct.MultivariateFunctionPenaltyAdapter;
-import org.apache.commons.math4.optimization.direct.NelderMeadSimplex;
-import org.apache.commons.math4.optimization.direct.SimplexOptimizer;
-import org.junit.Assert;
-import org.junit.Test;
-
-@Deprecated
-public class MultivariateFunctionPenaltyAdapterTest {
-
- @Test
- public void testStartSimplexInsideRange() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
- final MultivariateFunctionPenaltyAdapter wrapped =
- new MultivariateFunctionPenaltyAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper(),
- 1000.0, new double[] { 100.0, 100.0 });
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));
-
- final PointValuePair optimum
- = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { 1.5, 2.25 });
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
-
- }
-
- @Test
- public void testStartSimplexOutsideRange() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
- final MultivariateFunctionPenaltyAdapter wrapped =
- new MultivariateFunctionPenaltyAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper(),
- 1000.0, new double[] { 100.0, 100.0 });
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));
-
- final PointValuePair optimum
- = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 });
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
-
- }
-
- @Test
- public void testOptimumOutsideRange() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
- final MultivariateFunctionPenaltyAdapter wrapped =
- new MultivariateFunctionPenaltyAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper(),
- 1000.0, new double[] { 100.0, 100.0 });
-
- SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));
-
- final PointValuePair optimum
- = optimizer.optimize(600, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 });
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
-
- }
-
- @Test
- public void testUnbounded() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0,
- Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
- Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
- final MultivariateFunctionPenaltyAdapter wrapped =
- new MultivariateFunctionPenaltyAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper(),
- 1000.0, new double[] { 100.0, 100.0 });
-
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));
-
- final PointValuePair optimum
- = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 });
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
-
- }
-
- @Test
- public void testHalfBounded() {
-
- final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
- 1.0, Double.POSITIVE_INFINITY,
- Double.NEGATIVE_INFINITY, 3.0);
- final MultivariateFunctionPenaltyAdapter wrapped =
- new MultivariateFunctionPenaltyAdapter(biQuadratic,
- biQuadratic.getLower(),
- biQuadratic.getUpper(),
- 1000.0, new double[] { 100.0, 100.0 });
-
- SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 }));
-
- final PointValuePair optimum
- = optimizer.optimize(400, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 });
-
- Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
-
- }
-
- private static class BiQuadratic implements MultivariateFunction {
-
- private final double xOptimum;
- private final double yOptimum;
-
- private final double xMin;
- private final double xMax;
- private final double yMin;
- private final double yMax;
-
- public BiQuadratic(final double xOptimum, final double yOptimum,
- final double xMin, final double xMax,
- final double yMin, final double yMax) {
- this.xOptimum = xOptimum;
- this.yOptimum = yOptimum;
- this.xMin = xMin;
- this.xMax = xMax;
- this.yMin = yMin;
- this.yMax = yMax;
- }
-
- public double value(double[] point) {
-
- // the function should never be called with out of range points
- Assert.assertTrue(point[0] >= xMin);
- Assert.assertTrue(point[0] <= xMax);
- Assert.assertTrue(point[1] >= yMin);
- Assert.assertTrue(point[1] <= yMax);
-
- final double dx = point[0] - xOptimum;
- final double dy = point[1] - yOptimum;
- return dx * dx + dy * dy;
-
- }
-
- public double[] getLower() {
- return new double[] { xMin, yMin };
- }
-
- public double[] getUpper() {
- return new double[] { xMax, yMax };
- }
-
- public double getBoundedXOptimum() {
- return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum);
- }
-
- public double getBoundedYOptimum() {
- return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum);
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/PowellOptimizerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/PowellOptimizerTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/PowellOptimizerTest.java
deleted file mode 100644
index 227277f..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/PowellOptimizerTest.java
+++ /dev/null
@@ -1,239 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.math4.optimization.direct;
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.analysis.SumSincFunction;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.MultivariateOptimizer;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.direct.PowellOptimizer;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Test for {@link PowellOptimizer}.
- */
-@Deprecated
-public class PowellOptimizerTest {
-
- @Test
- public void testSumSinc() {
- final MultivariateFunction func = new SumSincFunction(-1);
-
- int dim = 2;
- final double[] minPoint = new double[dim];
- for (int i = 0; i < dim; i++) {
- minPoint[i] = 0;
- }
-
- double[] init = new double[dim];
-
- // Initial is minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = minPoint[i];
- }
- doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9);
-
- // Initial is far from minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = minPoint[i] + 3;
- }
- doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-5);
- // More stringent line search tolerance enhances the precision
- // of the result.
- doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9, 1e-7);
- }
-
- @Test
- public void testQuadratic() {
- final MultivariateFunction func = new MultivariateFunction() {
- public double value(double[] x) {
- final double a = x[0] - 1;
- final double b = x[1] - 1;
- return a * a + b * b + 1;
- }
- };
-
- int dim = 2;
- final double[] minPoint = new double[dim];
- for (int i = 0; i < dim; i++) {
- minPoint[i] = 1;
- }
-
- double[] init = new double[dim];
-
- // Initial is minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = minPoint[i];
- }
- doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);
-
- // Initial is far from minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = minPoint[i] - 20;
- }
- doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);
- }
-
- @Test
- public void testMaximizeQuadratic() {
- final MultivariateFunction func = new MultivariateFunction() {
- public double value(double[] x) {
- final double a = x[0] - 1;
- final double b = x[1] - 1;
- return -a * a - b * b + 1;
- }
- };
-
- int dim = 2;
- final double[] maxPoint = new double[dim];
- for (int i = 0; i < dim; i++) {
- maxPoint[i] = 1;
- }
-
- double[] init = new double[dim];
-
- // Initial is minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = maxPoint[i];
- }
- doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8);
-
- // Initial is far from minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = maxPoint[i] - 20;
- }
- doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8);
- }
-
- /**
- * Ensure that we do not increase the number of function evaluations when
- * the function values are scaled up.
- * Note that the tolerances parameters passed to the constructor must
- * still hold sensible values because they are used to set the line search
- * tolerances.
- */
- @Test
- public void testRelativeToleranceOnScaledValues() {
- final MultivariateFunction func = new MultivariateFunction() {
- public double value(double[] x) {
- final double a = x[0] - 1;
- final double b = x[1] - 1;
- return a * a * FastMath.sqrt(FastMath.abs(a)) + b * b + 1;
- }
- };
-
- int dim = 2;
- final double[] minPoint = new double[dim];
- for (int i = 0; i < dim; i++) {
- minPoint[i] = 1;
- }
-
- double[] init = new double[dim];
- // Initial is far from minimum.
- for (int i = 0; i < dim; i++) {
- init[i] = minPoint[i] - 20;
- }
-
- final double relTol = 1e-10;
-
- final int maxEval = 1000;
- // Very small absolute tolerance to rely solely on the relative
- // tolerance as a stopping criterion
- final MultivariateOptimizer optim = new PowellOptimizer(relTol, 1e-100);
-
- final PointValuePair funcResult = optim.optimize(maxEval, func, GoalType.MINIMIZE, init);
- final double funcValue = func.value(funcResult.getPoint());
- final int funcEvaluations = optim.getEvaluations();
-
- final double scale = 1e10;
- final MultivariateFunction funcScaled = new MultivariateFunction() {
- public double value(double[] x) {
- return scale * func.value(x);
- }
- };
-
- final PointValuePair funcScaledResult = optim.optimize(maxEval, funcScaled, GoalType.MINIMIZE, init);
- final double funcScaledValue = funcScaled.value(funcScaledResult.getPoint());
- final int funcScaledEvaluations = optim.getEvaluations();
-
- // Check that both minima provide the same objective funciton values,
- // within the relative function tolerance.
- Assert.assertEquals(1, funcScaledValue / (scale * funcValue), relTol);
-
- // Check that the numbers of evaluations are the same.
- Assert.assertEquals(funcEvaluations, funcScaledEvaluations);
- }
-
- /**
- * @param func Function to optimize.
- * @param optimum Expected optimum.
- * @param init Starting point.
- * @param goal Minimization or maximization.
- * @param fTol Tolerance (relative error on the objective function) for
- * "Powell" algorithm.
- * @param pointTol Tolerance for checking that the optimum is correct.
- */
- private void doTest(MultivariateFunction func,
- double[] optimum,
- double[] init,
- GoalType goal,
- double fTol,
- double pointTol) {
- final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));
-
- final PointValuePair result = optim.optimize(1000, func, goal, init);
- final double[] point = result.getPoint();
-
- for (int i = 0, dim = optimum.length; i < dim; i++) {
- Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
- optimum[i], point[i], pointTol);
- }
- }
-
- /**
- * @param func Function to optimize.
- * @param optimum Expected optimum.
- * @param init Starting point.
- * @param goal Minimization or maximization.
- * @param fTol Tolerance (relative error on the objective function) for
- * "Powell" algorithm.
- * @param fLineTol Tolerance (relative error on the objective function)
- * for the internal line search algorithm.
- * @param pointTol Tolerance for checking that the optimum is correct.
- */
- private void doTest(MultivariateFunction func,
- double[] optimum,
- double[] init,
- GoalType goal,
- double fTol,
- double fLineTol,
- double pointTol) {
- final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
- fLineTol, Math.ulp(1d));
-
- final PointValuePair result = optim.optimize(1000, func, goal, init);
- final double[] point = result.getPoint();
-
- for (int i = 0, dim = optimum.length; i < dim; i++) {
- Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
- optimum[i], point[i], pointTol);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerMultiDirectionalTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerMultiDirectionalTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerMultiDirectionalTest.java
deleted file mode 100644
index 2ae7eaf..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerMultiDirectionalTest.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.math4.optimization.direct;
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.SimpleValueChecker;
-import org.apache.commons.math4.optimization.direct.MultiDirectionalSimplex;
-import org.apache.commons.math4.optimization.direct.SimplexOptimizer;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-@Deprecated
-public class SimplexOptimizerMultiDirectionalTest {
- @Test
- public void testMinimize1() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
- optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
- Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6);
- Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
- Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13);
- Assert.assertTrue(optimizer.getEvaluations() > 120);
- Assert.assertTrue(optimizer.getEvaluations() < 150);
- }
-
- @Test
- public void testMinimize2() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
- optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
- Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
- Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
- Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12);
- Assert.assertTrue(optimizer.getEvaluations() > 120);
- Assert.assertTrue(optimizer.getEvaluations() < 150);
- }
-
- @Test
- public void testMaximize1() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
- optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
- Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
- Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
- Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
- Assert.assertTrue(optimizer.getEvaluations() > 120);
- Assert.assertTrue(optimizer.getEvaluations() < 150);
- }
-
- @Test
- public void testMaximize2() {
- SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30));
- optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
- Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
- Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
- Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12);
- Assert.assertTrue(optimizer.getEvaluations() > 180);
- Assert.assertTrue(optimizer.getEvaluations() < 220);
- }
-
- @Test
- public void testRosenbrock() {
- MultivariateFunction rosenbrock =
- new MultivariateFunction() {
- public double value(double[] x) {
- ++count;
- double a = x[1] - x[0] * x[0];
- double b = 1.0 - x[0];
- return 100 * a * a + b * b;
- }
- };
-
- count = 0;
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
- optimizer.setSimplex(new MultiDirectionalSimplex(new double[][] {
- { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
- }));
- PointValuePair optimum =
- optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 });
-
- Assert.assertEquals(count, optimizer.getEvaluations());
- Assert.assertTrue(optimizer.getEvaluations() > 50);
- Assert.assertTrue(optimizer.getEvaluations() < 100);
- Assert.assertTrue(optimum.getValue() > 1e-2);
- }
-
- @Test
- public void testPowell() {
- MultivariateFunction powell =
- new MultivariateFunction() {
- public double value(double[] x) {
- ++count;
- double a = x[0] + 10 * x[1];
- double b = x[2] - x[3];
- double c = x[1] - 2 * x[2];
- double d = x[0] - x[3];
- return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
- }
- };
-
- count = 0;
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
- optimizer.setSimplex(new MultiDirectionalSimplex(4));
- PointValuePair optimum =
- optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
- Assert.assertEquals(count, optimizer.getEvaluations());
- Assert.assertTrue(optimizer.getEvaluations() > 800);
- Assert.assertTrue(optimizer.getEvaluations() < 900);
- Assert.assertTrue(optimum.getValue() > 1e-2);
- }
-
- @Test
- public void testMath283() {
- // fails because MultiDirectional.iterateSimplex is looping forever
- // the while(true) should be replaced with a convergence check
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);
- optimizer.setSimplex(new MultiDirectionalSimplex(2));
- final Gaussian2D function = new Gaussian2D(0, 0, 1);
- PointValuePair estimate = optimizer.optimize(1000, function,
- GoalType.MAXIMIZE, function.getMaximumPosition());
- final double EPSILON = 1e-5;
- final double expectedMaximum = function.getMaximum();
- final double actualMaximum = estimate.getValue();
- Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);
-
- final double[] expectedPosition = function.getMaximumPosition();
- final double[] actualPosition = estimate.getPoint();
- Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
- Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
- }
-
- private static class FourExtrema implements MultivariateFunction {
- // The following function has 4 local extrema.
- final double xM = -3.841947088256863675365;
- final double yM = -1.391745200270734924416;
- final double xP = 0.2286682237349059125691;
- final double yP = -yM;
- final double valueXmYm = 0.2373295333134216789769; // Local maximum.
- final double valueXmYp = -valueXmYm; // Local minimum.
- final double valueXpYm = -0.7290400707055187115322; // Global minimum.
- final double valueXpYp = -valueXpYm; // Global maximum.
-
- public double value(double[] variables) {
- final double x = variables[0];
- final double y = variables[1];
- return (x == 0 || y == 0) ? 0 :
- FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y);
- }
- }
-
- private static class Gaussian2D implements MultivariateFunction {
- private final double[] maximumPosition;
- private final double std;
-
- public Gaussian2D(double xOpt, double yOpt, double std) {
- maximumPosition = new double[] { xOpt, yOpt };
- this.std = std;
- }
-
- public double getMaximum() {
- return value(maximumPosition);
- }
-
- public double[] getMaximumPosition() {
- return maximumPosition.clone();
- }
-
- public double value(double[] point) {
- final double x = point[0], y = point[1];
- final double twoS2 = 2.0 * std * std;
- return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2);
- }
- }
-
- private int count;
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerNelderMeadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerNelderMeadTest.java b/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerNelderMeadTest.java
deleted file mode 100644
index 80a8476..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/direct/SimplexOptimizerNelderMeadTest.java
+++ /dev/null
@@ -1,268 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.math4.optimization.direct;
-
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.analysis.MultivariateVectorFunction;
-import org.apache.commons.math4.exception.TooManyEvaluationsException;
-import org.apache.commons.math4.linear.Array2DRowRealMatrix;
-import org.apache.commons.math4.linear.RealMatrix;
-import org.apache.commons.math4.optimization.GoalType;
-import org.apache.commons.math4.optimization.LeastSquaresConverter;
-import org.apache.commons.math4.optimization.PointValuePair;
-import org.apache.commons.math4.optimization.direct.NelderMeadSimplex;
-import org.apache.commons.math4.optimization.direct.SimplexOptimizer;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-@Deprecated
-public class SimplexOptimizerNelderMeadTest {
- @Test
- public void testMinimize1() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(100, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
- Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 2e-7);
- Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 2e-5);
- Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 6e-12);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 90);
- }
-
- @Test
- public void testMinimize2() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(100, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
- Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 5e-6);
- Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 6e-6);
- Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 1e-11);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 90);
- }
-
- @Test
- public void testMaximize1() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(100, fourExtrema, GoalType.MAXIMIZE, new double[] { -3, 0 });
- Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 1e-5);
- Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
- Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 3e-12);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 90);
- }
-
- @Test
- public void testMaximize2() {
- SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
- optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
- final FourExtrema fourExtrema = new FourExtrema();
-
- final PointValuePair optimum
- = optimizer.optimize(100, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
- Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 4e-6);
- Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 5e-6);
- Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 7e-12);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 90);
- }
-
- @Test
- public void testRosenbrock() {
-
- Rosenbrock rosenbrock = new Rosenbrock();
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
- optimizer.setSimplex(new NelderMeadSimplex(new double[][] {
- { -1.2, 1 }, { 0.9, 1.2 } , { 3.5, -2.3 }
- }));
- PointValuePair optimum =
- optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 });
-
- Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
- Assert.assertTrue(optimizer.getEvaluations() > 40);
- Assert.assertTrue(optimizer.getEvaluations() < 50);
- Assert.assertTrue(optimum.getValue() < 8e-4);
- }
-
- @Test
- public void testPowell() {
-
- Powell powell = new Powell();
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
- optimizer.setSimplex(new NelderMeadSimplex(4));
- PointValuePair optimum =
- optimizer.optimize(200, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
- Assert.assertEquals(powell.getCount(), optimizer.getEvaluations());
- Assert.assertTrue(optimizer.getEvaluations() > 110);
- Assert.assertTrue(optimizer.getEvaluations() < 130);
- Assert.assertTrue(optimum.getValue() < 2e-3);
- }
-
- @Test
- public void testLeastSquares1() {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1, 0 },
- { 0, 1 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2.0, -3.0 });
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
- optimizer.setSimplex(new NelderMeadSimplex(2));
- PointValuePair optimum =
- optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
- Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
- Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 80);
- Assert.assertTrue(optimum.getValue() < 1.0e-6);
- }
-
- @Test
- public void testLeastSquares2() {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1, 0 },
- { 0, 1 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
- optimizer.setSimplex(new NelderMeadSimplex(2));
- PointValuePair optimum =
- optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
- Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
- Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 80);
- Assert.assertTrue(optimum.getValue() < 1e-6);
- }
-
- @Test
- public void testLeastSquares3() {
-
- final RealMatrix factors =
- new Array2DRowRealMatrix(new double[][] {
- { 1, 0 },
- { 0, 1 }
- }, false);
- LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
- public double[] value(double[] variables) {
- return factors.operate(variables);
- }
- }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
- { 1, 1.2 }, { 1.2, 2 }
- }));
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
- optimizer.setSimplex(new NelderMeadSimplex(2));
- PointValuePair optimum =
- optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
- Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
- Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
- Assert.assertTrue(optimizer.getEvaluations() > 60);
- Assert.assertTrue(optimizer.getEvaluations() < 80);
- Assert.assertTrue(optimum.getValue() < 1e-6);
- }
-
- @Test(expected = TooManyEvaluationsException.class)
- public void testMaxIterations() {
- Powell powell = new Powell();
- SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
- optimizer.setSimplex(new NelderMeadSimplex(4));
- optimizer.optimize(20, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
- }
-
- private static class FourExtrema implements MultivariateFunction {
- // The following function has 4 local extrema.
- final double xM = -3.841947088256863675365;
- final double yM = -1.391745200270734924416;
- final double xP = 0.2286682237349059125691;
- final double yP = -yM;
- final double valueXmYm = 0.2373295333134216789769; // Local maximum.
- final double valueXmYp = -valueXmYm; // Local minimum.
- final double valueXpYm = -0.7290400707055187115322; // Global minimum.
- final double valueXpYp = -valueXpYm; // Global maximum.
-
- public double value(double[] variables) {
- final double x = variables[0];
- final double y = variables[1];
- return (x == 0 || y == 0) ? 0 :
- FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y);
- }
- }
-
- private static class Rosenbrock implements MultivariateFunction {
- private int count;
-
- public Rosenbrock() {
- count = 0;
- }
-
- public double value(double[] x) {
- ++count;
- double a = x[1] - x[0] * x[0];
- double b = 1.0 - x[0];
- return 100 * a * a + b * b;
- }
-
- public int getCount() {
- return count;
- }
- }
-
- private static class Powell implements MultivariateFunction {
- private int count;
-
- public Powell() {
- count = 0;
- }
-
- public double value(double[] x) {
- ++count;
- double a = x[0] + 10 * x[1];
- double b = x[2] - x[3];
- double c = x[1] - 2 * x[2];
- double d = x[0] - x[3];
- return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
- }
-
- public int getCount() {
- return count;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/b4669aad/src/test/java/org/apache/commons/math4/optimization/fitting/CurveFitterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optimization/fitting/CurveFitterTest.java b/src/test/java/org/apache/commons/math4/optimization/fitting/CurveFitterTest.java
deleted file mode 100644
index 3857fc7..0000000
--- a/src/test/java/org/apache/commons/math4/optimization/fitting/CurveFitterTest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-package org.apache.commons.math4.optimization.fitting;
-
-import org.apache.commons.math4.analysis.ParametricUnivariateFunction;
-import org.apache.commons.math4.optimization.fitting.CurveFitter;
-import org.apache.commons.math4.optimization.general.LevenbergMarquardtOptimizer;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-@Deprecated
-public class CurveFitterTest {
-
- @Test
- public void testMath303() {
-
- LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
- CurveFitter<ParametricUnivariateFunction> fitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
- fitter.addObservedPoint(2.805d, 0.6934785852953367d);
- fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d);
- fitter.addObservedPoint(1.655d, 0.9474675497289684);
- fitter.addObservedPoint(1.725d, 0.9013594835804194d);
-
- ParametricUnivariateFunction sif = new SimpleInverseFunction();
-
- double[] initialguess1 = new double[1];
- initialguess1[0] = 1.0d;
- Assert.assertEquals(1, fitter.fit(sif, initialguess1).length);
-
- double[] initialguess2 = new double[2];
- initialguess2[0] = 1.0d;
- initialguess2[1] = .5d;
- Assert.assertEquals(2, fitter.fit(sif, initialguess2).length);
-
- }
-
- @Test
- public void testMath304() {
-
- LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
- CurveFitter<ParametricUnivariateFunction> fitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
- fitter.addObservedPoint(2.805d, 0.6934785852953367d);
- fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d);
- fitter.addObservedPoint(1.655d, 0.9474675497289684);
- fitter.addObservedPoint(1.725d, 0.9013594835804194d);
-
- ParametricUnivariateFunction sif = new SimpleInverseFunction();
-
- double[] initialguess1 = new double[1];
- initialguess1[0] = 1.0d;
- Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14);
-
- double[] initialguess2 = new double[1];
- initialguess2[0] = 10.0d;
- Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14);
-
- }
-
- @Test
- public void testMath372() {
- LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
- CurveFitter<ParametricUnivariateFunction> curveFitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
-
- curveFitter.addObservedPoint( 15, 4443);
- curveFitter.addObservedPoint( 31, 8493);
- curveFitter.addObservedPoint( 62, 17586);
- curveFitter.addObservedPoint(125, 30582);
- curveFitter.addObservedPoint(250, 45087);
- curveFitter.addObservedPoint(500, 50683);
-
- ParametricUnivariateFunction f = new ParametricUnivariateFunction() {
-
- public double value(double x, double ... parameters) {
-
- double a = parameters[0];
- double b = parameters[1];
- double c = parameters[2];
- double d = parameters[3];
-
- return d + ((a - d) / (1 + FastMath.pow(x / c, b)));
- }
-
- public double[] gradient(double x, double ... parameters) {
-
- double a = parameters[0];
- double b = parameters[1];
- double c = parameters[2];
- double d = parameters[3];
-
- double[] gradients = new double[4];
- double den = 1 + FastMath.pow(x / c, b);
-
- // derivative with respect to a
- gradients[0] = 1 / den;
-
- // derivative with respect to b
- // in the reported (invalid) issue, there was a sign error here
- gradients[1] = -((a - d) * FastMath.pow(x / c, b) * FastMath.log(x / c)) / (den * den);
-
- // derivative with respect to c
- gradients[2] = (b * FastMath.pow(x / c, b - 1) * (x / (c * c)) * (a - d)) / (den * den);
-
- // derivative with respect to d
- gradients[3] = 1 - (1 / den);
-
- return gradients;
-
- }
- };
-
- double[] initialGuess = new double[] { 1500, 0.95, 65, 35000 };
- double[] estimatedParameters = curveFitter.fit(f, initialGuess);
-
- Assert.assertEquals( 2411.00, estimatedParameters[0], 500.00);
- Assert.assertEquals( 1.62, estimatedParameters[1], 0.04);
- Assert.assertEquals( 111.22, estimatedParameters[2], 0.30);
- Assert.assertEquals(55347.47, estimatedParameters[3], 300.00);
- Assert.assertTrue(optimizer.getRMS() < 600.0);
-
- }
-
- private static class SimpleInverseFunction implements ParametricUnivariateFunction {
-
- public double value(double x, double ... parameters) {
- return parameters[0] / x + (parameters.length < 2 ? 0 : parameters[1]);
- }
-
- public double[] gradient(double x, double ... doubles) {
- double[] gradientVector = new double[doubles.length];
- gradientVector[0] = 1 / x;
- if (doubles.length >= 2) {
- gradientVector[1] = 1;
- }
- return gradientVector;
- }
- }
-
-}