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