-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_demos.py
More file actions
318 lines (248 loc) · 10.6 KB
/
run_demos.py
File metadata and controls
318 lines (248 loc) · 10.6 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
"""
Comprehensive Demo Runner for Ising Model Simulations
This script runs all the different simulations and generates comprehensive plots
for the Ising model project.
"""
import os
import sys
import time
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
def setup_output_directories():
"""Create output directories for all plots."""
dirs = [
"plots/ising_demo",
"plots/vectorized_demo",
"plots/exponential_analysis",
"plots/parameter_sweep"
]
for dir_path in dirs:
Path(dir_path).mkdir(parents=True, exist_ok=True)
print(f"✅ Created directory: {dir_path}")
return dirs
def run_ising_demo():
"""Run the main Ising model demo."""
print("\n🔬 Running Main Ising Model Demo...")
try:
from main import IsingNetworkSimulator
# Initialize simulator with smaller network for demo
simulator = IsingNetworkSimulator(n_nodes=100, connection_prob=0.05)
simulator.create_network('erdos_renyi')
# Run simulation
initial_spins, initial_posteriors = simulator.initialize_system()
phi_hist, spin_hist, kld_hist, accur_hist = simulator.run_simulation(
time_steps=500,
initial_spins=initial_spins,
initial_posteriors=initial_posteriors,
po=0.65
)
# Generate all plots
plots = {}
# Network visualization
plots['network'] = simulator.plot_network_visualization(
spin_hist[:, -1],
save_path="plots/ising_demo/network_visualization.png"
)
# Phase space trajectories
plots['phase_trajectories'] = simulator.plot_phase_space_trajectory(
phi_hist,
nodes_to_plot=[0, 1, 2, 3, 4],
save_path="plots/ising_demo/phase_trajectories.png"
)
# Correlation analysis
plots['correlation'] = simulator.plot_correlation_matrix(
spin_hist,
time_window=(100, 500),
save_path="plots/ising_demo/correlation_matrix.png"
)
# Avalanche analysis
plots['avalanche'] = simulator.plot_avalanche_analysis(
spin_hist,
save_path="plots/ising_demo/avalanche_analysis.png"
)
# Energy landscape
plots['energy'] = simulator.plot_energy_landscape(
spin_hist, phi_hist,
save_path="plots/ising_demo/energy_landscape.png"
)
# Synchronization analysis
plots['synchronization'] = simulator.plot_synchronization_analysis(
phi_hist,
save_path="plots/ising_demo/synchronization_analysis.png"
)
# Activity heatmap
simulator.plot_activity_heatmap(spin_hist, "Activity Heatmap")
plt.savefig("plots/ising_demo/activity_heatmap.png", dpi=300, bbox_inches="tight")
plt.close()
plots['activity'] = "plots/ising_demo/activity_heatmap.png"
# Regime comparison
po_values = [0.50001, 0.601, 0.75]
simulator.plot_regime_comparison(po_values)
plt.savefig("plots/ising_demo/regime_comparison.png", dpi=300, bbox_inches="tight")
plt.close()
plots['regime'] = "plots/ising_demo/regime_comparison.png"
print("✅ Main Ising demo completed successfully!")
return plots
except Exception as e:
print(f"❌ Error in main Ising demo: {e}")
return {}
def run_vectorized_demo():
"""Run the vectorized Ising model demo."""
print("\n⚡ Running Vectorized Ising Model Demo...")
try:
from vectorized import VectorizedIsingSimulator
# Initialize simulator
simulator = VectorizedIsingSimulator(n_nodes=256, connection_prob=0.01)
# Run complete demo with Erdős-Rényi network
print("Running Erdős-Rényi network demo...")
er_results = simulator.run_complete_demo(
network_type='erdos_renyi',
time_steps=500,
include_learning=True,
show_plots=False
)
# Run demo with ring of cliques
print("Running ring of cliques demo...")
clique_results = simulator.run_complete_demo(
network_type='ring_of_cliques',
time_steps=500,
include_learning=True,
show_plots=False
)
print("✅ Vectorized demo completed successfully!")
return {'er_results': er_results, 'clique_results': clique_results}
except Exception as e:
print(f"❌ Error in vectorized demo: {e}")
return {}
def run_exponential_analysis():
"""Run the exponential term analysis."""
print("\n📊 Running Exponential Term Analysis...")
try:
from exponential_term.main import ExponentialTermAnalyzer, AnalysisParameters
# Create analyzer with custom parameters
params = AnalysisParameters(
omega_range=(0.0, 1.0),
omega_points=50,
k_range=(0.0, 5.0),
k_points=25
)
analyzer = ExponentialTermAnalyzer(params)
# Generate comprehensive analysis
analyzer.generate_comprehensive_analysis("plots/exponential_analysis")
# Find critical points
critical_points = analyzer.find_critical_points(tolerance=0.01)
if critical_points:
print(f"Found {len(critical_points)} critical points:")
for i, (omega, k) in enumerate(critical_points):
print(f" {i+1}. ω = {omega:.4f}, k = {k:.4f}")
print("✅ Exponential analysis completed successfully!")
return {'critical_points': critical_points}
except Exception as e:
print(f"❌ Error in exponential analysis: {e}")
return {}
def run_parameter_sweep():
"""Run a parameter sweep demo."""
print("\n🔄 Running Parameter Sweep Demo...")
try:
from parameter_sweep import ParameterSweepRunner, SweepParameters
# Create sweep parameters for a small demo
sweep_params = SweepParameters(
network_size=50, # Smaller for demo
num_trials=10, # Fewer trials for demo
time_steps=200, # Shorter for demo
graph_type="er"
)
# Initialize sweep runner
runner = ParameterSweepRunner(sweep_params, "plots/parameter_sweep")
# Generate parameter combinations
param_combinations = runner.generate_parameter_combinations(
po_range=(0.5, 0.9),
n_p_points=5, # Fewer points for demo
n_po_points=10 # Fewer points for demo
)
# Run the sweep
runner.run_sweep(param_combinations)
print("✅ Parameter sweep completed successfully!")
return {'n_configurations': len(param_combinations)}
except Exception as e:
print(f"❌ Error in parameter sweep: {e}")
return {}
def create_summary_report(results):
"""Create a summary report of all results."""
print("\n📋 Creating Summary Report...")
report = []
report.append("# Ising Model Simulation Results Summary")
report.append("")
report.append(f"Generated on: {time.strftime('%Y-%m-%d %H:%M:%S')}")
report.append("")
# Summary of each demo
if 'ising_demo' in results:
report.append("## Main Ising Model Demo")
report.append("- Network visualization with spin states")
report.append("- Phase space trajectories")
report.append("- Correlation matrix analysis")
report.append("- Avalanche dynamics")
report.append("- Energy landscape")
report.append("- Synchronization analysis")
report.append("- Activity heatmaps")
report.append("- Regime comparison")
report.append("")
if 'vectorized_demo' in results:
report.append("## Vectorized Ising Model Demo")
report.append("- Erdős-Rényi network simulation")
report.append("- Ring of cliques network simulation")
report.append("- Learning dynamics with k-matrix evolution")
report.append("")
if 'exponential_analysis' in results:
report.append("## Exponential Term Analysis")
report.append("- Omega dependence plots")
report.append("- Precision parameter analysis")
report.append("- Parameter space heatmaps")
report.append("- 3D surface plots")
if results['exponential_analysis'].get('critical_points'):
report.append(f"- Found {len(results['exponential_analysis']['critical_points'])} critical points")
report.append("")
if 'parameter_sweep' in results:
report.append("## Parameter Sweep Analysis")
report.append(f"- Swept {results['parameter_sweep'].get('n_configurations', 'unknown')} parameter configurations")
report.append("- Variational free energy analysis")
report.append("- Complexity and accuracy decomposition")
report.append("")
report.append("## Output Directories")
report.append("- `plots/ising_demo/` - Main simulation visualizations")
report.append("- `plots/vectorized_demo/` - Vectorized simulation results")
report.append("- `plots/exponential_analysis/` - Exponential term analysis")
report.append("- `plots/parameter_sweep/` - Parameter sweep results")
# Write report
with open("plots/summary_report.md", "w") as f:
f.write("\n".join(report))
print("✅ Summary report created: plots/summary_report.md")
def main():
"""Main function to run all demos."""
print("🚀 Starting Comprehensive Ising Model Demo Suite")
print("=" * 60)
# Setup directories
setup_output_directories()
# Run all demos
results = {}
# Main Ising demo
results['ising_demo'] = run_ising_demo()
# Vectorized demo
results['vectorized_demo'] = run_vectorized_demo()
# Exponential analysis
results['exponential_analysis'] = run_exponential_analysis()
# Parameter sweep
results['parameter_sweep'] = run_parameter_sweep()
# Create summary report
create_summary_report(results)
print("\n" + "=" * 60)
print("🎉 All demos completed successfully!")
print("📁 Check the 'plots/' directory for all generated visualizations")
print("📋 See 'plots/summary_report.md' for a detailed summary")
print("=" * 60)
if __name__ == "__main__":
main()