- CMakeLists.txt with libtorch, GoogleTest, GoogleBenchmark, OpenMP, pybind11 - Header files: config, controller, population, fitness, evolution, spectral, oscillation, telemetry, optimizer - Source implementations: controller (full micro-MLP forward pass, mutation, crossover), fitness (Welford's algorithm), oscillation (DFT), spectral (SVD rank), optimizer (sign-SGD stub) - Tests: controller, population, fitness, optimizer (Google Test) - Benchmarks: evolve throughput, optimizer step (Google Benchmark) - Examples: simple optimization, PyTorch/libtorch integration - Python extension: pybind11 bindings with setup.py - README with architecture diagram and build instructions
91 lines
2.1 KiB
C++
91 lines
2.1 KiB
C++
#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 <cmath>
|
|
#include <vector>
|
|
|
|
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_;
|
|
};
|
|
|
|
/**
|
|
* Fitness metrics for multi-objective evaluation.
|
|
*/
|
|
struct FitnessMetrics {
|
|
float loss_improvement = 0.0f;
|
|
float sparsity_score = 0.0f;
|
|
float stability_score = 0.0f;
|
|
float novelty_score = 0.0f;
|
|
|
|
/// Weighted combination
|
|
float total(float alpha = 0.7f, float beta = 0.3f) const {
|
|
return alpha * loss_improvement + beta * sparsity_score;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* FuzzyFitnessEvaluator — multi-objective fitness evaluation with fuzzy weighting.
|
|
*/
|
|
class FuzzyFitnessEvaluator {
|
|
public:
|
|
FitnessMetrics evaluate(
|
|
float loss_before,
|
|
float loss_after,
|
|
float sparsity = 0.0f,
|
|
float val_loss = -1.0f
|
|
) const;
|
|
};
|
|
|
|
} // namespace fces
|