[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    [1/2] [math] Use new incrementor implementation (cf. MATH-1259).
From:       erans () apache ! org
Date:       2015-08-30 16:21:31
Message-ID: 71769212499e46deb7c3f10439875e80 () git ! apache ! org
[Download RAW message or body]

Repository: commons-math
Updated Branches:
  refs/heads/master d17cabe29 -> 46e97d9e0


Use new incrementor implementation (cf. MATH-1259).


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/47fa07ea
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/47fa07ea
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/47fa07ea

Branch: refs/heads/master
Commit: 47fa07eade06908ded4206651571e71a2fe684a6
Parents: d17cabe
Author: Gilles <erans@apache.org>
Authored: Sun Aug 30 18:08:31 2015 +0200
Committer: Gilles <erans@apache.org>
Committed: Sun Aug 30 18:08:31 2015 +0200

----------------------------------------------------------------------
 .../math4/optim/univariate/BracketFinder.java   | 78 +++++++++++++-------
 1 file changed, 51 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/47fa07ea/src/main/java/org/apache/commons/math4/optim/univariate/BracketFinder.java
                
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/optim/univariate/BracketFinder.java \
b/src/main/java/org/apache/commons/math4/optim/univariate/BracketFinder.java index \
                cf05237..d2890b9 100644
--- a/src/main/java/org/apache/commons/math4/optim/univariate/BracketFinder.java
+++ b/src/main/java/org/apache/commons/math4/optim/univariate/BracketFinder.java
@@ -22,7 +22,7 @@ import \
org.apache.commons.math4.exception.NotStrictlyPositiveException;  import \
org.apache.commons.math4.exception.TooManyEvaluationsException;  import \
org.apache.commons.math4.optim.nonlinear.scalar.GoalType;  import \
                org.apache.commons.math4.util.FastMath;
-import org.apache.commons.math4.util.Incrementor;
+import org.apache.commons.math4.util.IntegerSequence.Incrementor;
 
 /**
  * Provide an interval that brackets a local optimum of a function.
@@ -43,9 +43,13 @@ public class BracketFinder {
      */
     private final double growLimit;
     /**
-     * Counter for function evaluations.
+     * Number of allowed function evaluations.
      */
-    private final Incrementor evaluations = new Incrementor();
+    private final int maxEvaluations;
+    /**
+     * Number of function evaluations performed in the last search.
+     */
+    private int evaluations;
     /**
      * Lower bound of the bracket.
      */
@@ -96,7 +100,7 @@ public class BracketFinder {
         }
 
         this.growLimit = growLimit;
-        evaluations.setMaximalCount(maxEvaluations);
+        this.maxEvaluations = maxEvaluations;
     }
 
     /**
@@ -113,11 +117,11 @@ public class BracketFinder {
                        GoalType goal,
                        double xA,
                        double xB) {
-        evaluations.resetCount();
+        final FunctionEvaluator eval = new FunctionEvaluator(func);
         final boolean isMinim = goal == GoalType.MINIMIZE;
 
-        double fA = eval(func, xA);
-        double fB = eval(func, xB);
+        double fA = eval.value(xA);
+        double fB = eval.value(xB);
         if (isMinim ?
             fA < fB :
             fA > fB) {
@@ -132,7 +136,7 @@ public class BracketFinder {
         }
 
         double xC = xB + GOLD * (xB - xA);
-        double fC = eval(func, xC);
+        double fC = eval.value(xC);
 
         while (isMinim ? fC < fB : fC > fB) {
             double tmp1 = (xB - xA) * (fB - fC);
@@ -146,7 +150,7 @@ public class BracketFinder {
 
             double fW;
             if ((w - xC) * (xB - w) > 0) {
-                fW = eval(func, w);
+                fW = eval.value(w);
                 if (isMinim ?
                     fW < fC :
                     fW > fC) {
@@ -163,12 +167,12 @@ public class BracketFinder {
                     break;
                 }
                 w = xC + GOLD * (xC - xB);
-                fW = eval(func, w);
+                fW = eval.value(w);
             } else if ((w - wLim) * (wLim - xC) >= 0) {
                 w = wLim;
-                fW = eval(func, w);
+                fW = eval.value(w);
             } else if ((w - wLim) * (xC - w) > 0) {
-                fW = eval(func, w);
+                fW = eval.value(w);
                 if (isMinim ?
                     fW < fC :
                     fW > fC) {
@@ -177,11 +181,11 @@ public class BracketFinder {
                     w = xC + GOLD * (xC - xB);
                     fB = fC;
                     fC =fW;
-                    fW = eval(func, w);
+                    fW = eval.value(w);
                 }
             } else {
                 w = xC + GOLD * (xC - xB);
-                fW = eval(func, w);
+                fW = eval.value(w);
             }
 
             xA = xB;
@@ -214,14 +218,14 @@ public class BracketFinder {
      * @return the number of evalutations.
      */
     public int getMaxEvaluations() {
-        return evaluations.getMaximalCount();
+        return maxEvaluations;
     }
 
     /**
      * @return the number of evalutations.
      */
     public int getEvaluations() {
-        return evaluations.getCount();
+        return evaluations;
     }
 
     /**
@@ -273,18 +277,38 @@ public class BracketFinder {
     }
 
     /**
-     * @param f Function.
-     * @param x Argument.
-     * @return {@code f(x)}
-     * @throws TooManyEvaluationsException if the maximal number of evaluations is
-     * exceeded.
+     * Utility for incrementing a counter at each function evaluation.
      */
-    private double eval(UnivariateFunction f, double x) {
-        try {
-            evaluations.incrementCount();
-        } catch (MaxCountExceededException e) {
-            throw new TooManyEvaluationsException(e.getMax());
+    private class FunctionEvaluator {
+        /** Function. */
+        private final UnivariateFunction func;
+        /** Counter. */
+        private final Incrementor inc;
+
+        /**
+         * @param func Function.
+         */
+        FunctionEvaluator(UnivariateFunction func) {
+            this.func = func;
+            inc = Incrementor.create().withMaximalCount(maxEvaluations);
+            evaluations = 0;
+        }
+
+        /**
+         * @param x Argument.
+         * @return {@code f(x)}
+         * @throws TooManyEvaluationsException if the maximal number of evaluations \
is +         * exceeded.
+         */
+        double value(double x) {
+            try {
+                inc.increment();
+                evaluations = inc.getCount();
+            } catch (MaxCountExceededException e) {
+                throw new TooManyEvaluationsException(e.getMax());
+            }
+
+            return func.value(x);
         }
-        return f.value(x);
     }
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic