From 1fb5411a8119ec88bfdf5d1261afd6e3df73365a Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Tue, 10 Aug 2021 11:47:59 +0200 Subject: [PATCH] Move tests for expected exceptions to "SimplexOptimizerTest" class. --- .../SimplexOptimizerMultiDirectionalTest.java | 15 -------- .../SimplexOptimizerNelderMeadTest.java | 31 ----------------- .../scalar/noderiv/SimplexOptimizerTest.java | 34 +++++++++++++++++++ 3 files changed, 34 insertions(+), 46 deletions(-) diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java index 3b30b9525..591bfb125 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.java @@ -38,21 +38,6 @@ import org.junit.Ignore; public class SimplexOptimizerMultiDirectionalTest { private static final int DIM = 13; - @Test(expected=MathUnsupportedOperationException.class) - public void testBoundsUnsupported() { - SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); - final OptimTestUtils.FourExtrema fourExtrema = new OptimTestUtils.FourExtrema(); - - optimizer.optimize(new MaxEval(100), - new ObjectiveFunction(fourExtrema), - GoalType.MINIMIZE, - new InitialGuess(new double[] { -3, 0 }), - Simplex.alongAxes(new double[] { 0.2, 0.2 }), - new MultiDirectionalTransform(), - new SimpleBounds(new double[] { -5, -1 }, - new double[] { 5, 1 })); - } - @Test public void testMath283() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14); diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java index b73c0f8ff..d7a2989b7 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.java @@ -20,18 +20,14 @@ package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import org.apache.commons.math4.legacy.analysis.MultivariateFunction; import org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction; -import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException; -import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException; import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix; import org.apache.commons.math4.legacy.linear.RealMatrix; import org.apache.commons.math4.legacy.optim.InitialGuess; import org.apache.commons.math4.legacy.optim.MaxEval; import org.apache.commons.math4.legacy.optim.PointValuePair; -import org.apache.commons.math4.legacy.optim.SimpleBounds; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.LeastSquaresConverter; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction; -import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction; import org.apache.commons.math4.legacy.core.MathArrays; import org.junit.Assert; import org.junit.Test; @@ -43,21 +39,6 @@ import org.junit.Ignore; public class SimplexOptimizerNelderMeadTest { private static final int DIM = 13; - @Test(expected=MathUnsupportedOperationException.class) - public void testBoundsUnsupported() { - SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); - final OptimTestUtils.FourExtrema fourExtrema = new OptimTestUtils.FourExtrema(); - - optimizer.optimize(new MaxEval(100), - new ObjectiveFunction(fourExtrema), - GoalType.MINIMIZE, - new InitialGuess(new double[] { -3, 0 }), - Simplex.alongAxes(new double[] { 0.2, 0.2 }), - new NelderMeadTransform(), - new SimpleBounds(new double[] { -5, -1 }, - new double[] { 5, 1 })); - } - @Test public void testLeastSquares1() { final RealMatrix factors @@ -147,18 +128,6 @@ public class SimplexOptimizerNelderMeadTest { Assert.assertTrue(optimum.getValue() < 1e-6); } - @Test(expected=TooManyEvaluationsException.class) - public void testMaxIterations() { - final int dim = 4; - SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); - optimizer.optimize(new MaxEval(20), - new ObjectiveFunction(TestFunction.POWELL.withDimension(dim)), - GoalType.MINIMIZE, - new InitialGuess(new double[] { 3, -1, 0, 1 }), - Simplex.equalSidesAlongAxes(dim, 1d), - new NelderMeadTransform()); - } - @Test public void testFourExtremaMinimize1() { final OptimTestUtils.FourExtrema f = new OptimTestUtils.FourExtrema(); diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java index 3ce409248..d74850817 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizerTest.java @@ -19,6 +19,7 @@ package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import org.opentest4j.AssertionFailedError; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.aggregator.ArgumentsAggregator; @@ -28,10 +29,13 @@ import org.junit.jupiter.params.aggregator.AggregateWith; import org.junit.jupiter.params.provider.CsvFileSource; import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.math4.legacy.core.MathArrays; +import org.apache.commons.math4.legacy.exception.MathUnsupportedOperationException; +import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException; import org.apache.commons.math4.legacy.analysis.MultivariateFunction; import org.apache.commons.math4.legacy.optim.InitialGuess; import org.apache.commons.math4.legacy.optim.MaxEval; import org.apache.commons.math4.legacy.optim.PointValuePair; +import org.apache.commons.math4.legacy.optim.SimpleBounds; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.SimulatedAnnealing; @@ -46,6 +50,36 @@ public class SimplexOptimizerTest { private static final String MULTIDIRECTIONAL_INPUT_FILE = "std_test_func.simplex.multidirectional.csv"; private static final String HEDAR_FUKUSHIMA_INPUT_FILE = "std_test_func.simplex.hedar_fukushima.csv"; + @Test + public void testMaxEvaluations() { + Assertions.assertThrows(TooManyEvaluationsException.class, () -> { + final int dim = 4; + final SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); + optimizer.optimize(new MaxEval(20), + new ObjectiveFunction(TestFunction.PARABOLA.withDimension(dim)), + GoalType.MINIMIZE, + new InitialGuess(new double[] { 3, -1, -3, 1 }), + Simplex.equalSidesAlongAxes(dim, 1d), + new NelderMeadTransform()); + }); + } + + @Test + public void testBoundsUnsupported() { + Assertions.assertThrows(MathUnsupportedOperationException.class, () -> { + final int dim = 2; + final SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); + optimizer.optimize(new MaxEval(100), + new ObjectiveFunction(TestFunction.PARABOLA.withDimension(dim)), + GoalType.MINIMIZE, + new InitialGuess(new double[] { -3, 0 }), + Simplex.alongAxes(new double[] { 0.2, 0.2 }), + new NelderMeadTransform(), + new SimpleBounds(new double[] { -5, -1 }, + new double[] { 5, 1 })); + }); + } + @ParameterizedTest @CsvFileSource(resources = NELDER_MEAD_INPUT_FILE) void testFunctionWithNelderMead(@AggregateWith(TaskAggregator.class) Task task) {