Standard test functions defined in a dedicated factory ("TestFunction" enum).

This commit is contained in:
Gilles Sadowski 2021-08-01 04:39:30 +02:00
parent a791c576d5
commit b167967767
6 changed files with 416 additions and 716 deletions

View File

@ -0,0 +1,209 @@
/*
* 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.legacy.optim.nonlinear.scalar;
import java.util.function.Function;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath;
/**
* Multivariate scalar functions for testing an optimizer.
*/
public enum TestFunction {
SPHERE(dim -> {
return x -> {
double f = 0;
for (int i = 0; i < dim; i++) {
f += x[i] * x[i];
}
return f;
};
}),
CIGAR(dim -> {
return x -> {
double f = x[0] * x[0];
for (int i = 1; i < dim; i++) {
f += 1e3 * x[i] * x[i];
}
return f;
};
}),
TABLET(dim -> {
return x -> {
double f = 1e3 * x[0] * x[0];
for (int i = 1; i < dim; i++) {
f += x[i] * x[i];
}
return f;
};
}),
CIG_TAB(dim -> {
final double factor = 1e4;
final int last = dim - 1;
return x -> {
double f = x[0] * x[0] / factor + factor * x[last] * x[last];
for (int i = 1; i < last; i++) {
f += x[i] * x[i];
}
return f;
};
}),
TWO_AXES(dim -> {
return x -> {
double f = 0;
for (int i = 0; i < dim; i++) {
f += (i < dim / 2 ? 1e6 : 1) * x[i] * x[i];
}
return f;
};
}),
ELLI(dim -> {
final double last = dim - 1;
return x -> {
double f = 0;
for (int i = 0; i < dim; i++) {
f += Math.pow(1e3, i / last) * x[i] * x[i];
}
return f;
};
}),
MINUS_ELLI(dim -> {
final MultivariateFunction elli = ELLI.withDimension(dim);
return x -> {
return 1 - elli.value(x);
};
}),
DIFF_POW(dim -> {
final double A = 10d / (dim - 1);
return x -> {
double f = 0;
for (int i = 0; i < dim; i++) {
f += AccurateMath.pow(Math.abs(x[i]), A * i + 2);
}
return f;
};
}),
SS_DIFF_POW(dim -> {
final MultivariateFunction diffPow = DIFF_POW.withDimension(dim);
return x -> {
double f = Math.pow(diffPow.value(x), 0.25);
return f;
};
}),
ROSEN(dim -> {
final int last = dim - 1;
return x -> {
double f = 0;
for (int i = 0; i < last; i++) {
final double xi = x[i];
final double a = xi * xi - x[i + 1];
final double b = xi - 1;
f += 1e2 * a * a + b * b;
}
return f;
};
}),
ACKLEY(dim -> {
final double A = 20;
final double B = 0.2;
final double C = 2 * Math.PI;
return x -> {
double acc1 = 0;
double acc2 = 0;
for (int i = 0; i < dim; i++) {
final double v = x[i];
acc1 += v * v;
acc2 += Math.cos(C * v);
}
acc1 = -B * Math.sqrt(acc1 / dim);
acc2 /= dim;
return -A * Math.exp(acc1) - Math.exp(acc2) + A + Math.E;
};
}),
// https://www.sfu.ca/~ssurjano/rastr.html
RASTRIGIN(dim -> {
final double A = 10;
return x -> {
double sum = 0;
for (int i = 0; i < dim; i++) {
final double xi = x[i];
sum += xi * xi - A * Math.cos(2 * Math.PI * xi);
}
return A * dim + sum;
};
}),
// https://www.sfu.ca/~ssurjano/powell.html
POWELL(dim -> {
final int last = dim / 4;
return x -> {
double f = 0;
for (int i = 0; i < last; i++) {
final int fourI = 4 * i;
final double x4i = x[fourI];
final double x4iP1 = x[fourI + 1];
final double x4iP2 = x[fourI + 2];
final double x4iP3 = x[fourI + 3];
final double a = x4i + 10 * x4iP1;
final double b = x4iP2 - x4iP3;
final double c = x4iP1 - 2 * x4iP2;
final double d = x4i - x4iP3;
f += a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
}
return f;
};
}),
ROSENBROCK(dim -> {
final int last = dim - 1;
return x -> {
double f = 0;
for (int i = 0; i < last; i++) {
final double xi = x[i];
final double xiP1 = x[i + 1];
final double a = xiP1 - xi * xi;
final double b = xi - 1;
f += 100 * a * a + b * b;
}
return f;
};
});
/** Template for variable dimension. */
private final Function<Integer, MultivariateFunction> generator;
/**
* @param gen Template for variable dimension.
*/
TestFunction(Function<Integer, MultivariateFunction> gen) {
generator = gen;
}
/**
* @param dim Dimension.
* @return the function for the given dimension.
*/
public MultivariateFunction withDimension(final int dim) {
final MultivariateFunction f = generator.apply(dim);
return x -> {
if (x.length != dim) {
throw new IllegalArgumentException("Dimension mismatch: " + x.length +
"(expected: " + dim + ")");
}
return f.value(x);
};
}
}

View File

@ -27,6 +27,7 @@ import org.apache.commons.math4.legacy.optim.PointValuePair;
import org.apache.commons.math4.legacy.optim.SimpleBounds;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@ -41,7 +42,7 @@ public class BOBYQAOptimizerTest {
public void testInitOutOfBounds() {
double[] startPoint = OptimTestUtils.point(DIM, 3);
double[][] boundaries = boundaries(DIM, -1, 2);
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, null);
}
@ -50,7 +51,7 @@ public class BOBYQAOptimizerTest {
public void testBoundariesDimensionMismatch() {
double[] startPoint = OptimTestUtils.point(DIM, 0.5);
double[][] boundaries = boundaries(DIM + 1, -1, 2);
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, null);
}
@ -58,7 +59,7 @@ public class BOBYQAOptimizerTest {
@Test(expected=NumberIsTooSmallException.class)
public void testProblemDimensionTooSmall() {
double[] startPoint = OptimTestUtils.point(1, 0.5);
doTest(new OptimTestUtils.Rosen(), startPoint, null,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, null,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, null);
}
@ -68,7 +69,7 @@ public class BOBYQAOptimizerTest {
final int lowMaxEval = 2;
double[] startPoint = OptimTestUtils.point(DIM, 0.1);
double[][] boundaries = null;
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, lowMaxEval, null);
}
@ -78,7 +79,7 @@ public class BOBYQAOptimizerTest {
double[] startPoint = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected = new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, expected);
}
@ -88,12 +89,12 @@ public class BOBYQAOptimizerTest {
double[] startPoint = OptimTestUtils.point(DIM,1.0);
double[][] boundaries = null;
PointValuePair expected = new PointValuePair(OptimTestUtils.point(DIM,0.0),1.0);
doTest(new OptimTestUtils.MinusElli(), startPoint, boundaries,
doTest(TestFunction.MINUS_ELLI.withDimension(DIM), startPoint, boundaries,
GoalType.MAXIMIZE,
2e-10, 5e-6, 1000, expected);
boundaries = boundaries(DIM,-0.3,0.3);
startPoint = OptimTestUtils.point(DIM,0.1);
doTest(new OptimTestUtils.MinusElli(), startPoint, boundaries,
doTest(TestFunction.MINUS_ELLI.withDimension(DIM), startPoint, boundaries,
GoalType.MAXIMIZE,
2e-10, 5e-6, 1000, expected);
}
@ -104,7 +105,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Elli(), startPoint, boundaries,
doTest(TestFunction.ELLI.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 1000, expected);
}
@ -126,7 +127,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Cigar(), startPoint, boundaries,
doTest(TestFunction.CIGAR.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@ -137,9 +138,9 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.TwoAxes(), startPoint, boundaries,
GoalType.MINIMIZE, 2*
1e-13, 1e-6, 100, expected);
doTest(TestFunction.TWO_AXES.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
2e-13, 1e-6, 100, expected);
}
@Test
@ -148,7 +149,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.CigTab(), startPoint, boundaries,
doTest(TestFunction.CIG_TAB.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 5e-5, 100, expected);
}
@ -159,7 +160,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Sphere(), startPoint, boundaries,
doTest(TestFunction.CIG_TAB.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@ -170,29 +171,31 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Tablet(), startPoint, boundaries,
doTest(TestFunction.TABLET.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@Test
public void testDiffPow() {
double[] startPoint = OptimTestUtils.point(DIM/2,1.0);
final int dim = DIM / 2;
double[] startPoint = OptimTestUtils.point(dim, 1.0);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM/2,0.0),0.0);
doTest(new OptimTestUtils.DiffPow(), startPoint, boundaries,
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0);
doTest(TestFunction.DIFF_POW.withDimension(dim), startPoint, boundaries,
GoalType.MINIMIZE,
1e-8, 1e-1, 21000, expected);
}
@Test
public void testSsDiffPow() {
double[] startPoint = OptimTestUtils.point(DIM/2,1.0);
final int dim = DIM / 2;
double[] startPoint = OptimTestUtils.point(dim, 1.0);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM/2,0.0),0.0);
doTest(new OptimTestUtils.SsDiffPow(), startPoint, boundaries,
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0);
doTest(TestFunction.SS_DIFF_POW.withDimension(dim), startPoint, boundaries,
GoalType.MINIMIZE,
1e-2, 1.3e-1, 50000, expected);
}
@ -203,7 +206,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Ackley(), startPoint, boundaries,
doTest(TestFunction.ACKLEY.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-7, 1e-5, 1000, expected);
}
@ -215,7 +218,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.Rastrigin(), startPoint, boundaries,
doTest(TestFunction.RASTRIGIN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 1000, expected);
}
@ -227,7 +230,7 @@ public class BOBYQAOptimizerTest {
double[][] boundaries = boundaries(DIM,-1,2);
PointValuePair expected =
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, expected);
}
@ -249,7 +252,7 @@ public class BOBYQAOptimizerTest {
final int maxAdditionalPoints = 47;
for (int num = 1; num <= maxAdditionalPoints; num++) {
doTest(new OptimTestUtils.Rosen(), startPoint, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, boundaries,
GoalType.MINIMIZE,
1e-12, 1e-6, 2000,
num,

View File

@ -16,9 +16,6 @@
*/
package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
import java.util.Arrays;
import java.util.Random;
import org.apache.commons.math4.legacy.Retry;
import org.apache.commons.math4.legacy.RetryRunner;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
@ -33,6 +30,7 @@ import org.apache.commons.math4.legacy.optim.PointValuePair;
import org.apache.commons.math4.legacy.optim.SimpleBounds;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath;
import org.junit.Assert;
@ -50,71 +48,71 @@ public class CMAESOptimizerTest {
@Test(expected = NumberIsTooLargeException.class)
public void testInitOutofbounds1() {
double[] startPoint = point(DIM,3);
double[] insigma = point(DIM, 0.3);
double[] startPoint = OptimTestUtils.point(DIM,3);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM, -2);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.5);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.5);
double[] insigma = OptimTestUtils.point(DIM,-0.5);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,1.0),0.0);
doTest(new Rosen(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.5);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.5);
double[] insigma = OptimTestUtils.point(DIM + 1, 0.5);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,1.0),0.0);
doTest(new Rosen(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
}
@ -122,15 +120,15 @@ public class CMAESOptimizerTest {
@Test
@Retry(3)
public void testRosen() {
double[] startPoint = point(DIM,0.1);
double[] insigma = point(DIM,0.1);
double[] startPoint = OptimTestUtils.point(DIM,0.1);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,1.0),0.0);
doTest(new Rosen(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new Rosen(), startPoint, insigma, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
}
@ -138,20 +136,20 @@ public class CMAESOptimizerTest {
@Test
@Retry(3)
public void testMaximize() {
double[] startPoint = point(DIM,1.0);
double[] insigma = point(DIM,0.1);
double[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),1.0);
doTest(new MinusElli(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),1.0);
doTest(TestFunction.MINUS_ELLI.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13,
2e-10, 5e-6, 100000, expected);
doTest(new MinusElli(), startPoint, insigma, boundaries,
doTest(TestFunction.MINUS_ELLI.withDimension(DIM), 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,
startPoint = OptimTestUtils.point(DIM,0.1);
doTest(TestFunction.MINUS_ELLI.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13,
2e-10, 5e-6, 100000, expected);
}
@ -182,207 +180,207 @@ public class CMAESOptimizerTest {
@Test
public void testEllipse() {
double[] startPoint = point(DIM,1.0);
double[] insigma = point(DIM,0.1);
double[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Elli(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.ELLI.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new Elli(), startPoint, insigma, boundaries,
doTest(TestFunction.ELLI.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new ElliRotated(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(new OptimTestUtils.ElliRotated(), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new ElliRotated(), startPoint, insigma, boundaries,
doTest(new OptimTestUtils.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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Cigar(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.CIGAR.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new Cigar(), startPoint, insigma, boundaries,
doTest(TestFunction.CIGAR.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.CIGAR.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new Cigar(), startPoint, insigma, boundaries,
doTest(TestFunction.CIGAR.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new TwoAxes(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.TWO_AXES.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new TwoAxes(), startPoint, insigma, boundaries,
doTest(TestFunction.TWO_AXES.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.3);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new CigTab(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.CIG_TAB.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 5e-5, 100000, expected);
doTest(new CigTab(), startPoint, insigma, boundaries,
doTest(TestFunction.CIG_TAB.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Sphere(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.SPHERE.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new Sphere(), startPoint, insigma, boundaries,
doTest(TestFunction.SPHERE.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Tablet(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.TABLET.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new Tablet(), startPoint, insigma, boundaries,
doTest(TestFunction.TABLET.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new DiffPow(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.DIFF_POW.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, 10, true, 0, 1e-13,
1e-8, 1e-1, 100000, expected);
doTest(new DiffPow(), startPoint, insigma, boundaries,
doTest(TestFunction.DIFF_POW.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new SsDiffPow(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.SS_DIFF_POW.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, 10, true, 0, 1e-13,
1e-4, 1e-1, 200000, expected);
doTest(new SsDiffPow(), startPoint, insigma, boundaries,
doTest(TestFunction.SS_DIFF_POW.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,1.0);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Ackley(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.ACKLEY.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
1e-9, 1e-5, 100000, expected);
doTest(new Ackley(), startPoint, insigma, boundaries,
doTest(TestFunction.ACKLEY.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.1);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,0.0),0.0);
doTest(TestFunction.RASTRIGIN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*AccurateMath.sqrt(DIM)), true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
doTest(TestFunction.RASTRIGIN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*AccurateMath.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[] startPoint = OptimTestUtils.point(DIM, 0.1);
double[] insigma = OptimTestUtils.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,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13,
1e-13, 1e-6, 100000, expected);
doTest(new Rosen(), startPoint, insigma, boundaries,
doTest(TestFunction.ROSEN.withDimension(DIM), 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[] startPoint = OptimTestUtils.point(DIM,0.1);
double[] insigma = OptimTestUtils.point(DIM,0.1);
double[][] boundaries = null;
PointValuePair expected =
new PointValuePair(point(DIM,1.0),0.0);
doTest(new Rosen(), startPoint, insigma, boundaries,
new PointValuePair(OptimTestUtils.point(DIM,1.0),0.0);
doTest(TestFunction.ROSEN.withDimension(DIM), startPoint, insigma, boundaries,
GoalType.MINIMIZE, LAMBDA, false, 1, 1e-13,
1e-10, 1e-4, 1000000, expected);
}
@ -529,7 +527,6 @@ public class CMAESOptimizerTest {
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);
@ -538,12 +535,6 @@ public class CMAESOptimizerTest {
Assert.assertTrue(optim.getIterations() > 0);
}
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];
@ -555,300 +546,4 @@ public class CMAESOptimizerTest {
}
return boundaries;
}
private static class Sphere implements MultivariateFunction {
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
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;
}
@Override
public double value(double[] x) {
double f = 0;
x = B.Rotate(x);
for (int i = 0; i < x.length; ++i) {
f += AccurateMath.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;
}
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i) {
f += AccurateMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
}
return f;
}
}
private static class MinusElli implements MultivariateFunction {
@Override
public double value(double[] x) {
return 1.0-(new Elli().value(x));
}
}
private static class DiffPow implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i) {
f += AccurateMath.pow(AccurateMath.abs(x[i]), 2. + 10 * (double) i
/ (x.length - 1.));
}
return f;
}
}
private static class SsDiffPow implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = AccurateMath.pow(new DiffPow().value(x), 0.25);
return f;
}
}
private static class Rosen implements MultivariateFunction {
@Override
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;
}
Ackley() {
this(1);
}
@Override
public double value(double[] x) {
double f = 0;
double res2 = 0;
double fac = 0;
for (int i = 0; i < x.length; ++i) {
fac = AccurateMath.pow(axisratio, (i - 1.) / (x.length - 1.));
f += fac * fac * x[i] * x[i];
res2 += AccurateMath.cos(2. * AccurateMath.PI * fac * x[i]);
}
f = 20. - 20. * AccurateMath.exp(-0.2 * AccurateMath.sqrt(f / x.length))
+ AccurateMath.exp(1.) - AccurateMath.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;
}
@Override
public double value(double[] x) {
double f = 0;
double fac;
for (int i = 0; i < x.length; ++i) {
fac = AccurateMath.pow(axisratio, (i - 1.) / (x.length - 1.));
if (i == 0 && x[i] < 0) {
fac *= 1.;
}
f += fac * fac * x[i] * x[i] + amplitude
* (1. - AccurateMath.cos(2. * AccurateMath.PI * fac * x[i]));
}
return f;
}
}
private static class Basis {
private double[][] basis;
private 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;
int j;
int 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] /= AccurateMath.sqrt(sp);
}
}
}
}
}

View File

@ -32,103 +32,6 @@ final class OptimTestUtils {
/** No instances. */
private OptimTestUtils() {}
static class Sphere implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i) {
f += x[i] * x[i];
}
return f;
}
}
static class Cigar implements MultivariateFunction {
private double factor;
Cigar() {
this(1e3);
}
Cigar(double axisratio) {
factor = axisratio * axisratio;
}
@Override
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;
}
}
static class Tablet implements MultivariateFunction {
private double factor;
Tablet() {
this(1e3);
}
Tablet(double axisratio) {
factor = axisratio * axisratio;
}
@Override
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;
}
}
static class CigTab implements MultivariateFunction {
private double factor;
CigTab() {
this(1e4);
}
CigTab(double axisratio) {
factor = axisratio;
}
@Override
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;
}
}
static class TwoAxes implements MultivariateFunction {
private double factor;
TwoAxes() {
this(1e6);
}
TwoAxes(double axisratio) {
factor = axisratio * axisratio;
}
@Override
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;
}
}
static class ElliRotated implements MultivariateFunction {
private Basis B = new Basis();
@ -153,105 +56,6 @@ final class OptimTestUtils {
}
}
static class Elli implements MultivariateFunction {
private double factor;
Elli() {
this(1e3);
}
Elli(double axisratio) {
factor = axisratio * axisratio;
}
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i) {
f += AccurateMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
}
return f;
}
}
static class MinusElli implements MultivariateFunction {
private final Elli elli = new Elli();
@Override
public double value(double[] x) {
return 1.0 - elli.value(x);
}
}
static class DiffPow implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i) {
f += AccurateMath.pow(AccurateMath.abs(x[i]), 2. + 10 * (double) i
/ (x.length - 1.));
}
return f;
}
}
static class SsDiffPow implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = AccurateMath.pow(new DiffPow().value(x), 0.25);
return f;
}
}
static class Rosen implements MultivariateFunction {
@Override
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length - 1; i++) {
final double a = x[i] * x[i] - x[i + 1];
final double b = x[i] - 1;
f += 1e2 * a * a + b * b;
}
return f;
}
}
static class Ackley implements MultivariateFunction {
private static final double A = 20;
private static final double B = 0.2;
private static final double C = 2 * Math.PI;
@Override
public double value(double[] x) {
final int dim = x.length;
double acc1 = 0;
double acc2 = 0;
for (int i = 0; i < dim; i++) {
final double v = x[i];
acc1 += v * v;
acc2 += Math.cos(C * v);
}
acc1 = -B * Math.sqrt(acc1 / dim);
acc2 /= dim;
return -A * Math.exp(acc1) - Math.exp(acc2) + A + Math.E;
}
}
// https://www.sfu.ca/~ssurjano/rastr.html
static class Rastrigin implements MultivariateFunction {
private static final double A = 10;
@Override
public double value(double[] x) {
double sum = 0;
for (int i = 0; i < x.length; i++) {
final double xi = x[i];
sum += xi * xi - A * AccurateMath.cos(2 * Math.PI * xi);
}
return A * x.length + sum;
}
}
static class FourExtrema implements MultivariateFunction {
// The following function has 4 local extrema.
static final double xM = -3.841947088256863675365;
@ -272,52 +76,6 @@ final class OptimTestUtils {
}
}
static class Rosenbrock implements MultivariateFunction {
@Override
public double value(double[] x) {
double a = x[1] - x[0] * x[0];
double b = 1.0 - x[0];
return 100 * a * a + b * b;
}
}
static class Powell implements MultivariateFunction {
@Override
public double value(double[] x) {
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;
}
}
static class Gaussian2D implements MultivariateFunction {
private final double[] maximumPosition;
private final double std;
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();
}
@Override
public double value(double[] point) {
final double x = point[0];
final double y = point[1];
final double twoS2 = 2.0 * std * std;
return 1.0 / (twoS2 * AccurateMath.PI) * AccurateMath.exp(-(x * x + y * y) / twoS2);
}
}
static double[] point(int n, double value) {
final double[] ds = new double[n];
Arrays.fill(ds, value);

View File

@ -26,7 +26,9 @@ import org.apache.commons.math4.legacy.optim.PointValuePair;
import org.apache.commons.math4.legacy.optim.SimpleBounds;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.apache.commons.math4.legacy.core.MathArrays;
import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;
@ -52,7 +54,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testMath283() {
SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);
final OptimTestUtils.Gaussian2D function = new OptimTestUtils.Gaussian2D(0, 0, 1);
final Gaussian2D function = new Gaussian2D(0, 0, 1);
PointValuePair estimate = optimizer.optimize(new MaxEval(1000),
new ObjectiveFunction(function),
GoalType.MAXIMIZE,
@ -117,27 +119,29 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testRosenbrock() {
doTest(new OptimTestUtils.Rosenbrock(),
final int dim = 2;
doTest(TestFunction.ROSENBROCK.withDimension(dim),
OptimTestUtils.point(new double[] { -1.2, 1 }, 1e-1),
GoalType.MINIMIZE,
190,
new PointValuePair(OptimTestUtils.point(2, 1.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0),
1e-4);
}
@Test
public void testPowell() {
doTest(new OptimTestUtils.Powell(),
final int dim =4;
doTest(TestFunction.POWELL.withDimension(dim),
OptimTestUtils.point(new double[] { 3, -1, 0, 1 }, 1e-1),
GoalType.MINIMIZE,
420,
new PointValuePair(OptimTestUtils.point(4, 0.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0),
2e-4);
}
@Test
public void testRosen() {
doTest(new OptimTestUtils.Rosen(),
doTest(TestFunction.ROSEN.withDimension(DIM),
OptimTestUtils.point(DIM, 0.1),
GoalType.MINIMIZE,
186915,
@ -147,7 +151,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testEllipse() {
doTest(new OptimTestUtils.Elli(),
doTest(TestFunction.ELLI.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
911,
@ -167,7 +171,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testCigar() {
doTest(new OptimTestUtils.Cigar(),
doTest(TestFunction.CIGAR.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
7000,
@ -177,7 +181,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testTwoAxes() {
doTest(new OptimTestUtils.TwoAxes(),
doTest(TestFunction.TWO_AXES.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
1219,
@ -187,7 +191,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testCigTab() {
doTest(new OptimTestUtils.CigTab(),
doTest(TestFunction.CIG_TAB.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
827,
@ -197,7 +201,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testSphere() {
doTest(new OptimTestUtils.Sphere(),
doTest(TestFunction.SPHERE.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
2580,
@ -207,7 +211,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testTablet() {
doTest(new OptimTestUtils.Tablet(),
doTest(TestFunction.TABLET.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
911,
@ -217,7 +221,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testDiffPow() {
doTest(new OptimTestUtils.DiffPow(),
doTest(TestFunction.DIFF_POW.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
631,
@ -227,17 +231,18 @@ public class SimplexOptimizerMultiDirectionalTest {
@Test
public void testSsDiffPow() {
doTest(new OptimTestUtils.SsDiffPow(),
OptimTestUtils.point(DIM / 2, 1.0, 1e-1),
final int dim = DIM / 2;
doTest(TestFunction.SS_DIFF_POW.withDimension(dim),
OptimTestUtils.point(dim, 1.0, 1e-1),
GoalType.MINIMIZE,
4000,
new PointValuePair(OptimTestUtils.point(DIM / 2, 0.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0),
1e-3);
}
@Ignore("See MATH-1552")@Test
public void testAckley() {
doTest(new OptimTestUtils.Ackley(),
doTest(TestFunction.ACKLEY.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
7900,
@ -247,7 +252,7 @@ public class SimplexOptimizerMultiDirectionalTest {
@Ignore("See MATH-1552")@Test
public void testRastrigin() {
doTest(new OptimTestUtils.Rastrigin(),
doTest(TestFunction.RASTRIGIN.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
4600,
@ -318,4 +323,30 @@ public class SimplexOptimizerMultiDirectionalTest {
Assert.assertTrue(name + ": nEval=" + nEval,
nEval < maxEvaluations);
}
private static class Gaussian2D implements MultivariateFunction {
private final double[] maximumPosition;
private final double std;
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();
}
@Override
public double value(double[] point) {
final double x = point[0];
final double y = point[1];
final double twoS2 = 2.0 * std * std;
return 1.0 / (twoS2 * AccurateMath.PI) * AccurateMath.exp(-(x * x + y * y) / twoS2);
}
}
}

View File

@ -31,6 +31,7 @@ import org.apache.commons.math4.legacy.optim.SimpleBounds;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.LeastSquaresConverter;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.apache.commons.math4.legacy.core.MathArrays;
import org.junit.Assert;
import org.junit.Test;
@ -145,13 +146,13 @@ public class SimplexOptimizerNelderMeadTest {
@Test(expected=TooManyEvaluationsException.class)
public void testMaxIterations() {
OptimTestUtils.Powell powell = new OptimTestUtils.Powell();
final int dim = 4;
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
optimizer.optimize(new MaxEval(20),
new ObjectiveFunction(powell),
new ObjectiveFunction(TestFunction.POWELL.withDimension(dim)),
GoalType.MINIMIZE,
new InitialGuess(new double[] { 3, -1, 0, 1 }),
Simplex.of(4),
Simplex.of(dim),
new NelderMeadTransform());
}
@ -202,27 +203,29 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testRosenbrock() {
doTest(new OptimTestUtils.Rosenbrock(),
final int dim = 2;
doTest(TestFunction.ROSENBROCK.withDimension(dim),
OptimTestUtils.point(new double[] { -1.2, 1 }, 1e-1),
GoalType.MINIMIZE,
180,
new PointValuePair(OptimTestUtils.point(2, 1.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 1.0), 0.0),
1e-4);
}
@Test
public void testPowell() {
doTest(new OptimTestUtils.Powell(),
final int dim = 4;
doTest(TestFunction.POWELL.withDimension(dim),
OptimTestUtils.point(new double[] { 3, -1, 0, 1 }, 1e-1),
GoalType.MINIMIZE,
420,
new PointValuePair(OptimTestUtils.point(4, 0.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0),
2e-4);
}
@Test
public void testRosen() {
doTest(new OptimTestUtils.Rosen(),
doTest(TestFunction.ROSEN.withDimension(DIM),
OptimTestUtils.point(DIM, 0.1),
GoalType.MINIMIZE,
9078,
@ -232,7 +235,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testEllipse() {
doTest(new OptimTestUtils.Elli(),
doTest(TestFunction.ELLI.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
15000,
@ -252,7 +255,7 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testCigar() {
doTest(new OptimTestUtils.Cigar(),
doTest(TestFunction.CIGAR.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
7000,
@ -262,7 +265,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testTwoAxes() {
doTest(new OptimTestUtils.TwoAxes(),
doTest(TestFunction.TWO_AXES.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
3451,
@ -272,7 +275,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testCigTab() {
doTest(new OptimTestUtils.CigTab(),
doTest(TestFunction.CIG_TAB.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
7000,
@ -282,7 +285,7 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testSphere() {
doTest(new OptimTestUtils.Sphere(),
doTest(TestFunction.SPHERE.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
3000,
@ -292,7 +295,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testTablet() {
doTest(new OptimTestUtils.Tablet(),
doTest(TestFunction.TABLET.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 1e-1),
GoalType.MINIMIZE,
10000,
@ -302,7 +305,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testDiffPow() {
doTest(new OptimTestUtils.DiffPow(),
doTest(TestFunction.DIFF_POW.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
7000,
@ -312,17 +315,18 @@ public class SimplexOptimizerNelderMeadTest {
@Test
public void testSsDiffPow() {
doTest(new OptimTestUtils.SsDiffPow(),
OptimTestUtils.point(DIM / 2, 1.0, 2e-1),
final int dim = DIM / 2;
doTest(TestFunction.SS_DIFF_POW.withDimension(dim),
OptimTestUtils.point(dim, 1.0, 2e-1),
GoalType.MINIMIZE,
4000,
new PointValuePair(OptimTestUtils.point(DIM / 2, 0.0), 0.0),
new PointValuePair(OptimTestUtils.point(dim, 0.0), 0.0),
1e-3);
}
@Ignore("See MATH-1552")@Test
public void testAckley() {
doTest(new OptimTestUtils.Ackley(),
doTest(TestFunction.ACKLEY.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
7900,
@ -332,7 +336,7 @@ public class SimplexOptimizerNelderMeadTest {
@Ignore("See MATH-1552")@Test
public void testRastrigin() {
doTest(new OptimTestUtils.Rastrigin(),
doTest(TestFunction.RASTRIGIN.withDimension(DIM),
OptimTestUtils.point(DIM, 1.0, 2e-1),
GoalType.MINIMIZE,
4600,