Percepta

Percepta Architecture Design Documentation

System Architecture Overview

Percepta adopts a World-first 5-layer architecture design to ensure system extensibility and long-term maintainability.

Architecture Layers

Layer 0: Simulation Engine

Responsibilities:

Core Class: SimulationEngine

Key Methods:


Layer 1: World State

Responsibilities:

Core Classes:

Design Principles:


Layer 2: Agent State

Responsibilities:

Core Classes:

Design Principles:


Layer 3: Perception

Responsibilities:

Core Classes: LLMPerceptionService, OllamaService, SimplePerceptionService

Design Principles:


Layer 4: Decision

Responsibilities:

Implementation Location: Agent.decide() method

Design Principles:


Layer 5: Interaction & Transition

Responsibilities:

Core Function: transition()

Design Principles:


Data Flow

1. Initialization
   WorldState + Agents → SimulationEngine

2. Each time step:
   a. Agent Perception (Layer 3)
      WorldState → Perception → Agent internal state
   
   b. Agent Decision (Layer 4)
      Perception + AgentState → Action
   
   c. State Transition (Layer 5)
      WorldState + Agents + Actions → WorldState' + Agents'
   
   d. Save Snapshot
      WorldState' + Agents' → Database

3. Continuous Evolution
   Repeat step 2 until stopped

Extension Mechanisms

Adding New World State Components

  1. Inherit from WorldStateComponent
  2. Implement to_dict(), from_dict(), update()
  3. Add component to WorldState

Example:

class NetworkState(WorldStateComponent):
    def __init__(self):
        self.graph = nx.Graph()
    
    def to_dict(self):
        return {'graph': ...}
    
    def update(self, delta_time, **kwargs):
        # Update network state
        pass

# Usage
world = WorldState()
world.add_component('network', NetworkState())

Adding New Agent Types

  1. Inherit from Agent base class
  2. Implement perceive() and decide() methods
  3. Define agent-specific state and behavior

Example:

class EntrepreneurAgent(Agent):
    def perceive(self, world_state, perception_service):
        # Perception logic
        pass
    
    def decide(self, perception, world_state):
        # Decision logic
        return {'type': 'invest', 'amount': 1000}

Adding New Interactions

Add logic to apply_interactions() in world/transitions/transition.py.

Example:

def apply_interactions(world_state, agents, actions):
    # Existing logic...
    
    # New: Enterprise hiring affects employment
    for agent in agents:
        if agent.state.role == 'entrepreneur':
            # Handle hiring logic
            pass
    
    return world_state, agents

Adding New Metrics

Add calculation methods in metrics/calculator.py.

Example:

@staticmethod
def calculate_gini_coefficient(agents):
    # Calculate Gini coefficient
    pass

Database Design

Table Structure

  1. simulation_runs: Simulation run records
  2. world_state_snapshots: World state snapshots
  3. agent_snapshots: Agent state snapshots
  4. policy_changes: Policy change records
  5. llm_requests: LLM request records

Storage Strategy


Performance Optimization

  1. LLM Perception Optimization:
    • Use LLM for only 10% of agents every 10 steps
    • Other agents use simple perception
  2. Database Optimization:
    • Periodic saves, not every step
    • Use batch inserts
  3. Frontend Updates:
    • 500ms polling interval
    • Chart data limited to 500 points

Security Considerations

  1. API Authentication: Should add authentication in production environment
  2. Input Validation: All API inputs should be validated
  3. Error Handling: Comprehensive error handling and logging

Future Extension Directions

  1. More Agent Types: Enterprises, banks, government, etc.
  2. Network State: Network relationships between agents
  3. Technology Diffusion: Spread of technological innovation
  4. Regional Differences: Multi-region simulation
  5. Distributed Simulation: Support for large-scale parallel processing