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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.nonlinear.scalar;

  22. import org.hipparchus.analysis.MultivariateFunction;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.MathUtils;

  27. /**
  28.  * <p>Adapter extending bounded {@link MultivariateFunction} to an unbouded
  29.  * domain using a penalty function.</p>
  30.  *
  31.  * <p>
  32.  * This adapter can be used to wrap functions subject to simple bounds on
  33.  * parameters so they can be used by optimizers that do <em>not</em> directly
  34.  * support simple bounds.
  35.  * </p>
  36.  * <p>
  37.  * The principle is that the user function that will be wrapped will see its
  38.  * parameters bounded as required, i.e when its {@code value} method is called
  39.  * with argument array {@code point}, the elements array will fulfill requirement
  40.  * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
  41.  * may be unbounded or bounded only on one side if the corresponding bound is
  42.  * set to an infinite value. The optimizer will not manage the user function by
  43.  * itself, but it will handle this adapter and it is this adapter that will take
  44.  * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
  45.  * be called by the optimizer with unbound parameters, and the adapter will check
  46.  * if the parameters is within range or not. If it is in range, then the underlying
  47.  * user function will be called, and if it is not the value of a penalty function
  48.  * will be returned instead.
  49.  * </p>
  50.  * <p>
  51.  * This adapter is only a poor-man's solution to simple bounds optimization
  52.  * constraints that can be used with simple optimizers like
  53.  * {@link org.hipparchus.optim.nonlinear.scalar.noderiv.SimplexOptimizer
  54.  * SimplexOptimizer}.
  55.  * A better solution is to use an optimizer that directly supports simple bounds like
  56.  * {@link org.hipparchus.optim.nonlinear.scalar.noderiv.CMAESOptimizer
  57.  * CMAESOptimizer} or
  58.  * {@link org.hipparchus.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer
  59.  * BOBYQAOptimizer}.
  60.  * One caveat of this poor-man's solution is that if start point or start simplex
  61.  * is completely outside of the allowed range, only the penalty function is used,
  62.  * and the optimizer may converge without ever entering the range.
  63.  * </p>
  64.  *
  65.  * @see MultivariateFunctionMappingAdapter
  66.  *
  67.  */
  68. public class MultivariateFunctionPenaltyAdapter
  69.     implements MultivariateFunction {
  70.     /** Underlying bounded function. */
  71.     private final MultivariateFunction bounded;
  72.     /** Lower bounds. */
  73.     private final double[] lower;
  74.     /** Upper bounds. */
  75.     private final double[] upper;
  76.     /** Penalty offset. */
  77.     private final double offset;
  78.     /** Penalty scales. */
  79.     private final double[] scale;

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

  126.         // safety checks
  127.         MathUtils.checkNotNull(lower);
  128.         MathUtils.checkNotNull(upper);
  129.         MathUtils.checkNotNull(scale);
  130.         if (lower.length != upper.length) {
  131.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  132.                                                    lower.length, upper.length);
  133.         }
  134.         if (lower.length != scale.length) {
  135.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  136.                                                    lower.length, scale.length);
  137.         }
  138.         for (int i = 0; i < lower.length; ++i) {
  139.             if (!(upper[i] >= lower[i])) { // NOPMD - the test is written in such a way it also fails for NaN
  140.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
  141.                                                        upper[i], lower[i]);
  142.             }
  143.         }

  144.         this.bounded = bounded;
  145.         this.lower   = lower.clone();
  146.         this.upper   = upper.clone();
  147.         this.offset  = offset;
  148.         this.scale   = scale.clone();
  149.     }

  150.     /**
  151.      * Computes the underlying function value from an unbounded point.
  152.      * <p>
  153.      * This method simply returns the value of the underlying function
  154.      * if the unbounded point already fulfills the bounds, and compute
  155.      * a replacement value using the offset and scale if bounds are
  156.      * violated, without calling the function at all.
  157.      * </p>
  158.      * @param point unbounded point
  159.      * @return either underlying function value or penalty function value
  160.      */
  161.     @Override
  162.     public double value(double[] point) {

  163.         for (int i = 0; i < scale.length; ++i) {
  164.             if ((point[i] < lower[i]) || (point[i] > upper[i])) {
  165.                 // bound violation starting at this component
  166.                 double sum = 0;
  167.                 for (int j = i; j < scale.length; ++j) {
  168.                     final double overshoot;
  169.                     if (point[j] < lower[j]) {
  170.                         overshoot = scale[j] * (lower[j] - point[j]);
  171.                     } else if (point[j] > upper[j]) {
  172.                         overshoot = scale[j] * (point[j] - upper[j]);
  173.                     } else {
  174.                         overshoot = 0;
  175.                     }
  176.                     sum += FastMath.sqrt(overshoot);
  177.                 }
  178.                 return offset + sum;
  179.             }
  180.         }

  181.         // all boundaries are fulfilled, we are in the expected
  182.         // domain of the underlying function
  183.         return bounded.value(point);
  184.     }
  185. }