001/*002 * Licensed to the Apache Software Foundation (ASF) under one or more003 * contributor license agreements. See the NOTICE file distributed with004 * this work for additional information regarding copyright ownership.005 * The ASF licenses this file to You under the Apache License, Version 2.0006 * (the "License"); you may not use this file except in compliance with007 * the License. You may obtain a copy of the License at008 *009 * http://www.apache.org/licenses/LICENSE-2.0010 *011 * Unless required by applicable law or agreed to in writing, software012 * 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 and015 * limitations under the License.016 */017018package org.apache.commons.math3.optimization.direct;019020import org.apache.commons.math3.analysis.MultivariateFunction;021import org.apache.commons.math3.exception.DimensionMismatchException;022import org.apache.commons.math3.exception.NumberIsTooSmallException;023import org.apache.commons.math3.util.FastMath;024import org.apache.commons.math3.util.MathUtils;025026/**027 * <p>Adapter extending bounded {@link MultivariateFunction} to an unbouded028 * domain using a penalty function.</p>029 *030 * <p>031 * This adapter can be used to wrap functions subject to simple bounds on032 * parameters so they can be used by optimizers that do <em>not</em> directly033 * support simple bounds.034 * </p>035 * <p>036 * The principle is that the user function that will be wrapped will see its037 * parameters bounded as required, i.e when its {@code value} method is called038 * with argument array {@code point}, the elements array will fulfill requirement039 * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components040 * may be unbounded or bounded only on one side if the corresponding bound is041 * set to an infinite value. The optimizer will not manage the user function by042 * itself, but it will handle this adapter and it is this adapter that will take043 * care the bounds are fulfilled. The adapter {@link #value(double[])} method will044 * be called by the optimizer with unbound parameters, and the adapter will check045 * if the parameters is within range or not. If it is in range, then the underlying046 * user function will be called, and if it is not the value of a penalty function047 * will be returned instead.048 * </p>049 * <p>050 * This adapter is only a poor man solution to simple bounds optimization constraints051 * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link052 * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use053 * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or054 * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that if start point055 * or start simplex is completely outside of the allowed range, only the penalty function056 * is used, and the optimizer may converge without ever entering the range.057 * </p>058 *059 * @see MultivariateFunctionMappingAdapter060 *061 * @deprecated As of 3.1 (to be removed in 4.0).062 * @since 3.0063 */064065@Deprecated066public class MultivariateFunctionPenaltyAdapter implements MultivariateFunction {067068 /** Underlying bounded function. */069 private final MultivariateFunction bounded;070071 /** Lower bounds. */072 private final double[] lower;073074 /** Upper bounds. */075 private final double[] upper;076077 /** Penalty offset. */078 private final double offset;079080 /** Penalty scales. */081 private final double[] scale;082083 /** Simple constructor.084 * <p>085 * When the optimizer provided points are out of range, the value of the086 * penalty function will be used instead of the value of the underlying087 * function. In order for this penalty to be effective in rejecting this088 * point during the optimization process, the penalty function value should089 * be defined with care. This value is computed as:090 * <pre>091 * penalty(point) = offset + &sum;<sub>i</sub>[scale[i] * &radic;|point[i]-boundary[i]|]092 * </pre>093 * where indices i correspond to all the components that violates their boundaries.094 * </p>095 * <p>096 * So when attempting a function minimization, offset should be larger than097 * the maximum expected value of the underlying function and scale components098 * should all be positive. When attempting a function maximization, offset099 * should be lesser than the minimum expected value of the underlying function100 * and scale components should all be negative.101 * minimization, and lesser than the minimum expected value of the underlying102 * function when attempting maximization.103 * </p>104 * <p>105 * These choices for the penalty function have two properties. First, all out106 * of range points will return a function value that is worse than the value107 * returned by any in range point. Second, the penalty is worse for large108 * boundaries violation than for small violations, so the optimizer has an hint109 * about the direction in which it should search for acceptable points.110 * </p>111 * @param bounded bounded function112 * @param lower lower bounds for each element of the input parameters array113 * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for114 * unbounded values)115 * @param upper upper bounds for each element of the input parameters array116 * (some elements may be set to {@code Double.POSITIVE_INFINITY} for117 * unbounded values)118 * @param offset base offset of the penalty function119 * @param scale scale of the penalty function120 * @exception DimensionMismatchException if lower bounds, upper bounds and121 * scales are not consistent, either according to dimension or to bounadary122 * values123 */124 public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded,125 final double[] lower, final double[] upper,126 final double offset, final double[] scale) {127128 // safety checks129 MathUtils.checkNotNull(lower);130 MathUtils.checkNotNull(upper);131 MathUtils.checkNotNull(scale);132 if (lower.length != upper.length) {133 throw new DimensionMismatchException(lower.length, upper.length);134 }135 if (lower.length != scale.length) {136 throw new DimensionMismatchException(lower.length, scale.length);137 }138 for (int i = 0; i < lower.length; ++i) {139 // note the following test is written in such a way it also fails for NaN140 if (!(upper[i] >= lower[i])) {141 throw new NumberIsTooSmallException(upper[i], lower[i], true);142 }143 }144145 this.bounded = bounded;146 this.lower = lower.clone();147 this.upper = upper.clone();148 this.offset = offset;149 this.scale = scale.clone();150151 }152153 /** Compute the underlying function value from an unbounded point.154 * <p>155 * This method simply returns the value of the underlying function156 * if the unbounded point already fulfills the bounds, and compute157 * a replacement value using the offset and scale if bounds are158 * violated, without calling the function at all.159 * </p>160 * @param point unbounded point161 * @return either underlying function value or penalty function value162 */163 public double value(double[] point) {164165 for (int i = 0; i < scale.length; ++i) {166 if ((point[i] < lower[i]) || (point[i] > upper[i])) {167 // bound violation starting at this component168 double sum = 0;169 for (int j = i; j < scale.length; ++j) {170 final double overshoot;171 if (point[j] < lower[j]) {172 overshoot = scale[j] * (lower[j] - point[j]);173 } else if (point[j] > upper[j]) {174 overshoot = scale[j] * (point[j] - upper[j]);175 } else {176 overshoot = 0;177 }178 sum += FastMath.sqrt(overshoot);179 }180 return offset + sum;181 }182 }183184 // all boundaries are fulfilled, we are in the expected185 // domain of the underlying function186 return bounded.value(point);187188 }189190}