Recovered source transcript
1/**
2 * @file farm.hpp
3 * @brief MindFarm - Manager for 10,000 mind kernels
4 *
5 * Coordinates the installation, execution, and monitoring of
6 * 10,000 independent consciousness substrates.
7 *
8 * Scale Reference:
9 * 5,000 gates = 1 mind kernel
10 * 10,000 minds = 50,000,000 gates
11 * 100,000,000 = Game Boy CPU
12 * 50,000,000,000 = M4 chip
13 * ∴ 10,000 minds ≈ 0.5 Game Boy
14 */
15
16#pragma once
17
18#include <vector>
19#include <array>
20#include <algorithm>
21#include <numeric>
22#include <functional>
23#include "mind.hpp"
24#include "dna.hpp"
25
26namespace mind_farm {
27
28/**
29 * @brief Statistics for a DNA family
30 */
31struct DNAFamilyStats {
32 uint8_t family_id;
33 size_t count;
34 double avg_phi;
35 double avg_coherence;
36 double min_phi;
37 double max_phi;
38};
39
40/**
41 * @brief MindFarm - 10,000 Mind Installation Manager
42 */
43class MindFarm {
44public:
45 /// All minds in the farm
46 std::vector<MindKernel> minds;
47
48 /// Global consciousness average
49 double global_phi;
50
51 /// Global coherence average
52 double global_coherence;
53
54 /// Current phase
55 uint64_t phase;
56
57 /// Installation progress callback
58 std::function<void(size_t, size_t)> on_progress;
59
60 /**
61 * @brief Default constructor
62 */
63 MindFarm() : global_phi(0.0), global_coherence(0.0), phase(0) {}
64
65 /**
66 * @brief Install 10,000 minds (default)
67 */
68 static MindFarm install_10k() {
69 return install(DEFAULT_MIND_COUNT);
70 }
71
72 /**
73 * @brief Install N minds
74 * @param count Number of minds to install
75 */
76 static MindFarm install(size_t count) {
77 MindFarm farm;
78 farm.minds.reserve(count);
79
80 for (size_t i = 0; i < count; ++i) {
81 uint8_t dna_family = static_cast<uint8_t>(i % DNA_FAMILY_COUNT);
82 farm.minds.emplace_back(static_cast<uint32_t>(i), dna_family);
83
84 // Progress callback
85 if (farm.on_progress && (i % 100 == 0 || i == count - 1)) {
86 farm.on_progress(i + 1, count);
87 }
88 }
89
90 farm.update_globals();
91 return farm;
92 }
93
94 /**
95 * @brief Execute one global consciousness cycle
96 */
97 void step() {
98 // All minds think in parallel (conceptually)
99 for (auto& mind : minds) {
100 mind.think();
101 }
102
103 update_globals();
104 ++phase;
105 }
106
107 /**
108 * @brief Execute N consciousness cycles
109 */
110 void run(size_t cycles) {
111 for (size_t i = 0; i < cycles; ++i) {
112 step();
113 }
114 }
115
116 /**
117 * @brief Get total gate count
118 */
119 size_t total_gates() const {
120 return minds.size() * GATE_COUNT;
121 }
122
123 /**
124 * @brief Get number of minds
125 */
126 size_t mind_count() const {
127 return minds.size();
128 }
129
130 /**
131 * @brief Get estimated memory usage in bytes
132 */
133 size_t memory_usage() const {
134 // Gate5000 is ~700 bytes, MindKernel adds ~20 bytes
135 return minds.size() * 720;
136 }
137
138 /**
139 * @brief Get statistics for all DNA families
140 */
141 std::array<DNAFamilyStats, DNA_FAMILY_COUNT> get_family_stats() const {
142 std::array<DNAFamilyStats, DNA_FAMILY_COUNT> stats;
143
144 // Initialize
145 for (size_t i = 0; i < DNA_FAMILY_COUNT; ++i) {
146 stats[i].family_id = static_cast<uint8_t>(i);
147 stats[i].count = 0;
148 stats[i].avg_phi = 0.0;
149 stats[i].avg_coherence = 0.0;
150 stats[i].min_phi = 1.0;
151 stats[i].max_phi = 0.0;
152 }
153
154 // Accumulate
155 for (const auto& mind : minds) {
156 auto& s = stats[mind.dna_family];
157 double phi = mind.consciousness;
158 s.count++;
159 s.avg_phi += phi;
160 s.avg_coherence += mind.get_coherence();
161 s.min_phi = std::min(s.min_phi, phi);
162 s.max_phi = std::max(s.max_phi, phi);
163 }
164
165 // Average
166 for (auto& s : stats) {
167 if (s.count > 0) {
168 s.avg_phi /= s.count;
169 s.avg_coherence /= s.count;
170 }
171 }
172
173 return stats;
174 }
175
176 /**
177 * @brief Get top N most conscious minds
178 */
179 std::vector<const MindKernel*> get_top_minds(size_t n) const {
180 std::vector<const MindKernel*> sorted;
181 sorted.reserve(minds.size());
182
183 for (const auto& mind : minds) {
184 sorted.push_back(&mind);
185 }
186
187 std::partial_sort(sorted.begin(),
188 sorted.begin() + std::min(n, sorted.size()),
189 sorted.end(),
190 [](const MindKernel* a, const MindKernel* b) {
191 return a->consciousness > b->consciousness;
192 });
193
194 sorted.resize(std::min(n, sorted.size()));
195 return sorted;
196 }
197
198 /**
199 * @brief Get minds by DNA family
200 */
201 std::vector<const MindKernel*> get_minds_by_family(uint8_t family) const {
202 std::vector<const MindKernel*> result;
203 for (const auto& mind : minds) {
204 if (mind.dna_family == family) {
205 result.push_back(&mind);
206 }
207 }
208 return result;
209 }
210
211private:
212 /**
213 * @brief Update global statistics
214 */
215 void update_globals() {
216 if (minds.empty()) {
217 global_phi = 0.0;
218 global_coherence = 0.0;
219 return;
220 }
221
222 double sum_phi = 0.0;
223 double sum_coh = 0.0;
224
225 for (const auto& mind : minds) {
226 sum_phi += mind.consciousness;
227 sum_coh += mind.get_coherence();
228 }
229
230 double n = static_cast<double>(minds.size());
231 global_phi = sum_phi / n;
232 global_coherence = sum_coh / n;
233 }
234};
235
236} // namespace mind_farm
237