MultiStartMultivariateOptimizer.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.nonlinear.scalar;

  22. import java.util.ArrayList;
  23. import java.util.Comparator;
  24. import java.util.List;

  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.exception.NullArgumentException;
  27. import org.hipparchus.optim.BaseMultiStartMultivariateOptimizer;
  28. import org.hipparchus.optim.PointValuePair;
  29. import org.hipparchus.random.RandomVectorGenerator;

  30. /**
  31.  * Multi-start optimizer.
  32.  * This class wraps an optimizer in order to use it several times in
  33.  * turn with different starting points (trying to avoid being trapped
  34.  * in a local extremum when looking for a global one).
  35.  *
  36.  */
  37. public class MultiStartMultivariateOptimizer
  38.     extends BaseMultiStartMultivariateOptimizer<PointValuePair> {
  39.     /** Underlying optimizer. */
  40.     private final MultivariateOptimizer optimizer;
  41.     /** Found optima. */
  42.     private final List<PointValuePair> optima;

  43.     /**
  44.      * Create a multi-start optimizer from a single-start optimizer.
  45.      *
  46.      * @param optimizer Single-start optimizer to wrap.
  47.      * @param starts Number of starts to perform.
  48.      * If {@code starts == 1}, the result will be same as if {@code optimizer}
  49.      * is called directly.
  50.      * @param generator Random vector generator to use for restarts.
  51.      * @throws NullArgumentException if {@code optimizer} or {@code generator}
  52.      * is {@code null}.
  53.      * @throws MathIllegalArgumentException if {@code starts < 1}.
  54.      */
  55.     public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer,
  56.                                            final int starts,
  57.                                            final RandomVectorGenerator generator)
  58.         throws MathIllegalArgumentException, NullArgumentException {
  59.         super(optimizer, starts, generator);
  60.         this.optimizer = optimizer;
  61.         this.optima   = new ArrayList<>();
  62.     }

  63.     /**
  64.      * {@inheritDoc}
  65.      */
  66.     @Override
  67.     public PointValuePair[] getOptima() {
  68.         optima.sort(getPairComparator());
  69.         return optima.toArray(new PointValuePair[0]);
  70.     }

  71.     /**
  72.      * {@inheritDoc}
  73.      */
  74.     @Override
  75.     protected void store(PointValuePair optimum) {
  76.         optima.add(optimum);
  77.     }

  78.     /**
  79.      * {@inheritDoc}
  80.      */
  81.     @Override
  82.     protected void clear() {
  83.         optima.clear();
  84.     }

  85.     /**
  86.      * @return a comparator for sorting the optima.
  87.      */
  88.     private Comparator<PointValuePair> getPairComparator() {
  89.         return new Comparator<PointValuePair>() {
  90.             /** {@inheritDoc} */
  91.             @Override
  92.             public int compare(final PointValuePair o1,
  93.                                final PointValuePair o2) {
  94.                 if (o1 == null) {
  95.                     return (o2 == null) ? 0 : 1;
  96.                 } else if (o2 == null) {
  97.                     return -1;
  98.                 }
  99.                 final double v1 = o1.getValue();
  100.                 final double v2 = o2.getValue();
  101.                 return (optimizer.getGoalType() == GoalType.MINIMIZE) ?
  102.                     Double.compare(v1, v2) : Double.compare(v2, v1);
  103.             }
  104.         };
  105.     }
  106. }