You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/03/24 23:12:33 UTC

svn commit: r758053 - in /commons/proper/math/trunk/src/java/org/apache/commons/math/optimization: RealConvergenceChecker.java SimpleRealPointChecker.java SimpleVectorialPointChecker.java

Author: luc
Date: Tue Mar 24 22:12:32 2009
New Revision: 758053

URL: http://svn.apache.org/viewvc?rev=758053&view=rev
Log:
added new simple convergence checkers implementations

Added:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java   (with props)
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java   (with props)
Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/RealConvergenceChecker.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/RealConvergenceChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/RealConvergenceChecker.java?rev=758053&r1=758052&r2=758053&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/RealConvergenceChecker.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/RealConvergenceChecker.java Tue Mar 24 22:12:32 2009
@@ -26,7 +26,7 @@
  * user should provide a class implementing this interface to allow the optimization
  * algorithm to stop its search according to the problem at hand.</p>
  * <p>For convenience, two implementations that fit simple needs are already provided:
- * {@link SimpleScalarValueChecker} and {@link SimpleScalarPointChecker}. The first
+ * {@link SimpleScalarValueChecker} and {@link SimpleRealPointChecker}. The first
  * one considers convergence is reached when the objective function value does not
  * change much anymore, it does not use the point set at all. The second one
  * considers convergence is reached when the input point set does not change

Added: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java?rev=758053&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java Tue Mar 24 22:12:32 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.optimization;
+
+import org.apache.commons.math.util.MathUtils;
+
+/** 
+ * Simple implementation of the {@link RealConvergenceChecker} interface using
+ * only point coordinates.
+ * <p>
+ * Convergence is considered to have been reached if either the relative
+ * difference between each point coordinate are smaller than a threshold
+ * or if either the absolute difference between the point coordinates are
+ * smaller than another threshold.
+ * </p>
+ * @version $Revision: 757180 $ $Date: 2009-03-22 13:54:45 +0100 (dim., 22 mars 2009) $
+ * @since 2.0
+ */
+public class SimpleRealPointChecker implements RealConvergenceChecker {
+
+    /** Serializable version identifier. */
+    private static final long serialVersionUID = 2490271385513842607L;
+
+    /** Default relative threshold. */
+    private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
+
+    /** Default absolute threshold. */
+    private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
+
+    /** Relative tolerance threshold. */
+    private final double relativeThreshold;
+
+    /** Absolute tolerance threshold. */
+    private final double absoluteThreshold;
+
+   /** Build an instance with default threshold.
+     */
+    public SimpleRealPointChecker() {
+        this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
+        this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
+    }
+
+    /** Build an instance with a specified threshold.
+     * <p>
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     * </p>
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public SimpleRealPointChecker(final double relativeThreshold,
+                                 final double absoluteThreshold) {
+        this.relativeThreshold = relativeThreshold;
+        this.absoluteThreshold = absoluteThreshold;
+    }
+
+    /** {@inheritDoc} */
+    public boolean converged(final int iteration,
+                             final RealPointValuePair previous,
+                             final RealPointValuePair current) {
+        final double[] p        = previous.getPoint();
+        final double[] c        = current.getPoint();
+        for (int i = 0; i < p.length; ++i) {
+            final double difference = Math.abs(p[i] - c[i]);
+            final double size       = Math.max(Math.abs(p[i]), Math.abs(c[i]));
+            if ((difference > (size * relativeThreshold)) && (difference > absoluteThreshold)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+}

Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleRealPointChecker.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java?rev=758053&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java Tue Mar 24 22:12:32 2009
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.optimization;
+
+import org.apache.commons.math.util.MathUtils;
+
+/** 
+ * Simple implementation of the {@link VectorialConvergenceChecker} interface using
+ * only point coordinates.
+ * <p>
+ * Convergence is considered to have been reached if either the relative
+ * difference between each point coordinate are smaller than a threshold
+ * or if either the absolute difference between the point coordinates are
+ * smaller than another threshold.
+ * </p>
+ * @version $Revision$ $Date$
+ * @since 2.0
+ */
+public class SimpleVectorialPointChecker implements VectorialConvergenceChecker {
+
+    /** Serializable version identifier. */
+    private static final long serialVersionUID = 6571098732056285089L;
+
+    /** Default relative threshold. */
+    private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
+
+    /** Default absolute threshold. */
+    private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
+
+    /** Relative tolerance threshold. */
+    private final double relativeThreshold;
+
+    /** Absolute tolerance threshold. */
+    private final double absoluteThreshold;
+
+   /** Build an instance with default threshold.
+     */
+    public SimpleVectorialPointChecker() {
+        this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
+        this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
+    }
+
+    /** Build an instance with a specified threshold.
+     * <p>
+     * In order to perform only relative checks, the absolute tolerance
+     * must be set to a negative value. In order to perform only absolute
+     * checks, the relative tolerance must be set to a negative value.
+     * </p>
+     * @param relativeThreshold relative tolerance threshold
+     * @param absoluteThreshold absolute tolerance threshold
+     */
+    public SimpleVectorialPointChecker(final double relativeThreshold,
+                                       final double absoluteThreshold) {
+        this.relativeThreshold = relativeThreshold;
+        this.absoluteThreshold = absoluteThreshold;
+    }
+
+    /** {@inheritDoc} */
+    public boolean converged(final int iteration,
+                             final VectorialPointValuePair previous,
+                             final VectorialPointValuePair current) {
+        final double[] p = previous.getPointRef();
+        final double[] c = current.getPointRef();
+        for (int i = 0; i < p.length; ++i) {
+            final double pi         = p[i];
+            final double ci         = c[i];
+            final double difference = Math.abs(pi - ci);
+            final double size       = Math.max(Math.abs(pi), Math.abs(ci));
+            if ((difference > (size * relativeThreshold)) &&
+                (difference > absoluteThreshold)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+}

Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/SimpleVectorialPointChecker.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision