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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /global/HEPNumerics/include/G4SimplexDownhill.icc (Version 11.3.0) and /global/HEPNumerics/include/G4SimplexDownhill.icc (Version 8.1)


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