org.apache.commons.math3.analysis.MultivariateFunction Java Examples

The following examples show how to use org.apache.commons.math3.analysis.MultivariateFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultiDirectionalSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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,
           "");
}
 
Example #3
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #4
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example #5
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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,
           "");
}
 
Example #6
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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]);
}
 
Example #7
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example #9
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #10
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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]);
}
 
Example #11
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #12
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example #13
Source File: MultiDirectionalSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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]);
}
 
Example #15
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #16
Source File: GaussianProcessLDPLMeanModel.java    From BLELocalization with MIT License 6 votes vote down vote up
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;
}
 
Example #17
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example #18
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #19
Source File: BaseAbstractMultivariateOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #20
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #21
Source File: GaussianProcessLDPLMeanModel.java    From BLELocalization with MIT License 6 votes vote down vote up
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;
	}
 
Example #22
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #23
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example #24
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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,
           "");
}
 
Example #25
Source File: CMAESOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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]);
}
 
Example #26
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    final MultivariateFunction func = new SumSincFunction(-1);
    final PowellOptimizer optim = new PowellOptimizer(1e-8, 1e-5,
                                                      1e-4, 1e-4);

    optim.optimize(new MaxEval(100),
                   new ObjectiveFunction(func),
                   GoalType.MINIMIZE,
                   new InitialGuess(new double[] { -3, 0 }),
                   new SimpleBounds(new double[] { -5, -1 },
                                    new double[] { 5, 1 }));
}
 
Example #27
Source File: MicrosphereInterpolator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MultivariateFunction interpolate(final double[][] xval,
                                        final double[] yval)
    throws DimensionMismatchException,
           NoDataException,
           NullArgumentException {
    final UnitSphereRandomVectorGenerator rand
        = new UnitSphereRandomVectorGenerator(xval[0].length);
    return new MicrosphereInterpolatingFunction(xval, yval,
                                                brightnessExponent,
                                                microsphereElements,
                                                rand);
}
 
Example #28
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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;
            }
        });
}
 
Example #29
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
     * @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, FastMath.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 = boundaries == null ?
            optim.optimize(maxEvaluations, func, goal,
                           new InitialGuess(startPoint)) :
            optim.optimize(maxEvaluations, func, goal,
                           new InitialGuess(startPoint),
                           new SimpleBounds(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
    }
 
Example #30
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}