AbstractOptimizationProblem.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim;

  22. import org.hipparchus.util.Incrementor;

  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 <P> Type of the point/value pair returned by the optimization algorithm.
  29.  */
  30. public abstract class AbstractOptimizationProblem<P>
  31.         implements OptimizationProblem<P> {

  32.     /** max evaluations */
  33.     private final int maxEvaluations;
  34.     /** max iterations */
  35.     private final int maxIterations;
  36.     /** Convergence checker. */
  37.     private final ConvergenceChecker<P> checker;

  38.     /**
  39.      * Create an {@link AbstractOptimizationProblem} from the given data.
  40.      *
  41.      * @param maxEvaluations the number of allowed model function evaluations.
  42.      * @param maxIterations  the number of allowed iterations.
  43.      * @param checker        the convergence checker.
  44.      */
  45.     protected AbstractOptimizationProblem(final int maxEvaluations,
  46.                                           final int maxIterations,
  47.                                           final ConvergenceChecker<P> checker) {
  48.         this.maxEvaluations = maxEvaluations;
  49.         this.maxIterations = maxIterations;
  50.         this.checker = checker;
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public Incrementor getEvaluationCounter() {
  55.         return new Incrementor(this.maxEvaluations);
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public Incrementor getIterationCounter() {
  60.         return new Incrementor(this.maxIterations);
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public ConvergenceChecker<P> getConvergenceChecker() {
  65.         return checker;
  66.     }
  67. }