forked from AB-CE/abce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generalized_framework.py
More file actions
299 lines (255 loc) · 9.61 KB
/
test_generalized_framework.py
File metadata and controls
299 lines (255 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Test script for the Generalized Network Framework
Demonstrates basic usage with a simple heterogeneous agent network.
"""
import json
import os
from generalized_simulation import GeneralizedSimulationRunner
def create_simple_test_config():
"""Create a simple test configuration"""
config = {
"simulation": {
"name": "simple_test",
"random_seed": 42,
"rounds": 10,
"result_path": "test_results"
},
"network": {
"connection_type": "random",
"connection_probability": 0.5,
"max_connections_per_agent": 4
},
"climate": {
"stress_enabled": True,
"heterogeneity_enabled": True,
"chronic_rules": [
{
"name": "gradual_warming",
"agent_types": ["producer", "consumer"],
"continents": ["all"],
"productivity_stress_factor": 0.99,
"overhead_stress_factor": 1.01
}
],
"shock_rules": [
{
"name": "heat_wave",
"probability": 0.2,
"agent_types": ["producer"],
"continents": ["Africa", "Asia"],
"productivity_stress_factor": 0.8,
"overhead_stress_factor": 1.2
}
]
},
"heterogeneity": {
"climate_vulnerability_productivity": {
"producer": 1.2,
"consumer": 0.9,
"Africa": 1.1,
"Asia": 1.0,
"Europe": 0.9
},
"climate_vulnerability_overhead": {
"producer": 1.1,
"consumer": 0.9,
"Africa": 1.2,
"Asia": 1.0,
"Europe": 0.9
},
"production_efficiency_base": 1.0,
"production_efficiency_variation": 0.2,
"risk_tolerance_base": 1.0,
"risk_tolerance_variation": 0.3
},
"agents": {
"producer": {
"count": 3,
"initial_money": 10.0,
"initial_inventory": {
"raw_material": 1.0
},
"production": {
"base_output_quantity": 1.0,
"profit_margin": 0.1,
"base_overhead": 0.05,
"inputs": {
"labor": 0.5,
"raw_material": 0.3
},
"outputs": ["final_good"]
},
"consumption": {
"preference": "final_good",
"consumption_fraction": 0.3,
"minimum_survival_consumption": 0.2
},
"labor": {
"endowment": 0.0,
"wage": 10.0
},
"geographical_distribution": ["Africa", "Asia", "Europe"]
},
"consumer": {
"count": 5,
"initial_money": 8.0,
"initial_inventory": {
"final_good": 0.5
},
"production": {
"base_output_quantity": 0.0,
"profit_margin": 0.0,
"base_overhead": 0.0,
"inputs": {},
"outputs": []
},
"consumption": {
"preference": "final_good",
"consumption_fraction": 0.5,
"minimum_survival_consumption": 0.3
},
"labor": {
"endowment": 1.0,
"wage": 10.0
},
"geographical_distribution": ["all"]
}
},
"visualization": {
"create_visualizations": True,
"create_network_visualization": True,
"save_summary_data": True
}
}
return config
def test_basic_functionality():
"""Test basic framework functionality"""
print("Testing Generalized Network Framework...")
# Create test configuration
config = create_simple_test_config()
# Save configuration to file
config_file = "test_config.json"
with open(config_file, 'w') as f:
json.dump(config, f, indent=2)
print(f"Created test configuration: {config_file}")
try:
# Create and run simulation
runner = GeneralizedSimulationRunner(config_file)
results = runner.run_complete_simulation(rounds=5, create_visualizations=True)
print("\nSimulation completed successfully!")
print(f"Results summary:")
print(f" - Total rounds: {len(results['rounds'])}")
print(f" - Final wealth: {results['total_wealth'][-1]:.2f}")
print(f" - Total production: {results['total_production'][-1]:.2f}")
print(f" - Total trades: {results['total_trades'][-1]}")
print(f" - Climate events: {len(results['climate_events'])}")
# Check if output files were created
output_dir = runner.simulation.path if runner.simulation else "test_results"
expected_files = [
"simulation_results.csv",
"agent_performance.csv",
"network_summary.csv",
"simulation_results.png"
]
print(f"\nChecking output files in {output_dir}:")
for filename in expected_files:
filepath = os.path.join(output_dir, filename)
if os.path.exists(filepath):
print(f" ✓ {filename}")
else:
print(f" ✗ {filename} (missing)")
return True
except Exception as e:
print(f"Error during simulation: {e}")
import traceback
traceback.print_exc()
return False
finally:
# Clean up test files
if os.path.exists(config_file):
os.remove(config_file)
print(f"\nCleaned up test configuration file")
def test_network_types():
"""Test different network types"""
print("\nTesting different network types...")
network_types = ["random", "supply_chain", "small_world", "scale_free"]
for network_type in network_types:
print(f"\nTesting {network_type} network...")
# Create configuration with specific network type
config = create_simple_test_config()
config["network"]["connection_type"] = network_type
# Adjust parameters for different network types
if network_type == "small_world":
config["network"]["small_world_k"] = 2
config["network"]["small_world_p"] = 0.1
elif network_type == "scale_free":
config["network"]["scale_free_m"] = 2
# Save configuration
config_file = f"test_config_{network_type}.json"
with open(config_file, 'w') as f:
json.dump(config, f, indent=2)
try:
# Run simulation
runner = GeneralizedSimulationRunner(config_file)
results = runner.run_complete_simulation(rounds=3, create_visualizations=True)
print(f" ✓ {network_type} network simulation completed")
print(f" Final wealth: {results['total_wealth'][-1]:.2f}")
except Exception as e:
print(f" ✗ {network_type} network failed: {e}")
finally:
# Clean up
if os.path.exists(config_file):
os.remove(config_file)
def test_climate_effects():
"""Test climate stress effects"""
print("\nTesting climate stress effects...")
# Create configuration with strong climate effects
config = create_simple_test_config()
config["climate"]["shock_rules"] = [
{
"name": "severe_shock",
"probability": 0.5, # High probability for testing
"agent_types": ["producer"],
"continents": ["all"],
"productivity_stress_factor": 0.5, # Strong effect
"overhead_stress_factor": 2.0
}
]
config_file = "test_climate_config.json"
with open(config_file, 'w') as f:
json.dump(config, f, indent=2)
try:
runner = GeneralizedSimulationRunner(config_file)
results = runner.run_complete_simulation(rounds=5, create_visualizations=True)
print(" ✓ Climate stress simulation completed")
print(f" Climate events: {len(results['climate_events'])}")
# Check if climate events affected production
if len(results['climate_events']) > 0:
print(" ✓ Climate events were applied")
else:
print(" ⚠ No climate events occurred (random)")
except Exception as e:
print(f" ✗ Climate stress test failed: {e}")
finally:
if os.path.exists(config_file):
os.remove(config_file)
def main():
"""Run all tests"""
print("=" * 60)
print("GENERALIZED NETWORK FRAMEWORK TEST SUITE")
print("=" * 60)
# Test basic functionality
basic_success = test_basic_functionality()
# Test network types
test_network_types()
# Test climate effects
test_climate_effects()
print("\n" + "=" * 60)
if basic_success:
print("✓ All tests completed successfully!")
print("The generalized framework is working correctly.")
else:
print("✗ Some tests failed. Check the error messages above.")
print("=" * 60)
if __name__ == "__main__":
main()