Java源码示例:org.apache.commons.math3.analysis.MultivariateFunction

示例1
/**
 * Compute and evaluate a new simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param original Original simplex (to be preserved).
 * @param coeff Linear coefficient.
 * @param comparator Comparator to use to sort simplex vertices from best
 * to poorest.
 * @return the best point in the transformed simplex.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction,
                                              final PointValuePair[] original,
                                              final double coeff,
                                              final Comparator<PointValuePair> comparator) {
    final double[] xSmallest = original[0].getPointRef();
    // Perform a linear transformation on all the simplex points,
    // except the first one.
    setPoint(0, original[0]);
    final int dim = getDimension();
    for (int i = 1; i < getSize(); i++) {
        final double[] xOriginal = original[i].getPointRef();
        final double[] xTransformed = new double[dim];
        for (int j = 0; j < dim; j++) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        setPoint(i, new PointValuePair(xTransformed, Double.NaN, false));
    }

    // Evaluate the simplex.
    evaluate(evaluationFunction, comparator);

    return getPoint(0);
}
 
示例2
/**
 * @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);
    }
}
 
示例3
MultivariateFunction generateLDPLObjectiveFunction(final GaussianProcessLDPLMeanModel model, final double[] initPoint){

		MultivariateFunction negaLLfunc = new MultivariateFunction(){
			@Override
			public double value(double[] point){
				double[] params = gpLDPL.getParams();
				for(int i=0; i<initPoint.length; i++){
					params[i] = point[i];
				}
				gpLDPL.setParams(params);
				model.train(samples);
				gpLDPL.updateByActiveBeaconList(activeBeaconList);
				double logLikelihood = gpLDPL.looPredLogLikelihood();
				double looMSE = gpLDPL.looMSE();

				StringBuilder sb = new StringBuilder();
				sb.append("optimizeForLDPL: n="+params[0]+",A="+params[1]+",fa="+params[2]+",fb="+params[3]);
				sb.append(", LOOMSE="+looMSE);
				sb.append(", logLikelihood="+(-logLikelihood));
				System.out.println(sb.toString());
				
				return -logLikelihood;
			}
		};
		return negaLLfunc;
	}
 
示例4
@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);
}
 
示例5
MultivariateFunction generateGPIsoScaleLOOMSE(final GaussianProcessLDPLMeanModel model){
	MultivariateFunction negaLLfunc = new MultivariateFunction(){
		@Override
		public double value(double[] point){
			double lengthx = Math.pow(10, point[0]);
			double lambda = Math.pow(10, point[1]);

			double stdev = model.stdev;
			double lz = model.lengthes[2];
			model.lengthes = new double[]{lengthx, lengthx, lz};
			model.sigmaN = Math.sqrt(lambda)*stdev;

			model.train(samples);
			gpLDPL.updateByActiveBeaconList(activeBeaconList);

			double looMSE = gpLDPL.looMSE();
			StringBuilder sb = new StringBuilder();
			sb.append("optimizeForGPLOOError: lx=ly="+lengthx+",lz="+lz+",lambda="+lambda+",looMSE="+looMSE);
			System.out.println(sb.toString());

			return looMSE;
		}
	};
	return negaLLfunc;
}
 
示例6
/**
 * Compute and evaluate a new simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param original Original simplex (to be preserved).
 * @param coeff Linear coefficient.
 * @param comparator Comparator to use to sort simplex vertices from best
 * to poorest.
 * @return the best point in the transformed simplex.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction,
                                              final PointValuePair[] original,
                                              final double coeff,
                                              final Comparator<PointValuePair> comparator) {
    final double[] xSmallest = original[0].getPointRef();
    // Perform a linear transformation on all the simplex points,
    // except the first one.
    setPoint(0, original[0]);
    final int dim = getDimension();
    for (int i = 1; i < getSize(); i++) {
        final double[] xOriginal = original[i].getPointRef();
        final double[] xTransformed = new double[dim];
        for (int j = 0; j < dim; j++) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        setPoint(i, new PointValuePair(xTransformed, Double.NaN, false));
    }

    // Evaluate the simplex.
    evaluate(evaluationFunction, comparator);

    return getPoint(0);
}
 
示例7
/**
 * @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);
    }
}
 
示例8
/**
 * @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);
    }
}
 
示例9
/**
 * @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 PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(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);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
示例10
@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]);
}
 
示例11
@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);
}
 
示例12
@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);
}
 
示例13
/**
 * @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);
    }
}
 
示例14
@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]);
}
 
示例15
@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);
}
 
示例16
/**
 * @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);
    }
}
 
示例17
/**
 * @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(MultivariateFunction func,
                    double[] startPoint,
                    double[][] boundaries,
                    GoalType goal,
                    double fTol,
                    double pointTol,
                    int maxEvaluations,
                    PointValuePair expected) {
    doTest(func,
           startPoint,
           boundaries,
           goal,
           fTol,
           pointTol,
           maxEvaluations,
           0,
           expected,
           "");
}
 
示例18
/**
 * @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(MultivariateFunction func,
                    double[] startPoint,
                    double[][] boundaries,
                    GoalType goal,
                    double fTol,
                    double pointTol,
                    int maxEvaluations,
                    PointValuePair expected) {
    doTest(func,
           startPoint,
           boundaries,
           goal,
           fTol,
           pointTol,
           maxEvaluations,
           0,
           expected,
           "");
}
 
示例19
/**
 * @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 PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(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);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
示例20
@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]);
}
 
示例21
/**
 * Optimize an objective function.
 *
 * @param f Objective function.
 * @param goalType Type of optimization goal: either
 * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
 * @param startPoint Start point for optimization.
 * @param maxEval Maximum number of function evaluations.
 * @return the point/value pair giving the optimal value for objective
 * function.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.NullArgumentException if
 * any argument is {@code null}.
 */
protected PointValuePair optimizeInternal(int maxEval, MultivariateFunction f, GoalType goalType,
                                          double[] startPoint) {
    // Checks.
    if (f == null) {
        throw new NullArgumentException();
    }
    if (goalType == null) {
        throw new NullArgumentException();
    }
    if (startPoint == null) {
        throw new NullArgumentException();
    }

    // Reset.
    evaluations.setMaximalCount(maxEval);
    evaluations.resetCount();

    // Store optimization problem characteristics.
    function = f;
    goal = goalType;
    start = startPoint.clone();

    // Perform computation.
    return doOptimize();
}
 
示例22
@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);
}
 
示例23
@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);
}
 
示例24
/**
 * @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(MultivariateFunction func,
                    double[] startPoint,
                    double[][] boundaries,
                    GoalType goal,
                    double fTol,
                    double pointTol,
                    int maxEvaluations,
                    PointValuePair expected) {
    doTest(func,
           startPoint,
           boundaries,
           goal,
           fTol,
           pointTol,
           maxEvaluations,
           0,
           expected,
           "");
}
 
示例25
/**
     * @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 additionalInterpolationPoints Number of interpolation to used
     * in addition to the default (2 * dim + 1).
     * @param expected Expected point / value.
     */
    private void doTest(MultivariateFunction func,
                        double[] startPoint,
                        double[][] boundaries,
                        GoalType goal,
                        double fTol,
                        double pointTol,
                        int maxEvaluations,
                        int additionalInterpolationPoints,
                        PointValuePair expected,
                        String assertMsg) {

//         System.out.println(func.getClass().getName() + " BEGIN"); // XXX

        int dim = startPoint.length;
//        MultivariateOptimizer optim =
//            new PowellOptimizer(1e-13, Math.ulp(1d));
//        PointValuePair result = optim.optimize(100000, func, goal, startPoint);
        final double[] lB = boundaries == null ? null : boundaries[0];
        final double[] uB = boundaries == null ? null : boundaries[1];
        final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints;
        BOBYQAOptimizer optim = new BOBYQAOptimizer(numIterpolationPoints);
        PointValuePair result = optim.optimize(maxEvaluations, func, goal, startPoint, lB, uB);
//        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(assertMsg, expected.getValue(), result.getValue(), fTol);
        for (int i = 0; i < dim; i++) {
            Assert.assertEquals(expected.getPoint()[i],
                                result.getPoint()[i], pointTol);
        }

//         System.out.println(func.getClass().getName() + " END"); // XXX
    }
 
示例26
/** Simple constructor.
 * @param bounded bounded function
 * @param lower lower bounds for each element of the input parameters array
 * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
 * unbounded values)
 * @param upper upper bounds for each element of the input parameters array
 * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
 * unbounded values)
 * @exception DimensionMismatchException if lower and upper bounds are not
 * consistent, either according to dimension or to values
 */
public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded,
                                              final double[] lower, final double[] upper) {

    // safety checks
    MathUtils.checkNotNull(lower);
    MathUtils.checkNotNull(upper);
    if (lower.length != upper.length) {
        throw new DimensionMismatchException(lower.length, upper.length);
    }
    for (int i = 0; i < lower.length; ++i) {
        // note the following test is written in such a way it also fails for NaN
        if (!(upper[i] >= lower[i])) {
            throw new NumberIsTooSmallException(upper[i], lower[i], true);
        }
    }

    this.bounded = bounded;
    this.mappers = new Mapper[lower.length];
    for (int i = 0; i < mappers.length; ++i) {
        if (Double.isInfinite(lower[i])) {
            if (Double.isInfinite(upper[i])) {
                // element is unbounded, no transformation is needed
                mappers[i] = new NoBoundsMapper();
            } else {
                // element is simple-bounded on the upper side
                mappers[i] = new UpperBoundMapper(upper[i]);
            }
        } else {
            if (Double.isInfinite(upper[i])) {
                // element is simple-bounded on the lower side
                mappers[i] = new LowerBoundMapper(lower[i]);
            } else {
                // element is double-bounded
                mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]);
            }
        }
    }

}
 
示例27
@Test
public void testMath864() {
    final CMAESOptimizer optimizer
        = new CMAESOptimizer(30000, 0, true, 10,
                             0, new MersenneTwister(), false, null);
    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[] sigma = { 1e-1 };
    final double[] result = optimizer.optimize(new MaxEval(10000),
                                               new ObjectiveFunction(fitnessFunction),
                                               GoalType.MINIMIZE,
                                               new CMAESOptimizer.PopulationSize(5),
                                               new CMAESOptimizer.Sigma(sigma),
                                               new InitialGuess(start),
                                               new SimpleBounds(lower, upper)).getPoint();
    Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
                      result[0] <= upper[0]);
}
 
示例28
public MultivariateFunction partialDerivative(final int k) {
    return new MultivariateFunction() {
        public double value(double[] point) {
            return gradient(point)[k];
        }
    };
}
 
示例29
@Test
public void testMath864() {
    final CMAESOptimizer optimizer
        = new CMAESOptimizer(30000, 0, true, 10,
                             0, new MersenneTwister(), false, null);
    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[] sigma = { 1e-1 };
    final double[] result = optimizer.optimize(new MaxEval(10000),
                                               new ObjectiveFunction(fitnessFunction),
                                               GoalType.MINIMIZE,
                                               new CMAESOptimizer.PopulationSize(5),
                                               new CMAESOptimizer.Sigma(sigma),
                                               new InitialGuess(start),
                                               new SimpleBounds(lower, upper)).getPoint();
    Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
                      result[0] <= upper[0]);
}
 
示例30
public ObjectiveFunction getObjectiveFunction() {
    return new ObjectiveFunction(new MultivariateFunction() {
            public double value(double[] params)  {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                double sum = 0;
                for (Vector2D point : points) {
                    double di = point.distance(center) - radius;
                    sum += di * di;
                }
                return sum;
            }
        });
}