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  package org.apache.commons.math4.legacy.optim;
18  
19  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
20  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
21  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
22  
23  /**
24   * Base class for implementing optimizers for multivariate functions.
25   * It contains the boiler-plate code for initial guess and bounds
26   * specifications.
27   * <em>It is not a "user" class.</em>
28   *
29   * @param <PAIR> Type of the point/value pair returned by the optimization
30   * algorithm.
31   *
32   * @since 3.1
33   */
34  public abstract class BaseMultivariateOptimizer<PAIR>
35      extends BaseOptimizer<PAIR> {
36      /** Initial guess. */
37      private double[] start;
38      /** Lower bounds. */
39      private double[] lowerBound;
40      /** Upper bounds. */
41      private double[] upperBound;
42  
43      /**
44       * @param checker Convergence checker.
45       */
46      protected BaseMultivariateOptimizer(ConvergenceChecker<PAIR> checker) {
47          super(checker);
48      }
49  
50      /**
51       * {@inheritDoc}
52       *
53       * @param optData Optimization data. In addition to those documented in
54       * {@link BaseOptimizer#parseOptimizationData(OptimizationData[]) BaseOptimizer},
55       * this method will register the following data:
56       * <ul>
57       *  <li>{@link InitialGuess}</li>
58       *  <li>{@link SimpleBounds}</li>
59       * </ul>
60       * @return {@inheritDoc}
61       */
62      @Override
63      public PAIR optimize(OptimizationData... optData) {
64          // Perform optimization.
65          return super.optimize(optData);
66      }
67  
68      /**
69       * Scans the list of (required and optional) optimization data that
70       * characterize the problem.
71       *
72       * @param optData Optimization data. The following data will be looked for:
73       * <ul>
74       *  <li>{@link InitialGuess}</li>
75       *  <li>{@link SimpleBounds}</li>
76       * </ul>
77       */
78      @Override
79      protected void parseOptimizationData(OptimizationData... optData) {
80          // Allow base class to register its own data.
81          super.parseOptimizationData(optData);
82  
83          // The existing values (as set by the previous call) are reused if
84          // not provided in the argument list.
85          for (OptimizationData data : optData) {
86              if (data instanceof InitialGuess) {
87                  start = ((InitialGuess) data).getInitialGuess();
88                  continue;
89              }
90              if (data instanceof SimpleBounds) {
91                  final SimpleBounds bounds = (SimpleBounds) data;
92                  lowerBound = bounds.getLower();
93                  upperBound = bounds.getUpper();
94                  continue;
95              }
96          }
97  
98          // Check input consistency.
99          checkParameters();
100     }
101 
102     /**
103      * Gets the initial guess.
104      *
105      * @return the initial guess, or {@code null} if not set.
106      */
107     public double[] getStartPoint() {
108         return start == null ? null : start.clone();
109     }
110     /**
111      * @return the lower bounds, or {@code null} if not set.
112      */
113     public double[] getLowerBound() {
114         return lowerBound == null ? null : lowerBound.clone();
115     }
116     /**
117      * @return the upper bounds, or {@code null} if not set.
118      */
119     public double[] getUpperBound() {
120         return upperBound == null ? null : upperBound.clone();
121     }
122 
123     /**
124      * Check parameters consistency.
125      */
126     private void checkParameters() {
127         if (start != null) {
128             final int dim = start.length;
129             if (lowerBound != null) {
130                 if (lowerBound.length != dim) {
131                     throw new DimensionMismatchException(lowerBound.length, dim);
132                 }
133                 for (int i = 0; i < dim; i++) {
134                     final double v = start[i];
135                     final double lo = lowerBound[i];
136                     if (v < lo) {
137                         throw new NumberIsTooSmallException(v, lo, true);
138                     }
139                 }
140             }
141             if (upperBound != null) {
142                 if (upperBound.length != dim) {
143                     throw new DimensionMismatchException(upperBound.length, dim);
144                 }
145                 for (int i = 0; i < dim; i++) {
146                     final double v = start[i];
147                     final double hi = upperBound[i];
148                     if (v > hi) {
149                         throw new NumberIsTooLargeException(v, hi, true);
150                     }
151                 }
152             }
153         }
154     }
155 }