You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/12/03 19:12:10 UTC

[commons-math] 01/03: Compare candidate to all list points

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit a97523700aca6679244607c29f6f7841f2cdd56d
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sat Oct 22 23:24:24 2022 +0100

    Compare candidate to all list points
---
 .../optim/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}.
      *
+     * <p>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) {