BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
MinimizerFactory Class Reference

Description

Factory to create minimizers.

Definition at line 26 of file MinimizerFactory.h.

Static Public Member Functions

static const MinimizerCatalogcatalog ()
 
static std::string catalogDetailsToString ()
 Returns multi-line string representing detailed catalog content: minimizer names, list of their algorithms and description, list of minimizer options. More...
 
static std::string catalogToString ()
 Returns multi-line string representing catalog content: minimizer names and list of their algorithms. More...
 
static IMinimizercreateMinimizer (const std::string &minimizerName, const std::string &algorithmType="", const std::string &optionString="")
 
static void printCatalog ()
 

Member Function Documentation

◆ catalog()

const MinimizerCatalog & MinimizerFactory::catalog ( )
static

Definition at line 124 of file MinimizerFactory.cpp.

125 {
126  static MinimizerCatalog s_catalog;
127  return s_catalog;
128 }
Hard-coded information about all minimizers available.

Referenced by catalogDetailsToString(), catalogToString(), and createMinimizer().

◆ catalogDetailsToString()

std::string MinimizerFactory::catalogDetailsToString ( )
static

Returns multi-line string representing detailed catalog content: minimizer names, list of their algorithms and description, list of minimizer options.

Definition at line 85 of file MinimizerFactory.cpp.

86 {
87  const int text_width = 80;
88  std::ostringstream result;
89  const std::string fmt("%-20s| %-65s\n");
90 
91  for (const auto& minimizerName : catalog().minimizerNames()) {
92  // general info
93  const MinimizerInfo& info = catalog().minimizerInfo(minimizerName);
94  result << std::string(text_width, '-') << "\n";
95  result << boost::format(fmt) % info.name() % info.description();
96  result << std::string(text_width, '-') << "\n";
97 
98  // algorithm names and description
99  result << "\nAlgorithm names\n";
100  auto algorithmNames = info.algorithmNames();
101  auto algorithmDescription = info.algorithmDescriptions();
102  for (size_t i = 0; i < algorithmNames.size(); ++i)
103  result << boost::format(fmt) % algorithmNames[i] % algorithmDescription[i];
104  if (algorithmNames.size() > 1)
105  result << boost::format(fmt) % "Default algorithm" % info.algorithmName();
106 
107  // list of minimizer options
108  std::unique_ptr<IMinimizer> minimizer(createMinimizer(minimizerName));
109  if (auto* rootMinimizer = dynamic_cast<MinimizerAdapter*>(minimizer.get())) {
110  result << "\nOptions\n";
111  for (const auto& option : rootMinimizer->options()) {
112  std::ostringstream opt;
113  opt << std::setw(5) << std::left << option->value_str() << option->description();
114  result << boost::format(fmt) % option->name() % opt.str();
115  }
116  }
117 
118  result << "\n";
119  }
120 
121  return result.str();
122 }
Abstract base class that adapts the CERN ROOT minimizer to our IMinimizer.
const MinimizerInfo & minimizerInfo(const std::string &minimizerName) const
Returns info for minimizer with given name.
static const MinimizerCatalog & catalog()
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:43
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.

References MinimizerInfo::algorithmDescriptions(), MinimizerInfo::algorithmName(), MinimizerInfo::algorithmNames(), catalog(), createMinimizer(), MinimizerInfo::description(), MinimizerCatalog::minimizerInfo(), and MinimizerInfo::name().

Here is the call graph for this function:

◆ catalogToString()

std::string MinimizerFactory::catalogToString ( )
static

Returns multi-line string representing catalog content: minimizer names and list of their algorithms.

Definition at line 77 of file MinimizerFactory.cpp.

78 {
79  return catalog().toString();
80 }
std::string toString() const
Returns multiline string representing catalog content.

References catalog(), and MinimizerCatalog::toString().

Referenced by printCatalog().

Here is the call graph for this function:

◆ createMinimizer()

IMinimizer * MinimizerFactory::createMinimizer ( const std::string &  minimizerName,
const std::string &  algorithmType = "",
const std::string &  optionString = "" 
)
static

Definition at line 27 of file MinimizerFactory.cpp.

30 {
31  IMinimizer* result(nullptr);
32 
33  if (minimizerName == "Minuit2")
34  result = new Minuit2Minimizer(algorithmType);
35 
36  else if (minimizerName == "GSLLMA")
37  result = new GSLLevenbergMarquardtMinimizer();
38 
39 
40  else if (minimizerName == "GSLSimAn")
41  result = new SimAnMinimizer();
42 
43 
44  else if (minimizerName == "GSLMultiMin")
45  result = new GSLMultiMinimizer(algorithmType);
46 
47 
48  else if (minimizerName == "Genetic")
49  result = new GeneticMinimizer();
50 
51 
52  if (!result) {
53  std::ostringstream ostr;
54  ostr << "MinimizerFactory::MinimizerFactory() -> Error! Can't create minimizer for given "
55  "collection name '"
56  << minimizerName << "' or algorithm '" << algorithmType << "'" << std::endl;
57  ostr << "Possible names are:" << std::endl;
58 
59  ostr << catalog().toString();
60  throw std::runtime_error(ostr.str());
61  }
62 
63  if (!optionString.empty())
64  result->setOptions(optionString);
65 
66  return result;
67 }
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.
Abstract base class for all kind minimizers.
Definition: IMinimizer.h:30
Wrapper for the CERN ROOT facade of the Minuit2 minimizer. See Minuit2 user manual https://root....
Wrapper for the CERN ROOT facade of the GSL simmulated annealing minimizer.

References catalog(), IMinimizer::setOptions(), and MinimizerCatalog::toString().

Referenced by catalogDetailsToString(), and mumufit::Kernel::setMinimizer().

Here is the call graph for this function:

◆ printCatalog()

void MinimizerFactory::printCatalog ( )
static

Definition at line 69 of file MinimizerFactory.cpp.

70 {
71  std::cout << catalogToString() << std::endl;
72 }
static std::string catalogToString()
Returns multi-line string representing catalog content: minimizer names and list of their algorithms.

References catalogToString().

Here is the call graph for this function:

The documentation for this class was generated from the following files: