2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
26// G4SimplexDownhill inline methods implementation
28// Author: Tatsumi Koi (SLAC/SCCS), 2007
29// --------------------------------------------------------------------------
36void G4SimplexDownhill<T>::init()
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
42 maximum_no_trial = 10000;
44 // max_ratio = FLT_EPSILON/1;
45 max_ratio = DBL_EPSILON / 1;
51void G4SimplexDownhill<class T>::
52SetFunction( G4int n , G4double( *afunc )( std::vector < G4double > ) )
62G4double G4SimplexDownhill<T>::GetMinimum()
68 // G4cout << "Begin First Trials" << G4endl;
70 // G4cout << "End First Trials" << G4endl;
72 std::vector<G4double>::const_iterator it_minh =
73 std::min_element(currentHeights.cbegin(), currentHeights.cend());
76 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
84 minimumPoint = currentSimplex[imin];
88 // std::vector< G4double > minimumPoint = currentSimplex[ 0 ];
91 currentSimplex[numberOfVariable] = minimumPoint;
93 // G4cout << "Begin Second Trials" << G4endl;
95 // G4cout << "End Second Trials" << G4endl;
98 std::accumulate(currentHeights.begin(), currentHeights.end(), 0.0);
99 G4double average = sum / (numberOfVariable + 1);
100 G4double minimum = average;
108void G4SimplexDownhill<T>::initialize()
110 currentSimplex.resize(numberOfVariable + 1);
111 currentHeights.resize(numberOfVariable + 1);
113 for(G4int i = 0; i < numberOfVariable; ++i)
115 std::vector<G4double> avec(numberOfVariable, 0.0);
117 currentSimplex[i] = avec;
120 // std::vector< G4double > avec ( numberOfVariable , 0.0 );
121 std::vector<G4double> avec(numberOfVariable, 1);
122 currentSimplex[numberOfVariable] = avec;
126void G4SimplexDownhill<T>::calHeights()
128 for(G4int i = 0; i <= numberOfVariable; ++i)
130 currentHeights[i] = getValue(currentSimplex[i]);
135std::vector<G4double> G4SimplexDownhill<T>::calCentroid(G4int ih)
137 std::vector<G4double> centroid(numberOfVariable, 0.0);
140 for(auto it = currentSimplex.cbegin(); it != currentSimplex.cend(); ++it)
144 for(G4int j = 0; j < numberOfVariable; ++j)
146 centroid[j] += (*it)[j] / numberOfVariable;
156std::vector<G4double> G4SimplexDownhill<T>::getReflectionPoint(
157 std::vector<G4double> p, std::vector<G4double> centroid)
159 // G4cout << "Reflection" << G4endl;
161 std::vector<G4double> reflectionP(numberOfVariable, 0.0);
163 for(G4int i = 0; i < numberOfVariable; ++i)
165 reflectionP[i] = (1 + alpha) * centroid[i] - alpha * p[i];
172std::vector<G4double> G4SimplexDownhill<T>::getExpansionPoint(
173 std::vector<G4double> p, std::vector<G4double> centroid)
175 // G4cout << "Expantion" << G4endl;
177 std::vector<G4double> expansionP(numberOfVariable, 0.0);
179 for(G4int i = 0; i < numberOfVariable; ++i)
181 expansionP[i] = (1 - gamma) * centroid[i] + gamma * p[i];
188std::vector<G4double> G4SimplexDownhill<T>::getContractionPoint(
189 std::vector<G4double> p, std::vector<G4double> centroid)
191 std::vector<G4double> contractionP(numberOfVariable, 0.0);
193 for(G4int i = 0; i < numberOfVariable; ++i)
195 contractionP[i] = (1 - beta) * centroid[i] + beta * p[i];
202G4bool G4SimplexDownhill<T>::isItGoodEnough()
204 G4bool result = false;
207 std::accumulate(currentHeights.begin(), currentHeights.end(), 0.0);
208 G4double average = sum / (numberOfVariable + 1);
210 G4double delta = 0.0;
211 for(G4int i = 0; i <= numberOfVariable; ++i)
213 delta += std::abs(currentHeights[i] - average);
216 if(delta / (numberOfVariable + 1) / average < max_ratio)
225void G4SimplexDownhill<T>::doDownhill()
229 while(nth_trial < maximum_no_trial)
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());
243 G4double h_H = *it_maxh;
244 G4double h_L = *it_minh;
250 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
258 h_H2 = std::max(h_H2, *it);
268 std::vector<G4double> centroidPoint = calCentroid(ih);
271 std::vector<G4double> reflectionPoint =
272 getReflectionPoint(currentSimplex[ih], centroidPoint);
274 G4double h = getValue(reflectionPoint);
279 std::vector<G4double> expansionPoint =
280 getExpansionPoint(reflectionPoint, centroidPoint);
281 G4double hh = getValue(expansionPoint);
286 currentSimplex[ih] = expansionPoint;
287 // G4cout << "A" << G4endl;
292 currentSimplex[ih] = reflectionPoint;
293 // G4cout << "B1" << G4endl;
301 currentSimplex[ih] = reflectionPoint;
302 // G4cout << "B2" << G4endl;
309 currentSimplex[ih] = reflectionPoint;
310 // G4cout << "BC" << G4endl;
313 std::vector<G4double> contractionPoint =
314 getContractionPoint(currentSimplex[ih], centroidPoint);
315 G4double hh = getValue(contractionPoint);
319 currentSimplex[ih] = contractionPoint;
320 // G4cout << "C" << G4endl;
325 for(G4int j = 0; j <= numberOfVariable; ++j)
327 std::vector<G4double> vec(numberOfVariable, 0.0);
328 for(G4int k = 0; k < numberOfVariable; ++k)
330 vec[k] = (currentSimplex[j][k] + currentSimplex[il][k]) / 2.0;
332 currentSimplex[j] = vec;
343std::vector<G4double> G4SimplexDownhill<T>::GetMinimumPoint()
345 if(minimized != true)
350 std::vector<G4double>::const_iterator it_minh =
351 std::min_element(currentHeights.cbegin(), currentHeights.cend());
355 for(auto it = currentHeights.cbegin(); it != currentHeights.cend(); ++it)
363 minimumPoint = currentSimplex[imin];