#pragma once /** * @file fitness.hpp * @brief Fitness evaluation — loss signal processing and multi-objective evaluation. * * Port of: packages/fces/core/fitness_engine.py + fitness.py */ #include #include #include namespace fces { /** * Running statistics tracker (Welford's algorithm). * Thread-safe, O(1) memory, numerically stable. */ class RunningStats { public: void update(float value); float z_score(float value) const; float get_mean() const { return mean_; } float get_std() const; int get_count() const { return count_; } void reset(); private: int count_ = 0; float mean_ = 0.0f; float m2_ = 0.0f; }; /** * FitnessEngine — processes raw loss values into controller fitness signals. */ class FitnessEngine { public: explicit FitnessEngine(float grokking_coefficient = 0.1f); /** * Calculate loss velocity signal. * * @param current_loss Current step loss * @param ema_loss Exponential moving average loss * @param mode "relative" or "absolute" * @return Velocity signal (negative = improving) */ float calculate_loss_signal(float current_loss, float ema_loss, const std::string& mode = "relative") const; /** * Compute Kibble-Zurek Mechanism damping factor. * Prevents topological defects during phase transitions. */ float compute_kzm_damping(float spectral_alpha) const; private: float grokking_coefficient_; }; /** * FuzzySet represents a fuzzy set with a trapezoidal membership function. */ class FuzzySet { public: FuzzySet(std::string name, float a, float b, float c, float d) noexcept : name_(std::move(name)), a_(a), b_(b), c_(c), d_(d) {} float membership(float x) const noexcept { if (!std::isfinite(x)) { return 0.0f; } if (x <= a_ || x >= d_) { return 0.0f; } if (x >= b_ && x <= c_) { return 1.0f; } if (x > a_ && x < b_) { float range = b_ - a_; return (x - a_) / (range > 0.0f ? range : 1e-9f); } if (x > c_ && x < d_) { float range = d_ - c_; return (d_ - x) / (range > 0.0f ? range : 1e-9f); } return 0.0f; } const std::string& name() const noexcept { return name_; } private: std::string name_; float a_; float b_; float c_; float d_; }; /** * Fitness metrics for multi-objective evaluation. */ struct FitnessMetrics { float training_advantage = 0.0f; float validation_advantage = 0.0f; float grad_cv = 0.0f; float sparsity_delta = 0.0f; float consistency_gap = 0.0f; float stable_rank = 0.0f; }; /** * FuzzyFitnessEvaluator — multi-objective fitness evaluation with fuzzy weighting. */ class FuzzyFitnessEvaluator { public: FuzzyFitnessEvaluator() noexcept; float evaluate(const FitnessMetrics& metrics) const noexcept; private: FuzzySet stability_set_; FuzzySet train_set_; FuzzySet val_set_; FuzzySet sparsity_set_; FuzzySet consistency_set_; FuzzySet rank_set_; float w_stability_ = 0.2f; float w_train_ = 0.2f; float w_val_ = 0.3f; float w_sparsity_ = 0.1f; float w_consistency_ = 0.2f; float w_rank_ = 0.1f; }; } // namespace fces