MultivariateOptimizer.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.MultivariateFunction;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.optim.BaseMultivariateOptimizer;
  25. import org.hipparchus.optim.ConvergenceChecker;
  26. import org.hipparchus.optim.OptimizationData;
  27. import org.hipparchus.optim.PointValuePair;

  28. /**
  29.  * Base class for a multivariate scalar function optimizer.
  30.  *
  31.  */
  32. public abstract class MultivariateOptimizer
  33.     extends BaseMultivariateOptimizer<PointValuePair> {
  34.     /** Objective function. */
  35.     private MultivariateFunction function;
  36.     /** Type of optimization. */
  37.     private GoalType goal;

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

  44.     /**
  45.      * {@inheritDoc}
  46.      *
  47.      * @param optData Optimization data. In addition to those documented in
  48.      * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[])
  49.      * BaseMultivariateOptimizer}, this method will register the following data:
  50.      * <ul>
  51.      *  <li>{@link ObjectiveFunction}</li>
  52.      *  <li>{@link GoalType}</li>
  53.      * </ul>
  54.      * @return {@inheritDoc}
  55.      * @throws MathIllegalStateException if the maximal number of
  56.      * evaluations is exceeded.
  57.      */
  58.     @Override
  59.     public PointValuePair optimize(OptimizationData... optData)
  60.         throws MathIllegalStateException {
  61.         // Set up base class and perform computation.
  62.         return super.optimize(optData);
  63.     }

  64.     /**
  65.      * Scans the list of (required and optional) optimization data that
  66.      * characterize the problem.
  67.      *
  68.      * @param optData Optimization data.
  69.      * The following data will be looked for:
  70.      * <ul>
  71.      *  <li>{@link ObjectiveFunction}</li>
  72.      *  <li>{@link GoalType}</li>
  73.      * </ul>
  74.      */
  75.     @Override
  76.     protected void parseOptimizationData(OptimizationData... optData) {
  77.         // Allow base class to register its own data.
  78.         super.parseOptimizationData(optData);

  79.         // The existing values (as set by the previous call) are reused if
  80.         // not provided in the argument list.
  81.         for (OptimizationData data : optData) {
  82.             if (data instanceof GoalType) {
  83.                 goal = (GoalType) data;
  84.                 continue;
  85.             }
  86.             if (data instanceof ObjectiveFunction) {
  87.                 function = ((ObjectiveFunction) data).getObjectiveFunction();
  88.                 continue;
  89.             }
  90.         }
  91.     }

  92.     /** Get optimization type.
  93.      * @return the optimization type.
  94.      */
  95.     public GoalType getGoalType() {
  96.         return goal;
  97.     }

  98.     /**
  99.      * Computes the objective function value.
  100.      * This method <em>must</em> be called by subclasses to enforce the
  101.      * evaluation counter limit.
  102.      *
  103.      * @param params Point at which the objective function must be evaluated.
  104.      * @return the objective function value at the specified point.
  105.      * @throws MathIllegalStateException if the maximal number of
  106.      * evaluations is exceeded.
  107.      */
  108.     public double computeObjectiveValue(double[] params) {
  109.         super.incrementEvaluationCount();
  110.         return function.value(params);
  111.     }
  112. }