C4 Architecture Model
View SourceThis document presents the Macula TWEANN architecture using the C4 model (Context, Container, Component, Code), providing multiple levels of abstraction from high-level system context down to detailed component interactions.
Level 1: System Context
The system context diagram shows how Macula TWEANN fits into the broader environment.
Key Elements
Users:
- Developer - Creates evolutionary neural network applications, constructs agents, and runs evolution experiments
System:
- Macula TWEANN - Erlang/OTP library that evolves neural network topology and weights through natural selection
External Systems:
- Mnesia - Distributed database that stores genotypes (network blueprints) persistently
- Environment - Problem domain that provides fitness feedback (XOR, cart-pole, trading, games, etc.)
Interactions
- Developer constructs agents and runs evolution using the TWEANN API
- TWEANN reads/writes genotypes to Mnesia for persistence
- TWEANN evaluates networks against the environment to compute fitness
- Environment provides feedback that drives selection and evolution
Level 2: Container
The container diagram shows the high-level architecture of Macula TWEANN, broken down into major containers (applications, databases, services).
Containers
Core Data:
- Genotype Store - Mnesia database storing neural network blueprints as Erlang records
- Morphology System - Problem-specific configurations defining sensors, actuators, and fitness functions
Evolution:
- Genome Mutator - Applies topology and weight mutations (add/remove neurons, modify connections)
- Population Monitor - gen_server managing evolutionary lifecycle (selection, survival, reproduction)
Construction:
- Constructor - Builds running process networks (phenotypes) from stored genotypes
Execution:
- Network Runtime - OTP processes implementing cortex, sensors, neurons, and actuators as linked processes
External:
- Environment - External system providing observations and receiving actions
Key Flows
- Developer creates agents via Genotype Store
- Genome Mutator modifies genotypes with random mutations
- Constructor spawns Network Runtime processes from genotypes
- Network Runtime evaluates against Environment
- Population Monitor collects fitness and triggers next generation
Level 3: Component
The component diagram zooms into the Evolution System container, showing the internal modules and their responsibilities.
Component Groups
Core Data Layer:
genotype- CRUD operations for neural network blueprintsmorphology- Problem domain definitionsrecords.hrl- Type definitions (agent, neuron, sensor, actuator)
Construction Layer:
constructor- Phenotype builderexoself- Agent coordinator and evaluation manager
Network Components:
cortex- Network controller (gen_server)sensor- Input reading processneuron- Signal processing processactuator- Output generation process
Evolution Operators:
genome_mutator- Topology mutationscrossover- Sexual reproductionperturbation_utils- Weight mutations
Population Management:
population_monitor- Evolution loop (gen_server)selection_algorithm- Survivor selectionspecies_identifier- Speciation for diversityfitness_postprocessor- Multi-objective optimization
Utilities:
functions- Activation functions (tanh, sigmoid, ReLU, etc.)signal_aggregator- Dot product, signal combinationselection_utils- Roulette wheel, weighted selectiontweann_logger- Structured logging
Component Interactions
constructorreads fromgenotypeand spawns network processes- Network processes (
cortex,sensor,neuron,actuator) communicate via message passing exoselfcoordinates evaluation and reports fitness topopulation_monitorpopulation_monitortriggersgenome_mutatorandselection_algorithmgenome_mutatorupdates genotypes in Mnesia- Utility modules (
functions,signal_aggregator) support neuron computation
Level 4: Code (Evaluation Cycle)
For the code level, we show a detailed sequence diagram of the network evaluation cycle.
Detailed Flow
- Sync Trigger: Cortex sends
{sync, self()}to all sensors - Sense: Sensors read environment and forward inputs to neurons
- Think: Neurons aggregate inputs, apply weights, activate, and forward to next layer
- Act: Actuators collect outputs and interact with environment (scape)
- Fitness: Scape computes fitness and reports back to cortex
- Complete: Cortex reports evaluation complete to exoself
This cycle repeats for multiple episodes until fitness converges or generation limit is reached.
Architecture Principles
Separation of Concerns
Each layer has a clear responsibility:
- Data Layer - Persistence and definitions
- Construction - Genotype-to-phenotype translation
- Network - Runtime execution and computation
- Evolution - Genetic operators and selection
- Population - Multi-agent coordination
Process-Based Concurrency
Neural network components run as separate Erlang processes:
- Natural parallelism (neurons compute independently)
- Fault isolation (crashes propagate via
spawn_link) - Message-passing communication (no shared state)
Evolutionary Flexibility
Multiple extension points:
- New morphologies for different problems
- Pluggable selection algorithms
- Custom activation functions
- Multi-objective fitness functions
Safety and Robustness
Built-in protection mechanisms:
- Timeouts prevent infinite hangs (cortex: 30s, neuron: 10s)
- Crash propagation ensures clean failure
- Logging for debugging and analysis
Technology Stack
- Language: Erlang/OTP 24+
- Database: Mnesia (distributed, ACID)
- Concurrency: OTP processes, gen_server behaviors
- Persistence: Erlang records (not maps)
- Logging: OTP logger with structured output
Next Steps
- Architecture Overview - Detailed layer descriptions
- Quick Start - Hands-on code examples
- TWEANN Basics - Conceptual introduction