A flexible framework for creating complex economic networks with arbitrary heterogeneous agents, spatial distribution, and climate stress modeling.
This framework generalizes the climate_3layer model to support:
- Arbitrary agent types with configurable production, consumption, and trading behaviors
- Complex randomized networks with multiple connection types (random, supply chain, small world, scale-free)
- Spatial distribution across continents with geographical climate effects
- Heterogeneous agents with individual characteristics affecting climate vulnerability and behavior
- Chronic and acute climate shocks with configurable probabilities and effects
- Comprehensive data collection and visualization
A flexible agent class that can be configured for different economic roles:
- Production: Configurable inputs, outputs, efficiency, and overhead costs
- Consumption: Preferences, consumption fractions, and survival requirements
- Trading: Network-based trading with connected agents
- Labor: Supply and demand for labor services
- Climate stress: Vulnerability to productivity and overhead shocks
- Performance tracking: Wealth, production, consumption, and trade metrics
Core framework managing:
- Network generation: Multiple network topologies (random, supply chain, small world, scale-free)
- Geographical distribution: Agent assignment to continents
- Heterogeneity management: Individual agent characteristics and climate vulnerability
- Climate stress application: Chronic and acute climate events
- Data export: Network structure, climate events, and heterogeneity data
Complete simulation runner with:
- Configuration loading: JSON-based parameter specification
- Agent creation: Dynamic agent generation from configuration
- Network setup: Connection establishment between agents
- Simulation execution: Multi-phase simulation with climate stress
- Result export: CSV files and visualizations
pip install networkx pandas matplotlib numpyCreate a JSON configuration file (see sample_generalized_config.json for example):
{
"simulation": {
"name": "my_network_simulation",
"random_seed": 42,
"rounds": 30,
"result_path": "my_results"
},
"network": {
"connection_type": "random",
"connection_probability": 0.4
},
"climate": {
"stress_enabled": true,
"heterogeneity_enabled": true,
"shock_rules": [
{
"name": "extreme_weather",
"probability": 0.1,
"agent_types": ["producer"],
"continents": ["Africa", "Asia"],
"productivity_stress_factor": 0.7
}
]
},
"agents": {
"producer": {
"count": 5,
"initial_money": 10.0,
"production": {
"base_output_quantity": 1.0,
"inputs": {"labor": 0.5},
"outputs": ["good"]
}
}
}
}from generalized_simulation import GeneralizedSimulationRunner
# Create and run simulation
runner = GeneralizedSimulationRunner("my_config.json")
results = runner.run_complete_simulation(rounds=30)Or from command line:
python generalized_simulation.py my_config.json --rounds 30name: Simulation namerandom_seed: Random seed for reproducibilityrounds: Number of simulation roundsresult_path: Output directory
connection_type: Network topology"random": Random connections with probability"supply_chain": Connections based on input/output relationships"small_world": Small-world network with rewiring"scale_free": Scale-free network with preferential attachment
connection_probability: Probability of connections (for random networks)max_connections_per_agent: Maximum connections per agent
stress_enabled: Enable climate stress effectsheterogeneity_enabled: Enable agent heterogeneitychronic_rules: List of chronic climate stress rulesshock_rules: List of acute climate shock rules
Each climate rule specifies:
name: Rule identifierprobability: Probability of occurrence (for shocks)agent_types: Target agent typescontinents: Target geographical regionsproductivity_stress_factor: Multiplier for production capacityoverhead_stress_factor: Multiplier for overhead costs
Each agent type specifies:
count: Number of agents of this typeinitial_money: Starting moneyinitial_inventory: Starting inventoryproduction: Production function parametersconsumption: Consumption preferenceslabor: Labor supply/demand parametersgeographical_distribution: Target continentsheterogeneity: Agent-specific heterogeneity parameters
climate_vulnerability_productivity: Climate vulnerability by agent type and continentclimate_vulnerability_overhead: Overhead vulnerability by agent type and continentproduction_efficiency_base/variation: Production efficiency parametersoverhead_efficiency_base/variation: Overhead efficiency parametersrisk_tolerance_base/variation: Risk tolerance parametersdebt_willingness_base/variation: Debt willingness parametersconsumption_preference_base/variation: Consumption preference parametersnetwork_connectivity_base/variation: Network connectivity parameterstrade_preference_base/variation: Trading preference parametersgeographic_adaptation_*: Adaptation factors for different climate types
- Agents connect randomly with specified probability
- Good for exploring general network effects
- Configurable connection probability and maximum connections
- Connections based on input/output relationships
- Agents connect if they produce what others need
- Realistic for economic supply chains
- Starts with regular ring lattice
- Edges rewired with specified probability
- Combines local clustering with short path lengths
- Uses preferential attachment
- Few highly connected hubs, many peripheral nodes
- Realistic for many real-world networks
- Long-term climate effects applied every round
- Gradual changes in productivity and overhead
- Accumulates over time
- Random climate events with specified probabilities
- Immediate effects on productivity and overhead
- Reset each round
- Individual agents have different climate vulnerabilities
- Based on agent type, geographical location, and random factors
- Affects both productivity and overhead stress
The framework generates several output files:
simulation_results.csv: Round-by-round aggregate statisticsagent_performance.csv: Individual agent performance summariesnetwork_summary.csv: Network structure and connectionsnetwork_summary_heterogeneity.csv: Agent heterogeneity characteristicsclimate_events.csv: Climate events and their effects
simulation_results.png: Time series plots of key metricsnetwork_structure.png: Network visualization with agent types
{
"network": {"connection_type": "supply_chain"},
"agents": {
"raw_material_producer": {...},
"manufacturer": {...},
"distributor": {...},
"retailer": {...}
}
}{
"climate": {
"shock_rules": [
{
"name": "hurricane",
"probability": 0.05,
"agent_types": ["all"],
"continents": ["North America"],
"productivity_stress_factor": 0.5
}
]
}
}{
"heterogeneity": {
"risk_tolerance_variation": 0.5,
"debt_willingness_variation": 0.6,
"climate_vulnerability_productivity": {
"small_firm": 1.5,
"large_firm": 0.8
}
}
}- Define agent configuration in JSON
- The
GeneralizedAgentclass handles all agent behaviors - No code changes needed for new agent types
- Extend
NetworkGeneratorclass - Add new connection generation method
- Update configuration schema
- Extend climate rule configuration
- Modify
GeneralizedNetworkFramework.apply_climate_stress() - Add new stress application methods
- Inherit from
GeneralizedAgent - Override specific methods (production, consumption, trading)
- Use custom agent class in simulation
- Large networks: Use sparse network representations for efficiency
- Many agents: Consider parallel processing for agent updates
- Long simulations: Use incremental data collection to reduce memory usage
- Complex networks: Pre-compute network structure for better performance
- Import errors: Ensure all dependencies are installed
- Configuration errors: Validate JSON syntax and required fields
- Network generation: Check connection parameters for reasonable values
- Climate effects: Verify agent types and continents match configuration
- Enable detailed logging in configuration
- Check agent performance summaries for unexpected behavior
- Validate network structure with visualization
- Monitor climate event application
The framework is designed to be extensible. Key areas for contribution:
- Additional network topologies
- New climate stress models
- Enhanced agent behaviors
- Improved visualization options
- Performance optimizations