source: src/main/java/agents/anac/y2019/harddealer/math3/optimization/BaseMultivariateMultiStartOptimizer.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 7.6 KB
Line 
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
18package agents.anac.y2019.harddealer.math3.optimization;
19
20import java.util.Arrays;
21import java.util.Comparator;
22
23import agents.anac.y2019.harddealer.math3.analysis.MultivariateFunction;
24import agents.anac.y2019.harddealer.math3.exception.MathIllegalStateException;
25import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
26import agents.anac.y2019.harddealer.math3.exception.NullArgumentException;
27import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
28import agents.anac.y2019.harddealer.math3.random.RandomVectorGenerator;
29
30/**
31 * Base class for all implementations of a multi-start optimizer.
32 *
33 * This interface is mainly intended to enforce the internal coherence of
34 * Commons-Math. Users of the API are advised to base their code on
35 * {@link MultivariateMultiStartOptimizer} or on
36 * {@link DifferentiableMultivariateMultiStartOptimizer}.
37 *
38 * @param <FUNC> Type of the objective function to be optimized.
39 *
40 * @deprecated As of 3.1 (to be removed in 4.0).
41 * @since 3.0
42 */
43@Deprecated
44public class BaseMultivariateMultiStartOptimizer<FUNC extends MultivariateFunction>
45 implements BaseMultivariateOptimizer<FUNC> {
46 /** Underlying classical optimizer. */
47 private final BaseMultivariateOptimizer<FUNC> optimizer;
48 /** Maximal number of evaluations allowed. */
49 private int maxEvaluations;
50 /** Number of evaluations already performed for all starts. */
51 private int totalEvaluations;
52 /** Number of starts to go. */
53 private int starts;
54 /** Random generator for multi-start. */
55 private RandomVectorGenerator generator;
56 /** Found optima. */
57 private PointValuePair[] optima;
58
59 /**
60 * Create a multi-start optimizer from a single-start optimizer.
61 *
62 * @param optimizer Single-start optimizer to wrap.
63 * @param starts Number of starts to perform. If {@code starts == 1},
64 * the {@link #optimize(int,MultivariateFunction,GoalType,double[])
65 * optimize} will return the same solution as {@code optimizer} would.
66 * @param generator Random vector generator to use for restarts.
67 * @throws NullArgumentException if {@code optimizer} or {@code generator}
68 * is {@code null}.
69 * @throws NotStrictlyPositiveException if {@code starts < 1}.
70 */
71 protected BaseMultivariateMultiStartOptimizer(final BaseMultivariateOptimizer<FUNC> optimizer,
72 final int starts,
73 final RandomVectorGenerator generator) {
74 if (optimizer == null ||
75 generator == null) {
76 throw new NullArgumentException();
77 }
78 if (starts < 1) {
79 throw new NotStrictlyPositiveException(starts);
80 }
81
82 this.optimizer = optimizer;
83 this.starts = starts;
84 this.generator = generator;
85 }
86
87 /**
88 * Get all the optima found during the last call to {@link
89 * #optimize(int,MultivariateFunction,GoalType,double[]) optimize}.
90 * The optimizer stores all the optima found during a set of
91 * restarts. The {@link #optimize(int,MultivariateFunction,GoalType,double[])
92 * optimize} method returns the best point only. This method
93 * returns all the points found at the end of each starts,
94 * including the best one already returned by the {@link
95 * #optimize(int,MultivariateFunction,GoalType,double[]) optimize} method.
96 * <br/>
97 * The returned array as one element for each start as specified
98 * in the constructor. It is ordered with the results from the
99 * runs that did converge first, sorted from best to worst
100 * objective value (i.e in ascending order if minimizing and in
101 * descending order if maximizing), followed by and null elements
102 * corresponding to the runs that did not converge. This means all
103 * elements will be null if the {@link #optimize(int,MultivariateFunction,GoalType,double[])
104 * optimize} method did throw an exception.
105 * This also means that if the first element is not {@code null}, it
106 * is the best point found across all starts.
107 *
108 * @return an array containing the optima.
109 * @throws MathIllegalStateException if {@link
110 * #optimize(int,MultivariateFunction,GoalType,double[]) optimize}
111 * has not been called.
112 */
113 public PointValuePair[] getOptima() {
114 if (optima == null) {
115 throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
116 }
117 return optima.clone();
118 }
119
120 /** {@inheritDoc} */
121 public int getMaxEvaluations() {
122 return maxEvaluations;
123 }
124
125 /** {@inheritDoc} */
126 public int getEvaluations() {
127 return totalEvaluations;
128 }
129
130 /** {@inheritDoc} */
131 public ConvergenceChecker<PointValuePair> getConvergenceChecker() {
132 return optimizer.getConvergenceChecker();
133 }
134
135 /**
136 * {@inheritDoc}
137 */
138 public PointValuePair optimize(int maxEval, final FUNC f,
139 final GoalType goal,
140 double[] startPoint) {
141 maxEvaluations = maxEval;
142 RuntimeException lastException = null;
143 optima = new PointValuePair[starts];
144 totalEvaluations = 0;
145
146 // Multi-start loop.
147 for (int i = 0; i < starts; ++i) {
148 // CHECKSTYLE: stop IllegalCatch
149 try {
150 optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
151 i == 0 ? startPoint : generator.nextVector());
152 } catch (RuntimeException mue) {
153 lastException = mue;
154 optima[i] = null;
155 }
156 // CHECKSTYLE: resume IllegalCatch
157
158 totalEvaluations += optimizer.getEvaluations();
159 }
160
161 sortPairs(goal);
162
163 if (optima[0] == null) {
164 throw lastException; // cannot be null if starts >=1
165 }
166
167 // Return the found point given the best objective function value.
168 return optima[0];
169 }
170
171 /**
172 * Sort the optima from best to worst, followed by {@code null} elements.
173 *
174 * @param goal Goal type.
175 */
176 private void sortPairs(final GoalType goal) {
177 Arrays.sort(optima, new Comparator<PointValuePair>() {
178 /** {@inheritDoc} */
179 public int compare(final PointValuePair o1,
180 final PointValuePair o2) {
181 if (o1 == null) {
182 return (o2 == null) ? 0 : 1;
183 } else if (o2 == null) {
184 return -1;
185 }
186 final double v1 = o1.getValue();
187 final double v2 = o2.getValue();
188 return (goal == GoalType.MINIMIZE) ?
189 Double.compare(v1, v2) : Double.compare(v2, v1);
190 }
191 });
192 }
193}
Note: See TracBrowser for help on using the repository browser.