JacobianMultivariateVectorOptimizer.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.optim.nonlinear.vector;

  18. import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
  19. import org.apache.commons.math3.optim.ConvergenceChecker;
  20. import org.apache.commons.math3.optim.OptimizationData;
  21. import org.apache.commons.math3.optim.PointVectorValuePair;
  22. import org.apache.commons.math3.exception.TooManyEvaluationsException;
  23. import org.apache.commons.math3.exception.DimensionMismatchException;

  24. /**
  25.  * Base class for implementing optimizers for multivariate vector
  26.  * differentiable functions.
  27.  * It contains boiler-plate code for dealing with Jacobian evaluation.
  28.  * It assumes that the rows of the Jacobian matrix iterate on the model
  29.  * functions while the columns iterate on the parameters; thus, the numbers
  30.  * of rows is equal to the dimension of the {@link Target} while the
  31.  * number of columns is equal to the dimension of the
  32.  * {@link org.apache.commons.math3.optim.InitialGuess InitialGuess}.
  33.  *
  34.  * @since 3.1
  35.  * @deprecated All classes and interfaces in this package are deprecated.
  36.  * The optimizers that were provided here were moved to the
  37.  * {@link org.apache.commons.math3.fitting.leastsquares} package
  38.  * (cf. MATH-1008).
  39.  */
  40. @Deprecated
  41. public abstract class JacobianMultivariateVectorOptimizer
  42.     extends MultivariateVectorOptimizer {
  43.     /**
  44.      * Jacobian of the model function.
  45.      */
  46.     private MultivariateMatrixFunction jacobian;

  47.     /**
  48.      * @param checker Convergence checker.
  49.      */
  50.     protected JacobianMultivariateVectorOptimizer(ConvergenceChecker<PointVectorValuePair> checker) {
  51.         super(checker);
  52.     }

  53.     /**
  54.      * Computes the Jacobian matrix.
  55.      *
  56.      * @param params Point at which the Jacobian must be evaluated.
  57.      * @return the Jacobian at the specified point.
  58.      */
  59.     protected double[][] computeJacobian(final double[] params) {
  60.         return jacobian.value(params);
  61.     }

  62.     /**
  63.      * {@inheritDoc}
  64.      *
  65.      * @param optData Optimization data. In addition to those documented in
  66.      * {@link MultivariateVectorOptimizer#optimize(OptimizationData...)}
  67.      * MultivariateOptimizer}, this method will register the following data:
  68.      * <ul>
  69.      *  <li>{@link ModelFunctionJacobian}</li>
  70.      * </ul>
  71.      * @return {@inheritDoc}
  72.      * @throws TooManyEvaluationsException if the maximal number of
  73.      * evaluations is exceeded.
  74.      * @throws DimensionMismatchException if the initial guess, target, and weight
  75.      * arguments have inconsistent dimensions.
  76.      */
  77.     @Override
  78.     public PointVectorValuePair optimize(OptimizationData... optData)
  79.         throws TooManyEvaluationsException,
  80.                DimensionMismatchException {
  81.         // Set up base class and perform computation.
  82.         return super.optimize(optData);
  83.     }

  84.     /**
  85.      * Scans the list of (required and optional) optimization data that
  86.      * characterize the problem.
  87.      *
  88.      * @param optData Optimization data.
  89.      * The following data will be looked for:
  90.      * <ul>
  91.      *  <li>{@link ModelFunctionJacobian}</li>
  92.      * </ul>
  93.      */
  94.     @Override
  95.     protected void parseOptimizationData(OptimizationData... optData) {
  96.         // Allow base class to register its own data.
  97.         super.parseOptimizationData(optData);

  98.         // The existing values (as set by the previous call) are reused if
  99.         // not provided in the argument list.
  100.         for (OptimizationData data : optData) {
  101.             if (data instanceof ModelFunctionJacobian) {
  102.                 jacobian = ((ModelFunctionJacobian) data).getModelFunctionJacobian();
  103.                 // If more data must be parsed, this statement _must_ be
  104.                 // changed to "continue".
  105.                 break;
  106.             }
  107.         }
  108.     }
  109. }