PineForge HPO 0.1.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
search_space.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <map>
5#include <optional>
6#include <string>
7#include <string_view>
8#include <variant>
9#include <vector>
10
12
13namespace pineforge::hpo {
14
16enum class DimensionKind {
17 Integer,
18 Real,
19 Boolean,
21};
22
25public:
35 std::int64_t low,
36 std::int64_t high,
37 std::int64_t step = 1,
38 bool log = false);
39
41 const std::string& name() const noexcept { return name_; }
43 std::int64_t low() const noexcept { return low_; }
45 std::int64_t high() const noexcept { return high_; }
47 std::int64_t step() const noexcept { return step_; }
49 bool log() const noexcept { return log_; }
51 bool contains(const ParameterValue& value) const noexcept;
52
53private:
54 std::string name_;
55 std::int64_t low_;
56 std::int64_t high_;
57 std::int64_t step_;
58 bool log_;
59};
60
63public:
71 RealDimension(std::string name,
72 double low,
73 double high,
74 std::optional<double> step = std::nullopt,
75 bool log = false);
76
78 const std::string& name() const noexcept { return name_; }
80 double low() const noexcept { return low_; }
82 double high() const noexcept { return high_; }
84 const std::optional<double>& step() const noexcept { return step_; }
86 bool log() const noexcept { return log_; }
88 bool contains(const ParameterValue& value) const noexcept;
89
90private:
91 std::string name_;
92 double low_;
93 double high_;
94 std::optional<double> step_;
95 bool log_;
96};
97
100public:
102 explicit BooleanDimension(std::string name);
103
105 const std::string& name() const noexcept { return name_; }
107 bool contains(const ParameterValue& value) const noexcept;
108
109private:
110 std::string name_;
111};
112
115public:
120 CategoricalDimension(std::string name, std::vector<ParameterValue> choices);
121
123 const std::string& name() const noexcept { return name_; }
125 const std::vector<ParameterValue>& choices() const noexcept { return choices_; }
127 bool contains(const ParameterValue& value) const noexcept;
128
129private:
130 std::string name_;
131 std::vector<ParameterValue> choices_;
132};
133
136 std::variant<IntegerDimension, RealDimension, BooleanDimension, CategoricalDimension>;
137
139DimensionKind dimension_kind(const Dimension& dimension) noexcept;
141std::string_view dimension_name(const Dimension& dimension) noexcept;
143bool dimension_contains(const Dimension& dimension, const ParameterValue& value) noexcept;
144
148 std::string parameter;
150 std::string message;
151};
152
158public:
160 SearchSpace() = default;
161
164 explicit SearchSpace(std::vector<Dimension> dimensions);
165
168 void add(Dimension dimension);
170 const std::vector<Dimension>& dimensions() const noexcept { return dimensions_; }
172 const Dimension* find(std::string_view name) const noexcept;
173
175 std::vector<ValidationIssue> validate(const Candidate& candidate,
176 bool reject_unknown = true) const;
178 bool is_valid(const Candidate& candidate, bool reject_unknown = true) const;
179
184 std::map<std::string, std::string> serialize_candidate(const Candidate& candidate,
185 bool reject_unknown = true) const;
186
195 std::optional<std::uint64_t> finite_cardinality() const;
196
202 Candidate candidate_at(std::uint64_t ordinal, std::uint64_t id = 0) const;
203
210 std::uint64_t candidate_ordinal(const Candidate& candidate) const;
211
212private:
213 std::vector<Dimension> dimensions_;
214};
215
216} // namespace pineforge::hpo
Named dimension whose legal values are exactly false and true.
bool contains(const ParameterValue &value) const noexcept
Returns true only when value contains a Boolean alternative.
const std::string & name() const noexcept
Returns the unique dimension name.
BooleanDimension(std::string name)
Named dimension backed by an ordered, non-empty set of scalar choices.
const std::string & name() const noexcept
Returns the unique dimension name.
bool contains(const ParameterValue &value) const noexcept
Tests exact type-and-value membership in the declared choices.
const std::vector< ParameterValue > & choices() const noexcept
Returns choices in their declared finite-grid order.
CategoricalDimension(std::string name, std::vector< ParameterValue > choices)
Constructs a categorical dimension.
Named signed-integer dimension with inclusive bounds and a positive step.
const std::string & name() const noexcept
Returns the unique dimension name.
IntegerDimension(std::string name, std::int64_t low, std::int64_t high, std::int64_t step=1, bool log=false)
Constructs an integer dimension.
std::int64_t step() const noexcept
Returns the positive lattice step.
std::int64_t high() const noexcept
Returns the inclusive upper bound.
std::int64_t low() const noexcept
Returns the inclusive lower bound.
bool log() const noexcept
Returns whether adaptive samplers operate in logarithmic latent coordinates.
bool contains(const ParameterValue &value) const noexcept
Tests both the scalar type and membership in this dimension's integer lattice.
Named binary64 dimension with inclusive bounds and an optional finite-grid step.
RealDimension(std::string name, double low, double high, std::optional< double > step=std::nullopt, bool log=false)
Constructs a real dimension.
double low() const noexcept
Returns the inclusive lower bound.
bool log() const noexcept
Returns whether adaptive samplers operate in logarithmic latent coordinates.
const std::optional< double > & step() const noexcept
Returns the finite-grid step, or std::nullopt for a continuous dimension.
bool contains(const ParameterValue &value) const noexcept
Tests both the scalar type and interval/lattice membership.
double high() const noexcept
Returns the inclusive upper bound.
const std::string & name() const noexcept
Returns the unique dimension name.
Ordered product of named dimensions with validation and finite-space codecs.
std::uint64_t candidate_ordinal(const Candidate &candidate) const
Encodes a canonical finite-space candidate into its mixed-radix ordinal.
SearchSpace()=default
Constructs an empty search space whose finite cardinality is one.
Candidate candidate_at(std::uint64_t ordinal, std::uint64_t id=0) const
Decodes a mixed-radix ordinal into its canonical finite-space candidate.
SearchSpace(std::vector< Dimension > dimensions)
Constructs a search space in declaration order.
void add(Dimension dimension)
Appends a dimension while preserving declaration order.
const Dimension * find(std::string_view name) const noexcept
Returns the named dimension, or nullptr when it is not declared.
std::map< std::string, std::string > serialize_candidate(const Candidate &candidate, bool reject_unknown=true) const
Validates and serializes a candidate for the PineForge strategy ABI.
std::vector< ValidationIssue > validate(const Candidate &candidate, bool reject_unknown=true) const
Returns every missing, mistyped, off-grid, or optionally unknown parameter issue.
bool is_valid(const Candidate &candidate, bool reject_unknown=true) const
Returns whether validate() would produce no issues for the same arguments.
std::optional< std::uint64_t > finite_cardinality() const
Returns the exact Cartesian-product cardinality when every dimension is finite.
const std::vector< Dimension > & dimensions() const noexcept
Returns all dimensions in declaration order.
std::string_view dimension_name(const Dimension &dimension) noexcept
Returns the name of the concrete dimension.
std::variant< IntegerDimension, RealDimension, BooleanDimension, CategoricalDimension > Dimension
Closed variant over every supported search-space dimension type.
DimensionKind
Concrete dimension category stored by Dimension.
@ Boolean
The two values false and true.
@ Categorical
Explicit ordered set of scalar choices.
@ Real
Continuous or stepped binary64 interval.
@ Integer
Discrete signed-integer lattice.
std::variant< std::int64_t, double, bool, std::string > ParameterValue
Scalar parameter value accepted by search spaces and the PineForge strategy ABI.
Definition types.hpp:11
bool dimension_contains(const Dimension &dimension, const ParameterValue &value) noexcept
Dispatches a type-aware membership test to the concrete dimension.
DimensionKind dimension_kind(const Dimension &dimension) noexcept
Returns the concrete category stored in dimension.
Complete parameter assignment proposed by a sampler.
Definition types.hpp:36
One candidate-validation failure tied to a parameter name.
std::string message
Human-readable validation failure.
std::string parameter
Dimension name, or the offending unknown candidate key.