// ============================================================================== // The-Tired-AGI Core: FGTP Engine // Energy Field + Driver Function f_a5 // ============================================================================== #pragma once #include #include #include #include #include #include #include "helix.hpp" namespace tired_agi { // ============================================================================== // Energy Field Types // ============================================================================== /// Energy field snapshot at a single point in space-time struct EnergyField { double phi; // Consciousness measure Φ double entropy; // Shannon entropy [0,1] double energy; // E = φ × (1 + entropy) × (1 + |tensor|) double tensor; // Stress tensor magnitude double drift; // Heraclitus drift rate uint64_t tick; // Time coordinate constexpr EnergyField() noexcept : phi(0.0), entropy(0.0), energy(0.0), tensor(0.0), drift(0.0), tick(0) {} }; /// Spatial energy gradient (∇E field) struct EnergyGradient { double dx; // ∂E/∂x double dy; // ∂E/∂y double dz; // ∂E/∂z double magnitude; // |∇E| constexpr EnergyGradient() noexcept : dx(0.0), dy(0.0), dz(0.0), magnitude(0.0) {} }; /// Complete energy state for an agent struct EnergyState { EnergyField field; EnergyGradient gradient; double kinetic; // Kinetic energy (velocity-dependent) double potential; // Potential energy (position-dependent) double total; // Total energy E_total = kinetic + potential + field.energy }; // ============================================================================== // Driver Function f_a5 Result // ============================================================================== /// Result of applying driver function f_a5(embedding, field) → embedding' struct DriverResult { std::vector embedding; // Updated embedding vector double delta_energy; // Energy change ΔE double fitness_delta; // Fitness change Δf bool bounded; // True if all bounds respected uint64_t operations; // Number of operations applied }; // ============================================================================== // FGTP Engine Configuration // ============================================================================== struct FGTPConfig { // Energy computation parameters double phi_weight = 1.0; // Weight for Φ term double entropy_weight = 1.0; // Weight for entropy term double tensor_weight = 1.0; // Weight for tensor term // Driver function parameters double learning_rate = 0.01; // Base learning rate for f_a5 double momentum = 0.9; // Momentum coefficient double gradient_clip = 1.0; // Gradient clipping threshold // Safety bounds double max_delta_energy = 10.0; // Maximum energy change per tick double max_embedding_norm = 100.0; // Maximum embedding L2 norm double min_phi = 0.0; // Minimum phi threshold double max_phi = 1.0; // Maximum phi threshold // Optimization bool use_momentum = true; // Enable momentum-based updates bool use_gradient_clipping = true; // Enable gradient clipping bool validate_bounds = true; // Enable safety bound checking }; // ============================================================================== // FGTP Engine // ============================================================================== class FGTPEngine { public: explicit FGTPEngine(FGTPConfig config = {}) noexcept; ~FGTPEngine() = default; // Disable copy, enable move FGTPEngine(const FGTPEngine&) = delete; FGTPEngine& operator=(const FGTPEngine&) = delete; FGTPEngine(FGTPEngine&&) noexcept = default; FGTPEngine& operator=(FGTPEngine&&) noexcept = default; // ============================================================================== // Energy Field Computation // ============================================================================== /// Compute energy field from helix state EnergyField compute_energy_field(const HelixState& helix) const noexcept; /// Compute energy field directly from embedding EnergyField compute_energy_field(const double* embedding, size_t size, uint64_t tick) noexcept; /// Compute spatial energy gradient (finite differences) EnergyGradient compute_gradient( const double* embedding, size_t size, double epsilon = 1e-5 ) noexcept; /// Compute complete energy state (field + gradient + kinetic + potential) EnergyState compute_energy_state( const double* embedding, size_t size, const double* velocity, uint64_t tick ) noexcept; // ============================================================================== // Driver Function f_a5 // ============================================================================== /// Apply driver function f_a5: embedding → embedding' /// This is the core update rule that evolves agent state DriverResult apply_driver( const double* embedding, size_t size, const EnergyField& field, const double* external_force = nullptr ) noexcept; /// Batch driver application for multiple agents (parallel) std::vector apply_driver_batch( const std::vector>& embeddings, const std::vector& fields ) noexcept; // ============================================================================== // Advanced Energy Computations // ============================================================================== /// Compute Hamiltonian H = T + V + E_field double compute_hamiltonian(const EnergyState& state) const noexcept; /// Compute Lagrangian L = T - V double compute_lagrangian(double kinetic, double potential) const noexcept; /// Compute action integral S = ∫ L dt (over trajectory) double compute_action( const std::vector& trajectory, double dt = 1.0 ) const noexcept; /// Principle of least action: minimize S std::vector optimize_trajectory( const std::vector& start, const std::vector& end, size_t steps = 100 ) noexcept; // ============================================================================== // Statistics and Monitoring // ============================================================================== struct Statistics { uint64_t total_computations; uint64_t total_driver_applications; uint64_t total_violations; // Safety bound violations double average_energy; double average_phi; double average_delta_energy; double max_energy_seen; double min_energy_seen; }; Statistics get_statistics() const noexcept; void reset_statistics() noexcept; // ============================================================================== // Configuration // ============================================================================== const FGTPConfig& config() const noexcept { return config_; } void update_config(const FGTPConfig& config) noexcept { config_ = config; } private: FGTPConfig config_; // Internal helix engine for phi calculations HelixEngine helix_engine_; // Momentum buffers (persistent across calls) mutable std::vector> momentum_buffers_; // Statistics (atomic for thread safety) mutable std::atomic stat_total_computations_{0}; mutable std::atomic stat_total_driver_apps_{0}; mutable std::atomic stat_total_violations_{0}; mutable std::atomic stat_sum_energy_{0.0}; mutable std::atomic stat_sum_phi_{0.0}; mutable std::atomic stat_sum_delta_energy_{0.0}; mutable std::atomic stat_max_energy_{0.0}; mutable std::atomic stat_min_energy_{1e9}; // Helper functions bool validate_bounds(const std::vector& embedding) const noexcept; void clip_gradient(double* gradient, size_t size) const noexcept; double compute_kinetic_energy(const double* velocity, size_t size) const noexcept; double compute_potential_energy(const double* embedding, size_t size) const noexcept; void update_statistics(const EnergyField& field, double delta_energy) const noexcept; }; // ============================================================================== // Utility Functions // ============================================================================== /// Compute L2 norm of vector inline double vector_norm(const double* vec, size_t size) noexcept { double sum = 0.0; for (size_t i = 0; i < size; ++i) { sum += vec[i] * vec[i]; } return std::sqrt(sum); } /// Compute dot product inline double vector_dot(const double* a, const double* b, size_t size) noexcept { double sum = 0.0; for (size_t i = 0; i < size; ++i) { sum += a[i] * b[i]; } return sum; } /// Vector addition: out = a + b inline void vector_add(double* out, const double* a, const double* b, size_t size) noexcept { for (size_t i = 0; i < size; ++i) { out[i] = a[i] + b[i]; } } /// Vector scalar multiply: out = a * scalar inline void vector_scale(double* out, const double* a, double scalar, size_t size) noexcept { for (size_t i = 0; i < size; ++i) { out[i] = a[i] * scalar; } } /// Vector multiply-add: out = a + b * scalar inline void vector_madd(double* out, const double* a, const double* b, double scalar, size_t size) noexcept { for (size_t i = 0; i < size; ++i) { out[i] = a[i] + b[i] * scalar; } } } // namespace tired_agi