BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MinimizerItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/MinimizerItem.cpp
6 //! @brief Implements MinimizerItem class
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 
25 
27 
28 // ----------------------------------------------------------------------------
29 
30 const QString MinimizerContainerItem::P_MINIMIZERS = "Minimizer";
31 const QString MinimizerContainerItem::P_METRIC = "Objective metric";
32 const QString MinimizerContainerItem::P_NORM = "Norm function";
33 
35 {
36  addGroupProperty(P_MINIMIZERS, "Minimizer library group")->setToolTip("Minimizer library");
37 
38  ComboProperty metric_combo;
40  metric_combo << QString::fromStdString(item);
41  metric_combo.setValue(QString::fromStdString(ObjectiveMetricUtils::defaultMetricName()));
42  addProperty(P_METRIC, metric_combo.variant())
43  ->setToolTip("Objective metric to use for estimating distance between simulated and "
44  "experimental data.");
45 
46  ComboProperty norm_combo;
48  norm_combo << QString::fromStdString(item);
49  norm_combo.setValue(QString::fromStdString(ObjectiveMetricUtils::defaultNormName()));
50  addProperty(P_NORM, norm_combo.variant())
51  ->setToolTip("Normalization to use for estimating distance between simulated and "
52  "experimental data.");
53 }
54 
55 std::unique_ptr<IMinimizer> MinimizerContainerItem::createMinimizer() const
56 {
57  return groupItem<MinimizerItem>(P_MINIMIZERS).createMinimizer();
58 }
59 
60 std::unique_ptr<ObjectiveMetric> MinimizerContainerItem::createMetric() const
61 {
62  QString metric = getItemValue(P_METRIC).value<ComboProperty>().getValue();
63  QString norm = getItemValue(P_NORM).value<ComboProperty>().getValue();
64  return ObjectiveMetricUtils::createMetric(metric.toStdString(), norm.toStdString());
65 }
66 
67 // ----------------------------------------------------------------------------
68 
69 const QString MinuitMinimizerItem::P_ALGORITHMS = "Algorithms";
70 const QString MinuitMinimizerItem::P_STRATEGY = QString::fromStdString("Strategy");
71 const QString MinuitMinimizerItem::P_ERRORDEF = QString::fromStdString("ErrorDef");
72 const QString MinuitMinimizerItem::P_TOLERANCE = QString::fromStdString("Tolerance");
73 const QString MinuitMinimizerItem::P_PRECISION = QString::fromStdString("Precision");
74 const QString MinuitMinimizerItem::P_MAXFUNCTIONCALLS = QString::fromStdString("MaxFunctionCalls");
75 
77 {
79 
81  ->setToolTip("Minimization strategy (0-low, 1-medium, 2-high quality)");
82 
84  ->setToolTip("Error definition factor for parameter error calculation");
85 
86  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
87 
88  addProperty(P_PRECISION, -1.0)->setToolTip("Relative floating point arithmetic precision");
89 
90  // Minimizer internal print level is working to std::cout and is not intercepted by GUI
91  // addProperty(P_PRINTLEVEL, 0)->setToolTip("Minimizer internal print level");
92 
93  addProperty(P_MAXFUNCTIONCALLS, 0)->setToolTip("Maximum number of function calls");
94 }
95 
96 std::unique_ptr<IMinimizer> MinuitMinimizerItem::createMinimizer() const
97 {
98  QString algorithmName = getItemValue(P_ALGORITHMS).value<ComboProperty>().getValue();
99 
100  Minuit2Minimizer* domainMinimizer = new Minuit2Minimizer(algorithmName.toStdString());
101  domainMinimizer->setStrategy(getItemValue(P_STRATEGY).toInt());
102  domainMinimizer->setErrorDefinition(getItemValue(P_ERRORDEF).toDouble());
103  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
104  domainMinimizer->setPrecision(getItemValue(P_PRECISION).toDouble());
105  domainMinimizer->setMaxFunctionCalls(getItemValue(P_MAXFUNCTIONCALLS).toInt());
106 
107  return std::unique_ptr<IMinimizer>(domainMinimizer);
108 }
109 
110 // ----------------------------------------------------------------------------
111 
112 const QString GSLMultiMinimizerItem::P_ALGORITHMS = "Algorithms";
113 const QString GSLMultiMinimizerItem::P_MAXITERATIONS = QString::fromStdString("MaxIterations");
114 
116 {
118  addProperty(P_MAXITERATIONS, 0)->setToolTip("Maximum number of iterations");
119 }
120 
121 std::unique_ptr<IMinimizer> GSLMultiMinimizerItem::createMinimizer() const
122 {
123  QString algorithmName = getItemValue(P_ALGORITHMS).value<ComboProperty>().getValue();
124 
125  GSLMultiMinimizer* domainMinimizer = new GSLMultiMinimizer(algorithmName.toStdString());
126  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
127  return std::unique_ptr<IMinimizer>(domainMinimizer);
128 }
129 
130 // ----------------------------------------------------------------------------
131 
132 const QString GeneticMinimizerItem::P_TOLERANCE = QString::fromStdString("Tolerance");
133 const QString GeneticMinimizerItem::P_MAXITERATIONS = QString::fromStdString("MaxIterations");
134 const QString GeneticMinimizerItem::P_POPULATIONSIZE = QString::fromStdString("PopSize");
135 const QString GeneticMinimizerItem::P_RANDOMSEED = QString::fromStdString("RandomSeed");
136 
138 {
139  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
140  addProperty(P_MAXITERATIONS, 3)->setToolTip("Maximum number of iterations");
141  addProperty(P_POPULATIONSIZE, 300)->setToolTip("Population size");
142  addProperty(P_RANDOMSEED, 0)->setToolTip("Random seed");
143 }
144 
145 std::unique_ptr<IMinimizer> GeneticMinimizerItem::createMinimizer() const
146 {
147  GeneticMinimizer* domainMinimizer = new GeneticMinimizer();
148  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
149  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
150  domainMinimizer->setPopulationSize(getItemValue(P_POPULATIONSIZE).toInt());
151  domainMinimizer->setRandomSeed(getItemValue(P_RANDOMSEED).toInt());
152  return std::unique_ptr<IMinimizer>(domainMinimizer);
153 }
154 
155 // ----------------------------------------------------------------------------
156 
157 const QString SimAnMinimizerItem::P_MAXITERATIONS = QString::fromStdString("MaxIterations");
158 const QString SimAnMinimizerItem::P_ITERATIONSTEMP = QString::fromStdString("IterationsAtTemp");
159 const QString SimAnMinimizerItem::P_STEPSIZE = QString::fromStdString("StepSize");
160 const QString SimAnMinimizerItem::P_BOLTZMANN_K = QString::fromStdString("k");
161 const QString SimAnMinimizerItem::P_BOLTZMANN_TINIT = QString::fromStdString("t_init");
162 const QString SimAnMinimizerItem::P_BOLTZMANN_MU = QString::fromStdString("mu");
163 const QString SimAnMinimizerItem::P_BOLTZMANN_TMIN = QString::fromStdString("t_min");
164 
166 {
167  addProperty(P_MAXITERATIONS, 100)->setToolTip("Number of points to try for each step");
168  addProperty(P_ITERATIONSTEMP, 10)->setToolTip("Number of iterations at each temperature");
169  addProperty(P_STEPSIZE, 1.0)->setToolTip("Max step size used in random walk");
170  addProperty(P_BOLTZMANN_K, 1.0)->setToolTip("Boltzmann k");
171  addProperty(P_BOLTZMANN_TINIT, 50.0)->setToolTip("Boltzmann initial temperature");
172  addProperty(P_BOLTZMANN_MU, 1.05)->setToolTip("Boltzmann mu");
173  addProperty(P_BOLTZMANN_TMIN, 0.1)->setToolTip("Boltzmann minimal temperature");
174 }
175 
176 std::unique_ptr<IMinimizer> SimAnMinimizerItem::createMinimizer() const
177 {
178  SimAnMinimizer* domainMinimizer = new SimAnMinimizer();
179  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
180  domainMinimizer->setIterationsAtEachTemp(getItemValue(P_ITERATIONSTEMP).toInt());
181  domainMinimizer->setStepSize(getItemValue(P_STEPSIZE).toDouble());
182  domainMinimizer->setBoltzmannK(getItemValue(P_BOLTZMANN_K).toDouble());
183  domainMinimizer->setBoltzmannInitialTemp(getItemValue(P_BOLTZMANN_TINIT).toDouble());
184  domainMinimizer->setBoltzmannMu(getItemValue(P_BOLTZMANN_MU).toDouble());
185  domainMinimizer->setBoltzmannMinTemp(getItemValue(P_BOLTZMANN_TMIN).toDouble());
186  return std::unique_ptr<IMinimizer>(domainMinimizer);
187 }
188 
189 // ----------------------------------------------------------------------------
190 
191 const QString GSLLMAMinimizerItem::P_TOLERANCE = QString::fromStdString("Tolerance");
192 const QString GSLLMAMinimizerItem::P_MAXITERATIONS = QString::fromStdString("MaxIterations");
193 
195 {
196  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
197  addProperty(P_MAXITERATIONS, 0)->setToolTip("Maximum number of iterations");
198 }
199 
200 std::unique_ptr<IMinimizer> GSLLMAMinimizerItem::createMinimizer() const
201 {
203  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
204  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
205  return std::unique_ptr<IMinimizer>(domainMinimizer);
206 }
207 
208 // ----------------------------------------------------------------------------
209 
211 
212 std::unique_ptr<IMinimizer> TestMinimizerItem::createMinimizer() const
213 {
214  return std::unique_ptr<IMinimizer>(new TestMinimizer());
215 }
Declares class GSLLevenbergMarquardtMinimizer.
Declares class GSLMultiMinimizer.
Declares class GeneticMinimizer.
Defines MinimizerItemCatalog class.
Defines MinimizerItem class.
Declares class Minuit2Minimizer.
Defines ObjectiveMetric utilities and corresponding namespace.
Defines ObjectiveMetric classes.
Declares class SimAnMinimizer.
Defines class TestMinimizer.
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setValue(const QString &name)
static const QString P_TOLERANCE
static const QString P_MAXITERATIONS
std::unique_ptr< IMinimizer > createMinimizer() const
It's a facade to ROOT::Math::GSLNLSMinimizer which, in turn, is a facade to the actual GSL's gsl_mult...
void setMaxIterations(int value)
Sets maximum number of iterations.
void setTolerance(double value)
Sets tolerance on the function value at the minimum.
std::unique_ptr< IMinimizer > createMinimizer() const
static const QString P_MAXITERATIONS
Definition: MinimizerItem.h:65
static const QString P_ALGORITHMS
Definition: MinimizerItem.h:64
Wrapper for the CERN ROOT facade of the GSL multi minimizer family (gradient descent based).
void setMaxIterations(int value)
Sets maximum number of iterations.
static const QString P_TOLERANCE
Definition: MinimizerItem.h:75
std::unique_ptr< IMinimizer > createMinimizer() const
static const QString P_RANDOMSEED
Definition: MinimizerItem.h:78
static const QString P_POPULATIONSIZE
Definition: MinimizerItem.h:77
static const QString P_MAXITERATIONS
Definition: MinimizerItem.h:76
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.
void setRandomSeed(int value)
Sets random seed.
void setMaxIterations(int value)
Sets maximum number of iterations to try at each step.
static const QString P_NORM
Definition: MinimizerItem.h:37
static const QString P_METRIC
Definition: MinimizerItem.h:36
std::unique_ptr< IMinimizer > createMinimizer() const
static const QString P_MINIMIZERS
Definition: MinimizerItem.h:35
std::unique_ptr< ObjectiveMetric > createMetric() const
static ComboProperty algorithmCombo(const QString &minimizerType)
Returns ComboProperty representing list of algorithms defined for given minimizerType.
The MinimizerItem class is the base item to hold minimizer settings.
Definition: MinimizerItem.h:25
MinimizerItem(const QString &model_type)
Wrapper for the CERN ROOT facade of the Minuit2 minimizer.
void setPrecision(double value)
Sets relative floating point arithmetic precision.
void setMaxFunctionCalls(int value)
Sets maximum number of objective function calls.
void setStrategy(int value)
Sets minimization strategy (0-low, 1-medium, 2-high minimization quality).
void setTolerance(double value)
Sets tolerance on the function value at the minimum.
void setErrorDefinition(double value)
Sets error definition factor for parameter error calculation.
static const QString P_ERRORDEF
Definition: MinimizerItem.h:51
static const QString P_STRATEGY
Definition: MinimizerItem.h:50
static const QString P_MAXFUNCTIONCALLS
Definition: MinimizerItem.h:54
static const QString P_PRECISION
Definition: MinimizerItem.h:53
static const QString P_TOLERANCE
Definition: MinimizerItem.h:52
static const QString P_ALGORITHMS
Definition: MinimizerItem.h:49
std::unique_ptr< IMinimizer > createMinimizer() const
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
SessionItem * addGroupProperty(const QString &groupTag, const QString &groupType)
Creates new group item and register new tag, returns GroupItem.
T * item(const QString &tag) const
Definition: SessionItem.h:151
SessionItem & setToolTip(const QString &tooltip)
QString modelType() const
Get model type.
static const QString P_BOLTZMANN_TMIN
Definition: MinimizerItem.h:94
std::unique_ptr< IMinimizer > createMinimizer() const
static const QString P_STEPSIZE
Definition: MinimizerItem.h:90
static const QString P_ITERATIONSTEMP
Definition: MinimizerItem.h:89
static const QString P_BOLTZMANN_K
Definition: MinimizerItem.h:91
static const QString P_BOLTZMANN_MU
Definition: MinimizerItem.h:93
static const QString P_MAXITERATIONS
Definition: MinimizerItem.h:88
static const QString P_BOLTZMANN_TINIT
Definition: MinimizerItem.h:92
Wrapper for the CERN ROOT facade of the GSL simmulated annealing minimizer.
void setIterationsAtEachTemp(int value)
Sets number of iterations at each temperature.
void setBoltzmannMu(double value)
Sets Boltzmann distribution parameter: mu.
void setBoltzmannK(double value)
Sets Boltzmann distribution parameter: k.
void setStepSize(double value)
Sets max step size used in random walk.
void setBoltzmannMinTemp(double value)
Sets Boltzmann distribution parameter: minimal temperature.
void setMaxIterations(int value)
Sets maximum number of iterations to try at each step.
void setBoltzmannInitialTemp(double value)
Sets Boltzmann distribution parameter: initial temperature.
std::unique_ptr< IMinimizer > createMinimizer() const
A trivial minimizer that calls the objective function once. Used to test the whole chain.
Definition: TestMinimizer.h:27
std::string model_type
Definition: types.h:23
std::string defaultMetricName()
Returns default metric name.
std::string defaultNormName()
Returns default norm name.
std::unique_ptr< ObjectiveMetric > createMetric(const std::string &metric)
Creates the specified metric with the default norm.
std::vector< std::string > metricNames()
Returns the names of the objective metrics used.
std::vector< std::string > normNames()
Returns the names of the norms used by ObjectiveMetric.