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.math4.legacy.optim;

  18. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
  19. import org.apache.commons.math4.legacy.exception.TooManyIterationsException;
  20. import org.apache.commons.math4.legacy.core.IntegerSequence;

  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.     @Override
  57.     public IntegerSequence.Incrementor getEvaluationCounter() {
  58.         return IntegerSequence.Incrementor.create()
  59.             .withMaximalCount(maxEvaluations)
  60.             .withCallback(MAX_EVAL_CALLBACK);
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public IntegerSequence.Incrementor getIterationCounter() {
  65.         return IntegerSequence.Incrementor.create()
  66.             .withMaximalCount(maxIterations)
  67.             .withCallback(MAX_ITER_CALLBACK);
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public ConvergenceChecker<PAIR> getConvergenceChecker() {
  72.         return checker;
  73.     }

  74.     /** Defines the action to perform when reaching the maximum number of evaluations. */
  75.     private static final class MaxEvalCallback
  76.         implements IntegerSequence.Incrementor.MaxCountExceededCallback {
  77.         /**
  78.          * {@inheritDoc}
  79.          *
  80.          * @throws TooManyEvaluationsException
  81.          */
  82.         @Override
  83.         public void trigger(int max) {
  84.             throw new TooManyEvaluationsException(max);
  85.         }
  86.     }

  87.     /** Defines the action to perform when reaching the maximum number of evaluations. */
  88.     private static final class MaxIterCallback
  89.         implements IntegerSequence.Incrementor.MaxCountExceededCallback {
  90.         /**
  91.          * {@inheritDoc}
  92.          *
  93.          * @throws TooManyIterationsException
  94.          */
  95.         @Override
  96.         public void trigger(int max) {
  97.             throw new TooManyIterationsException(max);
  98.         }
  99.     }
  100. }