From a97523700aca6679244607c29f6f7841f2cdd56d Mon Sep 17 00:00:00 2001 From: Alex Herbert Date: Sat, 22 Oct 2022 23:24:24 +0100 Subject: [PATCH] Compare candidate to all list points --- .../nonlinear/scalar/noderiv/SimplexOptimizer.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizer.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizer.java index 2a714b691..20d5eee8a 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizer.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/noderiv/SimplexOptimizer.java @@ -386,6 +386,10 @@ public class SimplexOptimizer extends MultivariateOptimizer { * Stores the given {@code candidate} if its fitness is better than * that of the last (assumed to be the worst) point in {@code list}. * + *

If the list is below the maximum size then the {@code candidate} + * is added if it is not already in the list. The list is sorted + * when it reaches the maximum size. + * * @param candidate Point to be stored. * @param comp Fitness comparator. * @param list Starting points (modified in-place). @@ -406,12 +410,14 @@ public class SimplexOptimizer extends MultivariateOptimizer { if (Arrays.equals(pPoint, candidatePoint)) { // Point was already stored. return; - } else { - // Store candidate. - list.add(candidate); - return; } } + // Store candidate. + list.add(candidate); + // Sort the list when required + if (list.size() == max) { + Collections.sort(list, comp); + } } else { final int last = max - 1; if (comp.compare(candidate, list.get(last)) < 0) {