Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/AI_Brain / the_tired_agi/core/src/agent_arrays.cpp |
| Date | Not specified in source |
| Repository | Janady13/AI_Brain |
| Revision | d6c309d3fa439714ca46b9d3bb94caf478ea5376 |
| Branch | main |
| Path or archive member | the_tired_agi/core/src/agent_arrays.cpp |
| Original SHA-256 | e6ddb7de177724b182f6e5970b4081376ee77871dd586df9cadf99bff6c69731 |
| Git blob SHA | 70db984104bdde5f7a0a00dabbbc695b823c1f1b |
| Reading-copy SHA-256 | e6ddb7de177724b182f6e5970b4081376ee77871dd586df9cadf99bff6c69731 |
Every recorded occurrence
These 2 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/AI_Brain / the_tired_agi/core/src/agent_arrays.cpp | main | d6c309d3fa439714ca46b9d3bb94caf478ea5376 | Open original |
| Janady13/AI_Brain / the_tired_agi/core/src/agent_arrays.cpp | claude/binary-helix-fibonacci-hive-011CUj7dx1WWJLVYcWCjBbYv | 527d4882c71eb041393f9d8945996177c5ac9e86 | Open original |
Recovered source transcript
Download complete source text ↓364 lines · 10.4 KB
// ==============================================================================
// The-Tired-AGI Core: Agent Arrays Implementation
// 12M-20M Agent Layout + Embedding Ring Buffer
// ==============================================================================
#include "agent_arrays.hpp"
#include <algorithm>
#include <numeric>
#include <execution>
#include <cstring>
namespace tired_agi {
// ==============================================================================
// Constructor / Destructor
// ==============================================================================
AgentArrays::AgentArrays(AgentArrayConfig config) noexcept
: config_(config)
, ring_buffer_(config.ring_buffer_capacity)
, fibonacci_index_(0)
{
// Pre-allocate agent storage to initial capacity
agents_.resize(config_.initial_agents);
// Initialize all agents as inactive
for (auto& agent : agents_) {
agent.active = false;
agent.agent_id = std::numeric_limits<uint64_t>::max();
}
// If using huge pages, try to enable them (Linux only)
if (config_.use_huge_pages) {
// This would require madvise(MADV_HUGEPAGE) on Linux
// For now, just a placeholder
}
}
AgentArrays::~AgentArrays() = default;
// ==============================================================================
// Agent Management
// ==============================================================================
std::optional<uint64_t> AgentArrays::create_agent(
const std::vector<double>& embedding,
uint64_t tick
) noexcept {
// Check if we have capacity
if (count_active() >= capacity()) {
if (!can_grow()) {
return std::nullopt;
}
if (!request_growth_step()) {
return std::nullopt;
}
}
// Allocate agent ID
uint64_t agent_id = allocate_agent_id();
// Find first inactive slot
for (auto& agent : agents_) {
if (!agent.active) {
// Initialize agent
agent.embedding = embedding;
agent.agent_id = agent_id;
agent.birth_tick = tick;
agent.last_update_tick = tick;
agent.fitness = 0.0;
agent.generation = 0;
agent.update_count = 0;
agent.active = true;
agent.frozen = false;
// Initialize helix and energy states (will be computed later)
agent.helix = HelixState{};
agent.energy = EnergyField{};
stat_total_creates_.fetch_add(1, std::memory_order_relaxed);
update_statistics(agent);
return agent_id;
}
}
return std::nullopt;
}
Agent* AgentArrays::get_agent(uint64_t agent_id) noexcept {
if (!is_valid_agent_id(agent_id)) {
return nullptr;
}
for (auto& agent : agents_) {
if (agent.active && agent.agent_id == agent_id) {
return &agent;
}
}
return nullptr;
}
const Agent* AgentArrays::get_agent(uint64_t agent_id) const noexcept {
if (!is_valid_agent_id(agent_id)) {
return nullptr;
}
for (const auto& agent : agents_) {
if (agent.active && agent.agent_id == agent_id) {
return &agent;
}
}
return nullptr;
}
bool AgentArrays::update_agent(uint64_t agent_id, const Agent& updated_state) noexcept {
Agent* agent = get_agent(agent_id);
if (agent == nullptr) {
return false;
}
if (agent->frozen) {
return false; // Cannot update frozen agent
}
// Update state
*agent = updated_state;
agent->update_count++;
stat_total_updates_.fetch_add(1, std::memory_order_relaxed);
update_statistics(*agent);
return true;
}
bool AgentArrays::deactivate_agent(uint64_t agent_id) noexcept {
Agent* agent = get_agent(agent_id);
if (agent == nullptr) {
return false;
}
agent->active = false;
stat_total_deactivations_.fetch_add(1, std::memory_order_relaxed);
return true;
}
bool AgentArrays::freeze_agent(uint64_t agent_id) noexcept {
Agent* agent = get_agent(agent_id);
if (agent == nullptr) {
return false;
}
agent->frozen = true;
return true;
}
bool AgentArrays::unfreeze_agent(uint64_t agent_id) noexcept {
Agent* agent = get_agent(agent_id);
if (agent == nullptr) {
return false;
}
agent->frozen = false;
return true;
}
// ==============================================================================
// Bulk Operations
// ==============================================================================
std::vector<Agent*> AgentArrays::get_active_agents() noexcept {
std::vector<Agent*> active;
active.reserve(count_active());
for (auto& agent : agents_) {
if (agent.active) {
active.push_back(&agent);
}
}
return active;
}
std::vector<const Agent*> AgentArrays::get_active_agents() const noexcept {
std::vector<const Agent*> active;
active.reserve(count_active());
for (const auto& agent : agents_) {
if (agent.active) {
active.push_back(&agent);
}
}
return active;
}
std::vector<Agent*> AgentArrays::get_agents_in_range(uint64_t start_id, uint64_t end_id) noexcept {
std::vector<Agent*> range;
for (auto& agent : agents_) {
if (agent.active && agent.agent_id >= start_id && agent.agent_id < end_id) {
range.push_back(&agent);
}
}
return range;
}
size_t AgentArrays::count_active() const noexcept {
return std::count_if(
agents_.begin(),
agents_.end(),
[](const Agent& a) { return a.active; }
);
}
size_t AgentArrays::count_total() const noexcept {
return agents_.size();
}
// ==============================================================================
// Growth Management (Fibonacci-based)
// ==============================================================================
bool AgentArrays::request_growth_step() noexcept {
if (!can_grow()) {
return false;
}
// Get next Fibonacci number
fibonacci_index_++;
if (fibonacci_index_ >= FibonacciSequence::fibonacci.size()) {
fibonacci_index_ = FibonacciSequence::fibonacci.size() - 1;
return false;
}
uint32_t growth_amount = FibonacciSequence::fibonacci[fibonacci_index_];
// Scale growth_amount to be meaningful (multiply by allocation_chunk_size)
size_t actual_growth = static_cast<size_t>(growth_amount) * config_.allocation_chunk_size;
// Check if growth would exceed max_agents
size_t new_capacity = agents_.size() + actual_growth;
if (new_capacity > config_.max_agents) {
new_capacity = config_.max_agents;
}
// Resize agent array
size_t old_size = agents_.size();
agents_.resize(new_capacity);
// Initialize new agents as inactive
for (size_t i = old_size; i < new_capacity; ++i) {
agents_[i].active = false;
agents_[i].agent_id = std::numeric_limits<uint64_t>::max();
}
return true;
}
bool AgentArrays::can_grow() const noexcept {
if (!config_.allow_growth) {
return false;
}
if (agents_.size() >= config_.max_agents) {
return false;
}
if (fibonacci_index_ >= config_.max_fibonacci_index) {
return false;
}
return true;
}
// ==============================================================================
// Ring Buffer Operations
// ==============================================================================
bool AgentArrays::push_embedding(const std::vector<double>& embedding) noexcept {
return ring_buffer_.push(embedding);
}
bool AgentArrays::pop_embedding(std::vector<double>& embedding) noexcept {
return ring_buffer_.pop(embedding);
}
// ==============================================================================
// Statistics
// ==============================================================================
AgentArrays::Statistics AgentArrays::get_statistics() const noexcept {
Statistics stats;
stats.total_agents = count_total();
stats.active_agents = count_active();
// Count frozen agents
stats.frozen_agents = std::count_if(
agents_.begin(),
agents_.end(),
[](const Agent& a) { return a.active && a.frozen; }
);
stats.total_creates = stat_total_creates_.load(std::memory_order_relaxed);
stats.total_updates = stat_total_updates_.load(std::memory_order_relaxed);
stats.total_deactivations = stat_total_deactivations_.load(std::memory_order_relaxed);
stats.ring_buffer_size = ring_buffer_.size();
stats.ring_buffer_capacity = ring_buffer_.capacity();
stats.fibonacci_index = fibonacci_index_;
// Memory usage estimate
stats.memory_bytes = agents_.size() * sizeof(Agent);
stats.memory_bytes += ring_buffer_.capacity() * config_.embedding_dim * sizeof(double);
// Average statistics
if (stats.active_agents > 0) {
stats.average_fitness = stat_sum_fitness_.load(std::memory_order_relaxed) / stats.active_agents;
stats.average_phi = stat_sum_phi_.load(std::memory_order_relaxed) / stats.active_agents;
stats.average_energy = stat_sum_energy_.load(std::memory_order_relaxed) / stats.active_agents;
} else {
stats.average_fitness = 0.0;
stats.average_phi = 0.0;
stats.average_energy = 0.0;
}
return stats;
}
void AgentArrays::reset_statistics() noexcept {
stat_total_creates_.store(0, std::memory_order_relaxed);
stat_total_updates_.store(0, std::memory_order_relaxed);
stat_total_deactivations_.store(0, std::memory_order_relaxed);
stat_sum_fitness_.store(0.0, std::memory_order_relaxed);
stat_sum_phi_.store(0.0, std::memory_order_relaxed);
stat_sum_energy_.store(0.0, std::memory_order_relaxed);
}
// ==============================================================================
// Helper Functions
// ==============================================================================
uint64_t AgentArrays::allocate_agent_id() noexcept {
return next_agent_id_.fetch_add(1, std::memory_order_relaxed);
}
bool AgentArrays::is_valid_agent_id(uint64_t agent_id) const noexcept {
return agent_id < next_agent_id_.load(std::memory_order_relaxed);
}
void AgentArrays::update_statistics(const Agent& agent) const noexcept {
stat_sum_fitness_.fetch_add(agent.fitness, std::memory_order_relaxed);
stat_sum_phi_.fetch_add(agent.helix.phi, std::memory_order_relaxed);
stat_sum_energy_.fetch_add(agent.energy.energy, std::memory_order_relaxed);
}
} // namespace tired_agi