1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.optim.nonlinear.scalar;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.List;
28
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.hipparchus.exception.NullArgumentException;
31 import org.hipparchus.optim.BaseMultiStartMultivariateOptimizer;
32 import org.hipparchus.optim.PointValuePair;
33 import org.hipparchus.random.RandomVectorGenerator;
34
35
36
37
38
39
40
41
42
43 public class MultiStartMultivariateOptimizer
44 extends BaseMultiStartMultivariateOptimizer<PointValuePair> {
45
46 private final MultivariateOptimizer optimizer;
47
48 private final List<PointValuePair> optima;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer,
63 final int starts,
64 final RandomVectorGenerator generator)
65 throws MathIllegalArgumentException, NullArgumentException {
66 super(optimizer, starts, generator);
67 this.optimizer = optimizer;
68 this.optima = new ArrayList<>();
69 }
70
71
72
73
74 @Override
75 public PointValuePair[] getOptima() {
76 Collections.sort(optima, getPairComparator());
77 return optima.toArray(new PointValuePair[0]);
78 }
79
80
81
82
83 @Override
84 protected void store(PointValuePair optimum) {
85 optima.add(optimum);
86 }
87
88
89
90
91 @Override
92 protected void clear() {
93 optima.clear();
94 }
95
96
97
98
99 private Comparator<PointValuePair> getPairComparator() {
100 return new Comparator<PointValuePair>() {
101
102 @Override
103 public int compare(final PointValuePair o1,
104 final PointValuePair o2) {
105 if (o1 == null) {
106 return (o2 == null) ? 0 : 1;
107 } else if (o2 == null) {
108 return -1;
109 }
110 final double v1 = o1.getValue();
111 final double v2 = o2.getValue();
112 return (optimizer.getGoalType() == GoalType.MINIMIZE) ?
113 Double.compare(v1, v2) : Double.compare(v2, v1);
114 }
115 };
116 }
117 }