    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.math3.optim.nonlinear.scalar;
    018
    019    import org.apache.commons.math3.analysis.MultivariateFunction;
    020    import org.apache.commons.math3.exception.DimensionMismatchException;
    021    import org.apache.commons.math3.exception.NumberIsTooSmallException;
    022    import org.apache.commons.math3.util.FastMath;
    023    import org.apache.commons.math3.util.MathUtils;
    024
    025    /**
    026     * <p>Adapter extending bounded {@link MultivariateFunction} to an unbouded
    027     * domain using a penalty function.</p>
    028     *
    029     * <p>
    030     * This adapter can be used to wrap functions subject to simple bounds on
    031     * parameters so they can be used by optimizers that do <em>not</em> directly
    032     * support simple bounds.
    033     * </p>
    034     * <p>
    035     * The principle is that the user function that will be wrapped will see its
    036     * parameters bounded as required, i.e when its {@code value} method is called
    037     * with argument array {@code point}, the elements array will fulfill requirement
    038     * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
    039     * may be unbounded or bounded only on one side if the corresponding bound is
    040     * set to an infinite value. The optimizer will not manage the user function by
    041     * itself, but it will handle this adapter and it is this adapter that will take
    042     * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
    043     * be called by the optimizer with unbound parameters, and the adapter will check
    044     * if the parameters is within range or not. If it is in range, then the underlying
    045     * user function will be called, and if it is not the value of a penalty function
    046     * will be returned instead.
    047     * </p>
    048     * <p>
    049     * This adapter is only a poor-man's solution to simple bounds optimization
    050     * constraints that can be used with simple optimizers like
    051     * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer
    052     * SimplexOptimizer}.
    053     * A better solution is to use an optimizer that directly supports simple bounds like
    054     * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer
    055     * CMAESOptimizer} or
    056     * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer
    057     * BOBYQAOptimizer}.
    058     * One caveat of this poor-man's solution is that if start point or start simplex
    059     * is completely outside of the allowed range, only the penalty function is used,
    060     * and the optimizer may converge without ever entering the range.
    061     * </p>
    062     *
    063     * @see MultivariateFunctionMappingAdapter
    064     *
    065     * @version $Id: MultivariateFunctionPenaltyAdapter.java 1416643 2012-12-03 19:37:14Z tn $
    066     * @since 3.0
    067     */
    068    public class MultivariateFunctionPenaltyAdapter
    069        implements MultivariateFunction {
    070        /** Underlying bounded function. */
    071        private final MultivariateFunction bounded;
    072        /** Lower bounds. */
    073        private final double[] lower;
    074        /** Upper bounds. */
    075        private final double[] upper;
    076        /** Penalty offset. */
    077        private final double offset;
    078        /** Penalty scales. */
    079        private final double[] scale;
    080
    081        /**
    082         * Simple constructor.
    083         * <p>
    084         * When the optimizer provided points are out of range, the value of the
    085         * penalty function will be used instead of the value of the underlying
    086         * function. In order for this penalty to be effective in rejecting this
    087         * point during the optimization process, the penalty function value should
    088         * be defined with care. This value is computed as:
    089         * <pre>
    090         *   penalty(point) = offset + &sum;<sub>i</sub>[scale[i] * &radic;|point[i]-boundary[i]|]
    091         * </pre>
    092         * where indices i correspond to all the components that violates their boundaries.
    093         * </p>
    094         * <p>
    095         * So when attempting a function minimization, offset should be larger than
    096         * the maximum expected value of the underlying function and scale components
    097         * should all be positive. When attempting a function maximization, offset
    098         * should be lesser than the minimum expected value of the underlying function
    099         * 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 DimensionMismatchException 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
    127            // safety checks
    128            MathUtils.checkNotNull(lower);
    129            MathUtils.checkNotNull(upper);
    130            MathUtils.checkNotNull(scale);
    131            if (lower.length != upper.length) {
    132                throw new DimensionMismatchException(lower.length, upper.length);
    133            }
    134            if (lower.length != scale.length) {
    135                throw new DimensionMismatchException(lower.length, scale.length);
    136            }
    137            for (int i = 0; i < lower.length; ++i) {
    138                // note the following test is written in such a way it also fails for NaN
    139                if (!(upper[i] >= lower[i])) {
    140                    throw new NumberIsTooSmallException(upper[i], lower[i], true);
    141                }
    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        /**
    152         * Computes the underlying function value from an unbounded point.
    153         * <p>
    154         * This method simply returns the value of the underlying function
    155         * if the unbounded point already fulfills the bounds, and compute
    156         * a replacement value using the offset and scale if bounds are
    157         * violated, without calling the function at all.
    158         * </p>
    159         * @param point unbounded point
    160         * @return either underlying function value or penalty function value
    161         */
    162        public double value(double[] point) {
    163
    164            for (int i = 0; i < scale.length; ++i) {
    165                if ((point[i] < lower[i]) || (point[i] > upper[i])) {
    166                    // bound violation starting at this component
    167                    double sum = 0;
    168                    for (int j = i; j < scale.length; ++j) {
    169                        final double overshoot;
    170                        if (point[j] < lower[j]) {
    171                            overshoot = scale[j] * (lower[j] - point[j]);
    172                        } else if (point[j] > upper[j]) {
    173                            overshoot = scale[j] * (point[j] - upper[j]);
    174                        } else {
    175                            overshoot = 0;
    176                        }
    177                        sum += FastMath.sqrt(overshoot);
    178                    }
    179                    return offset + sum;
    180                }
    181            }
    182
    183            // all boundaries are fulfilled, we are in the expected
    184            // domain of the underlying function
    185            return bounded.value(point);
    186        }
    187    }