AbstractScalarDifferentiableOptimizer.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.DifferentiableMultivariateFunction;
  19. import org.apache.commons.math3.analysis.MultivariateVectorFunction;
  20. import org.apache.commons.math3.analysis.FunctionUtils;
  21. import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
  22. import org.apache.commons.math3.optimization.DifferentiableMultivariateOptimizer;
  23. import org.apache.commons.math3.optimization.GoalType;
  24. import org.apache.commons.math3.optimization.ConvergenceChecker;
  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 2.0
  34.  */
  35. @Deprecated
  36. public abstract class AbstractScalarDifferentiableOptimizer
  37.     extends BaseAbstractMultivariateOptimizer<DifferentiableMultivariateFunction>
  38.     implements DifferentiableMultivariateOptimizer {
  39.     /**
  40.      * Objective function gradient.
  41.      */
  42.     private MultivariateVectorFunction gradient;

  43.     /**
  44.      * Simple constructor with default settings.
  45.      * The convergence check is set to a
  46.      * {@link org.apache.commons.math3.optimization.SimpleValueChecker
  47.      * SimpleValueChecker}.
  48.      * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()}
  49.      */
  50.     @Deprecated
  51.     protected AbstractScalarDifferentiableOptimizer() {}

  52.     /**
  53.      * @param checker Convergence checker.
  54.      */
  55.     protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
  56.         super(checker);
  57.     }

  58.     /**
  59.      * Compute the gradient vector.
  60.      *
  61.      * @param evaluationPoint Point at which the gradient must be evaluated.
  62.      * @return the gradient at the specified point.
  63.      * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
  64.      * if the allowed number of evaluations is exceeded.
  65.      */
  66.     protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
  67.         return gradient.value(evaluationPoint);
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     protected PointValuePair optimizeInternal(int maxEval,
  72.                                               final DifferentiableMultivariateFunction f,
  73.                                               final GoalType goalType,
  74.                                               final double[] startPoint) {
  75.         // Store optimization problem characteristics.
  76.         gradient = f.gradient();

  77.         return super.optimizeInternal(maxEval, f, goalType, startPoint);
  78.     }

  79.     /**
  80.      * Optimize an objective function.
  81.      *
  82.      * @param f Objective function.
  83.      * @param goalType Type of optimization goal: either
  84.      * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
  85.      * @param startPoint Start point for optimization.
  86.      * @param maxEval Maximum number of function evaluations.
  87.      * @return the point/value pair giving the optimal value for objective
  88.      * function.
  89.      * @throws org.apache.commons.math3.exception.DimensionMismatchException
  90.      * if the start point dimension is wrong.
  91.      * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
  92.      * if the maximal number of evaluations is exceeded.
  93.      * @throws org.apache.commons.math3.exception.NullArgumentException if
  94.      * any argument is {@code null}.
  95.      */
  96.     public PointValuePair optimize(final int maxEval,
  97.                                    final MultivariateDifferentiableFunction f,
  98.                                    final GoalType goalType,
  99.                                    final double[] startPoint) {
  100.         return optimizeInternal(maxEval,
  101.                                 FunctionUtils.toDifferentiableMultivariateFunction(f),
  102.                                 goalType,
  103.                                 startPoint);
  104.     }
  105. }