Recovered source transcript
1// ==============================================================================
2// The-Tired-AGI Core: FGTP Engine
3// Energy Field + Driver Function f_a5
4// ==============================================================================
5
6#pragma once
7
8#include <array>
9#include <vector>
10#include <cstdint>
11#include <memory>
12#include <atomic>
13#include <optional>
14#include "helix.hpp"
15
16namespace tired_agi {
17
18// ==============================================================================
19// Energy Field Types
20// ==============================================================================
21
22/// Energy field snapshot at a single point in space-time
23struct EnergyField {
24 double phi; // Consciousness measure Φ
25 double entropy; // Shannon entropy [0,1]
26 double energy; // E = φ × (1 + entropy) × (1 + |tensor|)
27 double tensor; // Stress tensor magnitude
28 double drift; // Heraclitus drift rate
29 uint64_t tick; // Time coordinate
30
31 constexpr EnergyField() noexcept
32 : phi(0.0), entropy(0.0), energy(0.0), tensor(0.0), drift(0.0), tick(0) {}
33};
34
35/// Spatial energy gradient (∇E field)
36struct EnergyGradient {
37 double dx; // ∂E/∂x
38 double dy; // ∂E/∂y
39 double dz; // ∂E/∂z
40 double magnitude; // |∇E|
41
42 constexpr EnergyGradient() noexcept
43 : dx(0.0), dy(0.0), dz(0.0), magnitude(0.0) {}
44};
45
46/// Complete energy state for an agent
47struct EnergyState {
48 EnergyField field;
49 EnergyGradient gradient;
50 double kinetic; // Kinetic energy (velocity-dependent)
51 double potential; // Potential energy (position-dependent)
52 double total; // Total energy E_total = kinetic + potential + field.energy
53};
54
55// ==============================================================================
56// Driver Function f_a5 Result
57// ==============================================================================
58
59/// Result of applying driver function f_a5(embedding, field) → embedding'
60struct DriverResult {
61 std::vector<double> embedding; // Updated embedding vector
62 double delta_energy; // Energy change ΔE
63 double fitness_delta; // Fitness change Δf
64 bool bounded; // True if all bounds respected
65 uint64_t operations; // Number of operations applied
66};
67
68// ==============================================================================
69// FGTP Engine Configuration
70// ==============================================================================
71
72struct FGTPConfig {
73 // Energy computation parameters
74 double phi_weight = 1.0; // Weight for Φ term
75 double entropy_weight = 1.0; // Weight for entropy term
76 double tensor_weight = 1.0; // Weight for tensor term
77
78 // Driver function parameters
79 double learning_rate = 0.01; // Base learning rate for f_a5
80 double momentum = 0.9; // Momentum coefficient
81 double gradient_clip = 1.0; // Gradient clipping threshold
82
83 // Safety bounds
84 double max_delta_energy = 10.0; // Maximum energy change per tick
85 double max_embedding_norm = 100.0; // Maximum embedding L2 norm
86 double min_phi = 0.0; // Minimum phi threshold
87 double max_phi = 1.0; // Maximum phi threshold
88
89 // Optimization
90 bool use_momentum = true; // Enable momentum-based updates
91 bool use_gradient_clipping = true; // Enable gradient clipping
92 bool validate_bounds = true; // Enable safety bound checking
93};
94
95// ==============================================================================
96// FGTP Engine
97// ==============================================================================
98
99class FGTPEngine {
100public:
101 explicit FGTPEngine(FGTPConfig config = {}) noexcept;
102 ~FGTPEngine() = default;
103
104 // Disable copy, enable move
105 FGTPEngine(const FGTPEngine&) = delete;
106 FGTPEngine& operator=(const FGTPEngine&) = delete;
107 FGTPEngine(FGTPEngine&&) noexcept = default;
108 FGTPEngine& operator=(FGTPEngine&&) noexcept = default;
109
110 // ==============================================================================
111 // Energy Field Computation
112 // ==============================================================================
113
114 /// Compute energy field from helix state
115 EnergyField compute_energy_field(const HelixState& helix) const noexcept;
116
117 /// Compute energy field directly from embedding
118 EnergyField compute_energy_field(const double* embedding, size_t size, uint64_t tick) noexcept;
119
120 /// Compute spatial energy gradient (finite differences)
121 EnergyGradient compute_gradient(
122 const double* embedding,
123 size_t size,
124 double epsilon = 1e-5
125 ) noexcept;
126
127 /// Compute complete energy state (field + gradient + kinetic + potential)
128 EnergyState compute_energy_state(
129 const double* embedding,
130 size_t size,
131 const double* velocity,
132 uint64_t tick
133 ) noexcept;
134
135 // ==============================================================================
136 // Driver Function f_a5
137 // ==============================================================================
138
139 /// Apply driver function f_a5: embedding → embedding'
140 /// This is the core update rule that evolves agent state
141 DriverResult apply_driver(
142 const double* embedding,
143 size_t size,
144 const EnergyField& field,
145 const double* external_force = nullptr
146 ) noexcept;
147
148 /// Batch driver application for multiple agents (parallel)
149 std::vector<DriverResult> apply_driver_batch(
150 const std::vector<std::vector<double>>& embeddings,
151 const std::vector<EnergyField>& fields
152 ) noexcept;
153
154 // ==============================================================================
155 // Advanced Energy Computations
156 // ==============================================================================
157
158 /// Compute Hamiltonian H = T + V + E_field
159 double compute_hamiltonian(const EnergyState& state) const noexcept;
160
161 /// Compute Lagrangian L = T - V
162 double compute_lagrangian(double kinetic, double potential) const noexcept;
163
164 /// Compute action integral S = ∫ L dt (over trajectory)
165 double compute_action(
166 const std::vector<EnergyState>& trajectory,
167 double dt = 1.0
168 ) const noexcept;
169
170 /// Principle of least action: minimize S
171 std::vector<double> optimize_trajectory(
172 const std::vector<double>& start,
173 const std::vector<double>& end,
174 size_t steps = 100
175 ) noexcept;
176
177 // ==============================================================================
178 // Statistics and Monitoring
179 // ==============================================================================
180
181 struct Statistics {
182 uint64_t total_computations;
183 uint64_t total_driver_applications;
184 uint64_t total_violations; // Safety bound violations
185 double average_energy;
186 double average_phi;
187 double average_delta_energy;
188 double max_energy_seen;
189 double min_energy_seen;
190 };
191
192 Statistics get_statistics() const noexcept;
193 void reset_statistics() noexcept;
194
195 // ==============================================================================
196 // Configuration
197 // ==============================================================================
198
199 const FGTPConfig& config() const noexcept { return config_; }
200 void update_config(const FGTPConfig& config) noexcept { config_ = config; }
201
202private:
203 FGTPConfig config_;
204
205 // Internal helix engine for phi calculations
206 HelixEngine helix_engine_;
207
208 // Momentum buffers (persistent across calls)
209 mutable std::vector<std::vector<double>> momentum_buffers_;
210
211 // Statistics (atomic for thread safety)
212 mutable std::atomic<uint64_t> stat_total_computations_{0};
213 mutable std::atomic<uint64_t> stat_total_driver_apps_{0};
214 mutable std::atomic<uint64_t> stat_total_violations_{0};
215 mutable std::atomic<double> stat_sum_energy_{0.0};
216 mutable std::atomic<double> stat_sum_phi_{0.0};
217 mutable std::atomic<double> stat_sum_delta_energy_{0.0};
218 mutable std::atomic<double> stat_max_energy_{0.0};
219 mutable std::atomic<double> stat_min_energy_{1e9};
220
221 // Helper functions
222 bool validate_bounds(const std::vector<double>& embedding) const noexcept;
223 void clip_gradient(double* gradient, size_t size) const noexcept;
224 double compute_kinetic_energy(const double* velocity, size_t size) const noexcept;
225 double compute_potential_energy(const double* embedding, size_t size) const noexcept;
226 void update_statistics(const EnergyField& field, double delta_energy) const noexcept;
227};
228
229// ==============================================================================
230// Utility Functions
231// ==============================================================================
232
233/// Compute L2 norm of vector
234inline double vector_norm(const double* vec, size_t size) noexcept {
235 double sum = 0.0;
236 for (size_t i = 0; i < size; ++i) {
237 sum += vec[i] * vec[i];
238 }
239 return std::sqrt(sum);
240}
241
242/// Compute dot product
243inline double vector_dot(const double* a, const double* b, size_t size) noexcept {
244 double sum = 0.0;
245 for (size_t i = 0; i < size; ++i) {
246 sum += a[i] * b[i];
247 }
248 return sum;
249}
250
251/// Vector addition: out = a + b
252inline void vector_add(double* out, const double* a, const double* b, size_t size) noexcept {
253 for (size_t i = 0; i < size; ++i) {
254 out[i] = a[i] + b[i];
255 }
256}
257
258/// Vector scalar multiply: out = a * scalar
259inline void vector_scale(double* out, const double* a, double scalar, size_t size) noexcept {
260 for (size_t i = 0; i < size; ++i) {
261 out[i] = a[i] * scalar;
262 }
263}
264
265/// Vector multiply-add: out = a + b * scalar
266inline void vector_madd(double* out, const double* a, const double* b, double scalar, size_t size) noexcept {
267 for (size_t i = 0; i < size; ++i) {
268 out[i] = a[i] + b[i] * scalar;
269 }
270}
271
272} // namespace tired_agi
273