AbstractDifferentiableOptimizer.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.optimization.general;

  18. import org.apache.commons.math3.analysis.MultivariateVectorFunction;
  19. import org.apache.commons.math3.analysis.differentiation.GradientFunction;
  20. import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
  21. import org.apache.commons.math3.optimization.ConvergenceChecker;
  22. import org.apache.commons.math3.optimization.GoalType;
  23. import org.apache.commons.math3.optimization.OptimizationData;
  24. import org.apache.commons.math3.optimization.InitialGuess;
  25. import org.apache.commons.math3.optimization.PointValuePair;
  26. import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer;

  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.  * @deprecated As of 3.1 (to be removed in 4.0).
  33.  * @since 3.1
  34.  */
  35. @Deprecated
  36. public abstract class AbstractDifferentiableOptimizer
  37.     extends BaseAbstractMultivariateOptimizer<MultivariateDifferentiableFunction> {
  38.     /**
  39.      * Objective function gradient.
  40.      */
  41.     private MultivariateVectorFunction gradient;

  42.     /**
  43.      * @param checker Convergence checker.
  44.      */
  45.     protected AbstractDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
  46.         super(checker);
  47.     }

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

  57.     /**
  58.      * {@inheritDoc}
  59.      *
  60.      * @deprecated In 3.1. Please use
  61.      * {@link #optimizeInternal(int,MultivariateDifferentiableFunction,GoalType,OptimizationData[])}
  62.      * instead.
  63.      */
  64.     @Override@Deprecated
  65.     protected PointValuePair optimizeInternal(final int maxEval,
  66.                                               final MultivariateDifferentiableFunction f,
  67.                                               final GoalType goalType,
  68.                                               final double[] startPoint) {
  69.         return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     protected PointValuePair optimizeInternal(final int maxEval,
  74.                                               final MultivariateDifferentiableFunction f,
  75.                                               final GoalType goalType,
  76.                                               final OptimizationData... optData) {
  77.         // Store optimization problem characteristics.
  78.         gradient = new GradientFunction(f);

  79.         // Perform optimization.
  80.         return super.optimizeInternal(maxEval, f, goalType, optData);
  81.     }
  82. }