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 2011/10/28 22:44:09 UTC

svn commit: r1190556 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization: ./ direct/ general/

Author: erans
Date: Fri Oct 28 20:44:09 2011
New Revision: 1190556

URL: http://svn.apache.org/viewvc?rev=1190556&view=rev
Log:
MATH-697
Added "optimize" method to allow passing simple bounds.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BaseAbstractScalarOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java?rev=1190556&r1=1190555&r2=1190556&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultiStartMultivariateRealOptimizer.java Fri Oct 28 20:44:09 2011
@@ -137,6 +137,16 @@ public class BaseMultiStartMultivariateR
     public RealPointValuePair optimize(int maxEval, final FUNC f,
                                        final GoalType goal,
                                        double[] startPoint) {
+        return optimize(maxEval, f, goal, startPoint, null, null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public RealPointValuePair optimize(int maxEval, final FUNC f,
+                                       final GoalType goal,
+                                       double[] startPoint,
+                                       double[] lowerBound, double[] upperBound) {
         maxEvaluations = maxEval;
         RuntimeException lastException = null;
         optima = new RealPointValuePair[starts];
@@ -147,7 +157,8 @@ public class BaseMultiStartMultivariateR
             // CHECKSTYLE: stop IllegalCatch
             try {
                 optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
-                                               i == 0 ? startPoint : generator.nextVector());
+                                               i == 0 ? startPoint : generator.nextVector(),
+                                               lowerBound, upperBound);
             } catch (RuntimeException mue) {
                 lastException = mue;
                 optima[i] = null;

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java?rev=1190556&r1=1190555&r2=1190556&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/BaseMultivariateRealOptimizer.java Fri Oct 28 20:44:09 2011
@@ -54,4 +54,27 @@ public interface BaseMultivariateRealOpt
      */
     RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
                                 double[] startPoint);
+
+    /**
+     * Optimize an objective function.
+     *
+     * @param f Objective function.
+     * @param goalType Type of optimization goal: either
+     * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
+     * @param startPoint Start point for optimization.
+     * @param maxEval Maximum number of function evaluations.
+     * @param lowerBound Lower bound for each of the parameters.
+     * @param upperBound Upper bound for each of the parameters.
+     * @return the point/value pair giving the optimal value for objective
+     * function.
+     * @throws org.apache.commons.math.exception.DimensionMismatchException
+     * if the array sizes are wrong.
+     * @throws org.apache.commons.math.exception.TooManyEvaluationsException
+     * if the maximal number of evaluations is exceeded.
+     * @throws org.apache.commons.math.exception.NullArgumentException if
+     * {@code f}, {@code goalType} or {@code startPoint} is {@code null}.
+     */
+    RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
+                                double[] startPoint,
+                                double[] lowerBound, double[] upperBound);
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BaseAbstractScalarOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BaseAbstractScalarOptimizer.java?rev=1190556&r1=1190555&r2=1190556&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BaseAbstractScalarOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BaseAbstractScalarOptimizer.java Fri Oct 28 20:44:09 2011
@@ -48,6 +48,10 @@ public abstract class BaseAbstractScalar
     private GoalType goal;
     /** Initial guess. */
     private double[] start;
+    /** Lower bounds. */
+    private double[] lowerBound;
+    /** Upper bounds. */
+    private double[] upperBound;
     /** Objective function. */
     private MultivariateRealFunction function;
 
@@ -101,6 +105,13 @@ public abstract class BaseAbstractScalar
     /** {@inheritDoc} */
     public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
                                        double[] startPoint) {
+        return optimize(maxEval, f, goalType, startPoint, null, null);
+    }
+
+    /** {@inheritDoc} */
+    public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
+                                       double[] startPoint,
+                                       double[] lower, double[] upper) {
         // Checks.
         if (f == null) {
             throw new NullArgumentException();
@@ -120,6 +131,23 @@ public abstract class BaseAbstractScalar
         function = f;
         goal = goalType;
         start = startPoint.clone();
+        final int dim = startPoint.length;
+        if (lower == null) {
+            lowerBound = new double[dim];
+            for (int i = 0; i < dim; i++) {
+                lowerBound[i] = Double.NEGATIVE_INFINITY;
+            }
+        } else {
+            lowerBound = lower.clone();
+        }
+        if (upper == null) {
+            upperBound = new double[dim];
+            for (int i = 0; i < dim; i++) {
+                upperBound[i] = Double.POSITIVE_INFINITY;
+            }
+        } else {
+            upperBound = upper.clone();
+        }
 
         // Perform computation.
         return doOptimize();
@@ -140,6 +168,20 @@ public abstract class BaseAbstractScalar
     }
 
     /**
+     * @return the lower bounds.
+     */
+    public double[] getLowerBound() {
+        return lowerBound.clone();
+    }
+
+    /**
+     * @return the upper bounds.
+     */
+    public double[] getUpperBound() {
+        return upperBound.clone();
+    }
+
+    /**
      * Perform the bulk of the optimization algorithm.
      *
      * @return the point/value pair giving the optimal value for the

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java?rev=1190556&r1=1190555&r2=1190556&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/AbstractScalarDifferentiableOptimizer.java Fri Oct 28 20:44:09 2011
@@ -73,9 +73,21 @@ public abstract class AbstractScalarDiff
                                        final DifferentiableMultivariateRealFunction f,
                                        final GoalType goalType,
                                        final double[] startPoint) {
+        return optimize(maxEval, f, goalType, startPoint, null, null);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public RealPointValuePair optimize(int maxEval,
+                                       final DifferentiableMultivariateRealFunction f,
+                                       final GoalType goalType,
+                                       final double[] startPoint,
+                                       double[] lowerBound, double[] upperBound) {
         // Store optimization problem characteristics.
         gradient = f.gradient();
 
-        return super.optimize(maxEval, f, goalType, startPoint);
+        return super.optimize(maxEval, f, goalType,
+                              startPoint,
+                              lowerBound, upperBound);
     }
 }