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

  22. import org.hipparchus.analysis.MultivariateVectorFunction;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.optim.ConvergenceChecker;
  25. import org.hipparchus.optim.OptimizationData;
  26. import org.hipparchus.optim.PointValuePair;

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

  39.     /** Simple constructor.
  40.      * @param checker Convergence checker.
  41.      */
  42.     protected GradientMultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) {
  43.         super(checker);
  44.     }

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

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

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

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