@Test
public void testQuinticMin() throws MathException {
// 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,
UnivariateRealFunction f = new QuinticFunction();
UnivariateRealOptimizer underlying = new BrentOptimizer();
underlying.setRelativeAccuracy(1e-15);
JDKRandomGenerator g = new JDKRandomGenerator();
g.setSeed(4312000053L);
MultiStartUnivariateRealOptimizer minimizer =
new MultiStartUnivariateRealOptimizer(underlying, 5, g);
minimizer.setAbsoluteAccuracy(10 * minimizer.getAbsoluteAccuracy());
minimizer.setRelativeAccuracy(10 * minimizer.getRelativeAccuracy());
try {
minimizer.getOptima();
fail("an exception should have been thrown");
} catch (IllegalStateException ise) {
// expected
} catch (Exception e) {
fail("wrong exception caught");
}
try {
minimizer.getOptimaValues();
fail("an exception should have been thrown");
} catch (IllegalStateException ise) {
// expected
} catch (Exception e) {
fail("wrong exception caught");
}
double result = minimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
assertEquals(-0.2719561270319131, result, 1.0e-13);
assertEquals(-0.2719561270319131, minimizer.getResult(), 1.0e-13);
assertEquals(-0.04433426954946637, minimizer.getFunctionValue(), 1.0e-13);
double[] optima = minimizer.getOptima();
double[] optimaValues = minimizer.getOptimaValues();
for (int i = 0; i < optima.length; ++i) {
assertEquals(f.value(optima[i]), optimaValues[i], 1.0e-10);
}
assertTrue(minimizer.getEvaluations() >= 120);
assertTrue(minimizer.getEvaluations() <= 170);
assertTrue(minimizer.getIterationCount() >= 120);
assertTrue(minimizer.getIterationCount() <= 170);