PineForge HPO 0.1.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
objective.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <memory>
6#include <stdexcept>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
12
13namespace pineforge::hpo {
14
16using MetricMap = std::unordered_map<std::string, double>;
17
20 Reject,
21 Ieee754,
22};
23
25enum class NonFinitePolicy {
26 Reject,
27 Allow,
28};
29
39
48
52 bool valid = false;
54 double value = 0.0;
58 std::string diagnostic;
59};
60
62class ExpressionError : public std::runtime_error {
63public:
65 ExpressionError(std::string message, std::size_t offset);
66
68 std::size_t offset() const noexcept { return offset_; }
69
70private:
71 std::size_t offset_;
72};
73
74namespace detail {
75struct ExpressionNode;
76} // namespace detail
77
84public:
87 explicit MetricExpression(std::string source);
89
91 MetricExpression& operator=(MetricExpression&&) noexcept;
93 MetricExpression& operator=(const MetricExpression&) = delete;
94
99 const EvaluationPolicy& policy = {}) const noexcept;
101 const std::string& source() const noexcept { return source_; }
103 const std::vector<std::string>& identifiers() const noexcept { return identifiers_; }
104
105private:
106 std::string source_;
107 std::unique_ptr<detail::ExpressionNode> root_;
108 std::vector<std::string> identifiers_;
109};
110
113 LessEqual,
115 Equal,
116};
117
121 std::string name;
123 double lhs = 0.0;
127 double rhs = 0.0;
129 double tolerance = 0.0;
130
132 bool satisfied() const noexcept;
133
137 double violation() const noexcept;
138};
139
143 std::vector<double> values;
145 std::vector<Constraint> constraints;
147 bool valid = true;
149 std::string diagnostic;
150
152 bool feasible() const noexcept;
153
155 static ObjectiveResult invalid(std::string diagnostic);
156};
157
159template <typename Observation>
160using ObjectiveFn = std::function<ObjectiveResult(const Observation&, const TrialContext&)>;
161
162} // namespace pineforge::hpo
Parse error raised while compiling a metric expression.
Definition objective.hpp:62
ExpressionError(std::string message, std::size_t offset)
Constructs an error whose what() text includes the byte offset.
std::size_t offset() const noexcept
Returns the zero-based byte offset in the original expression.
Definition objective.hpp:68
Parsed metric expression that is compiled once and evaluated repeatedly.
Definition objective.hpp:83
MetricExpression(std::string source)
Parses and compiles source.
MetricExpression(MetricExpression &&) noexcept
ExpressionEvaluation evaluate(const MetricMap &metrics, const EvaluationPolicy &policy={}) const noexcept
Evaluates the compiled expression without throwing.
const std::vector< std::string > & identifiers() const noexcept
Returns unique metric identifiers in first-appearance order.
const std::string & source() const noexcept
Returns the original expression text.
EvaluationError
Stable reason code for an invalid ExpressionEvaluation.
Definition objective.hpp:41
@ DivisionByZero
Division by zero was rejected.
@ NonFiniteMetric
A referenced metric violated the non-finite metric policy.
@ None
Evaluation succeeded.
@ MissingMetric
A referenced identifier was absent from the metric map.
@ NonFiniteResult
Evaluation failed or produced a disallowed non-finite result.
std::function< ObjectiveResult(const Observation &, const TrialContext &)> ObjectiveFn
Type-erased custom objective accepting an observation and immutable trial context.
NonFinitePolicy
Behavior when a metric or expression result is not finite.
Definition objective.hpp:25
@ Allow
Preserve the non-finite value for the caller.
@ Reject
Reject the non-finite value.
std::unordered_map< std::string, double > MetricMap
Metric values keyed by canonical objective path.
Definition objective.hpp:16
ConstraintRelation
Comparison relation applied by Constraint.
@ GreaterEqual
Require lhs >= rhs - tolerance.
@ LessEqual
Require lhs <= rhs + tolerance.
@ Equal
Require abs(lhs - rhs) <= tolerance.
DivisionByZeroPolicy
Behavior when an objective expression divides by zero.
Definition objective.hpp:19
@ Ieee754
Permit IEEE-754 infinity or NaN, subject to the result policy.
@ Reject
Return an invalid ExpressionEvaluation.
Named scalar feasibility constraint with an absolute non-negative tolerance.
double rhs
Configured right-hand side.
double lhs
Evaluated left-hand side.
std::string name
User-facing constraint identifier.
ConstraintRelation relation
Required comparison relation.
double violation() const noexcept
Returns zero when satisfied and the positive violation magnitude otherwise.
double tolerance
Absolute tolerance; negative values are treated as zero.
bool satisfied() const noexcept
Returns whether all operands are finite and the relation holds.
Policies controlling exceptional arithmetic during metric-expression evaluation.
Definition objective.hpp:31
DivisionByZeroPolicy division_by_zero
Division-by-zero behavior.
Definition objective.hpp:33
NonFinitePolicy non_finite_result
Behavior for a non-finite final expression value.
Definition objective.hpp:37
NonFinitePolicy non_finite_metric
Behavior for a referenced non-finite metric.
Definition objective.hpp:35
Non-throwing result envelope returned by MetricExpression::evaluate().
Definition objective.hpp:50
EvaluationError error
Machine-readable failure reason, or EvaluationError::None.
Definition objective.hpp:56
double value
Evaluated scalar; meaningful only when valid is true.
Definition objective.hpp:54
bool valid
True only when value is usable under the selected policy.
Definition objective.hpp:52
std::string diagnostic
Human-readable failure detail, empty after successful evaluation.
Definition objective.hpp:58
Objective values, constraints, and diagnostics produced for one observation.
std::string diagnostic
Human-readable invalid-result detail.
std::vector< Constraint > constraints
Feasibility constraints evaluated alongside the objective.
bool feasible() const noexcept
Returns true when valid, non-empty, finite-valued, and all constraints pass.
std::vector< double > values
Objective vector; the current native samplers consume one value.
Metadata supplied to a custom objective while evaluating one trial.
Definition types.hpp:50