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.  *      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.math3.optim;

  18. import org.apache.commons.math3.exception.TooManyEvaluationsException;
  19. import org.apache.commons.math3.exception.TooManyIterationsException;
  20. import org.apache.commons.math3.util.Incrementor;

  21. /**
  22.  * Base class for implementing optimization problems. It contains the boiler-plate code
  23.  * for counting the number of evaluations of the objective function and the number of
  24.  * iterations of the algorithm, and storing the convergence checker.
  25.  *
  26.  * @param <PAIR> Type of the point/value pair returned by the optimization algorithm.
  27.  * @since 3.3
  28.  */
  29. public abstract class AbstractOptimizationProblem<PAIR>
  30.         implements OptimizationProblem<PAIR> {

  31.     /** Callback to use for the evaluation counter. */
  32.     private static final MaxEvalCallback MAX_EVAL_CALLBACK = new MaxEvalCallback();
  33.     /** Callback to use for the iteration counter. */
  34.     private static final MaxIterCallback MAX_ITER_CALLBACK = new MaxIterCallback();

  35.     /** max evaluations */
  36.     private final int maxEvaluations;
  37.     /** max iterations */
  38.     private final int maxIterations;
  39.     /** Convergence checker. */
  40.     private final ConvergenceChecker<PAIR> checker;

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

  55.     /** {@inheritDoc} */
  56.     public Incrementor getEvaluationCounter() {
  57.         return new Incrementor(this.maxEvaluations, MAX_EVAL_CALLBACK);
  58.     }

  59.     /** {@inheritDoc} */
  60.     public Incrementor getIterationCounter() {
  61.         return new Incrementor(this.maxIterations, MAX_ITER_CALLBACK);
  62.     }

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

  67.     /** Defines the action to perform when reaching the maximum number of evaluations. */
  68.     private static class MaxEvalCallback
  69.             implements Incrementor.MaxCountExceededCallback {
  70.         /**
  71.          * {@inheritDoc}
  72.          *
  73.          * @throws TooManyEvaluationsException
  74.          */
  75.         public void trigger(int max) {
  76.             throw new TooManyEvaluationsException(max);
  77.         }
  78.     }

  79.     /** Defines the action to perform when reaching the maximum number of evaluations. */
  80.     private static class MaxIterCallback
  81.             implements Incrementor.MaxCountExceededCallback {
  82.         /**
  83.          * {@inheritDoc}
  84.          *
  85.          * @throws TooManyIterationsException
  86.          */
  87.         public void trigger(int max) {
  88.             throw new TooManyIterationsException(max);
  89.         }
  90.     }

  91. }