Original contribution due to Dietmar Wolz: Fortran code translated in Java.
This commit is for reference only; work is under way to adapt the code into
a more maintainable version.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1154543 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-08-06 16:54:39 +00:00
parent a9c5cda5bd
commit a821e798c3
4 changed files with 4090 additions and 0 deletions

View File

@ -162,6 +162,7 @@ public enum LocalizedFormats implements Localizable {
NEGATIVE_NUMBER_OF_SUCCESSES("number of successes must be non-negative ({0})"),
NUMBER_OF_SUCCESSES("number of successes ({0})"), /* keep */
NEGATIVE_NUMBER_OF_TRIALS("number of trials must be non-negative ({0})"),
NUMBER_OF_INTERPOLATION_POINTS("number of interpolation points ({0})"), /* keep */
NUMBER_OF_TRIALS("number of trials ({0})"),
ROBUSTNESS_ITERATIONS("number of robustness iterations ({0})"),
START_POSITION("start position ({0})"), /* keep */
@ -261,6 +262,7 @@ public enum LocalizedFormats implements Localizable {
N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED("{0} points Legendre-Gauss integrator not supported, number of points must be in the {1}-{2} range"),
OBSERVED_COUNTS_ALL_ZERO("observed counts are all 0 in observed array {0}"),
OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY("observed counts are both zero for entry {0}"),
BOBYQA_BOUND_DIFFERENCE_CONDITION("the difference between the upper and lower bound must be larger than twice the initial trust region radius ({0})"),
OUT_OF_BOUNDS_QUANTILE_VALUE("out of bounds quantile value: {0}, must be in (0, 100]"),
OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"),
SIGNIFICANCE_LEVEL("significance level ({0})"), /* keep */
@ -295,11 +297,13 @@ public enum LocalizedFormats implements Localizable {
SUBARRAY_ENDS_AFTER_ARRAY_END("subarray ends after array end"),
TOO_LARGE_CUTOFF_SINGULAR_VALUE("cutoff singular value is {0}, should be at most {1}"),
TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY("cannot discard {0} elements from a {1} elements array"),
TOO_MUCH_CANCELLATION("too much cancellation in a denominator"),
TOO_MANY_REGRESSORS("too many regressors ({0}) specified, only {1} in the model"),
TOO_SMALL_COST_RELATIVE_TOLERANCE("cost relative tolerance is too small ({0}), no further reduction in the sum of squares is possible"),
TOO_SMALL_INTEGRATION_INTERVAL("too small integration interval: length = {0}"),
TOO_SMALL_ORTHOGONALITY_TOLERANCE("orthogonality tolerance is too small ({0}), solution is orthogonal to the jacobian"),
TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE("parameters relative tolerance is too small ({0}), no further improvement in the approximate solution is possible"),
TRUST_REGION_STEP_FAILED("trust region step has failed to reduce Q"),
TWO_OR_MORE_CATEGORIES_REQUIRED("two or more categories required, got {0}"),
TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED("two or more values required in each category, one has {0}"),
UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH("unable to bracket optimum in line search"),

File diff suppressed because it is too large Load Diff

View File

@ -133,6 +133,7 @@ NEGATIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} est n\u00e9gatif : {1}
NEGATIVE_NUMBER_OF_SUCCESSES = le nombre de succ\u00e8s ne doit pas \u00eatre n\u00e9gatif ({0})
NUMBER_OF_SUCCESSES = nombre de succ\u00e8s ({0})
NEGATIVE_NUMBER_OF_TRIALS = le nombre d''essais ne doit pas \u00eatre n\u00e9gatif ({0})
NUMBER_OF_INTERPOLATION_POINTS = nombre de points d''interpolation ({0})
NUMBER_OF_TRIALS = nombre d''essais ({0})
NEGATIVE_ROBUSTNESS_ITERATIONS = le nombre d''it\u00e9rations robuste ne peut \u00eatre n\u00e9gatif, alors qu''il est de {0}
START_POSITION = position de d\u00e9part
@ -231,6 +232,7 @@ NUMERATOR_OVERFLOW_AFTER_MULTIPLY = d\u00e9passement de capacit\u00e9 pour le nu
N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED = l''int\u00e9grateur de Legendre-Gauss en {0} points n''est pas disponible, le nombre de points doit \u00eatre entre {1} et {2}
OBSERVED_COUNTS_ALL_ZERO = aucune occurrence dans le tableau des observations {0}
OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY = les occurrences observ\u00e9es sont toutes deux nulles pour l''entr\u00e9e {0}
BOBYQA_BOUND_DIFFERENCE_CONDITION = la diff\u00e9rence entre la contrainte sup\u00e9rieure et inf\u00e9rieure doit \u00eatre plus grande que deux fois le rayon de la r\u00e9gion de confiance initiale ({0})
OUT_OF_BOUNDS_QUANTILE_VALUE = valeur de quantile {0} hors bornes, doit \u00eatre dans l''intervalle ]0, 100]
OUT_OF_BOUND_SIGNIFICANCE_LEVEL = niveau de signification {0} hors domaine, doit \u00eatre entre {1} et {2}
SIGNIFICANCE_LEVEL = niveau de signification ({0})

View File

@ -0,0 +1,588 @@
/*
* 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.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.NoDataException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.MultivariateRealOptimizer;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.junit.Assert;
import org.junit.Test;
/**
* Test for {@link BOBYQAOptimizer}.
*/
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);
doTest(new Rosen(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, expected);
}
@Test(expected = MultiDimensionMismatchException.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);
doTest(new Rosen(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 2000, expected);
}
@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
public void testRosen() {
double[] startPoint = point(DIM,0.1);
double[][] boundaries = 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
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);
doTest(new MinusElli(), startPoint, boundaries,
GoalType.MAXIMIZE,
2e-10, 5e-6, 1000, expected);
boundaries = boundaries(DIM,-0.3,0.3);
startPoint = point(DIM,0.1);
doTest(new MinusElli(), startPoint, boundaries,
GoalType.MAXIMIZE,
2e-10, 5e-6, 1000, expected);
}
@Test
public void testEllipse() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Elli(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 1000, expected);
}
@Test
public void testElliRotated() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new ElliRotated(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-12, 1e-6, 10000, expected);
}
@Test
public void testCigar() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Cigar(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@Test
public void testTwoAxes() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new TwoAxes(), startPoint, boundaries,
GoalType.MINIMIZE, 2*
1e-13, 1e-6, 100, expected);
}
@Test
public void testCigTab() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new CigTab(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 5e-5, 100, expected);
}
@Test
public void testSphere() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Sphere(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@Test
public void testTablet() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Tablet(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 100, expected);
}
@Test
public void testDiffPow() {
double[] startPoint = point(DIM/2,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM/2,0.0),0.0);
doTest(new DiffPow(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-8, 1e-1, 12000, expected);
}
@Test
public void testSsDiffPow() {
double[] startPoint = point(DIM/2,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM/2,0.0),0.0);
doTest(new SsDiffPow(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-2, 1.3e-1, 50000, expected);
}
@Test
public void testAckley() {
double[] startPoint = point(DIM,0.1);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Ackley(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-8, 1e-5, 1000, expected);
}
@Test
public void testRastrigin() {
double[] startPoint = point(DIM,1.0);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,0.0),0.0);
doTest(new Rastrigin(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-13, 1e-6, 1000, expected);
}
@Test
public void testConstrainedRosen() {
double[] startPoint = point(DIM,0.1);
double[][] boundaries = boundaries(DIM,-1,2);
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 testDiagonalRosen() {
double[] startPoint = point(DIM,0.1);
double[][] boundaries = null;
RealPointValuePair expected =
new RealPointValuePair(point(DIM,1.0),0.0);
doTest(new Rosen(), startPoint, boundaries,
GoalType.MINIMIZE,
1e-10, 1e-4, 2000, expected);
}
/**
* @param func Function to optimize.
* @param startPoint Starting point.
* @param boundaries Upper / lower point limit.
* @param goal Minimization or maximization.
* @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(MultivariateRealFunction func,
double[] startPoint,
double[][] boundaries,
GoalType goal,
double fTol,
double pointTol,
int maxEvaluations,
RealPointValuePair expected) {
int dim = startPoint.length;
// MultivariateRealOptimizer optim =
// new PowellOptimizer(1e-13, Math.ulp(1d));
// RealPointValuePair result = optim.optimize(100000, func, goal, startPoint);
MultivariateRealOptimizer optim =
new BOBYQAOptimizer(boundaries);
RealPointValuePair result = optim.optimize(maxEvaluations, func, goal, startPoint);
// System.out.println(func.getClass().getName() + " = "
// + optim.getEvaluations() + " f(");
// for (double x: result.getPoint()) System.out.print(x + " ");
// System.out.println(") = " + result.getValue());
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 MultivariateRealFunction {
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 MultivariateRealFunction {
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 MultivariateRealFunction {
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 MultivariateRealFunction {
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 MultivariateRealFunction {
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 MultivariateRealFunction {
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 += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
private static class Elli implements MultivariateRealFunction {
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 += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
private static class MinusElli implements MultivariateRealFunction {
private int fcount = 0;
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;
}
}
private static class DiffPow implements MultivariateRealFunction {
private int fcount = 0;
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i)
f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i
/ (x.length - 1.));
// System.out.print("" + (fcount++) + ") ");
// for (int i = 0; i < x.length; i++)
// System.out.print(x[i] + " ");
// System.out.println(" = " + f);
return f;
}
}
private static class SsDiffPow implements MultivariateRealFunction {
public double value(double[] x) {
double f = Math.pow(new DiffPow().value(x), 0.25);
return f;
}
}
private static class Rosen implements MultivariateRealFunction {
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 MultivariateRealFunction {
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 = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
f += fac * fac * x[i] * x[i];
res2 += Math.cos(2. * Math.PI * fac * x[i]);
}
f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length))
+ Math.exp(1.) - Math.exp(res2 / x.length));
return f;
}
}
private static class Rastrigin implements MultivariateRealFunction {
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 = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
if (i == 0 && x[i] < 0)
fac *= 1.;
f += fac * fac * x[i] * x[i] + amplitude
* (1. - Math.cos(2. * Math.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] /= Math.sqrt(sp);
}
}
}
}