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 2012/10/13 03:09:02 UTC

svn commit: r1397758 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization: InitialGuess.java OptimizationData.java direct/BaseAbstractMultivariateOptimizer.java

Author: erans
Date: Sat Oct 13 01:09:02 2012
New Revision: 1397758

URL: http://svn.apache.org/viewvc?rev=1397758&view=rev
Log:
MATH-874
New "optimize" method.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java   (with props)
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java   (with props)
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java?rev=1397758&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java Sat Oct 13 01:09:02 2012
@@ -0,0 +1,47 @@
+/*
+ * 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.math3.optimization;
+
+/**
+ * Starting point (first guess) of the optimization procedure.
+ * <br/>
+ * Immutable class.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class InitialGuess implements OptimizationData {
+    /** Initial guess. */
+    private final double[] init;
+
+    /**
+     * @param startPoint Initial guess.
+     */
+    public InitialGuess(double[] startPoint) {
+        init = startPoint.clone();
+    }
+
+    /**
+     * Gets the initial guess.
+     *
+     * @return the initial guess.
+     */
+    public double[] getInitialGuess() {
+        return init.clone();
+    }
+}

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

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java?rev=1397758&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java Sat Oct 13 01:09:02 2012
@@ -0,0 +1,29 @@
+/*
+ * 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.math3.optimization;
+
+/**
+ * Marker interface.
+ * Implementations will provide functionality (optional or required) needed
+ * by the optimizers, and those will need to check the actual type of the
+ * arguments and perform the appropriate cast in order to access the data
+ * they need.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public interface OptimizationData {}

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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java?rev=1397758&r1=1397757&r2=1397758&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.java Sat Oct 13 01:09:02 2012
@@ -20,10 +20,11 @@ package org.apache.commons.math3.optimiz
 import org.apache.commons.math3.util.Incrementor;
 import org.apache.commons.math3.exception.MaxCountExceededException;
 import org.apache.commons.math3.exception.TooManyEvaluationsException;
-import org.apache.commons.math3.exception.NullArgumentException;
 import org.apache.commons.math3.analysis.MultivariateFunction;
 import org.apache.commons.math3.optimization.BaseMultivariateOptimizer;
+import org.apache.commons.math3.optimization.OptimizationData;
 import org.apache.commons.math3.optimization.GoalType;
+import org.apache.commons.math3.optimization.InitialGuess;
 import org.apache.commons.math3.optimization.ConvergenceChecker;
 import org.apache.commons.math3.optimization.PointValuePair;
 import org.apache.commons.math3.optimization.SimpleValueChecker;
@@ -99,15 +100,41 @@ public abstract class BaseAbstractMultiv
         return function.value(point);
     }
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     *
+     * @deprecated As of 3.1. Please use
+     * {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])}
+     * instead.
+     */
     public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
-                                       double[] startPoint) {
+                                   double[] startPoint) {
         return optimizeInternal(maxEval, f, goalType, startPoint);
     }
 
     /**
      * Optimize an objective function.
      *
+     * @param maxEval Allowed number of evaluations of the objective function.
+     * @param f Objective function.
+     * @param goalType Optimization type.
+     * @param optData Optimization data. The following data will be looked for:
+     * <ul>
+     *  <li>{@link InitialGuess}</li>
+     * </ul>
+     * @return the point/value pair giving the optimal value of the objective
+     * function.
+     */
+    public PointValuePair optimize(int maxEval,
+                                   FUNC f,
+                                   GoalType goalType,
+                                   OptimizationData... optData) {
+        return optimizeInternal(maxEval, f, goalType, optData);
+    }
+
+    /**
+     * Optimize an objective function.
+     *
      * @param f Objective function.
      * @param goalType Type of optimization goal: either
      * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
@@ -121,34 +148,67 @@ public abstract class BaseAbstractMultiv
      * if the maximal number of evaluations is exceeded.
      * @throws org.apache.commons.math3.exception.NullArgumentException if
      * any argument is {@code null}.
+     * @deprecated As of 3.1. Please use
+     * {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])}
+     * instead.
      */
+    @Deprecated
     protected PointValuePair optimizeInternal(int maxEval, MultivariateFunction f, GoalType goalType,
                                               double[] startPoint) {
-        // Checks.
-        if (f == null) {
-            throw new NullArgumentException();
-        }
-        if (goalType == null) {
-            throw new NullArgumentException();
-        }
-        if (startPoint == null) {
-            throw new NullArgumentException();
-        }
+        return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
+    }
 
-        // Reset.
+    /**
+     * Optimize an objective function.
+     *
+     * @param maxEval Allowed number of evaluations of the objective function.
+     * @param f Objective function.
+     * @param goalType Optimization type.
+     * @param optData Optimization data. The following data will be looked for:
+     * <ul>
+     *  <li>{@link InitialGuess}</li>
+     * </ul>
+     * @return the point/value pair giving the optimal value of the objective
+     * function.
+     * @throws TooManyEvaluationsException if the maximal number of
+     * evaluations is exceeded.
+     */
+    protected PointValuePair optimizeInternal(int maxEval,
+                                              MultivariateFunction f,
+                                              GoalType goalType,
+                                              OptimizationData... optData)
+        throws TooManyEvaluationsException {
         evaluations.setMaximalCount(maxEval);
         evaluations.resetCount();
-
-        // Store optimization problem characteristics.
         function = f;
         goal = goalType;
-        start = startPoint.clone();
+        parseOptimizationData(optData);
 
         // Perform computation.
         return doOptimize();
     }
 
     /**
+     * Scans the list of (required and optional) optimization data that
+     * characterize the problem.
+     *
+     * @param optData Optimization data. The following data will be looked for:
+     * <ul>
+     *  <li>{@link InitialGuess}</li>
+     * </ul>
+     */
+    private void parseOptimizationData(OptimizationData... optData) {
+        // The existing values (as set by the previous call) are reused if
+        // not provided in the argument list.
+        for (OptimizationData data : optData) {
+            if (data instanceof InitialGuess) {
+                start = ((InitialGuess) data).getInitialGuess();
+                continue;
+            }
+        }
+    }
+
+    /**
      * @return the optimization type.
      */
     public GoalType getGoalType() {
@@ -165,7 +225,7 @@ public abstract class BaseAbstractMultiv
     /**
      * Perform the bulk of the optimization algorithm.
      *
-     * @return the point/value pair giving the optimal value for the
+     * @return the point/value pair giving the optimal value of the
      * objective function.
      */
     protected abstract PointValuePair doOptimize();