use std::marker::PhantomData;
pub trait SupervisedLearning {}
pub trait UnsupervisedLearning {}
pub trait SemiSupervisedLearning {}
pub trait ReinforcementLearning {}
pub trait OnlineLearning {}
pub trait BatchLearning {}
pub trait StreamingLearning {}
#[cfg(feature = "linear_models")]
pub trait LinearModel: SupervisedLearning {}
#[cfg(feature = "tree_models")]
pub trait TreeBased {}
#[cfg(feature = "neural_networks")]
pub trait NeuralNetwork: SupervisedLearning {}
#[cfg(feature = "clustering")]
pub trait Clustering: UnsupervisedLearning {}
#[cfg(feature = "dimensionality_reduction")]
pub trait DimensionalityReduction: UnsupervisedLearning {}
#[cfg(feature = "ensemble_methods")]
pub trait EnsembleMethod {}
pub trait Classification: SupervisedLearning {}
pub trait Regression: SupervisedLearning {}
pub trait Ranking: SupervisedLearning {}
pub trait AnomalyDetection {}
pub trait DensityEstimation: UnsupervisedLearning {}
pub trait FeatureSelection {}
pub trait Parametric {}
pub trait NonParametric {}
pub trait Probabilistic {}
pub trait Deterministic {}
pub trait Interpretable {}
pub trait BlackBox {}
pub trait Incremental {}
pub trait ScaleSensitive {}
pub trait OutlierRobust {}
pub trait MissingValueTolerant {}
pub trait ParallelTraining {}
pub trait ParallelPrediction {}
#[cfg(feature = "gpu_support")]
pub trait GpuAccelerated {}
#[cfg(feature = "distributed")]
pub trait Distributed {}
pub trait MemoryEfficient {}
pub trait FastTraining {}
pub trait FastPrediction {}
pub trait NumericalData {}
pub trait CategoricalData {}
pub trait TextData {}
pub trait ImageData {}
pub trait TimeSeriesData {}
pub trait GraphData {}
pub trait SparseData {}
pub trait HighDimensionalData {}
pub trait FastAndEfficient: FastTraining + FastPrediction + MemoryEfficient {}
pub trait RealTime: FastPrediction + StreamingLearning + MemoryEfficient {}
pub trait BigData: ParallelTraining + MemoryEfficient + StreamingLearning {}
pub trait Robust: OutlierRobust + MissingValueTolerant {}
pub trait ExplainableAI: Interpretable + Probabilistic {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ComplexityClass {
Constant,
Logarithmic,
Linear,
Linearithmic,
Quadratic,
Cubic,
Exponential,
}
pub trait ComplexityBounds {
fn training_complexity() -> ComplexityClass;
fn prediction_complexity() -> ComplexityClass;
fn space_complexity() -> ComplexityClass;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StabilityClass {
Stable,
ModeratelySensitive,
Unstable,
}
pub trait StabilityBounds {
fn data_stability() -> StabilityClass;
fn hyperparameter_stability() -> StabilityClass;
}
pub mod category_system {
use super::*;
pub struct AlgorithmCategory<Learning, Problem, Model, Compute, Data> {
_phantom: PhantomData<(Learning, Problem, Model, Compute, Data)>,
}
pub struct Supervised;
pub struct Unsupervised;
pub struct SemiSupervised;
pub struct Reinforcement;
pub struct ClassificationProblem;
pub struct RegressionProblem;
pub struct ClusteringProblem;
pub struct DimensionalityReductionProblem;
pub struct ParametricModel;
pub struct NonParametricModel;
pub struct ProbabilisticModel;
pub struct DeterministicModel;
pub struct ParallelCompute;
pub struct SequentialCompute;
pub struct GpuCompute;
pub struct CpuCompute;
pub struct NumericalDataType;
pub struct CategoricalDataType;
pub struct MixedDataType;
impl<L, P, M, C, D> AlgorithmCategory<L, P, M, C, D> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<L, P, M, C, D> Default for AlgorithmCategory<L, P, M, C, D> {
fn default() -> Self {
Self::new()
}
}
pub type SupervisedClassifier = AlgorithmCategory<
Supervised,
ClassificationProblem,
ParametricModel,
ParallelCompute,
NumericalDataType,
>;
pub type SupervisedRegressor = AlgorithmCategory<
Supervised,
RegressionProblem,
ParametricModel,
ParallelCompute,
NumericalDataType,
>;
pub type UnsupervisedClusterer = AlgorithmCategory<
Unsupervised,
ClusteringProblem,
NonParametricModel,
ParallelCompute,
NumericalDataType,
>;
}
#[macro_export]
macro_rules! impl_algorithm_markers {
($algorithm:ty: $($trait:path),+ $(,)?) => {
$(
impl $trait for $algorithm {}
)+
};
}
#[macro_export]
macro_rules! define_algorithm_category {
(
$name:ident:
learning = $learning:ty,
problem = $problem:ty,
model = $model:ty,
compute = $compute:ty,
data = $data:ty,
) => {
pub type $name = $crate::algorithm_markers::category_system::AlgorithmCategory<
$learning,
$problem,
$model,
$compute,
$data,
>;
};
(
$name:ident:
learning = $learning:ty,
problem = $problem:ty,
model = $model:ty,
) => {
pub type $name = $crate::algorithm_markers::category_system::AlgorithmCategory<
$learning,
$problem,
$model,
$crate::algorithm_markers::category_system::CpuCompute,
$crate::algorithm_markers::category_system::NumericalDataType,
>;
};
}
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
struct MockLinearRegression;
impl_algorithm_markers!(
MockLinearRegression:
SupervisedLearning,
Regression,
Parametric,
Deterministic,
FastTraining,
FastPrediction,
ScaleSensitive,
NumericalData,
ParallelTraining,
ParallelPrediction
);
#[cfg(feature = "linear_models")]
impl LinearModel for MockLinearRegression {}
impl ComplexityBounds for MockLinearRegression {
fn training_complexity() -> ComplexityClass {
ComplexityClass::Cubic }
fn prediction_complexity() -> ComplexityClass {
ComplexityClass::Linear }
fn space_complexity() -> ComplexityClass {
ComplexityClass::Quadratic }
}
impl StabilityBounds for MockLinearRegression {
fn data_stability() -> StabilityClass {
StabilityClass::Stable
}
fn hyperparameter_stability() -> StabilityClass {
StabilityClass::Stable
}
}
#[test]
fn test_marker_traits() {
fn is_supervised<T: SupervisedLearning>() -> bool {
let _ = std::marker::PhantomData::<T>;
true
}
fn is_regression<T: Regression>() -> bool {
let _ = std::marker::PhantomData::<T>;
true
}
fn is_parametric<T: Parametric>() -> bool {
let _ = std::marker::PhantomData::<T>;
true
}
assert!(is_supervised::<MockLinearRegression>());
assert!(is_regression::<MockLinearRegression>());
assert!(is_parametric::<MockLinearRegression>());
}
#[test]
fn test_complexity_bounds() {
assert_eq!(
MockLinearRegression::training_complexity(),
ComplexityClass::Cubic
);
assert_eq!(
MockLinearRegression::prediction_complexity(),
ComplexityClass::Linear
);
assert_eq!(
MockLinearRegression::space_complexity(),
ComplexityClass::Quadratic
);
}
#[test]
fn test_stability_bounds() {
assert_eq!(
MockLinearRegression::data_stability(),
StabilityClass::Stable
);
assert_eq!(
MockLinearRegression::hyperparameter_stability(),
StabilityClass::Stable
);
}
#[test]
fn test_complexity_ordering() {
assert!(ComplexityClass::Constant < ComplexityClass::Linear);
assert!(ComplexityClass::Linear < ComplexityClass::Quadratic);
assert!(ComplexityClass::Quadratic < ComplexityClass::Exponential);
}
#[test]
fn test_category_system() {
let _classifier = category_system::SupervisedClassifier::new();
let _regressor = category_system::SupervisedRegressor::new();
let _clusterer = category_system::UnsupervisedClusterer::new();
}
#[test]
fn test_define_algorithm_category_macro() {
define_algorithm_category!(
MyCustomAlgorithm:
learning = category_system::Supervised,
problem = category_system::ClassificationProblem,
model = category_system::ParametricModel,
compute = category_system::ParallelCompute,
data = category_system::NumericalDataType,
);
let _my_algorithm = MyCustomAlgorithm::new();
}
struct MockRandomForest;
impl_algorithm_markers!(
MockRandomForest:
SupervisedLearning,
Classification,
NonParametric,
OutlierRobust,
MissingValueTolerant,
ParallelTraining,
ParallelPrediction,
MemoryEfficient,
StreamingLearning
);
#[cfg(feature = "tree_models")]
impl TreeBased for MockRandomForest {}
#[cfg(feature = "ensemble_methods")]
impl EnsembleMethod for MockRandomForest {}
impl Robust for MockRandomForest {}
impl BigData for MockRandomForest {}
#[test]
fn test_composite_traits() {
fn is_robust<T: Robust>() -> bool {
let _ = std::marker::PhantomData::<T>;
true
}
fn is_big_data<T: BigData>() -> bool {
let _ = std::marker::PhantomData::<T>;
true
}
assert!(is_robust::<MockRandomForest>());
assert!(is_big_data::<MockRandomForest>());
}
#[test]
fn test_algorithm_categorization() {
fn train_supervised<T: SupervisedLearning>(_algorithm: T) {
}
#[cfg(feature = "ensemble_methods")]
fn ensemble_predict<T: EnsembleMethod>(_algorithm: T) {
}
train_supervised(MockLinearRegression);
train_supervised(MockRandomForest);
#[cfg(feature = "ensemble_methods")]
ensemble_predict(MockRandomForest);
}
}