BaseMultivariateOptimizer.java
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /*
- * This is not the original file distributed by the Apache Software Foundation
- * It has been modified by the Hipparchus project
- */
- package org.hipparchus.optim;
- import org.hipparchus.exception.LocalizedCoreFormats;
- import org.hipparchus.exception.MathIllegalArgumentException;
- /**
- * Base class for implementing optimizers for multivariate functions.
- * It contains the boiler-plate code for initial guess and bounds
- * specifications.
- * <em>It is not a "user" class.</em>
- *
- * @param <P> Type of the point/value pair returned by the optimization
- * algorithm.
- *
- */
- public abstract class BaseMultivariateOptimizer<P>
- extends BaseOptimizer<P> {
- /** Initial guess. */
- private double[] start;
- /** Lower bounds. */
- private double[] lowerBound;
- /** Upper bounds. */
- private double[] upperBound;
- /** Simple constructor.
- * @param checker Convergence checker.
- */
- protected BaseMultivariateOptimizer(ConvergenceChecker<P> checker) {
- super(checker);
- }
- /**
- * {@inheritDoc}
- *
- * @param optData Optimization data. In addition to those documented in
- * {@link BaseOptimizer#parseOptimizationData(OptimizationData[]) BaseOptimizer},
- * this method will register the following data:
- * <ul>
- * <li>{@link InitialGuess}</li>
- * <li>{@link SimpleBounds}</li>
- * </ul>
- * @return {@inheritDoc}
- */
- @Override
- public P optimize(OptimizationData... optData) {
- // Perform optimization.
- return super.optimize(optData);
- }
- /**
- * Scans the list of (required and optional) optimization data that
- * characterize the problem.
- *
- * @param optData Optimization data. The following data will be looked for:
- * <ul>
- * <li>{@link InitialGuess}</li>
- * <li>{@link SimpleBounds}</li>
- * </ul>
- */
- @Override
- protected void parseOptimizationData(OptimizationData... optData) {
- // Allow base class to register its own data.
- super.parseOptimizationData(optData);
- // The existing values (as set by the previous call) are reused if
- // not provided in the argument list.
- for (OptimizationData data : optData) {
- if (data instanceof InitialGuess) {
- start = ((InitialGuess) data).getInitialGuess();
- continue;
- }
- if (data instanceof SimpleBounds) {
- final SimpleBounds bounds = (SimpleBounds) data;
- lowerBound = bounds.getLower();
- upperBound = bounds.getUpper();
- continue;
- }
- }
- // Check input consistency.
- checkParameters();
- }
- /**
- * Gets the initial guess.
- *
- * @return the initial guess, or {@code null} if not set.
- */
- public double[] getStartPoint() {
- return start == null ? null : start.clone();
- }
- /** Get lower bounds.
- * @return the lower bounds, or {@code null} if not set.
- */
- public double[] getLowerBound() {
- return lowerBound == null ? null : lowerBound.clone();
- }
- /** Get upper bounds.
- * @return the upper bounds, or {@code null} if not set.
- */
- public double[] getUpperBound() {
- return upperBound == null ? null : upperBound.clone();
- }
- /**
- * Check parameters consistency.
- */
- private void checkParameters() {
- if (start != null) {
- final int dim = start.length;
- if (lowerBound != null) {
- if (lowerBound.length != dim) {
- throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
- lowerBound.length, dim);
- }
- for (int i = 0; i < dim; i++) {
- final double v = start[i];
- final double lo = lowerBound[i];
- if (v < lo) {
- throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
- v, lo);
- }
- }
- }
- if (upperBound != null) {
- if (upperBound.length != dim) {
- throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
- upperBound.length, dim);
- }
- for (int i = 0; i < dim; i++) {
- final double v = start[i];
- final double hi = upperBound[i];
- if (v > hi) {
- throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
- v, hi);
- }
- }
- }
- }
- }
- }