PineForge HPO 1.11.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
objective.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstddef>
6#include <functional>
7#include <memory>
8#include <stdexcept>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
14
15namespace pineforge::hpo {
16
18using MetricMap = std::unordered_map<std::string, double>;
19
22 Reject,
23 Ieee754,
24};
25
27enum class NonFinitePolicy {
28 Reject,
29 Allow,
30};
31
41
50
54 bool valid = false;
56 double value = 0.0;
60 std::string diagnostic;
61};
62
65public:
67 ExpressionError(std::string message, std::size_t offset);
68
70 std::size_t offset() const noexcept { return offset_; }
71
72private:
73 std::size_t offset_;
74};
75
76namespace detail {
77struct ExpressionNode;
78} // namespace detail
79
86public:
89 explicit MetricExpression(std::string source);
91
93 MetricExpression& operator=(MetricExpression&&) noexcept;
95 MetricExpression& operator=(const MetricExpression&) = delete;
96
101 const EvaluationPolicy& policy = {}) const noexcept;
103 const std::string& source() const noexcept { return source_; }
105 const std::vector<std::string>& identifiers() const noexcept { return identifiers_; }
106
107private:
108 std::string source_;
109 std::unique_ptr<detail::ExpressionNode> root_;
110 std::vector<std::string> identifiers_;
111};
112
115 LessEqual,
117 Equal,
118};
119
123 std::string name;
125 double lhs = 0.0;
129 double rhs = 0.0;
131 double tolerance = 0.0;
132
134 bool satisfied() const noexcept;
135
139 double violation() const noexcept;
140};
141
145 std::vector<double> values;
147 std::vector<Constraint> constraints;
149 bool valid = true;
151 std::string diagnostic;
152
154 bool feasible() const noexcept;
155
157 static ObjectiveResult invalid(std::string diagnostic);
158};
159
161template <typename Observation>
162using ObjectiveFn = std::function<ObjectiveResult(const Observation&, const TrialContext&)>;
163
164} // namespace pineforge::hpo
Parse error raised while compiling a metric expression.
Definition objective.hpp:64
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:70
Parsed metric expression that is compiled once and evaluated repeatedly.
Definition objective.hpp:85
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.
Adds stable metadata while preserving a standard exception's catch compatibility.
Definition error.hpp:71
EvaluationError
Stable reason code for an invalid ExpressionEvaluation.
Definition objective.hpp:43
@ 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:27
@ 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:18
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:21
@ 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:33
DivisionByZeroPolicy division_by_zero
Division-by-zero behavior.
Definition objective.hpp:35
NonFinitePolicy non_finite_result
Behavior for a non-finite final expression value.
Definition objective.hpp:39
NonFinitePolicy non_finite_metric
Behavior for a referenced non-finite metric.
Definition objective.hpp:37
Non-throwing result envelope returned by MetricExpression::evaluate().
Definition objective.hpp:52
EvaluationError error
Machine-readable failure reason, or EvaluationError::None.
Definition objective.hpp:58
double value
Evaluated scalar; meaningful only when valid is true.
Definition objective.hpp:56
bool valid
True only when value is usable under the selected policy.
Definition objective.hpp:54
std::string diagnostic
Human-readable failure detail, empty after successful evaluation.
Definition objective.hpp:60
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