Geant4-11
G4SimplexDownhill.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4SimplexDownhill inline methods implementation
27//
28// Author: Tatsumi Koi (SLAC/SCCS), 2007
29// --------------------------------------------------------------------------
30
31#include <cfloat>
32#include <iostream>
33#include <numeric>
34
35template <class T>
36void G4SimplexDownhill<T>::init()
37{
38 alpha = 2.0; // refrection coefficient: 0 < alpha
39 beta = 0.5; // contraction coefficient: 0 < beta < 1
40 gamma = 2.0; // expantion coefficient: 1 < gamma
41
42 maximum_no_trial = 10000;
43 max_se = FLT_MIN;
44 // max_ratio = FLT_EPSILON/1;
45 max_ratio = DBL_EPSILON / 1;
46 minimized = false;
47}
48
49/*
50
51void G4SimplexDownhill<class T>::
52SetFunction( G4int n , G4double( *afunc )( std::vector < G4double > ) )
53{
54 numberOfVariable = n;
55 theFunction = afunc;
56 minimized = false;
57}
58
59*/
60
61template <class T>
62G4double G4SimplexDownhill<T>::GetMinimum()
63{
64 initialize();
65
66 // First Tryal;
67
68 // G4cout << "Begin First Trials" << G4endl;
69 doDownhill();
70 // G4cout << "End First Trials" << G4endl;
71
72 std::vector<G4double>::const_iterator it_minh =
73 std::min_element(currentHeights.cbegin(), currentHeights.cend());
74 G4int imin = 0;
75 G4int i = 0;
76 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
77 {
78 if(it == it_minh)
79 {
80 imin = i;
81 }
82 ++i;
83 }
84 minimumPoint = currentSimplex[imin];
85
86 // Second Trial
87
88 // std::vector< G4double > minimumPoint = currentSimplex[ 0 ];
89 initialize();
90
91 currentSimplex[numberOfVariable] = minimumPoint;
92
93 // G4cout << "Begin Second Trials" << G4endl;
94 doDownhill();
95 // G4cout << "End Second Trials" << G4endl;
96
97 G4double sum =
98 std::accumulate(currentHeights.begin(), currentHeights.end(), 0.0);
99 G4double average = sum / (numberOfVariable + 1);
100 G4double minimum = average;
101
102 minimized = true;
103
104 return minimum;
105}
106
107template <class T>
108void G4SimplexDownhill<T>::initialize()
109{
110 currentSimplex.resize(numberOfVariable + 1);
111 currentHeights.resize(numberOfVariable + 1);
112
113 for(G4int i = 0; i < numberOfVariable; ++i)
114 {
115 std::vector<G4double> avec(numberOfVariable, 0.0);
116 avec[i] = 1.0;
117 currentSimplex[i] = avec;
118 }
119
120 // std::vector< G4double > avec ( numberOfVariable , 0.0 );
121 std::vector<G4double> avec(numberOfVariable, 1);
122 currentSimplex[numberOfVariable] = avec;
123}
124
125template <class T>
126void G4SimplexDownhill<T>::calHeights()
127{
128 for(G4int i = 0; i <= numberOfVariable; ++i)
129 {
130 currentHeights[i] = getValue(currentSimplex[i]);
131 }
132}
133
134template <class T>
135std::vector<G4double> G4SimplexDownhill<T>::calCentroid(G4int ih)
136{
137 std::vector<G4double> centroid(numberOfVariable, 0.0);
138
139 G4int i = 0;
140 for(auto it = currentSimplex.cbegin(); it != currentSimplex.cend(); ++it)
141 {
142 if(i != ih)
143 {
144 for(G4int j = 0; j < numberOfVariable; ++j)
145 {
146 centroid[j] += (*it)[j] / numberOfVariable;
147 }
148 }
149 ++i;
150 }
151
152 return centroid;
153}
154
155template <class T>
156std::vector<G4double> G4SimplexDownhill<T>::getReflectionPoint(
157 std::vector<G4double> p, std::vector<G4double> centroid)
158{
159 // G4cout << "Reflection" << G4endl;
160
161 std::vector<G4double> reflectionP(numberOfVariable, 0.0);
162
163 for(G4int i = 0; i < numberOfVariable; ++i)
164 {
165 reflectionP[i] = (1 + alpha) * centroid[i] - alpha * p[i];
166 }
167
168 return reflectionP;
169}
170
171template <class T>
172std::vector<G4double> G4SimplexDownhill<T>::getExpansionPoint(
173 std::vector<G4double> p, std::vector<G4double> centroid)
174{
175 // G4cout << "Expantion" << G4endl;
176
177 std::vector<G4double> expansionP(numberOfVariable, 0.0);
178
179 for(G4int i = 0; i < numberOfVariable; ++i)
180 {
181 expansionP[i] = (1 - gamma) * centroid[i] + gamma * p[i];
182 }
183
184 return expansionP;
185}
186
187template <class T>
188std::vector<G4double> G4SimplexDownhill<T>::getContractionPoint(
189 std::vector<G4double> p, std::vector<G4double> centroid)
190{
191 std::vector<G4double> contractionP(numberOfVariable, 0.0);
192
193 for(G4int i = 0; i < numberOfVariable; ++i)
194 {
195 contractionP[i] = (1 - beta) * centroid[i] + beta * p[i];
196 }
197
198 return contractionP;
199}
200
201template <class T>
202G4bool G4SimplexDownhill<T>::isItGoodEnough()
203{
204 G4bool result = false;
205
206 G4double sum =
207 std::accumulate(currentHeights.begin(), currentHeights.end(), 0.0);
208 G4double average = sum / (numberOfVariable + 1);
209
210 G4double delta = 0.0;
211 for(G4int i = 0; i <= numberOfVariable; ++i)
212 {
213 delta += std::abs(currentHeights[i] - average);
214 }
215
216 if(delta / (numberOfVariable + 1) / average < max_ratio)
217 {
218 result = true;
219 }
220
221 return result;
222}
223
224template <class T>
225void G4SimplexDownhill<T>::doDownhill()
226{
227 G4int nth_trial = 0;
228
229 while(nth_trial < maximum_no_trial)
230 {
231 calHeights();
232
233 if(isItGoodEnough())
234 {
235 break;
236 }
237
238 std::vector<G4double>::const_iterator it_maxh =
239 std::max_element(currentHeights.cbegin(), currentHeights.cend());
240 std::vector<G4double>::const_iterator it_minh =
241 std::min_element(currentHeights.cbegin(), currentHeights.cend());
242
243 G4double h_H = *it_maxh;
244 G4double h_L = *it_minh;
245
246 G4int ih = 0;
247 G4int il = 0;
248 G4double h_H2 = 0.0;
249 G4int i = 0;
250 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
251 {
252 if(it == it_maxh)
253 {
254 ih = i;
255 }
256 else
257 {
258 h_H2 = std::max(h_H2, *it);
259 }
260
261 if(it == it_minh)
262 {
263 il = i;
264 }
265 ++i;
266 }
267
268 std::vector<G4double> centroidPoint = calCentroid(ih);
269
270 // REFLECTION
271 std::vector<G4double> reflectionPoint =
272 getReflectionPoint(currentSimplex[ih], centroidPoint);
273
274 G4double h = getValue(reflectionPoint);
275
276 if(h <= h_L)
277 {
278 // EXPANSION
279 std::vector<G4double> expansionPoint =
280 getExpansionPoint(reflectionPoint, centroidPoint);
281 G4double hh = getValue(expansionPoint);
282
283 if(hh <= h_L)
284 {
285 // Replace
286 currentSimplex[ih] = expansionPoint;
287 // G4cout << "A" << G4endl;
288 }
289 else
290 {
291 // Replace
292 currentSimplex[ih] = reflectionPoint;
293 // G4cout << "B1" << G4endl;
294 }
295 }
296 else
297 {
298 if(h <= h_H2)
299 {
300 // Replace
301 currentSimplex[ih] = reflectionPoint;
302 // G4cout << "B2" << G4endl;
303 }
304 else
305 {
306 if(h <= h_H)
307 {
308 // Replace
309 currentSimplex[ih] = reflectionPoint;
310 // G4cout << "BC" << G4endl;
311 }
312 // CONTRACTION
313 std::vector<G4double> contractionPoint =
314 getContractionPoint(currentSimplex[ih], centroidPoint);
315 G4double hh = getValue(contractionPoint);
316 if(hh <= h_H)
317 {
318 // Replace
319 currentSimplex[ih] = contractionPoint;
320 // G4cout << "C" << G4endl;
321 }
322 else
323 {
324 // Replace
325 for(G4int j = 0; j <= numberOfVariable; ++j)
326 {
327 std::vector<G4double> vec(numberOfVariable, 0.0);
328 for(G4int k = 0; k < numberOfVariable; ++k)
329 {
330 vec[k] = (currentSimplex[j][k] + currentSimplex[il][k]) / 2.0;
331 }
332 currentSimplex[j] = vec;
333 }
334 }
335 }
336 }
337
338 ++nth_trial;
339 }
340}
341
342template <class T>
343std::vector<G4double> G4SimplexDownhill<T>::GetMinimumPoint()
344{
345 if(minimized != true)
346 {
347 GetMinimum();
348 }
349
350 std::vector<G4double>::const_iterator it_minh =
351 std::min_element(currentHeights.cbegin(), currentHeights.cend());
352
353 G4int imin = 0;
354 G4int i = 0;
355 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
356 {
357 if(it == it_minh)
358 {
359 imin = i;
360 }
361 ++i;
362 }
363 minimumPoint = currentSimplex[imin];
364
365 return minimumPoint;
366}