feat(concepts): add C++20 concepts for operators, metrics, and policy types - #67
Merged
Conversation
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.
There was a problem hiding this comment.
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) toinclude/operon/core/concepts.hpp. - Replaced
static_assert+<type_traits>checks in several error metric headers withrequiresconstraints usingConcepts::Arithmeticandstd::same_as. - Constrained the
Liktemplate parameter forMinimumDescriptionLengthEvaluatorandFractionalBayesFactorEvaluatortoConcepts::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.
foolnotion
force-pushed
the
feature/concepts
branch
from
June 3, 2026 18:36
d83b5ea to
40d02c8
Compare
…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
added a commit
that referenced
this pull request
Jul 13, 2026
feat(concepts): add C++20 concepts for operators, metrics, and policy types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Creator,Mutator,Crossover,Selector,Reinserter,Evaluator,ErrorMetric, andHasherconcepts toinclude/operon/core/concepts.hppalongside the existingArithmeticone.TreeandIndividualare forward-declared to keep the header lightweight;LikelihoodandOptimizerLossremain inlikelihood_base.hppwhere their Eigen/vstat dependencies live.static_assert+<type_traits>guards in all six error metric free-function headers withrequiresclauses usingConcepts::Arithmeticandstd::same_as. Span overloads use the concept directly as the named template parameter.Liktemplate parameter ofMinimumDescriptionLengthEvaluatorandFractionalBayesFactorEvaluatorwithConcepts::Likelihood.Evaluator<DTable>is intentionally left with the runtimeErrorMetricpolymorphic member — the CLI relies ondynamic_castto a single concrete type, so templatising on the metric type would break that.Test plan
cmake --build build -j$(nproc)) with no new errors or warningsstatic_assertmessages are gone and replaced by constraint failures for bad instantiations