MATH-413 (point 13)

Selecting a random start value (instead of interval bounds).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1000422 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-09-23 12:11:56 +00:00
parent 8232bfbded
commit 51aa6e6ca2
2 changed files with 14 additions and 20 deletions

View File

@ -143,18 +143,22 @@ public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFuncti
final GoalType goal,
final double min, final double max)
throws FunctionEvaluationException {
return optimize(f, goal, min, max, min + 0.5 * (max - min));
}
/** {@inheritDoc} */
public UnivariateRealPointValuePair optimize(final FUNC f, final GoalType goal,
final double min, final double max,
final double startValue)
throws FunctionEvaluationException {
optima = new UnivariateRealPointValuePair[starts];
totalEvaluations = 0;
// Multi-start loop.
for (int i = 0; i < starts; ++i) {
try {
final double bound1 = (i == 0) ? min : min + generator.nextDouble() * (max - min);
final double bound2 = (i == 0) ? max : min + generator.nextDouble() * (max - min);
optima[i] = optimizer.optimize(f, goal,
FastMath.min(bound1, bound2),
FastMath.max(bound1, bound2));
final double s = (i == 0) ? startValue : min + generator.nextDouble() * (max - min);
optima[i] = optimizer.optimize(f, goal, min, max, s);
} catch (FunctionEvaluationException fee) {
optima[i] = null;
} catch (ConvergenceException ce) {
@ -177,16 +181,6 @@ public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFuncti
return optima[0];
}
/** {@inheritDoc} */
public UnivariateRealPointValuePair optimize(final FUNC f, final GoalType goalType,
final double min, final double max,
final double startValue)
throws FunctionEvaluationException {
// XXX Main code should be here, using "startValue" for the first start.
// XXX This method should set "startValue" to min + 0.5 * (max - min)
return optimize(f, goalType, min, max);
}
/**
* Sort the optima from best to worst, followed by {@code null} elements.
*

View File

@ -50,8 +50,8 @@ public class MultiStartUnivariateRealOptimizerTest {
assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
}
assertTrue(optimizer.getEvaluations() > 150);
assertTrue(optimizer.getEvaluations() < 250);
assertTrue(optimizer.getEvaluations() > 200);
assertTrue(optimizer.getEvaluations() < 300);
}
@Test
@ -68,14 +68,14 @@ public class MultiStartUnivariateRealOptimizerTest {
UnivariateRealPointValuePair optimum
= optimizer.optimize(f, GoalType.MINIMIZE, -0.3, -0.2);
assertEquals(-0.2719561271, optimum.getPoint(), 1e-9);
assertEquals(-0.2719561293, optimum.getPoint(), 1e-9);
assertEquals(-0.0443342695, optimum.getValue(), 1e-9);
UnivariateRealPointValuePair[] optima = optimizer.getOptima();
for (int i = 0; i < optima.length; ++i) {
assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9);
}
assertTrue(optimizer.getEvaluations() >= 110);
assertTrue(optimizer.getEvaluations() <= 150);
assertTrue(optimizer.getEvaluations() >= 50);
assertTrue(optimizer.getEvaluations() <= 100);
}
}