BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerFactory.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Minimizer/MinimizerFactory.cpp
6 //! @brief Implements class MinimizerFactory.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
22 #include <boost/format.hpp>
23 #include <iomanip>
24 #include <iostream>
25 #include <memory>
26 #include <sstream>
27 
28 IMinimizer* MinimizerFactory::createMinimizer(const std::string& minimizerName,
29  const std::string& algorithmType,
30  const std::string& optionString)
31 {
32  IMinimizer* result(0);
33 
34  if (minimizerName == "Minuit2") {
35  result = new Minuit2Minimizer(algorithmType);
36  }
37 
38  else if (minimizerName == "GSLLMA") {
39  result = new GSLLevenbergMarquardtMinimizer();
40  }
41 
42  else if (minimizerName == "GSLSimAn") {
43  result = new SimAnMinimizer();
44  }
45 
46  else if (minimizerName == "GSLMultiMin") {
47  result = new GSLMultiMinimizer(algorithmType);
48  }
49 
50  else if (minimizerName == "Genetic") {
51  result = new GeneticMinimizer();
52  }
53 
54  else if (minimizerName == "Test") {
55  result = new TestMinimizer();
56  }
57 
58  if (!result) {
59  std::ostringstream ostr;
60  ostr << "MinimizerFactory::MinimizerFactory() -> Error! Can't create minimizer for given "
61  "collection name '"
62  << minimizerName << "' or algorithm '" << algorithmType << "'" << std::endl;
63  ostr << "Possible names are:" << std::endl;
64 
65  ostr << catalog().toString();
66  throw std::runtime_error(ostr.str());
67  }
68 
69  if (!optionString.empty())
70  result->setOptions(optionString);
71 
72  return result;
73 }
74 
76 {
77  std::cout << catalogToString() << std::endl;
78 }
79 
80 //! Returns multi-line string representing catalog content: minimizer names and list of their
81 //! algorithms.
82 
84 {
85  return catalog().toString();
86 }
87 
88 //! Returns multi-line string representing detailed catalog content:
89 //! minimizer names, list of their algorithms and description, list of minimizer options.
90 
92 {
93  const int text_width = 80;
94  std::ostringstream result;
95  const std::string fmt("%-20s| %-65s\n");
96 
97  for (const auto& minimizerName : catalog().minimizerNames()) {
98  // general info
99  const MinimizerInfo& info = catalog().minimizerInfo(minimizerName);
100  result << std::string(text_width, '-') << "\n";
101  result << boost::format(fmt) % info.name() % info.description();
102  result << std::string(text_width, '-') << "\n";
103 
104  // algorithm names and description
105  result << "\nAlgorithm names\n";
106  auto algorithmNames = info.algorithmNames();
107  auto algorithmDescription = info.algorithmDescriptions();
108  for (size_t i = 0; i < algorithmNames.size(); ++i)
109  result << boost::format(fmt) % algorithmNames[i] % algorithmDescription[i];
110  if (algorithmNames.size() > 1)
111  result << boost::format(fmt) % "Default algorithm" % info.algorithmName();
112 
113  // list of minimizer options
114  std::unique_ptr<IMinimizer> minimizer(createMinimizer(minimizerName));
115  if (auto rootMinimizer = dynamic_cast<RootMinimizerAdapter*>(minimizer.get())) {
116  result << "\nOptions\n";
117  for (auto option : rootMinimizer->options()) {
118  std::ostringstream opt;
119  opt << std::setw(5) << std::left << option->value() << option->description();
120  result << boost::format(fmt) % option->name() % opt.str();
121  }
122  }
123 
124  result << "\n";
125  }
126 
127  return result.str();
128 }
129 
131 {
132  static MinimizerCatalog s_catalog;
133  return s_catalog;
134 }
Declares class GSLLevenbergMarquardtMinimizer.
Declares class GSLMultiMinimizer.
Declares class GeneticMinimizer.
Defines class MinimizerFactory.
Declares class Minuit2Minimizer.
Declares class SimAnMinimizer.
Defines class TestMinimizer.
It's a facade to ROOT::Math::GSLNLSMinimizer which, in turn, is a facade to the actual GSL's gsl_mult...
Wrapper for the CERN ROOT facade of the GSL multi minimizer family (gradient descent based).
Wrapper for the CERN ROOT Genetic minimizer.
Pure virtual interface for all kind minimizers.
Definition: IMinimizer.h:31
virtual void setOptions(const std::string &options)
Sets option string to the minimizer.
Definition: IMinimizer.cpp:36
Hard-coded information about all minimizers available.
const MinimizerInfo & minimizerInfo(const std::string &minimizerName) const
Returns info for minimizer with given name.
std::string toString() const
Returns multiline string representing catalog content.
static void printCatalog()
static const MinimizerCatalog & catalog()
static std::string catalogDetailsToString()
Returns multi-line string representing detailed catalog content: minimizer names, list of their algor...
static std::string catalogToString()
Returns multi-line string representing catalog content: minimizer names and list of their algorithms.
static IMinimizer * createMinimizer(const std::string &minimizerName, const std::string &algorithmType="", const std::string &optionString="")
Info about a minimizer, including list of defined minimization algorithms.
Definition: MinimizerInfo.h:45
std::string algorithmName() const
Definition: MinimizerInfo.h:59
std::vector< std::string > algorithmDescriptions() const
Returns list of string with description of all available algorithms.
std::string name() const
Definition: MinimizerInfo.h:56
std::string description() const
Definition: MinimizerInfo.h:57
std::vector< std::string > algorithmNames() const
Return list of defined algorithm names.
Wrapper for the CERN ROOT facade of the Minuit2 minimizer.
Pure virtual interface that adapts the CERN ROOT minimizer to our IMinimizer.
Wrapper for the CERN ROOT facade of the GSL simmulated annealing minimizer.
A trivial minimizer that calls the objective function once. Used to test the whole chain.
Definition: TestMinimizer.h:23