ComPWA
Common Partial-Wave-Analysis Framework
GenevaIF.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013, 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 /*
6  * Copyright (C) Gemfony scientific UG (haftungsbeschraenkt)
7  *
8  * See the AUTHORS file in the top-level directory for a list of authors.
9  *
10  * Contact: contact [at] gemfony (dot) com
11  *
12  * This file is part of the Geneva library collection.
13  *
14  * Geneva was developed with kind support from Karlsruhe Institute of
15  * Technology (KIT) and Steinbuch Centre for Computing (SCC). Further
16  * information about KIT and SCC can be found at http://www.kit.edu/english
17  * and http://scc.kit.edu .
18  *
19  * Geneva is free software: you can redistribute and/or modify it under
20  * the terms of version 3 of the GNU Affero General Public License
21  * as published by the Free Software Foundation.
22  *
23  * Geneva is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Affero General Public License for more details.
27  *
28  * You should have received a copy of the GNU Affero General Public License
29  * along with the Geneva library. If not, see <http://www.gnu.org/licenses/>.
30  *
31  * For further information on Gemfony scientific and Geneva, visit
32  * http://www.gemfony.com .
33  */
34 
36 
37 #include "Core/Logging.hpp"
40 
41 #include "geneva/Go2.hpp"
42 
43 #include <chrono>
44 
45 namespace ComPWA {
46 namespace Optimizer {
47 namespace Geneva {
48 
50  std::vector<ComPWA::Optimizer::Geneva::AlgorithmTypes> AlgorithmOrder_,
51  std::string ConfigFileDir_)
52  : AlgorithmOrder(AlgorithmOrder_), ConfigFileDir(ConfigFileDir_) {
53  LOG(INFO) << "GenevaIF::GenevaIF(): Starting Geneva interface (config dir="
54  << ConfigFileDir << ")";
55 }
56 
58  FitParameterList FitParameters) {
59  if (!isValid(FitParameters, Estimator.getParameters()))
60  return GenevaResult();
61 
62  using namespace Gem::Geneva;
63  // IMPORTANT: for some reason the other constructor (with just a config file)
64  // does not work correctly and the program is waiting endlessly! I did not
65  // find any way to get it running except by handing the constructor below some
66  // dummy arguments...
67  int argc(1);
68  char temp[] = {'a'};
69  char *argv[] = {temp};
70  Go2 go(argc, argv, ConfigFileDir + "Go2.json");
71 
72  // Initialize a client, if requested
73  if (go.clientMode()) {
74  LOG(INFO) << "Geneva Client waiting for action!";
75  go.clientRun();
76  return GenevaResult();
77  }
78 
79  std::vector<double> initialpars;
80  for (auto const &x : FitParameters)
81  initialpars.push_back(x.Value);
82  Estimator.updateParametersFrom(initialpars);
83  double InitialEstimatorValue(Estimator.evaluate());
84 
85  std::shared_ptr<GFMinIndividual> p(
86  new GFMinIndividual(Estimator, FitParameters));
87  go.push_back(p);
88 
89  for (auto AlgorithmType : AlgorithmOrder) {
90  switch (AlgorithmType) {
92  GEvolutionaryAlgorithmFactory eaf(ConfigFileDir + "GEvolutionary.json");
93  go &eaf();
94  break;
95  }
97  GSwarmAlgorithmFactory sf(ConfigFileDir + "GGradientDescent.json");
98  go &sf();
99  break;
100  }
102  GGradientDescentFactory gdf(ConfigFileDir + "GGradientDescent.json");
103  go &gdf();
104  break;
105  }
106  }
107  }
108 
109  std::chrono::steady_clock::time_point StartTime =
110  std::chrono::steady_clock::now();
111  // Perform the actual optimization
112  std::shared_ptr<GFMinIndividual> bestIndividual_ptr =
113  go.optimize<GFMinIndividual>();
114  std::chrono::steady_clock::time_point EndTime =
115  std::chrono::steady_clock::now();
116  auto finalFitPars = getFinalParameters(FitParameters, bestIndividual_ptr);
118  FitParameters, finalFitPars, finalFitPars.size(), InitialEstimatorValue,
119  std::get<1>(bestIndividual_ptr->getBestKnownPrimaryFitness()),
120  std::chrono::duration_cast<std::chrono::seconds>(EndTime - StartTime)});
121 }
122 
124  const FitParameterList &ParList,
125  std::shared_ptr<Gem::Geneva::GFMinIndividual> BestIndividual) const {
126 
127  std::vector<double> finalpars;
128  BestIndividual->streamline(finalpars);
129 
130  FitParameterList FinalParameters;
131  size_t counter(0);
132  for (auto const &p : ParList) {
133  if (p.IsFixed)
134  continue;
135  // TODO: error estimation
136  FinalParameters.push_back(
137  ComPWA::FitParameter<double>(p.Name, finalpars[counter], false));
138  ++counter;
139  }
140  return FinalParameters;
141 }
142 
143 } // namespace Geneva
144 } // namespace Optimizer
145 } // namespace ComPWA
This class provides a wrapper around the Geneva library.
virtual void updateParametersFrom(const std::vector< double > &)=0
It is important to input the vector in the same length and order as defined in the getParameters() me...
ComPWA::FitParameterList getFinalParameters(const FitParameterList &ParList, std::shared_ptr< Gem::Geneva::GFMinIndividual > min) const
Definition: GenevaIF.cpp:123
bool isValid(const FitParameterList &FitParameters, const std::vector< ComPWA::Parameter > &EstimatorParameters)
std::vector< FitParameter< double > > FitParameterList
GenevaIF(std::vector< ComPWA::Optimizer::Geneva::AlgorithmTypes > AlgorithmOrder_={AlgorithmTypes::EVOLUTIONARY, AlgorithmTypes::GRADIENT_DECENT}, std::string ConfigFileDir_="./")
Definition: GenevaIF.cpp:49
std::vector< ComPWA::Optimizer::Geneva::AlgorithmTypes > AlgorithmOrder
Definition: GenevaIF.hpp:52
virtual OutputType evaluate(const InputTypes &... args) noexcept=0
Data structure which resembles a general fit result.
Definition: FitResult.hpp:19
virtual std::vector< Parameter > getParameters() const =0
GenevaResult optimize(Estimator::Estimator< double > &Estimator, FitParameterList FitParameters) final
Finds the optimal value of the Estimator, by varying its parameters.
Definition: GenevaIF.cpp:57
This class template provides the interface to optimization libraries.
Definition: Optimizer.hpp:22