MultiStartMultivariateVectorOptimizer.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.  *      http://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. package org.apache.commons.math3.optim.nonlinear.vector;

  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.ArrayList;
  21. import java.util.Comparator;
  22. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  23. import org.apache.commons.math3.exception.NullArgumentException;
  24. import org.apache.commons.math3.linear.RealMatrix;
  25. import org.apache.commons.math3.linear.RealVector;
  26. import org.apache.commons.math3.linear.ArrayRealVector;
  27. import org.apache.commons.math3.random.RandomVectorGenerator;
  28. import org.apache.commons.math3.optim.BaseMultiStartMultivariateOptimizer;
  29. import org.apache.commons.math3.optim.PointVectorValuePair;

  30. /**
  31.  * Multi-start optimizer for a (vector) model function.
  32.  *
  33.  * This class wraps an optimizer in order to use it several times in
  34.  * turn with different starting points (trying to avoid being trapped
  35.  * in a local extremum when looking for a global one).
  36.  *
  37.  * @since 3.0
  38.  */
  39. @Deprecated
  40. public class MultiStartMultivariateVectorOptimizer
  41.     extends BaseMultiStartMultivariateOptimizer<PointVectorValuePair> {
  42.     /** Underlying optimizer. */
  43.     private final MultivariateVectorOptimizer optimizer;
  44.     /** Found optima. */
  45.     private final List<PointVectorValuePair> optima = new ArrayList<PointVectorValuePair>();

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

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     @Override
  70.     public PointVectorValuePair[] getOptima() {
  71.         Collections.sort(optima, getPairComparator());
  72.         return optima.toArray(new PointVectorValuePair[0]);
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     protected void store(PointVectorValuePair optimum) {
  79.         optima.add(optimum);
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     @Override
  85.     protected void clear() {
  86.         optima.clear();
  87.     }

  88.     /**
  89.      * @return a comparator for sorting the optima.
  90.      */
  91.     private Comparator<PointVectorValuePair> getPairComparator() {
  92.         return new Comparator<PointVectorValuePair>() {
  93.             private final RealVector target = new ArrayRealVector(optimizer.getTarget(), false);
  94.             private final RealMatrix weight = optimizer.getWeight();

  95.             public int compare(final PointVectorValuePair o1,
  96.                                final PointVectorValuePair o2) {
  97.                 if (o1 == null) {
  98.                     return (o2 == null) ? 0 : 1;
  99.                 } else if (o2 == null) {
  100.                     return -1;
  101.                 }
  102.                 return Double.compare(weightedResidual(o1),
  103.                                       weightedResidual(o2));
  104.             }

  105.             private double weightedResidual(final PointVectorValuePair pv) {
  106.                 final RealVector v = new ArrayRealVector(pv.getValueRef(), false);
  107.                 final RealVector r = target.subtract(v);
  108.                 return r.dotProduct(weight.operate(r));
  109.             }
  110.         };
  111.     }
  112. }