001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math3.optim.nonlinear.scalar; 018 019 import org.apache.commons.math3.analysis.MultivariateVectorFunction; 020 import org.apache.commons.math3.optim.ConvergenceChecker; 021 import org.apache.commons.math3.optim.OptimizationData; 022 import org.apache.commons.math3.optim.PointValuePair; 023 import org.apache.commons.math3.exception.TooManyEvaluationsException; 024 025 /** 026 * Base class for implementing optimizers for multivariate scalar 027 * differentiable functions. 028 * It contains boiler-plate code for dealing with gradient evaluation. 029 * 030 * @version $Id: GradientMultivariateOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ 031 * @since 3.1 032 */ 033 public abstract class GradientMultivariateOptimizer 034 extends MultivariateOptimizer { 035 /** 036 * Gradient of the objective function. 037 */ 038 private MultivariateVectorFunction gradient; 039 040 /** 041 * @param checker Convergence checker. 042 */ 043 protected GradientMultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) { 044 super(checker); 045 } 046 047 /** 048 * Compute the gradient vector. 049 * 050 * @param params Point at which the gradient must be evaluated. 051 * @return the gradient at the specified point. 052 */ 053 protected double[] computeObjectiveGradient(final double[] params) { 054 return gradient.value(params); 055 } 056 057 /** 058 * {@inheritDoc} 059 * 060 * @param optData Optimization data. In addition to those documented in 061 * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) 062 * MultivariateOptimizer}, this method will register the following data: 063 * <ul> 064 * <li>{@link ObjectiveFunctionGradient}</li> 065 * </ul> 066 * @return {@inheritDoc} 067 * @throws TooManyEvaluationsException if the maximal number of 068 * evaluations (of the objective function) is exceeded. 069 */ 070 @Override 071 public PointValuePair optimize(OptimizationData... optData) 072 throws TooManyEvaluationsException { 073 // Set up base class and perform computation. 074 return super.optimize(optData); 075 } 076 077 /** 078 * Scans the list of (required and optional) optimization data that 079 * characterize the problem. 080 * 081 * @param optData Optimization data. 082 * The following data will be looked for: 083 * <ul> 084 * <li>{@link ObjectiveFunctionGradient}</li> 085 * </ul> 086 */ 087 @Override 088 protected void parseOptimizationData(OptimizationData... optData) { 089 // Allow base class to register its own data. 090 super.parseOptimizationData(optData); 091 092 // The existing values (as set by the previous call) are reused if 093 // not provided in the argument list. 094 for (OptimizationData data : optData) { 095 if (data instanceof ObjectiveFunctionGradient) { 096 gradient = ((ObjectiveFunctionGradient) data).getObjectiveFunctionGradient(); 097 // If more data must be parsed, this statement _must_ be 098 // changed to "continue". 099 break; 100 } 101 } 102 } 103 }