MultivariateFunctionPenaltyAdapter.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.exception.DimensionMismatchException;
  20. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  21. import org.apache.commons.math3.util.FastMath;
  22. import org.apache.commons.math3.util.MathUtils;

  23. /**
  24.  * <p>Adapter extending bounded {@link MultivariateFunction} to an unbouded
  25.  * domain using a penalty function.</p>
  26.  *
  27.  * <p>
  28.  * This adapter can be used to wrap functions subject to simple bounds on
  29.  * parameters so they can be used by optimizers that do <em>not</em> directly
  30.  * support simple bounds.
  31.  * </p>
  32.  * <p>
  33.  * The principle is that the user function that will be wrapped will see its
  34.  * parameters bounded as required, i.e when its {@code value} method is called
  35.  * with argument array {@code point}, the elements array will fulfill requirement
  36.  * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
  37.  * may be unbounded or bounded only on one side if the corresponding bound is
  38.  * set to an infinite value. The optimizer will not manage the user function by
  39.  * itself, but it will handle this adapter and it is this adapter that will take
  40.  * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
  41.  * be called by the optimizer with unbound parameters, and the adapter will check
  42.  * if the parameters is within range or not. If it is in range, then the underlying
  43.  * user function will be called, and if it is not the value of a penalty function
  44.  * will be returned instead.
  45.  * </p>
  46.  * <p>
  47.  * This adapter is only a poor man solution to simple bounds optimization constraints
  48.  * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link
  49.  * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use
  50.  * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or
  51.  * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that if start point
  52.  * or start simplex is completely outside of the allowed range, only the penalty function
  53.  * is used, and the optimizer may converge without ever entering the range.
  54.  * </p>
  55.  *
  56.  * @see MultivariateFunctionMappingAdapter
  57.  *
  58.  * @deprecated As of 3.1 (to be removed in 4.0).
  59.  * @since 3.0
  60.  */

  61. @Deprecated
  62. public class MultivariateFunctionPenaltyAdapter implements MultivariateFunction {

  63.     /** Underlying bounded function. */
  64.     private final MultivariateFunction bounded;

  65.     /** Lower bounds. */
  66.     private final double[] lower;

  67.     /** Upper bounds. */
  68.     private final double[] upper;

  69.     /** Penalty offset. */
  70.     private final double offset;

  71.     /** Penalty scales. */
  72.     private final double[] scale;

  73.     /** Simple constructor.
  74.      * <p>
  75.      * When the optimizer provided points are out of range, the value of the
  76.      * penalty function will be used instead of the value of the underlying
  77.      * function. In order for this penalty to be effective in rejecting this
  78.      * point during the optimization process, the penalty function value should
  79.      * be defined with care. This value is computed as:
  80.      * <pre>
  81.      *   penalty(point) = offset + &sum;<sub>i</sub>[scale[i] * &radic;|point[i]-boundary[i]|]
  82.      * </pre>
  83.      * where indices i correspond to all the components that violates their boundaries.
  84.      * </p>
  85.      * <p>
  86.      * So when attempting a function minimization, offset should be larger than
  87.      * the maximum expected value of the underlying function and scale components
  88.      * should all be positive. When attempting a function maximization, offset
  89.      * should be lesser than the minimum expected value of the underlying function
  90.      * and scale components should all be negative.
  91.      * minimization, and lesser than the minimum expected value of the underlying
  92.      * function when attempting maximization.
  93.      * </p>
  94.      * <p>
  95.      * These choices for the penalty function have two properties. First, all out
  96.      * of range points will return a function value that is worse than the value
  97.      * returned by any in range point. Second, the penalty is worse for large
  98.      * boundaries violation than for small violations, so the optimizer has an hint
  99.      * about the direction in which it should search for acceptable points.
  100.      * </p>
  101.      * @param bounded bounded function
  102.      * @param lower lower bounds for each element of the input parameters array
  103.      * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
  104.      * unbounded values)
  105.      * @param upper upper bounds for each element of the input parameters array
  106.      * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
  107.      * unbounded values)
  108.      * @param offset base offset of the penalty function
  109.      * @param scale scale of the penalty function
  110.      * @exception DimensionMismatchException if lower bounds, upper bounds and
  111.      * scales are not consistent, either according to dimension or to bounadary
  112.      * values
  113.      */
  114.     public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded,
  115.                                                   final double[] lower, final double[] upper,
  116.                                                   final double offset, final double[] scale) {

  117.         // safety checks
  118.         MathUtils.checkNotNull(lower);
  119.         MathUtils.checkNotNull(upper);
  120.         MathUtils.checkNotNull(scale);
  121.         if (lower.length != upper.length) {
  122.             throw new DimensionMismatchException(lower.length, upper.length);
  123.         }
  124.         if (lower.length != scale.length) {
  125.             throw new DimensionMismatchException(lower.length, scale.length);
  126.         }
  127.         for (int i = 0; i < lower.length; ++i) {
  128.             // note the following test is written in such a way it also fails for NaN
  129.             if (!(upper[i] >= lower[i])) {
  130.                 throw new NumberIsTooSmallException(upper[i], lower[i], true);
  131.             }
  132.         }

  133.         this.bounded = bounded;
  134.         this.lower   = lower.clone();
  135.         this.upper   = upper.clone();
  136.         this.offset  = offset;
  137.         this.scale   = scale.clone();

  138.     }

  139.     /** Compute the underlying function value from an unbounded point.
  140.      * <p>
  141.      * This method simply returns the value of the underlying function
  142.      * if the unbounded point already fulfills the bounds, and compute
  143.      * a replacement value using the offset and scale if bounds are
  144.      * violated, without calling the function at all.
  145.      * </p>
  146.      * @param point unbounded point
  147.      * @return either underlying function value or penalty function value
  148.      */
  149.     public double value(double[] point) {

  150.         for (int i = 0; i < scale.length; ++i) {
  151.             if ((point[i] < lower[i]) || (point[i] > upper[i])) {
  152.                 // bound violation starting at this component
  153.                 double sum = 0;
  154.                 for (int j = i; j < scale.length; ++j) {
  155.                     final double overshoot;
  156.                     if (point[j] < lower[j]) {
  157.                         overshoot = scale[j] * (lower[j] - point[j]);
  158.                     } else if (point[j] > upper[j]) {
  159.                         overshoot = scale[j] * (point[j] - upper[j]);
  160.                     } else {
  161.                         overshoot = 0;
  162.                     }
  163.                     sum += FastMath.sqrt(overshoot);
  164.                 }
  165.                 return offset + sum;
  166.             }
  167.         }

  168.         // all boundaries are fulfilled, we are in the expected
  169.         // domain of the underlying function
  170.         return bounded.value(point);

  171.     }

  172. }