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 */ 017package org.apache.commons.math4.legacy.optim.nonlinear.scalar; 018 019import org.apache.commons.math4.legacy.analysis.MultivariateFunction; 020import org.apache.commons.math4.legacy.optim.BaseMultivariateOptimizer; 021import org.apache.commons.math4.legacy.optim.ConvergenceChecker; 022import org.apache.commons.math4.legacy.optim.OptimizationData; 023import org.apache.commons.math4.legacy.optim.PointValuePair; 024 025/** 026 * Base class for a multivariate scalar function optimizer. 027 * 028 * @since 3.1 029 */ 030public abstract class MultivariateOptimizer 031 extends BaseMultivariateOptimizer<PointValuePair> { 032 /** Objective function. */ 033 private MultivariateFunction function; 034 /** Type of optimization. */ 035 private GoalType goal; 036 037 /** 038 * @param checker Convergence checker. 039 */ 040 protected MultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) { 041 super(checker); 042 } 043 044 /** 045 * {@inheritDoc} 046 * 047 * @param optData Optimization data. In addition to those documented in 048 * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[]) 049 * BaseMultivariateOptimizer}, this method will register the following data: 050 * <ul> 051 * <li>{@link ObjectiveFunction}</li> 052 * <li>{@link GoalType}</li> 053 * </ul> 054 * @return {@inheritDoc} 055 * @throws org.apache.commons.math4.legacy.exception.TooManyEvaluationsException 056 * if the maximal number of evaluations is exceeded. 057 */ 058 @Override 059 public PointValuePair optimize(OptimizationData... optData) { 060 // Set up base class and perform computation. 061 return super.optimize(optData); 062 } 063 064 /** 065 * Scans the list of (required and optional) optimization data that 066 * characterize the problem. 067 * 068 * @param optData Optimization data. 069 * The following data will be looked for: 070 * <ul> 071 * <li>{@link ObjectiveFunction}</li> 072 * <li>{@link GoalType}</li> 073 * </ul> 074 */ 075 @Override 076 protected void parseOptimizationData(OptimizationData... optData) { 077 // Allow base class to register its own data. 078 super.parseOptimizationData(optData); 079 080 // The existing values (as set by the previous call) are reused if 081 // not provided in the argument list. 082 for (OptimizationData data : optData) { 083 if (data instanceof GoalType) { 084 goal = (GoalType) data; 085 continue; 086 } 087 if (data instanceof ObjectiveFunction) { 088 function = ((ObjectiveFunction) data).getObjectiveFunction(); 089 continue; 090 } 091 } 092 } 093 094 /** 095 * @return the optimization type. 096 */ 097 public GoalType getGoalType() { 098 return goal; 099 } 100 101 /** 102 * Computes the objective function value. 103 * This method <em>must</em> be called by subclasses to enforce the 104 * evaluation counter limit. 105 * 106 * @param params Point at which the objective function must be evaluated. 107 * @return the objective function value at the specified point. 108 * @throws org.apache.commons.math4.legacy.exception.TooManyEvaluationsException 109 * if the maximal number of evaluations is exceeded. 110 */ 111 public double computeObjectiveValue(double[] params) { 112 super.incrementEvaluationCount(); 113 return function.value(params); 114 } 115}