GradientMultivariateOptimizer.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.nonlinear.scalar;

  18. import org.apache.commons.math4.legacy.analysis.MultivariateVectorFunction;
  19. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
  20. import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
  21. import org.apache.commons.math4.legacy.optim.OptimizationData;
  22. import org.apache.commons.math4.legacy.optim.PointValuePair;

  23. /**
  24.  * Base class for implementing optimizers for multivariate scalar
  25.  * differentiable functions.
  26.  * It contains boiler-plate code for dealing with gradient evaluation.
  27.  *
  28.  * @since 3.1
  29.  */
  30. public abstract class GradientMultivariateOptimizer
  31.     extends MultivariateOptimizer {
  32.     /**
  33.      * Gradient of the objective function.
  34.      */
  35.     private MultivariateVectorFunction gradient;

  36.     /**
  37.      * @param checker Convergence checker.
  38.      */
  39.     protected GradientMultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) {
  40.         super(checker);
  41.     }

  42.     /**
  43.      * Compute the gradient vector.
  44.      *
  45.      * @param params Point at which the gradient must be evaluated.
  46.      * @return the gradient at the specified point.
  47.      */
  48.     protected double[] computeObjectiveGradient(final double[] params) {
  49.         return gradient.value(params);
  50.     }

  51.     /**
  52.      * {@inheritDoc}
  53.      *
  54.      * @param optData Optimization data. In addition to those documented in
  55.      * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
  56.      * MultivariateOptimizer}, this method will register the following data:
  57.      * <ul>
  58.      *  <li>{@link ObjectiveFunctionGradient}</li>
  59.      * </ul>
  60.      * @return {@inheritDoc}
  61.      * @throws TooManyEvaluationsException if the maximal number of
  62.      * evaluations (of the objective function) is exceeded.
  63.      */
  64.     @Override
  65.     public PointValuePair optimize(OptimizationData... optData)
  66.         throws TooManyEvaluationsException {
  67.         // Set up base class and perform computation.
  68.         return super.optimize(optData);
  69.     }

  70.     /**
  71.      * Scans the list of (required and optional) optimization data that
  72.      * characterize the problem.
  73.      *
  74.      * @param optData Optimization data.
  75.      * The following data will be looked for:
  76.      * <ul>
  77.      *  <li>{@link ObjectiveFunctionGradient}</li>
  78.      * </ul>
  79.      */
  80.     @Override
  81.     protected void parseOptimizationData(OptimizationData... optData) {
  82.         // Allow base class to register its own data.
  83.         super.parseOptimizationData(optData);

  84.         // The existing values (as set by the previous call) are reused if
  85.         // not provided in the argument list.
  86.         for (OptimizationData data : optData) {
  87.             if  (data instanceof ObjectiveFunctionGradient) {
  88.                 gradient = ((ObjectiveFunctionGradient) data).getObjectiveFunctionGradient();
  89.                 // If more data must be parsed, this statement _must_ be
  90.                 // changed to "continue".
  91.                 break;
  92.             }
  93.         }
  94.     }
  95. }