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 018package org.apache.commons.math3.optimization.direct; 019 020import org.apache.commons.math3.analysis.MultivariateFunction; 021import org.apache.commons.math3.analysis.UnivariateFunction; 022import org.apache.commons.math3.analysis.function.Logit; 023import org.apache.commons.math3.analysis.function.Sigmoid; 024import org.apache.commons.math3.exception.DimensionMismatchException; 025import org.apache.commons.math3.exception.NumberIsTooSmallException; 026import org.apache.commons.math3.util.FastMath; 027import org.apache.commons.math3.util.MathUtils; 028 029/** 030 * <p>Adapter for mapping bounded {@link MultivariateFunction} to unbounded ones.</p> 031 * 032 * <p> 033 * This adapter can be used to wrap functions subject to simple bounds on 034 * parameters so they can be used by optimizers that do <em>not</em> directly 035 * support simple bounds. 036 * </p> 037 * <p> 038 * The principle is that the user function that will be wrapped will see its 039 * parameters bounded as required, i.e when its {@code value} method is called 040 * with argument array {@code point}, the elements array will fulfill requirement 041 * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components 042 * may be unbounded or bounded only on one side if the corresponding bound is 043 * set to an infinite value. The optimizer will not manage the user function by 044 * itself, but it will handle this adapter and it is this adapter that will take 045 * care the bounds are fulfilled. The adapter {@link #value(double[])} method will 046 * be called by the optimizer with unbound parameters, and the adapter will map 047 * the unbounded value to the bounded range using appropriate functions like 048 * {@link Sigmoid} for double bounded elements for example. 049 * </p> 050 * <p> 051 * As the optimizer sees only unbounded parameters, it should be noted that the 052 * start point or simplex expected by the optimizer should be unbounded, so the 053 * user is responsible for converting his bounded point to unbounded by calling 054 * {@link #boundedToUnbounded(double[])} before providing them to the optimizer. 055 * For the same reason, the point returned by the {@link 056 * org.apache.commons.math3.optimization.BaseMultivariateOptimizer#optimize(int, 057 * MultivariateFunction, org.apache.commons.math3.optimization.GoalType, double[])} 058 * method is unbounded. So to convert this point to bounded, users must call 059 * {@link #unboundedToBounded(double[])} by themselves!</p> 060 * <p> 061 * This adapter is only a poor man solution to simple bounds optimization constraints 062 * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link 063 * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use 064 * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or 065 * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that behavior near 066 * the bounds may be numerically unstable as bounds are mapped from infinite values. 067 * Another caveat is that convergence values are evaluated by the optimizer with respect 068 * to unbounded variables, so there will be scales differences when converted to bounded 069 * variables. 070 * </p> 071 * 072 * @see MultivariateFunctionPenaltyAdapter 073 * 074 * @deprecated As of 3.1 (to be removed in 4.0). 075 * @since 3.0 076 */ 077 078@Deprecated 079public class MultivariateFunctionMappingAdapter implements MultivariateFunction { 080 081 /** Underlying bounded function. */ 082 private final MultivariateFunction bounded; 083 084 /** Mapping functions. */ 085 private final Mapper[] mappers; 086 087 /** Simple constructor. 088 * @param bounded bounded function 089 * @param lower lower bounds for each element of the input parameters array 090 * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for 091 * unbounded values) 092 * @param upper upper bounds for each element of the input parameters array 093 * (some elements may be set to {@code Double.POSITIVE_INFINITY} for 094 * unbounded values) 095 * @exception DimensionMismatchException if lower and upper bounds are not 096 * consistent, either according to dimension or to values 097 */ 098 public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded, 099 final double[] lower, final double[] upper) { 100 101 // safety checks 102 MathUtils.checkNotNull(lower); 103 MathUtils.checkNotNull(upper); 104 if (lower.length != upper.length) { 105 throw new DimensionMismatchException(lower.length, upper.length); 106 } 107 for (int i = 0; i < lower.length; ++i) { 108 // note the following test is written in such a way it also fails for NaN 109 if (!(upper[i] >= lower[i])) { 110 throw new NumberIsTooSmallException(upper[i], lower[i], true); 111 } 112 } 113 114 this.bounded = bounded; 115 this.mappers = new Mapper[lower.length]; 116 for (int i = 0; i < mappers.length; ++i) { 117 if (Double.isInfinite(lower[i])) { 118 if (Double.isInfinite(upper[i])) { 119 // element is unbounded, no transformation is needed 120 mappers[i] = new NoBoundsMapper(); 121 } else { 122 // element is simple-bounded on the upper side 123 mappers[i] = new UpperBoundMapper(upper[i]); 124 } 125 } else { 126 if (Double.isInfinite(upper[i])) { 127 // element is simple-bounded on the lower side 128 mappers[i] = new LowerBoundMapper(lower[i]); 129 } else { 130 // element is double-bounded 131 mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]); 132 } 133 } 134 } 135 136 } 137 138 /** Map an array from unbounded to bounded. 139 * @param point unbounded value 140 * @return bounded value 141 */ 142 public double[] unboundedToBounded(double[] point) { 143 144 // map unbounded input point to bounded point 145 final double[] mapped = new double[mappers.length]; 146 for (int i = 0; i < mappers.length; ++i) { 147 mapped[i] = mappers[i].unboundedToBounded(point[i]); 148 } 149 150 return mapped; 151 152 } 153 154 /** Map an array from bounded to unbounded. 155 * @param point bounded value 156 * @return unbounded value 157 */ 158 public double[] boundedToUnbounded(double[] point) { 159 160 // map bounded input point to unbounded point 161 final double[] mapped = new double[mappers.length]; 162 for (int i = 0; i < mappers.length; ++i) { 163 mapped[i] = mappers[i].boundedToUnbounded(point[i]); 164 } 165 166 return mapped; 167 168 } 169 170 /** Compute the underlying function value from an unbounded point. 171 * <p> 172 * This method simply bounds the unbounded point using the mappings 173 * set up at construction and calls the underlying function using 174 * the bounded point. 175 * </p> 176 * @param point unbounded value 177 * @return underlying function value 178 * @see #unboundedToBounded(double[]) 179 */ 180 public double value(double[] point) { 181 return bounded.value(unboundedToBounded(point)); 182 } 183 184 /** Mapping interface. */ 185 private interface Mapper { 186 187 /** Map a value from unbounded to bounded. 188 * @param y unbounded value 189 * @return bounded value 190 */ 191 double unboundedToBounded(double y); 192 193 /** Map a value from bounded to unbounded. 194 * @param x bounded value 195 * @return unbounded value 196 */ 197 double boundedToUnbounded(double x); 198 199 } 200 201 /** Local class for no bounds mapping. */ 202 private static class NoBoundsMapper implements Mapper { 203 204 /** Simple constructor. 205 */ 206 NoBoundsMapper() { 207 } 208 209 /** {@inheritDoc} */ 210 public double unboundedToBounded(final double y) { 211 return y; 212 } 213 214 /** {@inheritDoc} */ 215 public double boundedToUnbounded(final double x) { 216 return x; 217 } 218 219 } 220 221 /** Local class for lower bounds mapping. */ 222 private static class LowerBoundMapper implements Mapper { 223 224 /** Low bound. */ 225 private final double lower; 226 227 /** Simple constructor. 228 * @param lower lower bound 229 */ 230 LowerBoundMapper(final double lower) { 231 this.lower = lower; 232 } 233 234 /** {@inheritDoc} */ 235 public double unboundedToBounded(final double y) { 236 return lower + FastMath.exp(y); 237 } 238 239 /** {@inheritDoc} */ 240 public double boundedToUnbounded(final double x) { 241 return FastMath.log(x - lower); 242 } 243 244 } 245 246 /** Local class for upper bounds mapping. */ 247 private static class UpperBoundMapper implements Mapper { 248 249 /** Upper bound. */ 250 private final double upper; 251 252 /** Simple constructor. 253 * @param upper upper bound 254 */ 255 UpperBoundMapper(final double upper) { 256 this.upper = upper; 257 } 258 259 /** {@inheritDoc} */ 260 public double unboundedToBounded(final double y) { 261 return upper - FastMath.exp(-y); 262 } 263 264 /** {@inheritDoc} */ 265 public double boundedToUnbounded(final double x) { 266 return -FastMath.log(upper - x); 267 } 268 269 } 270 271 /** Local class for lower and bounds mapping. */ 272 private static class LowerUpperBoundMapper implements Mapper { 273 274 /** Function from unbounded to bounded. */ 275 private final UnivariateFunction boundingFunction; 276 277 /** Function from bounded to unbounded. */ 278 private final UnivariateFunction unboundingFunction; 279 280 /** Simple constructor. 281 * @param lower lower bound 282 * @param upper upper bound 283 */ 284 LowerUpperBoundMapper(final double lower, final double upper) { 285 boundingFunction = new Sigmoid(lower, upper); 286 unboundingFunction = new Logit(lower, upper); 287 } 288 289 /** {@inheritDoc} */ 290 public double unboundedToBounded(final double y) { 291 return boundingFunction.value(y); 292 } 293 294 /** {@inheritDoc} */ 295 public double boundedToUnbounded(final double x) { 296 return unboundingFunction.value(x); 297 } 298 299 } 300 301}