Refined test (follow-up of MATH-441).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1037372 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
22d63af83d
commit
b4129de756
|
@ -28,11 +28,12 @@ import org.apache.commons.math.optimization.GoalType;
|
||||||
import org.apache.commons.math.random.JDKRandomGenerator;
|
import org.apache.commons.math.random.JDKRandomGenerator;
|
||||||
import org.apache.commons.math.util.FastMath;
|
import org.apache.commons.math.util.FastMath;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
public class MultiStartUnivariateRealOptimizerTest {
|
public class MultiStartUnivariateRealOptimizerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSinMin() throws MathUserException {
|
public void testSinMin() {
|
||||||
UnivariateRealFunction f = new SinFunction();
|
UnivariateRealFunction f = new SinFunction();
|
||||||
UnivariateRealOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
UnivariateRealOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
|
||||||
underlying.setMaxEvaluations(300);
|
underlying.setMaxEvaluations(300);
|
||||||
|
@ -53,7 +54,7 @@ public class MultiStartUnivariateRealOptimizerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuinticMin() throws MathUserException {
|
public void testQuinticMin() {
|
||||||
// The quintic function has zeros at 0, +-0.5 and +-1.
|
// The quintic function has zeros at 0, +-0.5 and +-1.
|
||||||
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
|
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
|
||||||
UnivariateRealFunction f = new QuinticFunction();
|
UnivariateRealFunction f = new QuinticFunction();
|
||||||
|
@ -77,10 +78,10 @@ public class MultiStartUnivariateRealOptimizerTest {
|
||||||
assertTrue(optimizer.getEvaluations() <= 100);
|
assertTrue(optimizer.getEvaluations() <= 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=MathUserException.class)
|
@Test
|
||||||
public void testBadFunction() throws MathUserException {
|
public void testBadFunction() {
|
||||||
UnivariateRealFunction f = new UnivariateRealFunction() {
|
UnivariateRealFunction f = new UnivariateRealFunction() {
|
||||||
public double value(double x) throws MathUserException {
|
public double value(double x) {
|
||||||
if (x < 0) {
|
if (x < 0) {
|
||||||
throw new MathUserException();
|
throw new MathUserException();
|
||||||
}
|
}
|
||||||
|
@ -94,6 +95,15 @@ public class MultiStartUnivariateRealOptimizerTest {
|
||||||
MultiStartUnivariateRealOptimizer<UnivariateRealFunction> optimizer =
|
MultiStartUnivariateRealOptimizer<UnivariateRealFunction> optimizer =
|
||||||
new MultiStartUnivariateRealOptimizer<UnivariateRealFunction>(underlying, 5, g);
|
new MultiStartUnivariateRealOptimizer<UnivariateRealFunction>(underlying, 5, g);
|
||||||
|
|
||||||
optimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
|
try {
|
||||||
|
UnivariateRealPointValuePair optimum
|
||||||
|
= optimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (MathUserException e) {
|
||||||
|
// Expected.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the exception was thrown because no optimum was found.
|
||||||
|
Assert.assertTrue(optimizer.getOptima()[0] == null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue