source: src/main/java/agents/anac/y2019/harddealer/math3/optimization/BaseMultivariateVectorMultiStartOptimizer.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: 8.4 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.MultivariateVectorFunction;
24import agents.anac.y2019.harddealer.math3.exception.ConvergenceException;
25import agents.anac.y2019.harddealer.math3.exception.MathIllegalStateException;
26import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
27import agents.anac.y2019.harddealer.math3.exception.NullArgumentException;
28import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
29import agents.anac.y2019.harddealer.math3.random.RandomVectorGenerator;
30
31/**
32 * Base class for all implementations of a multi-start optimizer.
33 *
34 * This interface is mainly intended to enforce the internal coherence of
35 * Commons-Math. Users of the API are advised to base their code on
36 * {@link DifferentiableMultivariateVectorMultiStartOptimizer}.
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 BaseMultivariateVectorMultiStartOptimizer<FUNC extends MultivariateVectorFunction>
45 implements BaseMultivariateVectorOptimizer<FUNC> {
46 /** Underlying classical optimizer. */
47 private final BaseMultivariateVectorOptimizer<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 PointVectorValuePair[] 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,MultivariateVectorFunction,double[],double[],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 BaseMultivariateVectorMultiStartOptimizer(final BaseMultivariateVectorOptimizer<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,MultivariateVectorFunction,double[],double[],double[]) optimize}.
90 * The optimizer stores all the optima found during a set of
91 * restarts. The {@link #optimize(int,MultivariateVectorFunction,double[],double[],double[])
92 * optimize} method returns the best point only. This method
93 * returns all the points found at the end of each starts, including
94 * the best one already returned by the {@link
95 * #optimize(int,MultivariateVectorFunction,double[],double[],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
104 * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize} method did
105 * throw a {@link ConvergenceException}). This also means that if
106 * the first element is not {@code null}, it is the best point found
107 * across all starts.
108 *
109 * @return array containing the optima
110 * @throws MathIllegalStateException if {@link
111 * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize} has not been
112 * called.
113 */
114 public PointVectorValuePair[] getOptima() {
115 if (optima == null) {
116 throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET);
117 }
118 return optima.clone();
119 }
120
121 /** {@inheritDoc} */
122 public int getMaxEvaluations() {
123 return maxEvaluations;
124 }
125
126 /** {@inheritDoc} */
127 public int getEvaluations() {
128 return totalEvaluations;
129 }
130
131 /** {@inheritDoc} */
132 public ConvergenceChecker<PointVectorValuePair> getConvergenceChecker() {
133 return optimizer.getConvergenceChecker();
134 }
135
136 /**
137 * {@inheritDoc}
138 */
139 public PointVectorValuePair optimize(int maxEval, final FUNC f,
140 double[] target, double[] weights,
141 double[] startPoint) {
142 maxEvaluations = maxEval;
143 RuntimeException lastException = null;
144 optima = new PointVectorValuePair[starts];
145 totalEvaluations = 0;
146
147 // Multi-start loop.
148 for (int i = 0; i < starts; ++i) {
149
150 // CHECKSTYLE: stop IllegalCatch
151 try {
152 optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, target, weights,
153 i == 0 ? startPoint : generator.nextVector());
154 } catch (ConvergenceException oe) {
155 optima[i] = null;
156 } catch (RuntimeException mue) {
157 lastException = mue;
158 optima[i] = null;
159 }
160 // CHECKSTYLE: resume IllegalCatch
161
162 totalEvaluations += optimizer.getEvaluations();
163 }
164
165 sortPairs(target, weights);
166
167 if (optima[0] == null) {
168 throw lastException; // cannot be null if starts >=1
169 }
170
171 // Return the found point given the best objective function value.
172 return optima[0];
173 }
174
175 /**
176 * Sort the optima from best to worst, followed by {@code null} elements.
177 *
178 * @param target Target value for the objective functions at optimum.
179 * @param weights Weights for the least-squares cost computation.
180 */
181 private void sortPairs(final double[] target,
182 final double[] weights) {
183 Arrays.sort(optima, new Comparator<PointVectorValuePair>() {
184 /** {@inheritDoc} */
185 public int compare(final PointVectorValuePair o1,
186 final PointVectorValuePair o2) {
187 if (o1 == null) {
188 return (o2 == null) ? 0 : 1;
189 } else if (o2 == null) {
190 return -1;
191 }
192 return Double.compare(weightedResidual(o1), weightedResidual(o2));
193 }
194 private double weightedResidual(final PointVectorValuePair pv) {
195 final double[] value = pv.getValueRef();
196 double sum = 0;
197 for (int i = 0; i < value.length; ++i) {
198 final double ri = value[i] - target[i];
199 sum += weights[i] * ri * ri;
200 }
201 return sum;
202 }
203 });
204 }
205}
Note: See TracBrowser for help on using the repository browser.