You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2013/08/19 00:55:32 UTC

svn commit: r1515238 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/fitting/leastsquares/ main/java/org/apache/commons/math3/optim/ test/java/org/apache/commons/math3/fitting/leastsquares/

Author: erans
Date: Sun Aug 18 22:55:31 2013
New Revision: 1515238

URL: http://svn.apache.org/r1515238
Log:
MATH-1008
Added "shallowCopy" method.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/AbstractOptimizer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizer.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizer.java Sun Aug 18 22:55:31 2013
@@ -69,6 +69,8 @@ public abstract class AbstractLeastSquar
      * @param other Instance to copy.
      */
     protected AbstractLeastSquaresOptimizer(AbstractLeastSquaresOptimizer other) {
+        super(other);
+
         target = other.target == null ? null : other.target.clone();
         start = other.start == null ? null : other.start.clone();
         weight = other.weight == null ? null : other.weight.copy();

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizer.java Sun Aug 18 22:55:31 2013
@@ -49,6 +49,22 @@ public class GaussNewtonOptimizer extend
     private boolean useLU = true;
 
     /**
+     * Default constructor.
+     */
+    protected GaussNewtonOptimizer() {}
+
+    /**
+     * Copy constructor.
+     *
+     * @param other object to copy.
+     */
+    protected GaussNewtonOptimizer(GaussNewtonOptimizer other) {
+        super(other);
+
+        this.useLU = other.useLU;
+    }
+
+    /**
      * Creates a bare-bones instance.
      * Several calls to {@code withXxx} methods are necessary to obtain
      * an object with all necessary fields set to sensible values.
@@ -62,6 +78,12 @@ public class GaussNewtonOptimizer extend
         return new GaussNewtonOptimizer();
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public GaussNewtonOptimizer shallowCopy() {
+        return new GaussNewtonOptimizer(this);
+    }
+
     /**
      * @param useLU Whether to use LU decomposition.
      * @return this instance.
@@ -71,6 +93,13 @@ public class GaussNewtonOptimizer extend
         return self();
     }
 
+    /**
+     * @return {@code true} if LU decomposition is used.
+     */
+    public boolean getLU() {
+        return useLU;
+    }
+
     /** {@inheritDoc} */
     @Override
     public PointVectorValuePair doOptimize() {

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizer.java Sun Aug 18 22:55:31 2013
@@ -127,6 +127,29 @@ public class LevenbergMarquardtOptimizer
     private double[] lmDir;
 
     /**
+     * Default constructor.
+     */
+    protected LevenbergMarquardtOptimizer() {}
+
+    /**
+     * Copy constructor.
+     *
+     * @param other object to copy.
+     */
+    protected LevenbergMarquardtOptimizer(LevenbergMarquardtOptimizer other) {
+        super(other);
+
+        this.initialStepBoundFactor = other.initialStepBoundFactor;
+        this.costRelativeTolerance = other.costRelativeTolerance;
+        this.parRelativeTolerance = other.parRelativeTolerance;
+        this.orthoTolerance = other.orthoTolerance;
+        this.qrRankingThreshold = other.qrRankingThreshold;
+
+        lmPar = 0; // Re-initialized in "doOptimize".
+        lmDir = null; // Re-initialized in "doOptimize".
+    }
+
+    /**
      * Creates a bare-bones instance.
      * Several calls to {@code withXxx} methods are necessary to obtain
      * an object with all necessary fields set to sensible values.
@@ -146,6 +169,12 @@ public class LevenbergMarquardtOptimizer
         return new LevenbergMarquardtOptimizer();
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public LevenbergMarquardtOptimizer shallowCopy() {
+        return new LevenbergMarquardtOptimizer(this);
+    }
+
     /**
      * @param initialStepBoundFactor Positive input variable used in
      * determining the initial step bound. This bound is set to the

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/AbstractOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/AbstractOptimizer.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/AbstractOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/AbstractOptimizer.java Sun Aug 18 22:55:31 2013
@@ -165,6 +165,15 @@ public abstract class AbstractOptimizer<
     }
 
     /**
+     * Creates a shallow copy of this instance.
+     * Further modifications of the returned object will not modify the
+     * fields in this instance.
+     *
+     * @return a shallow copy.
+     */
+    public abstract OPTIM shallowCopy();
+
+    /**
      * Performs the bulk of the optimization algorithm.
      *
      * @return the point/value pair giving the optimal value of the

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java Sun Aug 18 22:55:31 2013
@@ -57,6 +57,87 @@ public abstract class AbstractLeastSquar
     public abstract int getMaxIterations();
 
     @Test
+    public void testShallowCopy() {
+        final int maxEval1 = 12;
+        final int maxIter1 = 23;
+        final double[] target1 = { 3.4 };
+        final double[] weight1 = { 4.5 };
+        final double[] start1 = { 5.6 };
+        final double factor1 = 6.7;
+        final MultivariateVectorFunction model1 = new MultivariateVectorFunction() {
+                public double[] value(double[] point) {
+                    return new double[] {
+                        factor1 * factor1 * point[0]
+                    };
+                }};
+        final MultivariateMatrixFunction jac1 = new MultivariateMatrixFunction() {
+                    public double[][] value(double[] point) {
+                        return new double[][] {
+                            { 2 * factor1 * point[0] }
+                        };
+                    }
+                };
+
+
+        final T optim1 = createOptimizer()
+            .withMaxEvaluations(maxEval1)
+            .withMaxIterations(maxIter1)
+            .withTarget(target1)
+            .withWeight(new DiagonalMatrix(weight1))
+            .withStartPoint(start1)
+            .withModelAndJacobian(model1, jac1);
+
+        final T optim2 = optim1.shallowCopy();
+
+        // Check that all fields have the same values.
+        Assert.assertTrue(optim1.getMaxEvaluations() == optim2.getMaxEvaluations());
+        Assert.assertTrue(optim1.getMaxIterations() == optim2.getMaxIterations());
+        Assert.assertTrue(optim1.getTarget()[0] == optim2.getTarget()[0]);
+        Assert.assertTrue(optim1.getWeight().getEntry(0, 0) == optim2.getWeight().getEntry(0, 0));
+        Assert.assertTrue(optim1.getStart()[0] == optim2.getStart()[0]);
+        Assert.assertTrue(optim1.getModel().value(new double[] {32})[0] == optim2.getModel().value(new double[] {32})[0]);
+        Assert.assertTrue(optim1.getJacobian().value(new double[] {54})[0][0] == optim2.getJacobian().value(new double[] {54})[0][0]);
+
+        // Change "optim2".
+        final int maxEval2 = 122;
+        final int maxIter2 = 232;
+        final double[] target2 = { 3.42 };
+        final double[] weight2 = { 4.52 };
+        final double[] start2 = { 5.62 };
+        final double factor2 = 6.72;
+        final MultivariateVectorFunction model2 = new MultivariateVectorFunction() {
+                public double[] value(double[] point) {
+                    return new double[] {
+                        factor2 * factor2 * point[0]
+                    };
+                }};
+        final MultivariateMatrixFunction jac2 = new MultivariateMatrixFunction() {
+                    public double[][] value(double[] point) {
+                        return new double[][] {
+                            { 2 * factor2 * point[0] }
+                        };
+                    }
+                };
+
+        optim2
+            .withMaxEvaluations(maxEval2)
+            .withMaxIterations(maxIter2)
+            .withTarget(target2)
+            .withWeight(new DiagonalMatrix(weight2))
+            .withStartPoint(start2)
+            .withModelAndJacobian(model2, jac2);
+
+        // Check that all fields now have different values.
+        Assert.assertFalse(optim1.getMaxEvaluations() == optim2.getMaxEvaluations());
+        Assert.assertFalse(optim1.getMaxIterations() == optim2.getMaxIterations());
+        Assert.assertFalse(optim1.getTarget()[0] == optim2.getTarget()[0]);
+        Assert.assertFalse(optim1.getWeight().getEntry(0, 0) == optim2.getWeight().getEntry(0, 0));
+        Assert.assertFalse(optim1.getStart()[0] == optim2.getStart()[0]);
+        Assert.assertFalse(optim1.getModel().value(new double[] {32})[0] == optim2.getModel().value(new double[] {32})[0]);
+        Assert.assertFalse(optim1.getJacobian().value(new double[] {54})[0][0] == optim2.getJacobian().value(new double[] {54})[0][0]);
+    }
+
+    @Test
     public void testGetIterations() {
         T optim = createOptimizer()
             .withMaxEvaluations(100)

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/GaussNewtonOptimizerTest.java Sun Aug 18 22:55:31 2013
@@ -24,6 +24,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.optim.SimpleVectorValueChecker;
 import org.apache.commons.math3.linear.DiagonalMatrix;
 import org.junit.Test;
+import org.junit.Assert;
 
 /**
  * <p>Some of the unit tests are re-implementations of the MINPACK <a
@@ -48,6 +49,28 @@ public class GaussNewtonOptimizerTest
     }
 
     @Override
+    @Test
+    public void testShallowCopy() {
+        super.testShallowCopy(); // Test copy of parent.
+
+        final boolean useLU1 = false;
+        final GaussNewtonOptimizer optim1 = createOptimizer()
+            .withLU(useLU1);
+
+        final GaussNewtonOptimizer optim2 = optim1.shallowCopy();
+
+        // Check that all fields have the same values.
+        Assert.assertTrue(optim1.getLU() == optim2.getLU());
+
+        // Change "optim2".
+        final boolean useLU2 = true;
+        optim2.withLU(useLU2);
+
+        // Check that all fields now have different values.
+        Assert.assertFalse(optim1.getLU() == optim2.getLU());
+    }
+
+    @Override
     @Test(expected=ConvergenceException.class)
     public void testMoreEstimatedParametersSimple() {
         /*

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java?rev=1515238&r1=1515237&r2=1515238&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java Sun Aug 18 22:55:31 2013
@@ -58,6 +58,53 @@ public class LevenbergMarquardtOptimizer
     }
 
     @Override
+    @Test
+    public void testShallowCopy() {
+        super.testShallowCopy(); // Test copy of parent.
+
+        final double initStep1 = 1e-1;
+        final double costTol1 = 1e-1;
+        final double parTol1 = 1e-1;
+        final double orthoTol1 = 1e-1;
+        final double threshold1 = 1e-1;
+        final LevenbergMarquardtOptimizer optim1 = createOptimizer()
+            .withInitialStepBoundFactor(initStep1)
+            .withCostRelativeTolerance(costTol1)
+            .withParameterRelativeTolerance(parTol1)
+            .withOrthoTolerance(orthoTol1)
+            .withRankingThreshold(threshold1);
+
+        final LevenbergMarquardtOptimizer optim2 = optim1.shallowCopy();
+
+        // Check that all fields have the same values.
+        Assert.assertTrue(optim1.getInitialStepBoundFactor() == optim2.getInitialStepBoundFactor());
+        Assert.assertTrue(optim1.getCostRelativeTolerance() == optim2.getCostRelativeTolerance());
+        Assert.assertTrue(optim1.getParameterRelativeTolerance() == optim2.getParameterRelativeTolerance());
+        Assert.assertTrue(optim1.getOrthoTolerance() == optim2.getOrthoTolerance());
+        Assert.assertTrue(optim1.getRankingThreshold() == optim2.getRankingThreshold());
+
+        // Change "optim2".
+        final double initStep2 = 2e-1;
+        final double costTol2 = 2e-1;
+        final double parTol2 = 2e-1;
+        final double orthoTol2 = 2e-1;
+        final double threshold2 = 2e-1;
+        optim2
+            .withInitialStepBoundFactor(initStep2)
+            .withCostRelativeTolerance(costTol2)
+            .withParameterRelativeTolerance(parTol2)
+            .withOrthoTolerance(orthoTol2)
+            .withRankingThreshold(threshold2);
+
+        // Check that all fields now have different values.
+        Assert.assertFalse(optim1.getInitialStepBoundFactor() == optim2.getInitialStepBoundFactor());
+        Assert.assertFalse(optim1.getCostRelativeTolerance() == optim2.getCostRelativeTolerance());
+        Assert.assertFalse(optim1.getParameterRelativeTolerance() == optim2.getParameterRelativeTolerance());
+        Assert.assertFalse(optim1.getOrthoTolerance() == optim2.getOrthoTolerance());
+        Assert.assertFalse(optim1.getRankingThreshold() == optim2.getRankingThreshold());
+    }
+
+    @Override
     @Test(expected=SingularMatrixException.class)
     public void testNonInvertible() {
         /*