Move tests for expected exceptions to "SimplexOptimizerTest" class.

This commit is contained in:
Gilles Sadowski 2021-08-10 11:47:59 +02:00
parent 9fd6725d51
commit 1fb5411a81
3 changed files with 34 additions and 46 deletions

View File

@ -38,21 +38,6 @@ import org.junit.Ignore;
public class SimplexOptimizerMultiDirectionalTest { public class SimplexOptimizerMultiDirectionalTest {
private static final int DIM = 13; 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 @Test
public void testMath283() { public void testMath283() {
SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14); SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);

View File

@ -20,18 +20,14 @@ package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction; import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction; 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.Array2DRowRealMatrix;
import org.apache.commons.math4.legacy.linear.RealMatrix; import org.apache.commons.math4.legacy.linear.RealMatrix;
import org.apache.commons.math4.legacy.optim.InitialGuess; import org.apache.commons.math4.legacy.optim.InitialGuess;
import org.apache.commons.math4.legacy.optim.MaxEval; import org.apache.commons.math4.legacy.optim.MaxEval;
import org.apache.commons.math4.legacy.optim.PointValuePair; 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.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.LeastSquaresConverter; 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.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.TestFunction;
import org.apache.commons.math4.legacy.core.MathArrays; import org.apache.commons.math4.legacy.core.MathArrays;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -43,21 +39,6 @@ import org.junit.Ignore;
public class SimplexOptimizerNelderMeadTest { public class SimplexOptimizerNelderMeadTest {
private static final int DIM = 13; 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 @Test
public void testLeastSquares1() { public void testLeastSquares1() {
final RealMatrix factors final RealMatrix factors
@ -147,18 +128,6 @@ public class SimplexOptimizerNelderMeadTest {
Assert.assertTrue(optimum.getValue() < 1e-6); 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 @Test
public void testFourExtremaMinimize1() { public void testFourExtremaMinimize1() {
final OptimTestUtils.FourExtrema f = new OptimTestUtils.FourExtrema(); final OptimTestUtils.FourExtrema f = new OptimTestUtils.FourExtrema();

View File

@ -19,6 +19,7 @@ package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
import java.util.Arrays; import java.util.Arrays;
import org.opentest4j.AssertionFailedError; import org.opentest4j.AssertionFailedError;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator; 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.junit.jupiter.params.provider.CsvFileSource;
import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.legacy.core.MathArrays; 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.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.optim.InitialGuess; import org.apache.commons.math4.legacy.optim.InitialGuess;
import org.apache.commons.math4.legacy.optim.MaxEval; import org.apache.commons.math4.legacy.optim.MaxEval;
import org.apache.commons.math4.legacy.optim.PointValuePair; 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.GoalType;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math4.legacy.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math4.legacy.optim.nonlinear.scalar.SimulatedAnnealing; 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 MULTIDIRECTIONAL_INPUT_FILE = "std_test_func.simplex.multidirectional.csv";
private static final String HEDAR_FUKUSHIMA_INPUT_FILE = "std_test_func.simplex.hedar_fukushima.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 @ParameterizedTest
@CsvFileSource(resources = NELDER_MEAD_INPUT_FILE) @CsvFileSource(resources = NELDER_MEAD_INPUT_FILE)
void testFunctionWithNelderMead(@AggregateWith(TaskAggregator.class) Task task) { void testFunctionWithNelderMead(@AggregateWith(TaskAggregator.class) Task task) {