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