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 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.optim.nonlinear.scalar; 23 24 import org.hipparchus.analysis.MultivariateFunction; 25 import org.hipparchus.exception.MathIllegalStateException; 26 import org.hipparchus.optim.BaseMultivariateOptimizer; 27 import org.hipparchus.optim.ConvergenceChecker; 28 import org.hipparchus.optim.OptimizationData; 29 import org.hipparchus.optim.PointValuePair; 30 31 /** 32 * Base class for a multivariate scalar function optimizer. 33 * 34 */ 35 public abstract class MultivariateOptimizer 36 extends BaseMultivariateOptimizer<PointValuePair> { 37 /** Objective function. */ 38 private MultivariateFunction function; 39 /** Type of optimization. */ 40 private GoalType goal; 41 42 /** Simple constructor. 43 * @param checker Convergence checker. 44 */ 45 protected MultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) { 46 super(checker); 47 } 48 49 /** 50 * {@inheritDoc} 51 * 52 * @param optData Optimization data. In addition to those documented in 53 * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[]) 54 * BaseMultivariateOptimizer}, this method will register the following data: 55 * <ul> 56 * <li>{@link ObjectiveFunction}</li> 57 * <li>{@link GoalType}</li> 58 * </ul> 59 * @return {@inheritDoc} 60 * @throws MathIllegalStateException if the maximal number of 61 * evaluations is exceeded. 62 */ 63 @Override 64 public PointValuePair optimize(OptimizationData... optData) 65 throws MathIllegalStateException { 66 // Set up base class and perform computation. 67 return super.optimize(optData); 68 } 69 70 /** 71 * Scans the list of (required and optional) optimization data that 72 * characterize the problem. 73 * 74 * @param optData Optimization data. 75 * The following data will be looked for: 76 * <ul> 77 * <li>{@link ObjectiveFunction}</li> 78 * <li>{@link GoalType}</li> 79 * </ul> 80 */ 81 @Override 82 protected void parseOptimizationData(OptimizationData... optData) { 83 // Allow base class to register its own data. 84 super.parseOptimizationData(optData); 85 86 // The existing values (as set by the previous call) are reused if 87 // not provided in the argument list. 88 for (OptimizationData data : optData) { 89 if (data instanceof GoalType) { 90 goal = (GoalType) data; 91 continue; 92 } 93 if (data instanceof ObjectiveFunction) { 94 function = ((ObjectiveFunction) data).getObjectiveFunction(); 95 continue; 96 } 97 } 98 } 99 100 /** Get optimization type. 101 * @return the optimization type. 102 */ 103 public GoalType getGoalType() { 104 return goal; 105 } 106 107 /** 108 * Computes the objective function value. 109 * This method <em>must</em> be called by subclasses to enforce the 110 * evaluation counter limit. 111 * 112 * @param params Point at which the objective function must be evaluated. 113 * @return the objective function value at the specified point. 114 * @throws MathIllegalStateException if the maximal number of 115 * evaluations is exceeded. 116 */ 117 public double computeObjectiveValue(double[] params) { 118 super.incrementEvaluationCount(); 119 return function.value(params); 120 } 121 }