MultivariateVectorOptimizer.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.exception.TooManyEvaluationsException;
  19. import org.apache.commons.math3.exception.DimensionMismatchException;
  20. import org.apache.commons.math3.analysis.MultivariateVectorFunction;
  21. import org.apache.commons.math3.optim.OptimizationData;
  22. import org.apache.commons.math3.optim.BaseMultivariateOptimizer;
  23. import org.apache.commons.math3.optim.ConvergenceChecker;
  24. import org.apache.commons.math3.optim.PointVectorValuePair;
  25. import org.apache.commons.math3.linear.RealMatrix;

  26. /**
  27.  * Base class for a multivariate vector function optimizer.
  28.  *
  29.  * @since 3.1
  30.  */
  31. @Deprecated
  32. public abstract class MultivariateVectorOptimizer
  33.     extends BaseMultivariateOptimizer<PointVectorValuePair> {
  34.     /** Target values for the model function at optimum. */
  35.     private double[] target;
  36.     /** Weight matrix. */
  37.     private RealMatrix weightMatrix;
  38.     /** Model function. */
  39.     private MultivariateVectorFunction model;

  40.     /**
  41.      * @param checker Convergence checker.
  42.      */
  43.     protected MultivariateVectorOptimizer(ConvergenceChecker<PointVectorValuePair> checker) {
  44.         super(checker);
  45.     }

  46.     /**
  47.      * Computes the objective function value.
  48.      * This method <em>must</em> be called by subclasses to enforce the
  49.      * evaluation counter limit.
  50.      *
  51.      * @param params Point at which the objective function must be evaluated.
  52.      * @return the objective function value at the specified point.
  53.      * @throws TooManyEvaluationsException if the maximal number of evaluations
  54.      * (of the model vector function) is exceeded.
  55.      */
  56.     protected double[] computeObjectiveValue(double[] params) {
  57.         super.incrementEvaluationCount();
  58.         return model.value(params);
  59.     }

  60.     /**
  61.      * {@inheritDoc}
  62.      *
  63.      * @param optData Optimization data. In addition to those documented in
  64.      * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[])
  65.      * BaseMultivariateOptimizer}, this method will register the following data:
  66.      * <ul>
  67.      *  <li>{@link Target}</li>
  68.      *  <li>{@link Weight}</li>
  69.      *  <li>{@link ModelFunction}</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.      * Gets the weight matrix of the observations.
  86.      *
  87.      * @return the weight matrix.
  88.      */
  89.     public RealMatrix getWeight() {
  90.         return weightMatrix.copy();
  91.     }
  92.     /**
  93.      * Gets the observed values to be matched by the objective vector
  94.      * function.
  95.      *
  96.      * @return the target values.
  97.      */
  98.     public double[] getTarget() {
  99.         return target.clone();
  100.     }

  101.     /**
  102.      * Gets the number of observed values.
  103.      *
  104.      * @return the length of the target vector.
  105.      */
  106.     public int getTargetSize() {
  107.         return target.length;
  108.     }

  109.     /**
  110.      * Scans the list of (required and optional) optimization data that
  111.      * characterize the problem.
  112.      *
  113.      * @param optData Optimization data. The following data will be looked for:
  114.      * <ul>
  115.      *  <li>{@link Target}</li>
  116.      *  <li>{@link Weight}</li>
  117.      *  <li>{@link ModelFunction}</li>
  118.      * </ul>
  119.      */
  120.     @Override
  121.     protected void parseOptimizationData(OptimizationData... optData) {
  122.         // Allow base class to register its own data.
  123.         super.parseOptimizationData(optData);

  124.         // The existing values (as set by the previous call) are reused if
  125.         // not provided in the argument list.
  126.         for (OptimizationData data : optData) {
  127.             if (data instanceof ModelFunction) {
  128.                 model = ((ModelFunction) data).getModelFunction();
  129.                 continue;
  130.             }
  131.             if (data instanceof Target) {
  132.                 target = ((Target) data).getTarget();
  133.                 continue;
  134.             }
  135.             if (data instanceof Weight) {
  136.                 weightMatrix = ((Weight) data).getWeight();
  137.                 continue;
  138.             }
  139.         }

  140.         // Check input consistency.
  141.         checkParameters();
  142.     }

  143.     /**
  144.      * Check parameters consistency.
  145.      *
  146.      * @throws DimensionMismatchException if {@link #target} and
  147.      * {@link #weightMatrix} have inconsistent dimensions.
  148.      */
  149.     private void checkParameters() {
  150.         if (target.length != weightMatrix.getColumnDimension()) {
  151.             throw new DimensionMismatchException(target.length,
  152.                                                  weightMatrix.getColumnDimension());
  153.         }
  154.     }
  155. }