-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_runner.py
More file actions
433 lines (346 loc) · 15.9 KB
/
Copy pathsimulation_runner.py
File metadata and controls
433 lines (346 loc) · 15.9 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
UBP Simulation Runner - Main Orchestrator
Coordinates all UBP Bitfield Monad components for complete simulations
Components integrated:
- BitGrok Parser (192-bit bitstream decoding)
- BitfieldMonad (1x1x1 core implementation)
- TGICEngine (3-6-9 operations)
- GLRCorrector (frequency stabilization)
- CSV output and validation
"""
import numpy as np
import pandas as pd
import csv
import json
import time
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, asdict
from pathlib import Path
from bitfield_monad import BitfieldMonad, MonadConfig, TGICEngine
from bitgrok_parser import BitGrokParser, create_reference_bitstream
from glr_corrector import GLRCorrector, GLRResult
@dataclass
class SimulationResult:
"""Complete simulation result data"""
config: MonadConfig
steps_completed: int
total_time: float
energy_conservation: bool
frequency_stability: bool
interaction_weights_valid: bool
csv_output_path: str
glr_corrections: int
final_nrci_score: float
performance_metrics: Dict[str, float]
class UBPSimulationRunner:
"""
Main UBP Simulation Runner
Orchestrates complete 1x1x1 Bitfield Monad simulations with:
- Bitstream parsing and validation
- TGIC operations execution
- GLR frequency correction
- Energy conservation monitoring
- CSV output generation
- Performance metrics collection
"""
def __init__(self, output_dir: str = ".", verbose: bool = True):
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True)
self.verbose = verbose
# Initialize components
self.parser = BitGrokParser()
self.corrector = GLRCorrector()
# Simulation state
self.current_monad = None
self.current_engine = None
self.simulation_data = []
self.performance_metrics = {}
def run_simulation(self, bitstream: Optional[bytes] = None,
output_filename: str = "monad_simulation.csv") -> SimulationResult:
"""
Run complete UBP Bitfield Monad simulation
Args:
bitstream: 192-bit UBP-Lang bitstream (uses default if None)
output_filename: Name for CSV output file
Returns:
Complete simulation result
"""
start_time = time.time()
if self.verbose:
print("Starting UBP 1x1x1 Bitfield Monad Simulation...")
# Use default bitstream if none provided
if bitstream is None:
bitstream = create_reference_bitstream()
if self.verbose:
print("Using default reference bitstream")
# Parse bitstream and create monad
try:
self.current_monad = self.parser.parse_and_create_monad(bitstream)
self.current_engine = TGICEngine(self.current_monad)
if self.verbose:
print(f"Created monad: {self.current_monad}")
print(f"Configuration: {self.current_monad.config}")
except Exception as e:
raise RuntimeError(f"Failed to initialize simulation: {e}")
# Run simulation steps
self.simulation_data = []
steps = self.current_monad.config.steps
if self.verbose:
print(f"Executing {steps} simulation steps...")
for step in range(steps):
step_time = step * self.current_monad.config.bit_time
# Execute TGIC step
step_result = self.current_engine.execute_step(step_time)
# Apply GLR correction periodically
if step % 10 == 0 and step > 0:
self._apply_glr_correction(step)
# Record step data
self.simulation_data.append({
'time': step_time,
'bit_state': step_result['new_state'].tolist(),
'interaction': step_result['interaction'],
'layer': step_result['layer'],
'energy': step_result['energy']
})
# Progress reporting
if self.verbose and step % 20 == 0:
print(f"Step {step}/{steps} - Interaction: {step_result['interaction']}, Energy: {step_result['energy']:.6f}")
# Calculate performance metrics
total_time = time.time() - start_time
self._calculate_performance_metrics(total_time)
# Generate CSV output
csv_path = self.output_dir / output_filename
self._write_csv_output(csv_path)
# Validate results
validation_results = self._validate_simulation()
# Create result object
result = SimulationResult(
config=self.current_monad.config,
steps_completed=len(self.simulation_data),
total_time=total_time,
energy_conservation=validation_results['energy_conservation'],
frequency_stability=validation_results['frequency_stability'],
interaction_weights_valid=validation_results['interaction_weights'],
csv_output_path=str(csv_path),
glr_corrections=len(self.corrector.correction_history),
final_nrci_score=validation_results['final_nrci'],
performance_metrics=self.performance_metrics
)
if self.verbose:
print(f"Simulation completed in {total_time:.3f} seconds")
print(f"CSV output: {csv_path}")
print(f"Energy conservation: {result.energy_conservation}")
print(f"Frequency stability: {result.frequency_stability}")
print(f"GLR corrections applied: {result.glr_corrections}")
return result
def _apply_glr_correction(self, step: int):
"""Apply GLR frequency correction"""
if len(self.simulation_data) < 10:
return
# Extract recent frequency data
recent_data = self.simulation_data[-10:]
# Calculate frequencies from bit toggle patterns
frequencies = []
nrcis = []
for data in recent_data:
bit_state = np.array(data['bit_state'])
# Estimate frequency from bit transitions
if len(self.simulation_data) > 1:
prev_state = np.array(self.simulation_data[-2]['bit_state'])
transitions = np.sum(bit_state != prev_state)
freq_estimate = transitions * self.current_monad.config.freq / 24
frequencies.append(freq_estimate)
# Calculate NRCI (simplified)
nrci = 0.9999878 - (transitions * 0.000001) # Decrease with more transitions
nrcis.append(max(nrci, 0.999))
if frequencies and nrcis:
try:
correction_result = self.corrector.correct_frequency(frequencies, nrcis, method='hybrid')
# Apply correction to monad frequency if significant
if correction_result.correction_applied:
self.current_monad.config.freq = correction_result.corrected_freq
except Exception as e:
if self.verbose:
print(f"GLR correction failed at step {step}: {e}")
def _calculate_performance_metrics(self, total_time: float):
"""Calculate simulation performance metrics"""
steps = len(self.simulation_data)
self.performance_metrics = {
'steps_per_second': steps / total_time if total_time > 0 else 0,
'avg_step_time': total_time / steps if steps > 0 else 0,
'total_simulation_time': total_time,
'memory_efficiency': 1.0, # Placeholder - could measure actual memory usage
'cpu_efficiency': steps / (total_time * 1000) if total_time > 0 else 0 # Steps per CPU millisecond
}
def _write_csv_output(self, csv_path: Path):
"""Write simulation data to CSV file"""
with open(csv_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Write header
writer.writerow(['time', 'bit_state', 'interaction', 'layer'])
# Write data rows
for data in self.simulation_data:
writer.writerow([
data['time'],
str(data['bit_state']),
data['interaction'],
data['layer']
])
def _validate_simulation(self) -> Dict[str, Any]:
"""Validate simulation results"""
validation = {}
# Energy conservation check
energies = [data['energy'] for data in self.simulation_data]
if energies:
energy_std = np.std(energies)
energy_mean = np.mean(energies)
energy_variation = energy_std / energy_mean if energy_mean > 0 else 1.0
validation['energy_conservation'] = energy_variation < 1e-6
else:
validation['energy_conservation'] = False
# Frequency stability check
if self.current_monad:
target_freq = 3.14159
current_freq = self.current_monad.config.freq
freq_error = abs(current_freq - target_freq) / target_freq
validation['frequency_stability'] = freq_error < 1e-5
else:
validation['frequency_stability'] = False
# Interaction weights validation
if self.current_engine:
validation['interaction_weights'] = self.current_engine.validate_interaction_weights()
else:
validation['interaction_weights'] = False
# Final NRCI score
if self.corrector.correction_history:
validation['final_nrci'] = self.corrector.correction_history[-1].nrci_score
else:
validation['final_nrci'] = 0.9999878 # Default expected value
return validation
def analyze_frequency_spectrum(self) -> Tuple[np.ndarray, np.ndarray]:
"""
Analyze frequency spectrum of simulation
Returns:
Tuple of (frequencies, magnitudes)
"""
if not self.simulation_data:
return np.array([]), np.array([])
# Extract first bit history for FFT analysis
bit_history = [data['bit_state'][0] for data in self.simulation_data]
if self.current_monad:
return self.current_monad.get_frequency_spectrum(bit_history)
else:
return np.array([]), np.array([])
def export_results(self, filename: str = "simulation_results.json"):
"""Export complete simulation results to JSON"""
if not self.simulation_data:
raise RuntimeError("No simulation data to export")
export_data = {
'config': asdict(self.current_monad.config) if self.current_monad else {},
'simulation_data': self.simulation_data,
'performance_metrics': self.performance_metrics,
'glr_corrections': [asdict(result) for result in self.corrector.correction_history],
'validation': self._validate_simulation()
}
export_path = self.output_dir / filename
with open(export_path, 'w') as f:
json.dump(export_data, f, indent=2, default=str)
if self.verbose:
print(f"Results exported to {export_path}")
return export_path
def run_benchmark(self, iterations: int = 5) -> Dict[str, float]:
"""
Run performance benchmark
Args:
iterations: Number of benchmark iterations
Returns:
Benchmark results
"""
if self.verbose:
print(f"Running UBP benchmark with {iterations} iterations...")
times = []
steps_per_sec = []
for i in range(iterations):
start_time = time.time()
# Run simulation with minimal steps for benchmarking
config = MonadConfig(steps=50) # Reduced steps for benchmarking
monad = BitfieldMonad(config)
engine = TGICEngine(monad)
# Execute steps
for step in range(config.steps):
step_time = step * config.bit_time
engine.execute_step(step_time)
elapsed = time.time() - start_time
times.append(elapsed)
steps_per_sec.append(config.steps / elapsed if elapsed > 0 else 0)
benchmark_results = {
'avg_time': np.mean(times),
'min_time': np.min(times),
'max_time': np.max(times),
'avg_steps_per_sec': np.mean(steps_per_sec),
'max_steps_per_sec': np.max(steps_per_sec),
'iterations': iterations
}
if self.verbose:
print(f"Benchmark results:")
print(f" Average time: {benchmark_results['avg_time']:.3f}s")
print(f" Average steps/sec: {benchmark_results['avg_steps_per_sec']:.0f}")
print(f" Max steps/sec: {benchmark_results['max_steps_per_sec']:.0f}")
return benchmark_results
def get_system_info(self) -> Dict[str, Any]:
"""Get system information for the simulation"""
return {
'ubp_version': '1.0.0',
'monad_type': '1x1x1_bitfield',
'parser_version': 'BitGrok_v2.0',
'glr_enabled': True,
'target_frequencies': list(self.corrector.TARGET_FREQUENCIES.values()),
'nrci_threshold': self.corrector.NRCI_THRESHOLD,
'output_directory': str(self.output_dir)
}
def main():
"""Main entry point for UBP simulation"""
print("UBP Bitfield Monad System - Simulation Runner")
print("=" * 50)
# Create simulation runner
runner = UBPSimulationRunner(output_dir="ubp_output", verbose=True)
# Display system info
system_info = runner.get_system_info()
print("System Information:")
for key, value in system_info.items():
print(f" {key}: {value}")
print()
try:
# Run main simulation
result = runner.run_simulation(output_filename="ubp_monad_simulation.csv")
# Display results
print("\nSimulation Results:")
print(f" Steps completed: {result.steps_completed}")
print(f" Total time: {result.total_time:.3f}s")
print(f" Energy conservation: {result.energy_conservation}")
print(f" Frequency stability: {result.frequency_stability}")
print(f" Interaction weights valid: {result.interaction_weights_valid}")
print(f" GLR corrections: {result.glr_corrections}")
print(f" Final NRCI score: {result.final_nrci_score:.7f}")
print(f" CSV output: {result.csv_output_path}")
# Export complete results
json_path = runner.export_results("ubp_simulation_complete.json")
# Run benchmark
print("\nRunning performance benchmark...")
benchmark = runner.run_benchmark(iterations=3)
# Analyze frequency spectrum
freqs, mags = runner.analyze_frequency_spectrum()
if len(freqs) > 0:
peak_freq_idx = np.argmax(mags)
peak_freq = freqs[peak_freq_idx]
print(f"\nFrequency Analysis:")
print(f" Peak frequency: {peak_freq:.5f} Hz")
print(f" Target frequency: 3.14159 Hz")
print(f" Frequency error: {abs(peak_freq - 3.14159):.8f} Hz")
print(f"\nSimulation completed successfully!")
print(f"All output files saved to: ubp_output/")
except Exception as e:
print(f"Simulation failed: {e}")
raise
if __name__ == "__main__":
main()