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.TooManyEvaluationsException;
20  import org.apache.commons.math4.legacy.exception.TooManyIterationsException;
21  import org.apache.commons.math4.legacy.core.IntegerSequence;
22  
23  /**
24   * Base class for implementing optimization problems. It contains the boiler-plate code
25   * for counting the number of evaluations of the objective function and the number of
26   * iterations of the algorithm, and storing the convergence checker.
27   *
28   * @param <PAIR> Type of the point/value pair returned by the optimization algorithm.
29   * @since 3.3
30   */
31  public abstract class AbstractOptimizationProblem<PAIR>
32          implements OptimizationProblem<PAIR> {
33  
34      /** Callback to use for the evaluation counter. */
35      private static final MaxEvalCallback MAX_EVAL_CALLBACK = new MaxEvalCallback();
36      /** Callback to use for the iteration counter. */
37      private static final MaxIterCallback MAX_ITER_CALLBACK = new MaxIterCallback();
38  
39      /** max evaluations. */
40      private final int maxEvaluations;
41      /** max iterations. */
42      private final int maxIterations;
43      /** Convergence checker. */
44      private final ConvergenceChecker<PAIR> checker;
45  
46      /**
47       * Create an {@link AbstractOptimizationProblem} from the given data.
48       *
49       * @param maxEvaluations the number of allowed model function evaluations.
50       * @param maxIterations  the number of allowed iterations.
51       * @param checker        the convergence checker.
52       */
53      protected AbstractOptimizationProblem(final int maxEvaluations,
54                                            final int maxIterations,
55                                            final ConvergenceChecker<PAIR> checker) {
56          this.maxEvaluations = maxEvaluations;
57          this.maxIterations = maxIterations;
58          this.checker = checker;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public IntegerSequence.Incrementor getEvaluationCounter() {
64          return IntegerSequence.Incrementor.create()
65              .withMaximalCount(maxEvaluations)
66              .withCallback(MAX_EVAL_CALLBACK);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public IntegerSequence.Incrementor getIterationCounter() {
72          return IntegerSequence.Incrementor.create()
73              .withMaximalCount(maxIterations)
74              .withCallback(MAX_ITER_CALLBACK);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public ConvergenceChecker<PAIR> getConvergenceChecker() {
80          return checker;
81      }
82  
83      /** Defines the action to perform when reaching the maximum number of evaluations. */
84      private static final class MaxEvalCallback
85          implements IntegerSequence.Incrementor.MaxCountExceededCallback {
86          /**
87           * {@inheritDoc}
88           *
89           * @throws TooManyEvaluationsException
90           */
91          @Override
92          public void trigger(int max) {
93              throw new TooManyEvaluationsException(max);
94          }
95      }
96  
97      /** Defines the action to perform when reaching the maximum number of evaluations. */
98      private static final class MaxIterCallback
99          implements IntegerSequence.Incrementor.MaxCountExceededCallback {
100         /**
101          * {@inheritDoc}
102          *
103          * @throws TooManyIterationsException
104          */
105         @Override
106         public void trigger(int max) {
107             throw new TooManyIterationsException(max);
108         }
109     }
110 }