Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3811D76C6 for ; Fri, 28 Oct 2011 20:44:32 +0000 (UTC) Received: (qmail 94370 invoked by uid 500); 28 Oct 2011 20:44:32 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 94292 invoked by uid 500); 28 Oct 2011 20:44:31 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 94285 invoked by uid 99); 28 Oct 2011 20:44:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Oct 2011 20:44:31 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Oct 2011 20:44:30 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2330E2388A32 for ; Fri, 28 Oct 2011 20:44:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1190556 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization: ./ direct/ general/ Date: Fri, 28 Oct 2011 20:44:09 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111028204410.2330E2388A32@eris.apache.org> 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); } }