Geant4  10.00.p02
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 //
27 // $Id: G4SimplexDownhill.icc 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 // Author: Tatsumi Koi (SLAC/SCCS), 2007
30 // --------------------------------------------------------------------------
31 
32 #include <iostream>
33 #include <numeric>
34 #include <cfloat>
35 
36 template<class T> void 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 /*
51 
52 void G4SimplexDownhill<class T>::
53 SetFunction( G4int n , G4double( *afunc )( std::vector < G4double > ) )
54 {
55  numberOfVariable = n;
56  theFunction = afunc;
57  minimized = false;
58 }
59 
60 */
61 
62 
63 template<class T>
64 G4double G4SimplexDownhill<T>::GetMinimum()
65 {
66 
67  initialize();
68 
69 // First Tryal;
70 
71  //G4cout << "Begin First Trials" << G4endl;
72  doDownhill();
73  //G4cout << "End First Trials" << G4endl;
74 
75  std::vector< G4double >::iterator it_minh =
76  std::min_element( currentHeights.begin() , currentHeights.end() );
77  G4int imin = -1;
78  G4int i = 0;
79  for ( std::vector< G4double >::iterator it = currentHeights.begin();
80  it != currentHeights.end(); it++ )
81  {
82  if ( it == it_minh )
83  {
84  imin = i;
85  }
86  i++;
87  }
88  minimumPoint = currentSimplex[ imin ];
89 
90 // Second Trial
91 
92  //std::vector< G4double > minimumPoint = currentSimplex[ 0 ];
93  initialize();
94 
95  currentSimplex[ numberOfVariable ] = minimumPoint;
96 
97  //G4cout << "Begin Second Trials" << G4endl;
98  doDownhill();
99  //G4cout << "End Second Trials" << G4endl;
100 
101  G4double sum = std::accumulate( currentHeights.begin() ,
102  currentHeights.end() , 0.0 );
103  G4double average = sum/(numberOfVariable+1);
104  G4double minimum = average;
105 
106  minimized = true;
107 
108  return minimum;
109 
110 }
111 
112 
113 
114 template<class T>
115 void G4SimplexDownhill<T>::initialize()
116 {
117 
118  currentSimplex.resize( numberOfVariable+1 );
119  currentHeights.resize( numberOfVariable+1 );
120 
121  for ( G4int i = 0 ; i < numberOfVariable ; i++ )
122  {
123  std::vector< G4double > avec ( numberOfVariable , 0.0 );
124  avec[ i ] = 1.0;
125  currentSimplex[ i ] = avec;
126  }
127 
128  //std::vector< G4double > avec ( numberOfVariable , 0.0 );
129  std::vector< G4double > avec ( numberOfVariable , 1 );
130  currentSimplex[ numberOfVariable ] = avec;
131 
132 }
133 
134 
135 
136 template<class T>
137 void G4SimplexDownhill<T>::calHeights()
138 {
139 
140  for ( G4int i = 0 ; i <= numberOfVariable ; i++ )
141  {
142  currentHeights[i] = getValue ( currentSimplex[i] );
143  }
144 
145 }
146 
147 
148 
149 template<class T>
150 std::vector< G4double > G4SimplexDownhill<T>::calCentroid( G4int ih )
151 {
152 
153  std::vector< G4double > centroid ( numberOfVariable , 0.0 );
154 
155  G4int i = 0;
156  for ( std::vector< std::vector< G4double > >::iterator
157  it = currentSimplex.begin(); it != currentSimplex.end() ; it++ )
158  {
159  if ( i != ih )
160  {
161  for ( G4int j = 0 ; j < numberOfVariable ; j++ )
162  {
163  centroid[j] += (*it)[j]/numberOfVariable;
164  }
165  }
166  i++;
167  }
168 
169  return centroid;
170 }
171 
172 
173 
174 template<class T>
175 std::vector< G4double > G4SimplexDownhill<T>::
176 getReflectionPoint( std::vector< G4double > p ,
177  std::vector< G4double > centroid )
178 {
179  //G4cout << "Reflection" << G4endl;
180 
181  std::vector< G4double > reflectionP ( numberOfVariable , 0.0 );
182 
183  for ( G4int i = 0 ; i < numberOfVariable ; i++ )
184  {
185  reflectionP[ i ] = ( 1 + alpha ) * centroid[ i ] - alpha * p[ i ];
186  }
187 
188  return reflectionP;
189 }
190 
191 
192 
193 template<class T>
194 std::vector< G4double > G4SimplexDownhill<T>::
195 getExpansionPoint( std::vector< G4double > p ,
196  std::vector< G4double > centroid )
197 {
198  //G4cout << "Expantion" << G4endl;
199 
200  std::vector< G4double > expansionP ( numberOfVariable , 0.0 );
201 
202  for ( G4int i = 0 ; i < numberOfVariable ; i++ )
203  {
204  expansionP[i] = ( 1 - gamma ) * centroid[i] + gamma * p[i];
205  }
206 
207  return expansionP;
208 }
209 
210 template<class T>
211 std::vector< G4double > G4SimplexDownhill<T>::
212 getContractionPoint( std::vector< G4double > p ,
213  std::vector< G4double > centroid )
214 {
215  //G4cout << "Contraction" << G4endl;
216 
217  std::vector< G4double > contractionP ( numberOfVariable , 0.0 );
218 
219  for ( G4int i = 0 ; i < numberOfVariable ; i++ )
220  {
221  contractionP[i] = ( 1 - beta ) * centroid[i] + beta * p[i];
222  }
223 
224  return contractionP;
225 }
226 
227 
228 
229 template<class T>
230 G4bool G4SimplexDownhill<T>::isItGoodEnough()
231 {
232  G4bool result = false;
233 
234  G4double sum = std::accumulate( currentHeights.begin() ,
235  currentHeights.end() , 0.0 );
236  G4double average = sum/(numberOfVariable+1);
237  //G4cout << "average " << average << G4endl;
238 
239  G4double delta = 0.0;
240  for ( G4int i = 0 ; i <= numberOfVariable ; i++ )
241  {
242  delta += std::abs ( currentHeights[ i ] - average );
243  }
244  //G4cout << "ratio of delta to average is "
245  // << delta / (numberOfVariable+1) / average << G4endl;
246 
247  if ( delta/(numberOfVariable+1)/average < max_ratio )
248  {
249  result = true;
250  }
251 
252 /*
253  G4double sigma = 0.0;
254  G4cout << "average " << average << G4endl;
255  for ( G4int i = 0 ; i <= numberOfVariable ; i++ )
256  {
257  sigma += ( currentHeights[ i ] - average )
258  *( currentHeights[ i ] - average );
259  }
260 
261  G4cout << "standard error of hs "
262  << std::sqrt ( sigma ) / (numberOfVariable+1) << G4endl;
263  if ( std::sqrt ( sigma ) / (numberOfVariable+1) < max_se )
264  {
265  result = true;
266  }
267 */
268 
269  return result;
270 }
271 
272 
273 
274 template<class T>
275 void G4SimplexDownhill<T>::doDownhill()
276 {
277 
278  G4int nth_trial = 0;
279 
280  while ( nth_trial < maximum_no_trial )
281  {
282 
283 /*
284  G4cout << "Begining " << nth_trial << "th trial " << G4endl;
285  for ( G4int j = 0 ; j <= numberOfVariable ; j++ )
286  {
287  G4cout << "SimplexPoint " << j << ": ";
288  for ( G4int i = 0 ; i < numberOfVariable ; i++ )
289  {
290  G4cout << currentSimplex[j][i]
291  << " ";
292  }
293  G4cout << G4endl;
294  }
295 */
296 
297  calHeights();
298 
299  if ( isItGoodEnough() )
300  {
301  break;
302  }
303 
304  std::vector< G4double >::iterator it_maxh =
305  std::max_element( currentHeights.begin() , currentHeights.end() );
306  std::vector< G4double >::iterator it_minh =
307  std::min_element( currentHeights.begin() , currentHeights.end() );;
308 
309  G4double h_H = *it_maxh;
310  G4double h_L = *it_minh;
311 
312  G4int ih = 0;;
313  G4int il = 0;
314  G4double h_H2 =0.0;
315  G4int i = 0;
316  for ( std::vector< G4double >::iterator
317  it = currentHeights.begin(); it != currentHeights.end(); it++ )
318  {
319  if ( it == it_maxh )
320  {
321  ih = i;
322  }
323  else
324  {
325  h_H2 = std::max( h_H2 , *it );
326  }
327 
328  if ( it == it_minh )
329  {
330  il = i;
331  }
332  i++;
333  }
334 
335  //G4cout << "max " << h_H << " " << ih << G4endl;
336  //G4cout << "max-dash " << h_H2 << G4endl;
337  //G4cout << "min " << h_L << " " << il << G4endl;
338 
339  std::vector< G4double > centroidPoint = calCentroid ( ih );
340 
341  // REFLECTION
342  std::vector< G4double > reflectionPoint =
343  getReflectionPoint( currentSimplex[ ih ] , centroidPoint );
344 
345  G4double h = getValue( reflectionPoint );
346 
347  if ( h <= h_L )
348  {
349  // EXPANSION
350  std::vector< G4double > expansionPoint =
351  getExpansionPoint( reflectionPoint , centroidPoint );
352  G4double hh = getValue( expansionPoint );
353 
354  if ( hh <= h_L )
355  {
356  // Replace
357  currentSimplex[ ih ] = expansionPoint;
358  //G4cout << "A" << G4endl;
359  }
360  else
361  {
362  // Replace
363  currentSimplex[ ih ] = reflectionPoint;
364  //G4cout << "B1" << G4endl;
365  }
366  }
367  else
368  {
369  if ( h <= h_H2 )
370  {
371  // Replace
372  currentSimplex[ ih ] = reflectionPoint;
373  //G4cout << "B2" << G4endl;
374  }
375  else
376  {
377  if ( h <= h_H )
378  {
379  // Replace
380  currentSimplex[ ih ] = reflectionPoint;
381  //G4cout << "BC" << G4endl;
382  }
383  // CONTRACTION
384  std::vector< G4double > contractionPoint =
385  getContractionPoint( currentSimplex[ ih ] , centroidPoint );
386  G4double hh = getValue( contractionPoint );
387  if ( hh <= h_H )
388  {
389  // Replace
390  currentSimplex[ ih ] = contractionPoint;
391  //G4cout << "C" << G4endl;
392  }
393  else
394  {
395  // Replace
396  for ( G4int j = 0 ; j <= numberOfVariable ; j++ )
397  {
398  std::vector< G4double > vec ( numberOfVariable , 0.0 );
399  for ( G4int k = 0 ; k < numberOfVariable ; k++ )
400  {
401  vec[ k ] = ( currentSimplex[ j ][ k ]
402  + currentSimplex[ il ][ k ] ) / 2.0;
403  }
404  currentSimplex[ j ] = vec;
405  }
406  //G4cout << "D" << G4endl;
407  }
408  }
409 
410  }
411 
412  nth_trial++;
413  }
414 }
415 
416 
417 
418 template<class T>
419 std::vector< G4double > G4SimplexDownhill<T>::GetMinimumPoint()
420 {
421  if ( minimized != true )
422  {
423  GetMinimum();
424  }
425 
426  std::vector< G4double >::iterator it_minh =
427  std::min_element( currentHeights.begin() , currentHeights.end() );;
428  G4int imin = -1;
429  G4int i = 0;
430  for ( std::vector< G4double >::iterator
431  it = currentHeights.begin(); it != currentHeights.end(); it++ )
432  {
433  if ( it == it_minh )
434  {
435  imin = i;
436  }
437  i++;
438  }
439  minimumPoint = currentSimplex[ imin ];
440 
441  return minimumPoint;
442 }