BornAgain  1.19.79
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/Model/Job/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 
16 #include "Fit/Adapter/GSLLevenbergMarquardtMinimizer.h"
17 #include "Fit/Adapter/GSLMultiMinimizer.h"
18 #include "Fit/Adapter/GeneticMinimizer.h"
19 #include "Fit/Adapter/Minuit2Minimizer.h"
20 #include "Fit/Adapter/SimAnMinimizer.h"
22 #include "Sim/Fitting/ObjectiveMetric.h"
23 #include "Sim/Fitting/ObjectiveMetricUtils.h"
24 
25 namespace {
26 
27 GroupInfo createMinimizerLibraryGroup()
28 {
29  GroupInfo info;
30  info.add(MinuitMinimizerItem::M_TYPE, "Minuit2");
31  info.add(GSLMultiMinimizerItem::M_TYPE, "GSL MultiMin");
32  info.add(GeneticMinimizerItem::M_TYPE, "TMVA Genetic");
33  info.add(SimAnMinimizerItem::M_TYPE, "GSL Simulated Annealing");
34  info.add(GSLLMAMinimizerItem::M_TYPE, "GSL Levenberg-Marquardt");
36  return info;
37 }
38 
39 } // namespace
40 
41 
42 MinimizerItem::MinimizerItem(const QString& model_type)
43  : SessionItem(model_type)
44 {
45 }
46 
47 // ----------------------------------------------------------------------------
48 
50  : MinimizerItem(M_TYPE)
51 {
52  static const GroupInfo minimizerLibraryGroup = createMinimizerLibraryGroup();
53  addGroupProperty(P_MINIMIZERS, minimizerLibraryGroup)->setToolTip("Minimizer library");
54 
55  ComboProperty metric_combo;
56  for (auto& item : ObjectiveMetricUtils::metricNames())
57  metric_combo << QString::fromStdString(item);
58  metric_combo.setValue(QString::fromStdString(ObjectiveMetricUtils::defaultMetricName()));
59  addProperty(P_METRIC, metric_combo.variant())
60  ->setToolTip("Objective metric to use for estimating distance between simulated and "
61  "experimental data.");
62 
63  ComboProperty norm_combo;
64  for (auto& item : ObjectiveMetricUtils::normNames())
65  norm_combo << QString::fromStdString(item);
66  norm_combo.setValue(QString::fromStdString(ObjectiveMetricUtils::defaultNormName()));
67  addProperty(P_NORM, norm_combo.variant())
68  ->setToolTip("Normalization to use for estimating distance between simulated and "
69  "experimental data.");
70 }
71 
73 {
74  return SelectionDescriptor<MinimizerItem*>(item<GroupItem>(P_MINIMIZERS));
75 }
76 
78 {
80 }
81 
83 {
85 }
86 
87 std::unique_ptr<IMinimizer> MinimizerContainerItem::createMinimizer() const
88 {
89  return groupItem<MinimizerItem>(P_MINIMIZERS).createMinimizer();
90 }
91 
92 std::unique_ptr<ObjectiveMetric> MinimizerContainerItem::createMetric() const
93 {
94  QString metric = getItemValue(P_METRIC).value<ComboProperty>().getValue();
95  QString norm = getItemValue(P_NORM).value<ComboProperty>().getValue();
96  return ObjectiveMetricUtils::createMetric(metric.toStdString(), norm.toStdString());
97 }
98 
99 // ----------------------------------------------------------------------------
100 
102  : MinimizerItem(M_TYPE)
103 {
105 
107  ->setToolTip("Minimization strategy (0-low, 1-medium, 2-high quality)");
108 
109  addProperty(P_ERRORDEF, 1.0)
110  ->setToolTip("Error definition factor for parameter error calculation");
111 
112  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
113 
114  addProperty(P_PRECISION, -1.0)->setToolTip("Relative floating point arithmetic precision");
115 
116  // Minimizer internal print level is working to std::cout and is not intercepted by GUI
117  // addProperty(P_PRINTLEVEL, 0)->setToolTip("Minimizer internal print level");
118 
119  addProperty(P_MAXFUNCTIONCALLS, 0)->setToolTip("Maximum number of function calls");
120 }
121 
122 std::unique_ptr<IMinimizer> MinuitMinimizerItem::createMinimizer() const
123 {
124  QString algorithmName = getItemValue(P_ALGORITHMS).value<ComboProperty>().getValue();
125 
126  auto* domainMinimizer = new Minuit2Minimizer(algorithmName.toStdString());
127  domainMinimizer->setStrategy(getItemValue(P_STRATEGY).toInt());
128  domainMinimizer->setErrorDefinition(getItemValue(P_ERRORDEF).toDouble());
129  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
130  domainMinimizer->setPrecision(getItemValue(P_PRECISION).toDouble());
131  domainMinimizer->setMaxFunctionCalls(getItemValue(P_MAXFUNCTIONCALLS).toInt());
132 
133  return std::unique_ptr<IMinimizer>(domainMinimizer);
134 }
135 
137 {
138  return {
145  };
146 }
147 
148 // ----------------------------------------------------------------------------
149 
151  : MinimizerItem(M_TYPE)
152 {
154  addProperty(P_MAXITERATIONS, 0)->setToolTip("Maximum number of iterations");
155 }
156 
157 std::unique_ptr<IMinimizer> GSLMultiMinimizerItem::createMinimizer() const
158 {
159  QString algorithmName = getItemValue(P_ALGORITHMS).value<ComboProperty>().getValue();
160 
161  auto* domainMinimizer = new GSLMultiMinimizer(algorithmName.toStdString());
162  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
163  return std::unique_ptr<IMinimizer>(domainMinimizer);
164 }
165 
167 {
168  return {
171  };
172 }
173 
174 // ----------------------------------------------------------------------------
175 
177  : MinimizerItem(M_TYPE)
178 {
179  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
180  addProperty(P_MAXITERATIONS, 3)->setToolTip("Maximum number of iterations");
181  addProperty(P_POPULATIONSIZE, 300)->setToolTip("Population size");
182  addProperty(P_RANDOMSEED, 0)->setToolTip("Random seed");
183 }
184 
185 std::unique_ptr<IMinimizer> GeneticMinimizerItem::createMinimizer() const
186 {
187  auto* domainMinimizer = new GeneticMinimizer();
188  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
189  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
190  domainMinimizer->setPopulationSize(getItemValue(P_POPULATIONSIZE).toInt());
191  domainMinimizer->setRandomSeed(getItemValue(P_RANDOMSEED).toInt());
192  return std::unique_ptr<IMinimizer>(domainMinimizer);
193 }
194 
196 {
197  return {
202  };
203 }
204 
205 // ----------------------------------------------------------------------------
206 
208  : MinimizerItem(M_TYPE)
209 {
210  addProperty(P_MAXITERATIONS, 100)->setToolTip("Number of points to try for each step");
211  addProperty(P_ITERATIONSTEMP, 10)->setToolTip("Number of iterations at each temperature");
212  addProperty(P_STEPSIZE, 1.0)->setToolTip("Max step size used in random walk");
213  addProperty(P_BOLTZMANN_K, 1.0)->setToolTip("Boltzmann k");
214  addProperty(P_BOLTZMANN_TINIT, 50.0)->setToolTip("Boltzmann initial temperature");
215  addProperty(P_BOLTZMANN_MU, 1.05)->setToolTip("Boltzmann mu");
216  addProperty(P_BOLTZMANN_TMIN, 0.1)->setToolTip("Boltzmann minimal temperature");
217 }
218 
219 std::unique_ptr<IMinimizer> SimAnMinimizerItem::createMinimizer() const
220 {
221  auto* domainMinimizer = new SimAnMinimizer();
222  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
223  domainMinimizer->setIterationsAtEachTemp(getItemValue(P_ITERATIONSTEMP).toInt());
224  domainMinimizer->setStepSize(getItemValue(P_STEPSIZE).toDouble());
225  domainMinimizer->setBoltzmannK(getItemValue(P_BOLTZMANN_K).toDouble());
226  domainMinimizer->setBoltzmannInitialTemp(getItemValue(P_BOLTZMANN_TINIT).toDouble());
227  domainMinimizer->setBoltzmannMu(getItemValue(P_BOLTZMANN_MU).toDouble());
228  domainMinimizer->setBoltzmannMinTemp(getItemValue(P_BOLTZMANN_TMIN).toDouble());
229  return std::unique_ptr<IMinimizer>(domainMinimizer);
230 }
231 
233 {
234  return {
242  };
243 }
244 
245 // ----------------------------------------------------------------------------
246 
248  : MinimizerItem(M_TYPE)
249 {
250  addProperty(P_TOLERANCE, 0.01)->setToolTip("Tolerance on the function value at the minimum");
251  addProperty(P_MAXITERATIONS, 0)->setToolTip("Maximum number of iterations");
252 }
253 
254 std::unique_ptr<IMinimizer> GSLLMAMinimizerItem::createMinimizer() const
255 {
256  auto* domainMinimizer = new GSLLevenbergMarquardtMinimizer();
257  domainMinimizer->setTolerance(getItemValue(P_TOLERANCE).toDouble());
258  domainMinimizer->setMaxIterations(getItemValue(P_MAXITERATIONS).toInt());
259  return std::unique_ptr<IMinimizer>(domainMinimizer);
260 }
261 
263 {
264  return {
267  };
268 }
Defines MinimizerItemCatalog class.
Defines MinimizerItem class.
QList< std::variant< DoubleDescriptor, UIntDescriptor, SelectionDescriptor< QString > >> ValueDescriptors
Definition: MinimizerItem.h:28
@ unitless
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setValue(const QString &name)
Describes properties of a double value which are necessary to allow GUI representation,...
std::unique_ptr< IMinimizer > createMinimizer() const override
static constexpr auto P_TOLERANCE
ValueDescriptors valueDescriptorsForUI() const override
static constexpr auto P_MAXITERATIONS
static constexpr auto M_TYPE
std::unique_ptr< IMinimizer > createMinimizer() const override
static constexpr auto M_TYPE
Definition: MinimizerItem.h:90
static constexpr auto P_ALGORITHMS
Definition: MinimizerItem.h:86
static constexpr auto P_MAXITERATIONS
Definition: MinimizerItem.h:87
ValueDescriptors valueDescriptorsForUI() const override
static constexpr auto P_RANDOMSEED
static constexpr auto M_TYPE
std::unique_ptr< IMinimizer > createMinimizer() const override
ValueDescriptors valueDescriptorsForUI() const override
static constexpr auto P_TOLERANCE
static constexpr auto P_MAXITERATIONS
static constexpr auto P_POPULATIONSIZE
Defines info for GroupProperty, i.e. collection of model types, their labels and the name of default ...
Definition: GroupInfo.h:25
void add(const QString &itemType, const QString &itemLabel)
Definition: GroupInfo.cpp:23
void setDefaultType(const QString &modelType)
Definition: GroupInfo.cpp:40
SelectionDescriptor< QString > objectiveMetric() const
static constexpr auto P_METRIC
Definition: MinimizerItem.h:47
static constexpr auto P_NORM
Definition: MinimizerItem.h:48
static constexpr auto P_MINIMIZERS
Definition: MinimizerItem.h:46
SelectionDescriptor< MinimizerItem * > minimizers() const
std::unique_ptr< IMinimizer > createMinimizer() const override
std::unique_ptr< ObjectiveMetric > createMetric() const
SelectionDescriptor< QString > normFunction() 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:32
MinimizerItem(const QString &model_type)
static constexpr auto M_TYPE
Definition: MinimizerItem.h:75
static constexpr auto P_TOLERANCE
Definition: MinimizerItem.h:70
static constexpr auto P_MAXFUNCTIONCALLS
Definition: MinimizerItem.h:72
static constexpr auto P_PRECISION
Definition: MinimizerItem.h:71
static constexpr auto P_ALGORITHMS
Definition: MinimizerItem.h:67
std::unique_ptr< IMinimizer > createMinimizer() const override
ValueDescriptors valueDescriptorsForUI() const override
static constexpr auto P_STRATEGY
Definition: MinimizerItem.h:68
static constexpr auto P_ERRORDEF
Definition: MinimizerItem.h:69
Describes a selection (various possibilities and the current one).
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag. name is the tag name and the display name....
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
T * item(const QString &tag) const
Definition: SessionItem.h:353
SessionItem * addGroupProperty(const QString &groupTag, const GroupInfo &groupInfo)
SessionItem & setToolTip(const QString &tooltip)
QString modelType() const
Get model type.
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
static constexpr auto P_BOLTZMANN_TINIT
ValueDescriptors valueDescriptorsForUI() const override
static constexpr auto P_BOLTZMANN_MU
static constexpr auto M_TYPE
static constexpr auto P_ITERATIONSTEMP
static constexpr auto P_BOLTZMANN_TMIN
std::unique_ptr< IMinimizer > createMinimizer() const override
static constexpr auto P_BOLTZMANN_K
static constexpr auto P_MAXITERATIONS
static constexpr auto P_STEPSIZE
Describes properties of a uint value which are necessary to allow GUI representation,...