BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
GeneticMinimizer.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/RootAdapter/GeneticMinimizer.cpp
6 //! @brief Implements class GeneticMinimizer.
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 
16 #include <Math/GeneticMinimizer.h>
17 
18 namespace
19 {
20 
21 std::map<int, std::string> statusDescription()
22 {
23  std::map<int, std::string> result;
24  result[0] = "OK, minimum found";
25  result[1] = "Maximum number of iterations reached";
26  return result;
27 }
28 } // namespace
29 
30 GeneticMinimizer::GeneticMinimizer()
31  : RootMinimizerAdapter(MinimizerInfo::buildGeneticInfo()),
32  m_genetic_minimizer(new ROOT::Math::GeneticMinimizer())
33 {
34  addOption("Tolerance", 0.01, "Tolerance on the function value at the minimum");
35  addOption("PrintLevel", 0, "Minimizer internal print level");
36  addOption("MaxIterations", 3, "Maximum number of iterations");
37  addOption("PopSize", 300, "Population size");
38  addOption("RandomSeed", 0, "Random seed");
39 
40  // Seems it is not used inside Root, no need to expose
41  // addOption("Cycles", 3, "Number of cycles");
42 
43  // It's hard to understand (without going to much into genetics details), what parameters below
44  // are doing. So better to not to expose and rely on their internal ROOT's default values.
45 
46  // addOption("sc_steps", 10, "Spread control steps");
47  // addOption("sc_rate", 5, "Spread control rate");
48  // addOption("sc_factor", 0.95, "Spread control factor");
49 }
50 
51 GeneticMinimizer::~GeneticMinimizer() = default;
52 
54 {
55  setOptionValue("Tolerance", value);
56 }
57 
58 double GeneticMinimizer::tolerance() const
59 {
60  return optionValue<double>("Tolerance");
61 }
62 
64 {
65  setOptionValue("PrintLevel", value);
66 }
67 
68 int GeneticMinimizer::printLevel() const
69 {
70  return optionValue<int>("PrintLevel");
71 }
72 
74 {
75  setOptionValue("MaxIterations", value);
76 }
77 
78 int GeneticMinimizer::maxIterations() const
79 {
80  return optionValue<int>("MaxIterations");
81 }
82 
84 {
85  setOptionValue("PopSize", value);
86 }
87 
88 int GeneticMinimizer::populationSize() const
89 {
90  return optionValue<int>("PopSize");
91 }
92 
94 {
95  setOptionValue("RandomSeed", value);
96 }
97 
98 int GeneticMinimizer::randomSeed() const
99 {
100  return optionValue<int>("RandomSeed");
101 }
102 
103 void GeneticMinimizer::setParameter(unsigned int index, const Fit::Parameter& par)
104 {
105  if (!par.limits().isFixed() && !par.limits().isLimited()) {
106  std::ostringstream ostr;
107  ostr << "GeneticMinimizer::setParameter() -> Error! "
108  << "Genetic minimizer requires either fixed or "
109  << "limited AttLimits::limited(left,right) parameter. "
110  << " Parameter name '" << par.name() << "', limits:" << par.limits().toString();
111  throw std::runtime_error(ostr.str());
112  }
113  RootMinimizerAdapter::setParameter(index, par);
114 }
115 
117 {
118  return statusDescription()[rootMinimizer()->Status()];
119 }
120 
121 std::map<std::string, std::string> GeneticMinimizer::statusMap() const
122 {
123  auto result = RootMinimizerAdapter::statusMap();
124  result["functionCalls"] = std::to_string(rootMinimizer()->NCalls());
125  return result;
126 }
127 
128 void GeneticMinimizer::propagateOptions()
129 {
130  ROOT::Math::GeneticMinimizerParameters pars;
131  pars.fPopSize = populationSize();
132  pars.fNsteps = maxIterations();
133  // pars.fCycles = m_options.getIntValue("Cycles"); // seems it's not used inside ROOT
134  // pars.fSC_steps = m_options.getIntValue("SC_steps"); // no idea what it is doing
135  // pars.fSC_rate = m_options.getIntValue("SC_rate"); // no idea what it is doing
136  // pars.fSC_factor = m_options.getRealValue("SC_factor"); // no idea what it is doing
137  const double scale_as_in_root = 10.0;
138  pars.fConvCrit = scale_as_in_root * tolerance();
139  pars.fSeed = randomSeed();
140  m_genetic_minimizer->SetParameters(pars);
141 }
142 
143 const RootMinimizerAdapter::root_minimizer_t* GeneticMinimizer::rootMinimizer() const
144 {
145  return m_genetic_minimizer.get();
146 }
Declares class GeneticMinimizer.
A fittable parameter with value, error, step, and limits.
Definition: Parameter.h:28
Wrapper for the CERN ROOT Genetic minimizer.
void setTolerance(double value)
Sets tolerance on the function value at the minimum.
void setPopulationSize(int value)
Sets population size.
std::map< std::string, std::string > statusMap() const override
Returns map of string representing different minimizer statuses.
std::string statusToString() const override
Returns string representation of current minimizer status.
void setPrintLevel(int value)
Sets minimizer internal print level.
void setRandomSeed(int value)
Sets random seed.
void setMaxIterations(int value)
Sets maximum number of iterations to try at each step.
Info about a minimizer, including list of defined minimization algorithms.
Definition: MinimizerInfo.h:45
Pure virtual interface that adapts the CERN ROOT minimizer to our IMinimizer.
virtual std::map< std::string, std::string > statusMap() const
Returns map of string representing different minimizer statuses.