Skip to content

feat(concepts): add C++20 concepts for operators, metrics, and policy types - #67

Merged
foolnotion merged 10 commits into
mainfrom
feature/concepts
Jun 4, 2026
Merged

feat(concepts): add C++20 concepts for operators, metrics, and policy types#67
foolnotion merged 10 commits into
mainfrom
feature/concepts

Conversation

@foolnotion

Copy link
Copy Markdown
Member

Summary

  • Adds Creator, Mutator, Crossover, Selector, Reinserter, Evaluator, ErrorMetric, and Hasher concepts to include/operon/core/concepts.hpp alongside the existing Arithmetic one. Tree and Individual are forward-declared to keep the header lightweight; Likelihood and OptimizerLoss remain in likelihood_base.hpp where their Eigen/vstat dependencies live.
  • Replaces static_assert + <type_traits> guards in all six error metric free-function headers with requires clauses using Concepts::Arithmetic and std::same_as. Span overloads use the concept directly as the named template parameter.
  • Constrains the Lik template parameter of MinimumDescriptionLengthEvaluator and FractionalBayesFactorEvaluator with Concepts::Likelihood.

Evaluator<DTable> is intentionally left with the runtime ErrorMetric polymorphic member — the CLI relies on dynamic_cast to a single concrete type, so templatising on the metric type would break that.

Test plan

  • Full build passes (cmake --build build -j$(nproc)) with no new errors or warnings
  • Verify static_assert messages are gone and replaced by constraint failures for bad instantiations
  • Check that existing evaluator/error metric tests still pass

Adds Creator, Mutator, Crossover, Selector, Reinserter, Evaluator,
ErrorMetric, and Hasher concepts alongside the existing Arithmetic one.
Tree and Individual are forward-declared to keep the header lightweight.
Likelihood and OptimizerLoss remain in likelihood_base.hpp (Eigen/vstat deps).
…tors

Replace static_assert+type_traits guards in all error metric free functions
with requires clauses using Concepts::Arithmetic and std::same_as. Use
Concepts::Arithmetic as a named template parameter on span overloads.

Constrain the Lik template parameter of MinimumDescriptionLengthEvaluator
and FractionalBayesFactorEvaluator with Concepts::Likelihood.
Copilot AI review requested due to automatic review settings June 3, 2026 08:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes Operon’s C++ type constraints by introducing a set of C++20 concepts in core/concepts.hpp, then applying them to operator/evaluator templates and the error-metric free-function APIs to replace static_assert-based checks with requires clauses.

Changes:

  • Added multiple C++20 concepts (e.g., Creator, Mutator, Crossover, Selector, Reinserter, Evaluator, ErrorMetric, Hasher) to include/operon/core/concepts.hpp.
  • Replaced static_assert + <type_traits> checks in several error metric headers with requires constraints using Concepts::Arithmetic and std::same_as.
  • Constrained the Lik template parameter for MinimumDescriptionLengthEvaluator and FractionalBayesFactorEvaluator to Concepts::Likelihood.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
include/operon/operators/evaluator.hpp Constrains evaluator likelihood template parameters via Concepts::Likelihood.
include/operon/error_metrics/sum_of_squared_errors.hpp Replaces static_assert type guards with requires constraints and concept-bound span overloads.
include/operon/error_metrics/r2_score.hpp Adds requires constraints and refactors type usage in accumulate calls.
include/operon/error_metrics/normalized_mean_squared_error.hpp Adds requires constraints and updates span overloads to use pointer-based calls.
include/operon/error_metrics/mean_squared_error.hpp Adds requires constraints and concept-bound span overloads.
include/operon/error_metrics/mean_absolute_error.hpp Adds requires constraints and concept-bound span overloads.
include/operon/error_metrics/correlation_coefficient.hpp Adds requires constraints and concept-bound span overloads (plus squared correlation helpers).
include/operon/core/concepts.hpp Introduces the new suite of C++20 concepts and forward declarations to reduce heavy includes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

auto sqres = [](auto a, auto b) { auto e=a-b; return e*e; };
auto const ssr = accumulate<V1>(begin1, end1, begin2, sqres).sum;
auto const sst = accumulate<V2>(begin2, begin2 + std::distance(begin1, end1)).ssr;
auto const sst = accumulate<V1>(begin2, begin2 + std::distance(begin1, end1)).ssr;
using vstat::univariate::accumulate;
auto sqres = [](auto a, auto b) { auto e=a-b; return e*e; };
auto const ssr = accumulate<V1>(begin1, end1, begin2, begin3, sqres).sum;
auto end2 = begin2 + std::distance(begin1, end1);
static_assert(std::is_arithmetic_v<V1>, "InputIt1: value_type must be arithmetic.");
static_assert(std::is_arithmetic_v<V2>, "InputIt2: value_type must be arithmetic.");
static_assert(std::is_same_v<V1, V2>, "The types must be the same");
auto varY = vstat::univariate::accumulate<V1>(begin2, begin2 + std::distance(begin1, end1)).variance;
static_assert(std::is_arithmetic_v<V1>, "InputIt1: value_type must be arithmetic.");
static_assert(std::is_arithmetic_v<V2>, "InputIt2: value_type must be arithmetic.");
static_assert(std::is_same_v<V1, V2>, "The types must be the same");
auto varY = vstat::univariate::accumulate<V1>(begin2, begin2 + std::distance(begin1, end1), begin3).variance;
operator+ on non-random-access iterators is ill-formed; std::next works
for all iterator categories and makes the template requirements explicit.
…s_iterator

vstat requires random-access iterators at its interface. Make that
requirement explicit on all metric iterator overloads so that
iterator arithmetic (begin + distance) is well-formed by contract
rather than relying on std::next to paper over the constraint.
MSE, MAE, R2Score, and NMSE iterator overloads now delegate to
vstat::metrics::{mean_squared_error,mean_absolute_error,r2_score}
which use EVE SIMD wide types. Iterator constraints tightened from
std::random_access_iterator to std::contiguous_iterator to match
vstat's interface; raw pointer (Span::data()) callers are unaffected.

SSE and CorrelationCoefficient have no dedicated SIMD path in vstat
and remain on the generic accumulate path (random_access_iterator).
… constraint

vstat::metrics::r2_score uses a different weighted-variance normalization
that diverges from the Elki reference by ~3e-4 in the weighted case.
Use vstat::univariate::accumulate directly to preserve the correct formula,
while keeping std::contiguous_iterator so the constraint reflects vstat's
actual requirement and begin + distance arithmetic remains valid.
The test's Elki::MeanVariance::R2(x,y,z) was computing TSS using the
unweighted mean of y instead of the weighted mean, giving a wrong
reference value. Fixed by passing weights to PopulationStats.

With the correct reference, vstat::metrics::r2_score passes at 1e-6
for both the unweighted and weighted cases, so the SIMD path is restored.
…me clash

Rename Concepts::ErrorMetric -> Concepts::ErrorMetricCallable and
Concepts::Evaluator -> Concepts::EvaluatorCallable to prevent ambiguity
with the Operon::ErrorMetric struct and Operon::Evaluator class template
in any translation unit that does `using namespace Operon::Concepts`.

Also adds the weighted t(x, y, w) requirement to ErrorMetricCallable so
the concept fully describes the Operon::ErrorMetric callable interface.
@foolnotion
foolnotion merged commit 5f94ac0 into main Jun 4, 2026
3 checks passed
@foolnotion
foolnotion deleted the feature/concepts branch June 4, 2026 14:10
foolnotion added a commit that referenced this pull request Jul 13, 2026
feat(concepts): add C++20 concepts for operators, metrics, and policy types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants