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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim;
23  
24  import org.hipparchus.util.Incrementor;
25  
26  /**
27   * Base class for implementing optimization problems. It contains the boiler-plate code
28   * for counting the number of evaluations of the objective function and the number of
29   * iterations of the algorithm, and storing the convergence checker.
30   *
31   * @param <P> Type of the point/value pair returned by the optimization algorithm.
32   */
33  public abstract class AbstractOptimizationProblem<P>
34          implements OptimizationProblem<P> {
35  
36      /** max evaluations */
37      private final int maxEvaluations;
38      /** max iterations */
39      private final int maxIterations;
40      /** Convergence checker. */
41      private final ConvergenceChecker<P> checker;
42  
43      /**
44       * Create an {@link AbstractOptimizationProblem} from the given data.
45       *
46       * @param maxEvaluations the number of allowed model function evaluations.
47       * @param maxIterations  the number of allowed iterations.
48       * @param checker        the convergence checker.
49       */
50      protected AbstractOptimizationProblem(final int maxEvaluations,
51                                            final int maxIterations,
52                                            final ConvergenceChecker<P> checker) {
53          this.maxEvaluations = maxEvaluations;
54          this.maxIterations = maxIterations;
55          this.checker = checker;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Incrementor getEvaluationCounter() {
61          return new Incrementor(this.maxEvaluations);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public Incrementor getIterationCounter() {
67          return new Incrementor(this.maxIterations);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public ConvergenceChecker<P> getConvergenceChecker() {
73          return checker;
74      }
75  }