MATH-621
Change history: Constants in procedures replaced by static final fields. NF eliminated (function evaluation counting is done in base class). MAXFUN eliminated (exception is thrown by base class). -1e300 replaced by NEGATIVE_INFINITY. 1e300 replaced by POSITIVE_INFINITY. Number of interpolation points set at construction (no automatic default to "2n+1" if set to "-1"). Replaced "checkParameters()" with "setup()" and moved validity checks from "doOptimize()" to "setup()". Replaced "boundaries[][]" with two "double[]" for the constraints. Removed unit test "testBoundariesNoData" ("null" is interpreted as "no constraints"). Replaced "xl" and "xu" with "lowerBound" and "upperBound", respectively (Fortran 1-based indexing still used). Replaced "x" with "currentBest". Replaced "rhobeg" with "initialTrustRegionRadius". Using instance field directly instead of passing it as function argument. Replaced "rhoend" with "stoppingTrustRegionRadius". Using instance field directly instead of passing it as function argument. Removed all parameters from "bobyqa" function (using instance fields directly). Removed (from "bobyqa" function) a test on the bound difference: It would never fail because of the auto-correction in "setup". Replaced "ScopedPtr" by "FortranArray" for all one-dimensional data. 0-based loop in "bobyqa". Replaced "ScopedPtr" by "FortranMatrix" for all matrix data. Loop-local counters in all functions. Replaced kopt with "trustRegionCenterInterpolationPointIndex" instance variable. Removed "ndim", "n" and "npt" from the arguments list of all functions. Removed "w" from the arguments list of "update". Removed "w" from the arguments list of "altmov" (replaced with local variables "work1" and "work2"). In "trsbox" arguments list, replaced "ScopedPtr" ("gnew", "xbdi", "s", "hs", "hred") by "FortranArray". Removed "ptsaux", "ptsid" from arguments list of "rescue" (replaced with local variables). Corrected bug in "rescue" function. Removed "w" from arguments list of "rescue". Removed "glag" and "hcol" from arguments list of "altmov" (replaced by local variables). Removed "w" from arguments list of "bobyqb" (replaced by local variables). Removed global work space "w". Removed auxiliary class "ScopedPtr". Removed "alpha" and "cauchy" ("DoubleRef") in "altmov" arguments list: Values returned in a "double[]" array. Removed "dsq" and "crvmin" ("DoubleRef") in "trsbox" arguments list: Values returned in a "double[]" array. Removed "DoubleRef" auxiliary class. Removed unused local variables; changed some to be "final". This is still an intermediate version. Please do not commit any changes without discussing it on JIRA. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1154550 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a821e798c3
commit
d451a1fb92
File diff suppressed because it is too large
Load Diff
|
@ -16,16 +16,15 @@
|
|||
*/
|
||||
package org.apache.commons.math.optimization.direct;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.exception.MultiDimensionMismatchException;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math.exception.TooManyEvaluationsException;
|
||||
import org.apache.commons.math.exception.NoDataException;
|
||||
import org.apache.commons.math.exception.OutOfRangeException;
|
||||
import org.apache.commons.math.exception.TooManyEvaluationsException;
|
||||
import org.apache.commons.math.exception.NumberIsTooSmallException;
|
||||
import org.apache.commons.math.optimization.GoalType;
|
||||
import org.apache.commons.math.optimization.MultivariateRealOptimizer;
|
||||
import org.apache.commons.math.optimization.RealPointValuePair;
|
||||
|
@ -38,73 +37,69 @@ import org.junit.Test;
|
|||
public class BOBYQAOptimizerTest {
|
||||
|
||||
static final int DIM = 13;
|
||||
|
||||
@Test(expected = OutOfRangeException.class)
|
||||
public void testInitOutofbounds() {
|
||||
double[] startPoint = point(DIM,3);
|
||||
double[][] boundaries = boundaries(DIM,-1,2);
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(DIM,1.0),0.0);
|
||||
|
||||
@Test(expected=OutOfRangeException.class)
|
||||
public void testInitOutOfBounds() {
|
||||
double[] startPoint = point(DIM, 3);
|
||||
double[][] boundaries = boundaries(DIM, -1, 2);
|
||||
doTest(new Rosen(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, expected);
|
||||
1e-13, 1e-6, 2000, null);
|
||||
}
|
||||
|
||||
@Test(expected = MultiDimensionMismatchException.class)
|
||||
@Test(expected=DimensionMismatchException.class)
|
||||
public void testBoundariesDimensionMismatch() {
|
||||
double[] startPoint = point(DIM,0.5);
|
||||
double[][] boundaries = boundaries(DIM+1,-1,2);
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(DIM,1.0),0.0);
|
||||
double[] startPoint = point(DIM, 0.5);
|
||||
double[][] boundaries = boundaries(DIM + 1, -1, 2);
|
||||
doTest(new Rosen(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, expected);
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, null);
|
||||
}
|
||||
|
||||
@Test(expected = NoDataException.class)
|
||||
public void testBoundariesNoData() {
|
||||
double[] startPoint = point(DIM,0.5);
|
||||
double[][] boundaries = boundaries(DIM,-1,2);
|
||||
boundaries[1] = null;
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(DIM,1.0),0.0);
|
||||
doTest(new Rosen(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, expected);
|
||||
@Test(expected=NumberIsTooSmallException.class)
|
||||
public void testProblemDimensionTooSmall() {
|
||||
double[] startPoint = point(1, 0.5);
|
||||
double[][] boundaries = null;
|
||||
doTest(new Rosen(), startPoint, null,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, null);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=TooManyEvaluationsException.class)
|
||||
public void testMaxEvaluations() {
|
||||
final int lowMaxEval = 2;
|
||||
double[] startPoint = point(DIM, 0.1);
|
||||
double[][] boundaries = null;
|
||||
doTest(new Rosen(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, lowMaxEval, null);
|
||||
}
|
||||
|
||||
@Test(expected=TooManyEvaluationsException.class)
|
||||
public void testRescue() {
|
||||
double[] startPoint = point(DIM, 1);
|
||||
double[][] boundaries = null;
|
||||
RealPointValuePair expected = new RealPointValuePair(point(DIM, 0), 0);
|
||||
doTest(new MinusElli(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 1000, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRosen() {
|
||||
double[] startPoint = point(DIM,0.1);
|
||||
double[][] boundaries = null;
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(DIM,1.0),0.0);
|
||||
RealPointValuePair expected = new RealPointValuePair(point(DIM,1.0),0.0);
|
||||
doTest(new Rosen(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 2000, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRescue() {
|
||||
double[] startPoint = point(13,1.0);
|
||||
double[][] boundaries = null;
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(13,0.0),0);
|
||||
try {
|
||||
doTest(new MinusElli(), startPoint, boundaries,
|
||||
GoalType.MINIMIZE,
|
||||
1e-13, 1e-6, 1000, expected);
|
||||
fail("An TooManyEvaluationsException should have been thrown");
|
||||
} catch(TooManyEvaluationsException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaximize() {
|
||||
double[] startPoint = point(DIM,1.0);
|
||||
double[][] boundaries = null;
|
||||
RealPointValuePair expected =
|
||||
new RealPointValuePair(point(DIM,0.0),1.0);
|
||||
RealPointValuePair expected = new RealPointValuePair(point(DIM,0.0),1.0);
|
||||
doTest(new MinusElli(), startPoint, boundaries,
|
||||
GoalType.MAXIMIZE,
|
||||
2e-10, 5e-6, 1000, expected);
|
||||
|
@ -278,12 +273,17 @@ public class BOBYQAOptimizerTest {
|
|||
double pointTol,
|
||||
int maxEvaluations,
|
||||
RealPointValuePair expected) {
|
||||
|
||||
System.out.println(func.getClass().getName() + " BEGIN"); // XXX
|
||||
|
||||
int dim = startPoint.length;
|
||||
// MultivariateRealOptimizer optim =
|
||||
// new PowellOptimizer(1e-13, Math.ulp(1d));
|
||||
// RealPointValuePair result = optim.optimize(100000, func, goal, startPoint);
|
||||
final double[] lB = boundaries == null ? null : boundaries[0];
|
||||
final double[] uB = boundaries == null ? null : boundaries[1];
|
||||
MultivariateRealOptimizer optim =
|
||||
new BOBYQAOptimizer(boundaries);
|
||||
new BOBYQAOptimizer(2 * dim + 1, lB, uB);
|
||||
RealPointValuePair result = optim.optimize(maxEvaluations, func, goal, startPoint);
|
||||
// System.out.println(func.getClass().getName() + " = "
|
||||
// + optim.getEvaluations() + " f(");
|
||||
|
@ -295,6 +295,8 @@ public class BOBYQAOptimizerTest {
|
|||
Assert.assertEquals(expected.getPoint()[i],
|
||||
result.getPoint()[i], pointTol);
|
||||
}
|
||||
|
||||
System.out.println(func.getClass().getName() + " END"); // XXX
|
||||
}
|
||||
|
||||
private static double[] point(int n, double value) {
|
||||
|
@ -443,15 +445,10 @@ public class BOBYQAOptimizerTest {
|
|||
}
|
||||
|
||||
private static class MinusElli implements MultivariateRealFunction {
|
||||
private int fcount = 0;
|
||||
private final Elli elli = new Elli();
|
||||
public double value(double[] x) {
|
||||
double f = 1.0-(new Elli().value(x));
|
||||
// System.out.print("" + (fcount++) + ") ");
|
||||
// for (int i = 0; i < x.length; i++)
|
||||
// System.out.print(x[i] + " ");
|
||||
// System.out.println(" = " + f);
|
||||
return f;
|
||||
}
|
||||
return 1.0 - elli.value(x);
|
||||
}
|
||||
}
|
||||
|
||||
private static class DiffPow implements MultivariateRealFunction {
|
||||
|
|
Loading…
Reference in New Issue