    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
    018    package org.apache.commons.math3.optimization.direct;
    019
    020    import org.apache.commons.math3.analysis.MultivariateFunction;
    021    import org.apache.commons.math3.exception.DimensionMismatchException;
    022    import org.apache.commons.math3.exception.NumberIsTooSmallException;
    023    import org.apache.commons.math3.util.FastMath;
    024    import org.apache.commons.math3.util.MathUtils;
    025
    026    /**
    027     * <p>Adapter extending bounded {@link MultivariateFunction} to an unbouded
    028     * domain using a penalty function.</p>
    029     *
    030     * <p>
    031     * This adapter can be used to wrap functions subject to simple bounds on
    032     * parameters so they can be used by optimizers that do <em>not</em> directly
    033     * support simple bounds.
    034     * </p>
    035     * <p>
    036     * The principle is that the user function that will be wrapped will see its
    037     * parameters bounded as required, i.e when its {@code value} method is called
    038     * with argument array {@code point}, the elements array will fulfill requirement
    039     * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
    040     * may be unbounded or bounded only on one side if the corresponding bound is
    041     * set to an infinite value. The optimizer will not manage the user function by
    042     * itself, but it will handle this adapter and it is this adapter that will take
    043     * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
    044     * be called by the optimizer with unbound parameters, and the adapter will check
    045     * if the parameters is within range or not. If it is in range, then the underlying
    046     * user function will be called, and if it is not the value of a penalty function
    047     * will be returned instead.
    048     * </p>
    049     * <p>
    050     * This adapter is only a poor man solution to simple bounds optimization constraints
    051     * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link
    052     * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use
    053     * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or
    054     * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that if start point
    055     * or start simplex is completely outside of the allowed range, only the penalty function
    056     * is used, and the optimizer may converge without ever entering the range.
    057     * </p>
    058     *
    059     * @see MultivariateFunctionMappingAdapter
    060     *
    061     * @version $Id: MultivariateFunctionPenaltyAdapter.java 1422230 2012-12-15 12:11:13Z erans $
    062     * @deprecated As of 3.1 (to be removed in 4.0).
    063     * @since 3.0
    064     */
    065
    066    @Deprecated
    067    public class MultivariateFunctionPenaltyAdapter implements MultivariateFunction {
    068
    069        /** Underlying bounded function. */
    070        private final MultivariateFunction bounded;
    071
    072        /** Lower bounds. */
    073        private final double[] lower;
    074
    075        /** Upper bounds. */
    076        private final double[] upper;
    077
    078        /** Penalty offset. */
    079        private final double offset;
    080
    081        /** Penalty scales. */
    082        private final double[] scale;
    083
    084        /** Simple constructor.
    085         * <p>
    086         * When the optimizer provided points are out of range, the value of the
    087         * penalty function will be used instead of the value of the underlying
    088         * function. In order for this penalty to be effective in rejecting this
    089         * point during the optimization process, the penalty function value should
    090         * be defined with care. This value is computed as:
    091         * <pre>
    092         *   penalty(point) = offset + &sum;<sub>i</sub>[scale[i] * &radic;|point[i]-boundary[i]|]
    093         * </pre>
    094         * where indices i correspond to all the components that violates their boundaries.
    095         * </p>
    096         * <p>
    097         * So when attempting a function minimization, offset should be larger than
    098         * the maximum expected value of the underlying function and scale components
    099         * should all be positive. When attempting a function maximization, offset
    100         * should be lesser than the minimum expected value of the underlying function
    101         * and scale components should all be negative.
    102         * minimization, and lesser than the minimum expected value of the underlying
    103         * function when attempting maximization.
    104         * </p>
    105         * <p>
    106         * These choices for the penalty function have two properties. First, all out
    107         * of range points will return a function value that is worse than the value
    108         * returned by any in range point. Second, the penalty is worse for large
    109         * boundaries violation than for small violations, so the optimizer has an hint
    110         * about the direction in which it should search for acceptable points.
    111         * </p>
    112         * @param bounded bounded function
    113         * @param lower lower bounds for each element of the input parameters array
    114         * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
    115         * unbounded values)
    116         * @param upper upper bounds for each element of the input parameters array
    117         * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
    118         * unbounded values)
    119         * @param offset base offset of the penalty function
    120         * @param scale scale of the penalty function
    121         * @exception DimensionMismatchException if lower bounds, upper bounds and
    122         * scales are not consistent, either according to dimension or to bounadary
    123         * values
    124         */
    125        public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded,
    126                                                      final double[] lower, final double[] upper,
    127                                                      final double offset, final double[] scale) {
    128
    129            // safety checks
    130            MathUtils.checkNotNull(lower);
    131            MathUtils.checkNotNull(upper);
    132            MathUtils.checkNotNull(scale);
    133            if (lower.length != upper.length) {
    134                throw new DimensionMismatchException(lower.length, upper.length);
    135            }
    136            if (lower.length != scale.length) {
    137                throw new DimensionMismatchException(lower.length, scale.length);
    138            }
    139            for (int i = 0; i < lower.length; ++i) {
    140                // note the following test is written in such a way it also fails for NaN
    141                if (!(upper[i] >= lower[i])) {
    142                    throw new NumberIsTooSmallException(upper[i], lower[i], true);
    143                }
    144            }
    145
    146            this.bounded = bounded;
    147            this.lower   = lower.clone();
    148            this.upper   = upper.clone();
    149            this.offset  = offset;
    150            this.scale   = scale.clone();
    151
    152        }
    153
    154        /** Compute the underlying function value from an unbounded point.
    155         * <p>
    156         * This method simply returns the value of the underlying function
    157         * if the unbounded point already fulfills the bounds, and compute
    158         * a replacement value using the offset and scale if bounds are
    159         * violated, without calling the function at all.
    160         * </p>
    161         * @param point unbounded point
    162         * @return either underlying function value or penalty function value
    163         */
    164        public double value(double[] point) {
    165
    166            for (int i = 0; i < scale.length; ++i) {
    167                if ((point[i] < lower[i]) || (point[i] > upper[i])) {
    168                    // bound violation starting at this component
    169                    double sum = 0;
    170                    for (int j = i; j < scale.length; ++j) {
    171                        final double overshoot;
    172                        if (point[j] < lower[j]) {
    173                            overshoot = scale[j] * (lower[j] - point[j]);
    174                        } else if (point[j] > upper[j]) {
    175                            overshoot = scale[j] * (point[j] - upper[j]);
    176                        } else {
    177                            overshoot = 0;
    178                        }
    179                        sum += FastMath.sqrt(overshoot);
    180                    }
    181                    return offset + sum;
    182                }
    183            }
    184
    185            // all boundaries are fulfilled, we are in the expected
    186            // domain of the underlying function
    187            return bounded.value(point);
    188
    189        }
    190
    191    }