View Javadoc
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    *      http://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  package org.apache.commons.math3.optimization.direct;
19  
20  import org.apache.commons.math3.analysis.MultivariateFunction;
21  import org.apache.commons.math3.analysis.UnivariateFunction;
22  import org.apache.commons.math3.analysis.function.Logit;
23  import org.apache.commons.math3.analysis.function.Sigmoid;
24  import org.apache.commons.math3.exception.DimensionMismatchException;
25  import org.apache.commons.math3.exception.NumberIsTooSmallException;
26  import org.apache.commons.math3.util.FastMath;
27  import org.apache.commons.math3.util.MathUtils;
28  
29  /**
30   * <p>Adapter for mapping bounded {@link MultivariateFunction} to unbounded ones.</p>
31   *
32   * <p>
33   * This adapter can be used to wrap functions subject to simple bounds on
34   * parameters so they can be used by optimizers that do <em>not</em> directly
35   * support simple bounds.
36   * </p>
37   * <p>
38   * The principle is that the user function that will be wrapped will see its
39   * parameters bounded as required, i.e when its {@code value} method is called
40   * with argument array {@code point}, the elements array will fulfill requirement
41   * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components
42   * may be unbounded or bounded only on one side if the corresponding bound is
43   * set to an infinite value. The optimizer will not manage the user function by
44   * itself, but it will handle this adapter and it is this adapter that will take
45   * care the bounds are fulfilled. The adapter {@link #value(double[])} method will
46   * be called by the optimizer with unbound parameters, and the adapter will map
47   * the unbounded value to the bounded range using appropriate functions like
48   * {@link Sigmoid} for double bounded elements for example.
49   * </p>
50   * <p>
51   * As the optimizer sees only unbounded parameters, it should be noted that the
52   * start point or simplex expected by the optimizer should be unbounded, so the
53   * user is responsible for converting his bounded point to unbounded by calling
54   * {@link #boundedToUnbounded(double[])} before providing them to the optimizer.
55   * For the same reason, the point returned by the {@link
56   * org.apache.commons.math3.optimization.BaseMultivariateOptimizer#optimize(int,
57   * MultivariateFunction, org.apache.commons.math3.optimization.GoalType, double[])}
58   * method is unbounded. So to convert this point to bounded, users must call
59   * {@link #unboundedToBounded(double[])} by themselves!</p>
60   * <p>
61   * This adapter is only a poor man solution to simple bounds optimization constraints
62   * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link
63   * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use
64   * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or
65   * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that behavior near
66   * the bounds may be numerically unstable as bounds are mapped from infinite values.
67   * Another caveat is that convergence values are evaluated by the optimizer with respect
68   * to unbounded variables, so there will be scales differences when converted to bounded
69   * variables.
70   * </p>
71   *
72   * @see MultivariateFunctionPenaltyAdapter
73   *
74   * @deprecated As of 3.1 (to be removed in 4.0).
75   * @since 3.0
76   */
77  
78  @Deprecated
79  public class MultivariateFunctionMappingAdapter implements MultivariateFunction {
80  
81      /** Underlying bounded function. */
82      private final MultivariateFunction bounded;
83  
84      /** Mapping functions. */
85      private final Mapper[] mappers;
86  
87      /** Simple constructor.
88       * @param bounded bounded function
89       * @param lower lower bounds for each element of the input parameters array
90       * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
91       * unbounded values)
92       * @param upper upper bounds for each element of the input parameters array
93       * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
94       * unbounded values)
95       * @exception DimensionMismatchException if lower and upper bounds are not
96       * consistent, either according to dimension or to values
97       */
98      public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded,
99                                                    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         public 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         public 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         public 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         public 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 }