24 const std::function<double(
double)> l1_norm = [](
double term) {
return std::abs(term); };
25 const std::function<double(
double)> l2_norm = [](
double term) {
return term * term; };
27 const std::map<std::string, std::function<std::unique_ptr<ObjectiveMetric>()>> metric_factory = {
28 {
"chi2", []() {
return std::make_unique<Chi2Metric>(); }},
29 {
"poisson-like", []() {
return std::make_unique<PoissonLikeMetric>(); }},
30 {
"log", []() {
return std::make_unique<LogMetric>(); }},
31 {
"reldiff", []() {
return std::make_unique<meanRelativeDifferenceMetric>(); }},
32 {
"rq4", []() {
return std::make_unique<RQ4Metric>(); }}};
33 const std::string default_metric_name =
"poisson-like";
35 const std::map<std::string, std::function<double(
double)>> norm_factory = {{
"l1", l1_norm},
37 const std::string default_norm_name =
"l2";
40 std::vector<std::string> keys(
const std::map<std::string, U>& map)
42 std::vector<std::string> result;
43 result.reserve(map.size());
44 for (
auto& item : map)
45 result.push_back(item.first);
69 std::transform(metric.begin(), metric.end(), metric.begin(), ::tolower);
70 std::transform(norm.begin(), norm.end(), norm.begin(), ::tolower);
71 const auto metric_iter = metric_factory.find(metric);
72 const auto norm_iter = norm_factory.find(norm);
73 if (metric_iter == metric_factory.end() || norm_iter == norm_factory.end()) {
75 ss <<
"Error in ObjectiveMetricUtils::createMetric: either metric (" << metric
76 <<
") or norm (" << norm <<
") name is unknown.\n";
78 throw std::runtime_error(ss.str());
81 auto result = metric_iter->second();
82 result->setNorm(norm_iter->second);
89 ss <<
"Available metrics:\n";
91 ss <<
"\t" << item <<
"\n";
93 ss <<
"Available norms:\n";
95 ss <<
"\t" << item <<
"\n";
102 return keys(norm_factory);
107 return keys(metric_factory);
112 return default_norm_name;
117 return default_metric_name;
Defines ObjectiveMetric utilities and corresponding namespace.
Defines ObjectiveMetric classes.
std::string defaultMetricName()
Returns default metric name.
std::string defaultNormName()
Returns default norm name.
std::string availableMetricOptions()
Prints available metric options.
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::function< double(double)> l2Norm()
Returns L2 normalization function.
std::vector< std::string > normNames()
Returns the names of the norms used by ObjectiveMetric.
std::function< double(double)> l1Norm()
Returns L1 normalization function.