# Recursive Self-Optimizing AGI Framework

A three-layer recursive optimization system implementing the Salience Functional approach to artificial general intelligence.

## Mathematical Foundation

The core AGI equation:

```
AGI = argmax_{π, θ, L} [ S'[ω] + S''[π, θ, L] + S'''[S'', S'] ]
```

Where:
- **S' (Trajectory Layer)**: Optimizes external behavior via salience rewards
- **S'' (Meta-Layer)**: Optimizes the learning process itself
- **S''' (Self-Layer)**: Optimizes the self-improvement dynamics

## Salience Functional Components

| Symbol | Name | Description |
|--------|------|-------------|
| ΔA_t | Novelty | Information gain about world model (surprise) |
| R_t | Retention | Predictive information about future states |
| M_t | Meaning | Instrumental value toward goals |
| C_t | Continuity | Temporal coherence of experience |
| φ_t | Fatigue | Diminishing returns / overfitting signal |

## Architecture

```
┌─────────────────────────────────────────────────────────┐
│                    SELF-LAYER (S''')                    │
│    - Monitors convergence of S''                        │
│    - Prevents divergence via complexity penalties       │
│    - Aligns with goal functions                         │
└────────────────────────┬────────────────────────────────┘
                         │ Modulates
┌────────────────────────▼────────────────────────────────┐
│                    META-LAYER (S'')                     │
│    - Optimizes learning rates, architecture             │
│    - MAML-style meta-learning                           │
│    - Hyperparameter evolution                           │
└────────────────────────┬────────────────────────────────┘
                         │ Modulates
┌────────────────────────▼────────────────────────────────┐
│                 TRAJECTORY LAYER (S')                   │
│    - World model (predictive coding)                    │
│    - Policy optimization                                │
│    - Salience reward computation                        │
└─────────────────────────────────────────────────────────┘
```

## Key Features

1. **Bio-Mimetic Sparse Networks**: Neural topology that prunes weak connections and grows new ones based on salience
2. **Dynamic Architecture Search**: Network grows complexity only when needed
3. **Self-Regulated Learning**: Automatic learning rate adaptation based on optimization volatility
4. **Homeostatic Constraints**: Metabolic budget prevents unbounded growth

## Usage

```bash
# Install dependencies
pip install -r requirements.txt

# Run the full AGI core experiment
python main.py

# Run specific experiments
python -m experiments.growing_brain
python -m experiments.sparse_topology
```

## Project Structure

```
2AP/
├── core/
│   ├── salience.py          # Salience functional mathematics
│   ├── trajectory_layer.py  # S' implementation
│   ├── meta_layer.py        # S'' implementation
│   ├── self_layer.py        # S''' implementation
│   └── world_model.py       # Predictive world model
├── networks/
│   ├── dynamic_net.py       # Self-growing neural network
│   └── sparse_mesh.py       # Bio-mimetic sparse topology
├── environments/
│   └── tasks.py             # Test environments
├── utils/
│   ├── visualization.py     # Plotting utilities
│   └── metrics.py           # Evaluation metrics
├── experiments/
│   ├── growing_brain.py     # Architecture evolution experiment
│   └── sparse_topology.py   # Sparse network experiment
├── main.py                  # Unified AGI core
└── requirements.txt
```

## References

- The Salience Functional Framework for recursive self-improvement
- MuZero / DreamerV3 for world model inspiration
- MAML for meta-learning foundations
- Elastic Weight Consolidation for catastrophic forgetting prevention
