ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Strategy.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "utils/Invariant.hpp"
4#include "utils/Types.hpp"
5
6#include <map>
7#include <memory>
8#include <optional>
9#include <set>
10#include <stdexcept>
11#include <string>
12
13#include <nlohmann/json.hpp>
14
34 inline std::map<std::string, std::string>& primary_params_registry() {
35 static std::map<std::string, std::string> r;
36 return r;
37 }
38
58 inline bool register_primary_param(std::string const& strategy, std::string const& param) {
59 auto [it, inserted] = primary_params_registry().try_emplace(strategy, param);
60 invariant(inserted, "Conflicting primary_param for '" + strategy + "': '" + it->second + "' vs '" + param + "'");
61 return true;
62 }
63
70 inline std::optional<std::string> primary_param_for(std::string const& strategy) {
71 auto& r = primary_params_registry();
72 if (auto it = r.find(strategy); it != r.end()) {
73 return it->second;
74 }
75 return std::nullopt;
76 }
77}
78
93template<typename Derived> class Strategy {
94public:
95 using Ptr = std::shared_ptr<Derived>;
96
98 using Factory = Ptr (*)(nlohmann::json const&);
99
100 virtual ~Strategy() = default;
101
108 virtual nlohmann::json to_json() const = 0;
109
111 virtual std::string display_name() const = 0;
112
125 virtual std::set<ppforest2::types::Mode> supported_modes() const = 0;
126
138 static bool register_strategy(std::string const& name, Factory factory) {
139 registry()[name] = factory;
140 return true;
141 }
142
151 static Ptr from_json(nlohmann::json const& j) {
152 auto name = j.at("name").get<std::string>();
153 auto& reg = registry();
154 auto it = reg.find(name);
155
156 if (it == reg.end()) {
157 throw std::runtime_error("Unknown strategy: " + name);
158 }
159
160 return it->second(j);
161 }
162
163private:
164 static std::map<std::string, Factory>& registry() {
165 static std::map<std::string, Factory> r;
166 return r;
167 }
168};
169
185#define PPFOREST2_REGISTER_STRATEGY(StrategyBase, name) \
186 inline static const bool registered_ = StrategyBase::register_strategy(name, from_json);
187
188
228#define PPFOREST2_REGISTER_PRIMARY_PARAM(strategy_name, param_name) \
229 inline static const bool registered_primary_param_ = \
230 ppforest2::strategies::register_primary_param(strategy_name, param_name);
void invariant(bool condition, char const *message)
Runtime assertion that throws on failure.
CRTP base class providing self-registration for strategy types.
Definition Strategy.hpp:93
Ptr(*)(nlohmann::json const &) Factory
Factory function type for deserializing a strategy from JSON.
Definition Strategy.hpp:98
std::shared_ptr< Derived > Ptr
Definition Strategy.hpp:95
virtual nlohmann::json to_json() const =0
Serialize this strategy's configuration to JSON.
static Ptr from_json(nlohmann::json const &j)
Construct a strategy from its JSON representation.
Definition Strategy.hpp:151
virtual std::set< ppforest2::types::Mode > supported_modes() const =0
Training modes this strategy supports.
static bool register_strategy(std::string const &name, Factory factory)
Register a concrete strategy for JSON deserialization.
Definition Strategy.hpp:138
virtual ~Strategy()=default
virtual std::string display_name() const =0
Human-readable name for display in summaries.
Definition Strategy.hpp:15
std::map< std::string, std::string > & primary_params_registry()
Cross-family registry for strategies that accept a CLI positional-shorthand value (e....
Definition Strategy.hpp:34
bool register_primary_param(std::string const &strategy, std::string const &param)
Register a strategy's primary (shorthand) parameter name.
Definition Strategy.hpp:58
std::optional< std::string > primary_param_for(std::string const &strategy)
Look up a strategy's primary shorthand parameter by name.
Definition Strategy.hpp:70