PowellOptimizer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math3.optim.nonlinear.scalar.noderiv;

  18. import org.apache.commons.math3.util.FastMath;
  19. import org.apache.commons.math3.util.MathArrays;
  20. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  21. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  22. import org.apache.commons.math3.exception.MathUnsupportedOperationException;
  23. import org.apache.commons.math3.exception.util.LocalizedFormats;
  24. import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
  25. import org.apache.commons.math3.optim.PointValuePair;
  26. import org.apache.commons.math3.optim.ConvergenceChecker;
  27. import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer;
  28. import org.apache.commons.math3.optim.nonlinear.scalar.LineSearch;
  29. import org.apache.commons.math3.optim.univariate.UnivariatePointValuePair;

  30. /**
  31.  * Powell's algorithm.
  32.  * This code is translated and adapted from the Python version of this
  33.  * algorithm (as implemented in module {@code optimize.py} v0.5 of
  34.  * <em>SciPy</em>).
  35.  * <br/>
  36.  * The default stopping criterion is based on the differences of the
  37.  * function value between two successive iterations. It is however possible
  38.  * to define a custom convergence checker that might terminate the algorithm
  39.  * earlier.
  40.  * <br/>
  41.  * Line search is performed by the {@link LineSearch} class.
  42.  * <br/>
  43.  * Constraints are not supported: the call to
  44.  * {@link #optimize(OptimizationData[]) optimize} will throw
  45.  * {@link MathUnsupportedOperationException} if bounds are passed to it.
  46.  * In order to impose simple constraints, the objective function must be
  47.  * wrapped in an adapter like
  48.  * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionMappingAdapter
  49.  * MultivariateFunctionMappingAdapter} or
  50.  * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionPenaltyAdapter
  51.  * MultivariateFunctionPenaltyAdapter}.
  52.  *
  53.  * @since 2.2
  54.  */
  55. public class PowellOptimizer
  56.     extends MultivariateOptimizer {
  57.     /**
  58.      * Minimum relative tolerance.
  59.      */
  60.     private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d);
  61.     /**
  62.      * Relative threshold.
  63.      */
  64.     private final double relativeThreshold;
  65.     /**
  66.      * Absolute threshold.
  67.      */
  68.     private final double absoluteThreshold;
  69.     /**
  70.      * Line search.
  71.      */
  72.     private final LineSearch line;

  73.     /**
  74.      * This constructor allows to specify a user-defined convergence checker,
  75.      * in addition to the parameters that control the default convergence
  76.      * checking procedure.
  77.      * <br/>
  78.      * The internal line search tolerances are set to the square-root of their
  79.      * corresponding value in the multivariate optimizer.
  80.      *
  81.      * @param rel Relative threshold.
  82.      * @param abs Absolute threshold.
  83.      * @param checker Convergence checker.
  84.      * @throws NotStrictlyPositiveException if {@code abs <= 0}.
  85.      * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
  86.      */
  87.     public PowellOptimizer(double rel,
  88.                            double abs,
  89.                            ConvergenceChecker<PointValuePair> checker) {
  90.         this(rel, abs, FastMath.sqrt(rel), FastMath.sqrt(abs), checker);
  91.     }

  92.     /**
  93.      * This constructor allows to specify a user-defined convergence checker,
  94.      * in addition to the parameters that control the default convergence
  95.      * checking procedure and the line search tolerances.
  96.      *
  97.      * @param rel Relative threshold for this optimizer.
  98.      * @param abs Absolute threshold for this optimizer.
  99.      * @param lineRel Relative threshold for the internal line search optimizer.
  100.      * @param lineAbs Absolute threshold for the internal line search optimizer.
  101.      * @param checker Convergence checker.
  102.      * @throws NotStrictlyPositiveException if {@code abs <= 0}.
  103.      * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
  104.      */
  105.     public PowellOptimizer(double rel,
  106.                            double abs,
  107.                            double lineRel,
  108.                            double lineAbs,
  109.                            ConvergenceChecker<PointValuePair> checker) {
  110.         super(checker);

  111.         if (rel < MIN_RELATIVE_TOLERANCE) {
  112.             throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true);
  113.         }
  114.         if (abs <= 0) {
  115.             throw new NotStrictlyPositiveException(abs);
  116.         }
  117.         relativeThreshold = rel;
  118.         absoluteThreshold = abs;

  119.         // Create the line search optimizer.
  120.         line = new LineSearch(this,
  121.                               lineRel,
  122.                               lineAbs,
  123.                               1d);
  124.     }

  125.     /**
  126.      * The parameters control the default convergence checking procedure.
  127.      * <br/>
  128.      * The internal line search tolerances are set to the square-root of their
  129.      * corresponding value in the multivariate optimizer.
  130.      *
  131.      * @param rel Relative threshold.
  132.      * @param abs Absolute threshold.
  133.      * @throws NotStrictlyPositiveException if {@code abs <= 0}.
  134.      * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
  135.      */
  136.     public PowellOptimizer(double rel,
  137.                            double abs) {
  138.         this(rel, abs, null);
  139.     }

  140.     /**
  141.      * Builds an instance with the default convergence checking procedure.
  142.      *
  143.      * @param rel Relative threshold.
  144.      * @param abs Absolute threshold.
  145.      * @param lineRel Relative threshold for the internal line search optimizer.
  146.      * @param lineAbs Absolute threshold for the internal line search optimizer.
  147.      * @throws NotStrictlyPositiveException if {@code abs <= 0}.
  148.      * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
  149.      */
  150.     public PowellOptimizer(double rel,
  151.                            double abs,
  152.                            double lineRel,
  153.                            double lineAbs) {
  154.         this(rel, abs, lineRel, lineAbs, null);
  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     protected PointValuePair doOptimize() {
  159.         checkParameters();

  160.         final GoalType goal = getGoalType();
  161.         final double[] guess = getStartPoint();
  162.         final int n = guess.length;

  163.         final double[][] direc = new double[n][n];
  164.         for (int i = 0; i < n; i++) {
  165.             direc[i][i] = 1;
  166.         }

  167.         final ConvergenceChecker<PointValuePair> checker
  168.             = getConvergenceChecker();

  169.         double[] x = guess;
  170.         double fVal = computeObjectiveValue(x);
  171.         double[] x1 = x.clone();
  172.         while (true) {
  173.             incrementIterationCount();

  174.             double fX = fVal;
  175.             double fX2 = 0;
  176.             double delta = 0;
  177.             int bigInd = 0;
  178.             double alphaMin = 0;

  179.             for (int i = 0; i < n; i++) {
  180.                 final double[] d = MathArrays.copyOf(direc[i]);

  181.                 fX2 = fVal;

  182.                 final UnivariatePointValuePair optimum = line.search(x, d);
  183.                 fVal = optimum.getValue();
  184.                 alphaMin = optimum.getPoint();
  185.                 final double[][] result = newPointAndDirection(x, d, alphaMin);
  186.                 x = result[0];

  187.                 if ((fX2 - fVal) > delta) {
  188.                     delta = fX2 - fVal;
  189.                     bigInd = i;
  190.                 }
  191.             }

  192.             // Default convergence check.
  193.             boolean stop = 2 * (fX - fVal) <=
  194.                 (relativeThreshold * (FastMath.abs(fX) + FastMath.abs(fVal)) +
  195.                  absoluteThreshold);

  196.             final PointValuePair previous = new PointValuePair(x1, fX);
  197.             final PointValuePair current = new PointValuePair(x, fVal);
  198.             if (!stop && checker != null) { // User-defined stopping criteria.
  199.                 stop = checker.converged(getIterations(), previous, current);
  200.             }
  201.             if (stop) {
  202.                 if (goal == GoalType.MINIMIZE) {
  203.                     return (fVal < fX) ? current : previous;
  204.                 } else {
  205.                     return (fVal > fX) ? current : previous;
  206.                 }
  207.             }

  208.             final double[] d = new double[n];
  209.             final double[] x2 = new double[n];
  210.             for (int i = 0; i < n; i++) {
  211.                 d[i] = x[i] - x1[i];
  212.                 x2[i] = 2 * x[i] - x1[i];
  213.             }

  214.             x1 = x.clone();
  215.             fX2 = computeObjectiveValue(x2);

  216.             if (fX > fX2) {
  217.                 double t = 2 * (fX + fX2 - 2 * fVal);
  218.                 double temp = fX - fVal - delta;
  219.                 t *= temp * temp;
  220.                 temp = fX - fX2;
  221.                 t -= delta * temp * temp;

  222.                 if (t < 0.0) {
  223.                     final UnivariatePointValuePair optimum = line.search(x, d);
  224.                     fVal = optimum.getValue();
  225.                     alphaMin = optimum.getPoint();
  226.                     final double[][] result = newPointAndDirection(x, d, alphaMin);
  227.                     x = result[0];

  228.                     final int lastInd = n - 1;
  229.                     direc[bigInd] = direc[lastInd];
  230.                     direc[lastInd] = result[1];
  231.                 }
  232.             }
  233.         }
  234.     }

  235.     /**
  236.      * Compute a new point (in the original space) and a new direction
  237.      * vector, resulting from the line search.
  238.      *
  239.      * @param p Point used in the line search.
  240.      * @param d Direction used in the line search.
  241.      * @param optimum Optimum found by the line search.
  242.      * @return a 2-element array containing the new point (at index 0) and
  243.      * the new direction (at index 1).
  244.      */
  245.     private double[][] newPointAndDirection(double[] p,
  246.                                             double[] d,
  247.                                             double optimum) {
  248.         final int n = p.length;
  249.         final double[] nP = new double[n];
  250.         final double[] nD = new double[n];
  251.         for (int i = 0; i < n; i++) {
  252.             nD[i] = d[i] * optimum;
  253.             nP[i] = p[i] + nD[i];
  254.         }

  255.         final double[][] result = new double[2][];
  256.         result[0] = nP;
  257.         result[1] = nD;

  258.         return result;
  259.     }

  260.     /**
  261.      * @throws MathUnsupportedOperationException if bounds were passed to the
  262.      * {@link #optimize(OptimizationData[]) optimize} method.
  263.      */
  264.     private void checkParameters() {
  265.         if (getLowerBound() != null ||
  266.             getUpperBound() != null) {
  267.             throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
  268.         }
  269.     }
  270. }