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 java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.List;
27
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.exception.NullArgumentException;
30 import org.hipparchus.optim.BaseMultiStartMultivariateOptimizer;
31 import org.hipparchus.optim.PointValuePair;
32 import org.hipparchus.random.RandomVectorGenerator;
33
34 /**
35 * Multi-start optimizer.
36 * This class wraps an optimizer in order to use it several times in
37 * turn with different starting points (trying to avoid being trapped
38 * in a local extremum when looking for a global one).
39 *
40 */
41 public class MultiStartMultivariateOptimizer
42 extends BaseMultiStartMultivariateOptimizer<PointValuePair> {
43 /** Underlying optimizer. */
44 private final MultivariateOptimizer optimizer;
45 /** Found optima. */
46 private final List<PointValuePair> optima;
47
48 /**
49 * Create a multi-start optimizer from a single-start optimizer.
50 *
51 * @param optimizer Single-start optimizer to wrap.
52 * @param starts Number of starts to perform.
53 * If {@code starts == 1}, the result will be same as if {@code optimizer}
54 * is called directly.
55 * @param generator Random vector generator to use for restarts.
56 * @throws NullArgumentException if {@code optimizer} or {@code generator}
57 * is {@code null}.
58 * @throws MathIllegalArgumentException if {@code starts < 1}.
59 */
60 public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer,
61 final int starts,
62 final RandomVectorGenerator generator)
63 throws MathIllegalArgumentException, NullArgumentException {
64 super(optimizer, starts, generator);
65 this.optimizer = optimizer;
66 this.optima = new ArrayList<>();
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public PointValuePair[] getOptima() {
74 optima.sort(getPairComparator());
75 return optima.toArray(new PointValuePair[0]);
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 protected void store(PointValuePair optimum) {
83 optima.add(optimum);
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 protected void clear() {
91 optima.clear();
92 }
93
94 /**
95 * @return a comparator for sorting the optima.
96 */
97 private Comparator<PointValuePair> getPairComparator() {
98 return new Comparator<PointValuePair>() {
99 /** {@inheritDoc} */
100 @Override
101 public int compare(final PointValuePair o1,
102 final PointValuePair o2) {
103 if (o1 == null) {
104 return (o2 == null) ? 0 : 1;
105 } else if (o2 == null) {
106 return -1;
107 }
108 final double v1 = o1.getValue();
109 final double v2 = o2.getValue();
110 return (optimizer.getGoalType() == GoalType.MINIMIZE) ?
111 Double.compare(v1, v2) : Double.compare(v2, v1);
112 }
113 };
114 }
115 }