MultivariateFunctionMappingAdapter.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.optimization.direct;

  18. import org.apache.commons.math3.analysis.MultivariateFunction;
  19. import org.apache.commons.math3.analysis.UnivariateFunction;
  20. import org.apache.commons.math3.analysis.function.Logit;
  21. import org.apache.commons.math3.analysis.function.Sigmoid;
  22. import org.apache.commons.math3.exception.DimensionMismatchException;
  23. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  24. import org.apache.commons.math3.util.FastMath;
  25. import org.apache.commons.math3.util.MathUtils;

  26. /**
  27.  * <p>Adapter for mapping bounded {@link MultivariateFunction} to unbounded ones.</p>
  28.  *
  29.  * <p>
  30.  * This adapter can be used to wrap functions subject to simple bounds on
  31.  * parameters so they can be used by optimizers that do <em>not</em> directly
  32.  * support simple bounds.
  33.  * </p>
  34.  * <p>
  35.  * The principle is that the user function that will be wrapped will see its
  36.  * parameters bounded as required, i.e when its {@code value} method is called
  37.  * with argument array {@code point}, the elements array will fulfill requirement
  38.  * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
  39.  * may be unbounded or bounded only on one side if the corresponding bound is
  40.  * set to an infinite value. The optimizer will not manage the user function by
  41.  * itself, but it will handle this adapter and it is this adapter that will take
  42.  * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
  43.  * be called by the optimizer with unbound parameters, and the adapter will map
  44.  * the unbounded value to the bounded range using appropriate functions like
  45.  * {@link Sigmoid} for double bounded elements for example.
  46.  * </p>
  47.  * <p>
  48.  * As the optimizer sees only unbounded parameters, it should be noted that the
  49.  * start point or simplex expected by the optimizer should be unbounded, so the
  50.  * user is responsible for converting his bounded point to unbounded by calling
  51.  * {@link #boundedToUnbounded(double[])} before providing them to the optimizer.
  52.  * For the same reason, the point returned by the {@link
  53.  * org.apache.commons.math3.optimization.BaseMultivariateOptimizer#optimize(int,
  54.  * MultivariateFunction, org.apache.commons.math3.optimization.GoalType, double[])}
  55.  * method is unbounded. So to convert this point to bounded, users must call
  56.  * {@link #unboundedToBounded(double[])} by themselves!</p>
  57.  * <p>
  58.  * This adapter is only a poor man solution to simple bounds optimization constraints
  59.  * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link
  60.  * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use
  61.  * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or
  62.  * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that behavior near
  63.  * the bounds may be numerically unstable as bounds are mapped from infinite values.
  64.  * Another caveat is that convergence values are evaluated by the optimizer with respect
  65.  * to unbounded variables, so there will be scales differences when converted to bounded
  66.  * variables.
  67.  * </p>
  68.  *
  69.  * @see MultivariateFunctionPenaltyAdapter
  70.  *
  71.  * @deprecated As of 3.1 (to be removed in 4.0).
  72.  * @since 3.0
  73.  */

  74. @Deprecated
  75. public class MultivariateFunctionMappingAdapter implements MultivariateFunction {

  76.     /** Underlying bounded function. */
  77.     private final MultivariateFunction bounded;

  78.     /** Mapping functions. */
  79.     private final Mapper[] mappers;

  80.     /** Simple constructor.
  81.      * @param bounded bounded function
  82.      * @param lower lower bounds for each element of the input parameters array
  83.      * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
  84.      * unbounded values)
  85.      * @param upper upper bounds for each element of the input parameters array
  86.      * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
  87.      * unbounded values)
  88.      * @exception DimensionMismatchException if lower and upper bounds are not
  89.      * consistent, either according to dimension or to values
  90.      */
  91.     public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded,
  92.                                                   final double[] lower, final double[] upper) {

  93.         // safety checks
  94.         MathUtils.checkNotNull(lower);
  95.         MathUtils.checkNotNull(upper);
  96.         if (lower.length != upper.length) {
  97.             throw new DimensionMismatchException(lower.length, upper.length);
  98.         }
  99.         for (int i = 0; i < lower.length; ++i) {
  100.             // note the following test is written in such a way it also fails for NaN
  101.             if (!(upper[i] >= lower[i])) {
  102.                 throw new NumberIsTooSmallException(upper[i], lower[i], true);
  103.             }
  104.         }

  105.         this.bounded = bounded;
  106.         this.mappers = new Mapper[lower.length];
  107.         for (int i = 0; i < mappers.length; ++i) {
  108.             if (Double.isInfinite(lower[i])) {
  109.                 if (Double.isInfinite(upper[i])) {
  110.                     // element is unbounded, no transformation is needed
  111.                     mappers[i] = new NoBoundsMapper();
  112.                 } else {
  113.                     // element is simple-bounded on the upper side
  114.                     mappers[i] = new UpperBoundMapper(upper[i]);
  115.                 }
  116.             } else {
  117.                 if (Double.isInfinite(upper[i])) {
  118.                     // element is simple-bounded on the lower side
  119.                     mappers[i] = new LowerBoundMapper(lower[i]);
  120.                 } else {
  121.                     // element is double-bounded
  122.                     mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]);
  123.                 }
  124.             }
  125.         }

  126.     }

  127.     /** Map an array from unbounded to bounded.
  128.      * @param point unbounded value
  129.      * @return bounded value
  130.      */
  131.     public double[] unboundedToBounded(double[] point) {

  132.         // map unbounded input point to bounded point
  133.         final double[] mapped = new double[mappers.length];
  134.         for (int i = 0; i < mappers.length; ++i) {
  135.             mapped[i] = mappers[i].unboundedToBounded(point[i]);
  136.         }

  137.         return mapped;

  138.     }

  139.     /** Map an array from bounded to unbounded.
  140.      * @param point bounded value
  141.      * @return unbounded value
  142.      */
  143.     public double[] boundedToUnbounded(double[] point) {

  144.         // map bounded input point to unbounded point
  145.         final double[] mapped = new double[mappers.length];
  146.         for (int i = 0; i < mappers.length; ++i) {
  147.             mapped[i] = mappers[i].boundedToUnbounded(point[i]);
  148.         }

  149.         return mapped;

  150.     }

  151.     /** Compute the underlying function value from an unbounded point.
  152.      * <p>
  153.      * This method simply bounds the unbounded point using the mappings
  154.      * set up at construction and calls the underlying function using
  155.      * the bounded point.
  156.      * </p>
  157.      * @param point unbounded value
  158.      * @return underlying function value
  159.      * @see #unboundedToBounded(double[])
  160.      */
  161.     public double value(double[] point) {
  162.         return bounded.value(unboundedToBounded(point));
  163.     }

  164.     /** Mapping interface. */
  165.     private interface Mapper {

  166.         /** Map a value from unbounded to bounded.
  167.          * @param y unbounded value
  168.          * @return bounded value
  169.          */
  170.         double unboundedToBounded(double y);

  171.         /** Map a value from bounded to unbounded.
  172.          * @param x bounded value
  173.          * @return unbounded value
  174.          */
  175.         double boundedToUnbounded(double x);

  176.     }

  177.     /** Local class for no bounds mapping. */
  178.     private static class NoBoundsMapper implements Mapper {

  179.         /** Simple constructor.
  180.          */
  181.         public NoBoundsMapper() {
  182.         }

  183.         /** {@inheritDoc} */
  184.         public double unboundedToBounded(final double y) {
  185.             return y;
  186.         }

  187.         /** {@inheritDoc} */
  188.         public double boundedToUnbounded(final double x) {
  189.             return x;
  190.         }

  191.     }

  192.     /** Local class for lower bounds mapping. */
  193.     private static class LowerBoundMapper implements Mapper {

  194.         /** Low bound. */
  195.         private final double lower;

  196.         /** Simple constructor.
  197.          * @param lower lower bound
  198.          */
  199.         public LowerBoundMapper(final double lower) {
  200.             this.lower = lower;
  201.         }

  202.         /** {@inheritDoc} */
  203.         public double unboundedToBounded(final double y) {
  204.             return lower + FastMath.exp(y);
  205.         }

  206.         /** {@inheritDoc} */
  207.         public double boundedToUnbounded(final double x) {
  208.             return FastMath.log(x - lower);
  209.         }

  210.     }

  211.     /** Local class for upper bounds mapping. */
  212.     private static class UpperBoundMapper implements Mapper {

  213.         /** Upper bound. */
  214.         private final double upper;

  215.         /** Simple constructor.
  216.          * @param upper upper bound
  217.          */
  218.         public UpperBoundMapper(final double upper) {
  219.             this.upper = upper;
  220.         }

  221.         /** {@inheritDoc} */
  222.         public double unboundedToBounded(final double y) {
  223.             return upper - FastMath.exp(-y);
  224.         }

  225.         /** {@inheritDoc} */
  226.         public double boundedToUnbounded(final double x) {
  227.             return -FastMath.log(upper - x);
  228.         }

  229.     }

  230.     /** Local class for lower and bounds mapping. */
  231.     private static class LowerUpperBoundMapper implements Mapper {

  232.         /** Function from unbounded to bounded. */
  233.         private final UnivariateFunction boundingFunction;

  234.         /** Function from bounded to unbounded. */
  235.         private final UnivariateFunction unboundingFunction;

  236.         /** Simple constructor.
  237.          * @param lower lower bound
  238.          * @param upper upper bound
  239.          */
  240.         public LowerUpperBoundMapper(final double lower, final double upper) {
  241.             boundingFunction   = new Sigmoid(lower, upper);
  242.             unboundingFunction = new Logit(lower, upper);
  243.         }

  244.         /** {@inheritDoc} */
  245.         public double unboundedToBounded(final double y) {
  246.             return boundingFunction.value(y);
  247.         }

  248.         /** {@inheritDoc} */
  249.         public double boundedToUnbounded(final double x) {
  250.             return unboundingFunction.value(x);
  251.         }

  252.     }

  253. }