-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrune_protocol.py
More file actions
1059 lines (850 loc) · 42.7 KB
/
rune_protocol.py
File metadata and controls
1059 lines (850 loc) · 42.7 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
UBP Framework v3.1.1 - Rune Protocol
Author: Euan Craig, New Zealand
Date: 18 August 2025
Rune Protocol provides Glyph operations with self-reference capability for the UBP system.
This module implements the advanced symbolic computation and recursive feedback mechanisms
that enable the UBP to achieve higher-order coherence and self-organization.
"""
import numpy as np
from typing import Dict, List, Optional, Tuple, Any, Union, Callable
from dataclasses import dataclass, field
import logging
import time
import json
from enum import Enum
from abc import ABC, abstractmethod
# Import configuration
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config'))
from ubp_config import get_config
class GlyphType(Enum):
"""Types of Glyphs in the Rune Protocol."""
QUANTIFY = "quantify"
CORRELATE = "correlate"
SELF_REFERENCE = "self_reference"
TRANSFORM = "transform"
RESONANCE = "resonance"
COHERENCE = "coherence"
FEEDBACK = "feedback"
EMERGENCE = "emergence"
@dataclass
class GlyphState:
"""Represents the state of a Glyph."""
glyph_id: str
glyph_type: GlyphType
activation_level: float
coherence_pressure: float
self_reference_depth: int
resonance_frequency: float
state_vector: np.ndarray
metadata: Dict = field(default_factory=dict)
@dataclass
class RuneOperationResult:
"""Result from a Rune Protocol operation."""
operation_type: str
input_glyphs: List[str]
output_glyph: Optional[str]
coherence_change: float
self_reference_loops: int
emergence_detected: bool
operation_time: float
nrci_score: float
metadata: Dict = field(default_factory=dict)
@dataclass
class CoherencePressureState:
"""State of coherence pressure in the system."""
current_pressure: float
target_pressure: float
pressure_gradient: float
stability_index: float
mitigation_active: bool
pressure_history: List[float] = field(default_factory=list)
class GlyphOperator(ABC):
"""Abstract base class for Glyph operators."""
@abstractmethod
def operate(self, glyph_state: GlyphState, *args, **kwargs) -> GlyphState:
"""Perform the glyph operation."""
pass
@abstractmethod
def get_operation_type(self) -> str:
"""Get the operation type name."""
pass
class QuantifyOperator(GlyphOperator):
"""
Quantify Glyph Operator: Q(G, state) = Σ G_i(state)
Quantifies the state of a Glyph by summing its components.
"""
def __init__(self):
self.logger = logging.getLogger(__name__)
def operate(self, glyph_state: GlyphState, target_state: Optional[np.ndarray] = None) -> GlyphState:
"""
Quantify operation on a Glyph state.
Args:
glyph_state: Input Glyph state
target_state: Optional target state for quantification
Returns:
Updated GlyphState
"""
if target_state is None:
target_state = glyph_state.state_vector
# Quantification: sum of state components weighted by activation
quantified_value = np.sum(glyph_state.state_vector * glyph_state.activation_level)
# Create new state vector with quantified value
new_state_vector = np.array([quantified_value])
# Update coherence based on quantification quality
# Ensure non-zero denominator for variance calculation
# Use a small epsilon to avoid division by zero when calculating state_coherence
mean_squared = np.mean(glyph_state.state_vector)**2
state_coherence = 1.0 - np.var(glyph_state.state_vector) / (mean_squared + 1e-10)
new_coherence_pressure = glyph_state.coherence_pressure * (1.0 + state_coherence * 0.1)
# Create updated state
updated_state = GlyphState(
glyph_id=glyph_state.glyph_id,
glyph_type=glyph_state.glyph_type,
activation_level=min(1.0, glyph_state.activation_level * 1.1),
coherence_pressure=new_coherence_pressure,
self_reference_depth=glyph_state.self_reference_depth,
resonance_frequency=glyph_state.resonance_frequency,
state_vector=new_state_vector,
metadata={
**glyph_state.metadata,
'quantified_value': quantified_value,
'quantification_time': time.time()
}
)
return updated_state
def get_operation_type(self) -> str:
return "quantify"
class CorrelateOperator(GlyphOperator):
"""
Correlate Glyph Operator: C(G, R_i, R_j) = P(R_i) * P(R_j) / P(R_i ∩ R_j)
Correlates Glyph states across different realms.
"""
def __init__(self):
self.logger = logging.getLogger(__name__)
def operate(self, glyph_state: GlyphState, other_glyph: GlyphState,
realm_i: str = "quantum", realm_j: str = "electromagnetic") -> GlyphState:
"""
Correlate operation between two Glyph states.
Args:
glyph_state: First Glyph state
other_glyph: Second Glyph state
realm_i: First realm
realm_j: Second realm
Returns:
Updated GlyphState with correlation information
"""
# Calculate state probabilities
state1_norm = np.linalg.norm(glyph_state.state_vector)
state2_norm = np.linalg.norm(other_glyph.state_vector)
if state1_norm == 0 or state2_norm == 0:
correlation = 0.0
else:
# Normalize states
norm_state1 = glyph_state.state_vector / state1_norm
norm_state2 = other_glyph.state_vector / state2_norm
# Ensure same length for correlation
min_len = min(len(norm_state1), len(norm_state2))
if min_len == 0:
correlation = 0.0
else:
s1 = norm_state1[:min_len]
s2 = norm_state2[:min_len]
# Calculate correlation coefficient
# np.corrcoef returns a 2x2 matrix, we need the off-diagonal elements
if len(s1) > 1 and len(s2) > 1: # corrcoef requires at least 2 observations
correlation = np.corrcoef(s1, s2)[0, 1]
if np.isnan(correlation):
correlation = 0.0
else: # Cannot calculate meaningful correlation for 0 or 1 element vectors
correlation = 0.0
# Calculate realm intersection probability (simplified)
realm_intersection = self._calculate_realm_intersection(realm_i, realm_j)
# Correlation formula: P(R_i) * P(R_j) / P(R_i ∩ R_j)
p_ri = glyph_state.activation_level
p_rj = other_glyph.activation_level
p_intersection = realm_intersection
if p_intersection > 1e-10: # Avoid division by zero
correlation_value = (p_ri * p_rj) / p_intersection
else:
correlation_value = 0.0
# Create correlated state vector
correlated_state = np.array([correlation, correlation_value])
# Update coherence pressure based on correlation strength
coherence_boost = abs(correlation) * 0.2
new_coherence_pressure = glyph_state.coherence_pressure * (1.0 + coherence_boost)
# Create updated state
updated_state = GlyphState(
glyph_id=glyph_state.glyph_id,
glyph_type=glyph_state.glyph_type,
activation_level=min(1.0, glyph_state.activation_level + abs(correlation) * 0.1),
coherence_pressure=new_coherence_pressure,
self_reference_depth=glyph_state.self_reference_depth,
resonance_frequency=glyph_state.resonance_frequency,
state_vector=correlated_state,
metadata={
**glyph_state.metadata,
'correlation_coefficient': correlation,
'correlation_value': correlation_value,
'correlated_with': other_glyph.glyph_id,
'realm_i': realm_i,
'realm_j': realm_j,
'correlation_time': time.time()
}
)
return updated_state
def _calculate_realm_intersection(self, realm_i: str, realm_j: str) -> float:
"""Calculate intersection probability between two realms."""
# Simplified realm intersection calculation
# In practice, this would involve complex physics
realm_overlaps = {
('quantum', 'electromagnetic'): 0.8,
('electromagnetic', 'optical'): 0.9,
('quantum', 'nuclear'): 0.7,
('gravitational', 'cosmological'): 0.6,
('biological', 'quantum'): 0.5,
('nuclear', 'optical'): 0.3,
}
# Check both directions
overlap = realm_overlaps.get((realm_i, realm_j),
realm_overlaps.get((realm_j, realm_i), 0.1))
return overlap
def get_operation_type(self) -> str:
return "correlate"
class SelfReferenceOperator(GlyphOperator):
"""
Self-Reference Glyph Operator with recursive feedback.
Implements recursive feedback mechanisms with coherence pressure mitigation.
"""
def __init__(self, max_depth: int = 10):
self.logger = logging.getLogger(__name__)
self.max_depth = max_depth
self.coherence_pressure_threshold = 0.8
def operate(self, glyph_state: GlyphState, feedback_strength: float = 0.1) -> GlyphState:
"""
Self-reference operation with recursive feedback.
Args:
glyph_state: Input Glyph state
feedback_strength: Strength of recursive feedback
Returns:
Updated GlyphState with self-reference applied
"""
# Check if we've reached maximum recursion depth
if glyph_state.self_reference_depth >= self.max_depth:
self.logger.warning(f"Maximum self-reference depth reached for {glyph_state.glyph_id}")
return glyph_state
# Check coherence pressure
if glyph_state.coherence_pressure > self.coherence_pressure_threshold:
# Apply coherence pressure mitigation
mitigated_state = self._apply_coherence_pressure_mitigation(glyph_state)
return mitigated_state
# Apply self-reference transformation
self_ref_state = self._apply_self_reference_transform(glyph_state, feedback_strength)
return self_ref_state
def _apply_self_reference_transform(self, glyph_state: GlyphState,
feedback_strength: float) -> GlyphState:
"""Apply self-reference transformation to Glyph state."""
# Self-reference: state becomes a function of itself
current_state = glyph_state.state_vector
# Recursive feedback: new_state = f(current_state, previous_states)
if len(current_state) > 0:
# Simple self-reference: weighted sum of current state with itself
# Convolve requires at least one element for 'same' mode,
# and performs a proper convolution if length > 1
if len(current_state) > 0:
self_feedback = np.convolve(current_state, current_state[::-1], mode='same')
else: # Single element or empty array, self_feedback is just current_state
self_feedback = current_state
# Normalize to prevent explosion
if np.linalg.norm(self_feedback) > 0:
self_feedback = self_feedback / np.linalg.norm(self_feedback)
# Combine with original state
new_state = (1.0 - feedback_strength) * current_state + feedback_strength * self_feedback
else:
new_state = current_state
# Update coherence pressure (self-reference increases pressure)
pressure_increase = feedback_strength * 0.1
new_coherence_pressure = glyph_state.coherence_pressure + pressure_increase
# Increase self-reference depth
new_depth = glyph_state.self_reference_depth + 1
# Update resonance frequency based on self-reference
frequency_modulation = 1.0 + feedback_strength * np.sin(2 * np.pi * new_depth / 10.0)
new_frequency = glyph_state.resonance_frequency * frequency_modulation
# Create updated state
updated_state = GlyphState(
glyph_id=glyph_state.glyph_id,
glyph_type=glyph_state.glyph_type,
activation_level=min(1.0, glyph_state.activation_level * (1.0 + feedback_strength * 0.05)),
coherence_pressure=new_coherence_pressure,
self_reference_depth=new_depth,
resonance_frequency=new_frequency,
state_vector=new_state,
metadata={
**glyph_state.metadata,
'self_reference_applied': True,
'feedback_strength': feedback_strength,
'self_reference_time': time.time()
}
)
return updated_state
def _apply_coherence_pressure_mitigation(self, glyph_state: GlyphState) -> GlyphState:
"""Apply coherence pressure mitigation to prevent system instability."""
self.logger.info(f"Applying coherence pressure mitigation to {glyph_state.glyph_id}")
# Reduce coherence pressure through state normalization
current_state = glyph_state.state_vector
if len(current_state) > 0 and np.linalg.norm(current_state) > 0:
# Normalize state to unit length
normalized_state = current_state / np.linalg.norm(current_state)
# Apply smoothing to reduce high-frequency components
if len(normalized_state) > 2: # Convolve requires kernel size <= signal size
smoothed_state = np.convolve(normalized_state, [0.25, 0.5, 0.25], mode='same')
else:
smoothed_state = normalized_state
else:
smoothed_state = current_state
# Reduce coherence pressure
mitigated_pressure = glyph_state.coherence_pressure * 0.7
# Reset self-reference depth if pressure was too high
reset_depth = max(0, glyph_state.self_reference_depth - 2)
# Create mitigated state
mitigated_state = GlyphState(
glyph_id=glyph_state.glyph_id,
glyph_type=glyph_state.glyph_type,
activation_level=glyph_state.activation_level * 0.9,
coherence_pressure=mitigated_pressure,
self_reference_depth=reset_depth,
resonance_frequency=glyph_state.resonance_frequency,
state_vector=smoothed_state,
metadata={
**glyph_state.metadata,
'coherence_pressure_mitigated': True,
'mitigation_time': time.time(),
'original_pressure': glyph_state.coherence_pressure
}
)
return mitigated_state
def get_operation_type(self) -> str:
return "self_reference"
class EmergenceDetector:
"""
Detects emergent properties in Glyph interactions.
Monitors for spontaneous organization and higher-order patterns
that emerge from Glyph operations.
"""
def __init__(self):
self.logger = logging.getLogger(__name__)
self.config = get_config()
# Emergence detection parameters
self.complexity_threshold = 0.7
self.coherence_threshold = 0.9
self.pattern_memory = []
self.max_memory_size = 100
def detect_emergence(self, glyph_states: List[GlyphState]) -> Dict:
"""
Detect emergent properties in a collection of Glyph states.
Args:
glyph_states: List of Glyph states to analyze
Returns:
Dictionary with emergence analysis results
"""
if not glyph_states:
return self._empty_emergence_result()
# Analyze complexity
complexity_score = self._calculate_system_complexity(glyph_states)
# Analyze coherence
coherence_score = self._calculate_system_coherence(glyph_states)
# Detect patterns
patterns = self._detect_patterns(glyph_states)
# Check for emergence criteria
emergence_detected = (
complexity_score > self.complexity_threshold and
coherence_score > self.coherence_threshold and
len(patterns) > 0
)
# Analyze emergence type
emergence_type = self._classify_emergence_type(patterns, complexity_score, coherence_score)
# Calculate emergence strength
emergence_strength = self._calculate_emergence_strength(
complexity_score, coherence_score, patterns
)
# Update pattern memory
self._update_pattern_memory(patterns)
result = {
'emergence_detected': emergence_detected,
'emergence_type': emergence_type,
'emergence_strength': emergence_strength,
'complexity_score': complexity_score,
'coherence_score': coherence_score,
'detected_patterns': patterns,
'glyph_count': len(glyph_states),
'analysis_time': time.time()
}
if emergence_detected:
self.logger.info(f"Emergence detected: Type={emergence_type}, "
f"Strength={emergence_strength:.3f}, "
f"Patterns={len(patterns)}")
return result
def _calculate_system_complexity(self, glyph_states: List[GlyphState]) -> float:
"""Calculate overall system complexity."""
if not glyph_states:
return 0.0
# Complexity based on state diversity and interactions
state_vectors = [g.state_vector for g in glyph_states if len(g.state_vector) > 0]
if not state_vectors: # Handle case where all state_vectors are empty
return 0.0
try:
all_values = np.concatenate(state_vectors)
except ValueError: # Catch error if state_vectors list is empty after filtering
return 0.0
if len(all_values) == 0: # Handle case where concatenation results in empty array
return 0.0
# Discretize values for entropy calculation
hist, _ = np.histogram(all_values, bins=20)
hist = hist + 1e-10 # Avoid log(0)
probabilities = hist / np.sum(hist)
entropy = -np.sum(probabilities * np.log2(probabilities))
# Normalize entropy to [0, 1]
max_entropy = np.log2(len(probabilities)) if len(probabilities) > 1 else 1.0 # Avoid log2(1) = 0
normalized_entropy = entropy / max_entropy if max_entropy > 0 else 0.0
# Factor in interaction complexity
interaction_complexity = self._calculate_interaction_complexity(glyph_states)
# Combined complexity
total_complexity = (normalized_entropy + interaction_complexity) / 2.0
return min(1.0, total_complexity)
def _calculate_system_coherence(self, glyph_states: List[GlyphState]) -> float:
"""Calculate overall system coherence."""
if not glyph_states:
return 0.0
# Coherence based on synchronization of Glyph states
coherence_pressures = [g.coherence_pressure for g in glyph_states]
activation_levels = [g.activation_level for g in glyph_states]
# Coherence from pressure synchronization
pressure_mean_sq = np.mean(coherence_pressures)**2
pressure_coherence = 1.0 - np.var(coherence_pressures) / (pressure_mean_sq + 1e-10)
# Coherence from activation synchronization
activation_mean_sq = np.mean(activation_levels)**2
activation_coherence = 1.0 - np.var(activation_levels) / (activation_mean_sq + 1e-10)
# Combined coherence
total_coherence = (pressure_coherence + activation_coherence) / 2.0
return min(1.0, max(0.0, total_coherence))
def _calculate_interaction_complexity(self, glyph_states: List[GlyphState]) -> float:
"""Calculate complexity of Glyph interactions."""
if len(glyph_states) < 2:
return 0.0
correlations = []
for i, glyph1 in enumerate(glyph_states):
for j, glyph2 in enumerate(glyph_states):
if i < j and len(glyph1.state_vector) > 1 and len(glyph2.state_vector) > 1: # corrcoef requires at least 2 observations
# Calculate correlation between state vectors
min_len = min(len(glyph1.state_vector), len(glyph2.state_vector))
if min_len > 1:
s1 = glyph1.state_vector[:min_len]
s2 = glyph2.state_vector[:min_len]
corr = np.corrcoef(s1, s2)[0, 1]
if not np.isnan(corr):
correlations.append(abs(corr))
if not correlations:
return 0.0
# Interaction complexity based on correlation diversity
# Sum of probabilities * log2(probabilities)
# Probabilities here are normalized correlation strengths
norm_correlations = np.array(correlations) / (np.sum(correlations) + 1e-10)
correlation_entropy = -np.sum(norm_correlations * np.log2(norm_correlations + 1e-10))
# Normalize entropy
max_entropy = np.log2(len(correlations)) if len(correlations) > 1 else 1.0
normalized_complexity = correlation_entropy / max_entropy if max_entropy > 0 else 0.0
return min(1.0, normalized_complexity)
def _detect_patterns(self, glyph_states: List[GlyphState]) -> List[Dict]:
"""Detect patterns in Glyph state collection."""
patterns = []
if len(glyph_states) < 2:
return patterns
# Pattern 1: Synchronization patterns
sync_pattern = self._detect_synchronization_pattern(glyph_states)
if sync_pattern:
patterns.append(sync_pattern)
# Pattern 2: Resonance patterns
resonance_pattern = self._detect_resonance_pattern(glyph_states)
if resonance_pattern:
patterns.append(resonance_pattern)
# Pattern 3: Hierarchical patterns
hierarchy_pattern = self._detect_hierarchy_pattern(glyph_states)
if hierarchy_pattern:
patterns.append(hierarchy_pattern)
return patterns
def _detect_synchronization_pattern(self, glyph_states: List[GlyphState]) -> Optional[Dict]:
"""Detect synchronization patterns in Glyph states."""
activation_levels = [g.activation_level for g in glyph_states]
if not activation_levels:
return None
# Check for synchronization (low variance in activation levels)
activation_var = np.var(activation_levels)
activation_mean = np.mean(activation_levels)
if activation_mean > 1e-10: # Avoid division by zero
if activation_var / (activation_mean**2) < 0.1:
return {
'type': 'synchronization',
'strength': 1.0 - activation_var / (activation_mean**2),
'participants': [g.glyph_id for g in glyph_states],
'sync_level': activation_mean
}
return None
def _detect_resonance_pattern(self, glyph_states: List[GlyphState]) -> Optional[Dict]:
"""Detect resonance patterns in Glyph frequencies."""
frequencies = [g.resonance_frequency for g in glyph_states]
# Look for harmonic relationships
for i, freq1 in enumerate(frequencies):
for j, freq2 in enumerate(frequencies):
if i < j and freq1 > 1e-10 and freq2 > 1e-10: # Avoid division by zero
ratio = freq2 / freq1
# Check if ratio is close to a simple harmonic (2, 3, 1.5, etc.)
simple_ratios = [0.5, 1.5, 2.0, 3.0, 4.0]
for simple_ratio in simple_ratios:
if abs(ratio - simple_ratio) < 0.1:
return {
'type': 'resonance',
'strength': 1.0 - abs(ratio - simple_ratio) / 0.1,
'participants': [glyph_states[i].glyph_id, glyph_states[j].glyph_id],
'frequency_ratio': ratio,
'harmonic_ratio': simple_ratio
}
return None
def _detect_hierarchy_pattern(self, glyph_states: List[GlyphState]) -> Optional[Dict]:
"""Detect hierarchical patterns in Glyph organization."""
# Sort by self-reference depth
sorted_glyphs = sorted(glyph_states, key=lambda g: g.self_reference_depth)
# Check for clear hierarchy (different depth levels)
depths = [g.self_reference_depth for g in sorted_glyphs]
unique_depths = len(set(depths))
if unique_depths > 1 and unique_depths < len(glyph_states):
return {
'type': 'hierarchy',
'strength': unique_depths / len(glyph_states),
'participants': [g.glyph_id for g in sorted_glyphs],
'depth_levels': unique_depths,
'max_depth': max(depths)
}
return None
def _classify_emergence_type(self, patterns: List[Dict],
complexity: float, coherence: float) -> str:
"""Classify the type of emergence detected."""
if not patterns:
return "none"
pattern_types = [p['type'] for p in patterns]
# Classification based on dominant patterns and metrics
if 'hierarchy' in pattern_types and complexity > 0.8:
return "hierarchical_emergence"
elif 'synchronization' in pattern_types and coherence > 0.9:
return "coherent_emergence"
elif 'resonance' in pattern_types:
return "resonant_emergence"
elif len(patterns) > 1:
return "complex_emergence"
else:
return "simple_emergence"
def _calculate_emergence_strength(self, complexity: float,
coherence: float, patterns: List[Dict]) -> float:
"""Calculate overall emergence strength."""
if not patterns:
return 0.0
# Base strength from complexity and coherence
base_strength = (complexity + coherence) / 2.0
# Pattern contribution
# Use a safe mean calculation for pattern strengths
pattern_strengths = [p.get('strength', 0.5) for p in patterns]
pattern_strength = np.mean(pattern_strengths) if pattern_strengths else 0.0
# Number of patterns bonus
pattern_bonus = min(0.2, len(patterns) * 0.05)
# Combined strength
total_strength = base_strength * pattern_strength + pattern_bonus
return min(1.0, total_strength)
def _update_pattern_memory(self, patterns: List[Dict]):
"""Update pattern memory for learning."""
for pattern in patterns:
self.pattern_memory.append({
'pattern': pattern,
'timestamp': time.time()
})
# Limit memory size
if len(self.pattern_memory) > self.max_memory_size:
self.pattern_memory = self.pattern_memory[-self.max_memory_size:]
def _empty_emergence_result(self) -> Dict:
"""Return empty emergence detection result."""
return {
'emergence_detected': False,
'emergence_type': 'none',
'emergence_strength': 0.0,
'complexity_score': 0.0,
'coherence_score': 0.0,
'detected_patterns': [],
'glyph_count': 0,
'analysis_time': time.time()
}
class RuneProtocol:
"""
Main Rune Protocol engine for UBP Framework v3.0.
Provides Glyph operations with self-reference capability and emergence detection.
Implements the symbolic computation layer that enables higher-order UBP operations.
"""
def __init__(self):
self.logger = logging.getLogger(__name__)
self.config = get_config()
# Initialize operators
self.operators = {
'quantify': QuantifyOperator(),
'correlate': CorrelateOperator(),
'self_reference': SelfReferenceOperator()
}
# Initialize emergence detector
self.emergence_detector = EmergenceDetector()
# Glyph registry
self.glyphs = {}
self.operation_history = []
# Coherence pressure monitoring
self.coherence_pressure_state = CoherencePressureState(
current_pressure=0.0,
target_pressure=0.8,
pressure_gradient=0.0,
stability_index=1.0,
mitigation_active=False
)
def create_glyph(self, glyph_id: str, glyph_type: GlyphType,
initial_state: Optional[np.ndarray] = None) -> GlyphState:
"""
Create a new Glyph with specified type and initial state.
Args:
glyph_id: Unique identifier for the Glyph
glyph_type: Type of Glyph to create
initial_state: Initial state vector (random if None)
Returns:
Created GlyphState
"""
if initial_state is None or initial_state.size == 0:
initial_state = np.random.random(10) * 0.1 # Small random initial state
# Create Glyph state
glyph_state = GlyphState(
glyph_id=glyph_id,
glyph_type=glyph_type,
activation_level=0.1,
coherence_pressure=0.0,
self_reference_depth=0,
resonance_frequency=1e12, # Default 1 THz
state_vector=initial_state,
metadata={
'creation_time': time.time(),
'creator': 'RuneProtocol'
}
)
# Register Glyph
self.glyphs[glyph_id] = glyph_state
self.logger.info(f"Created Glyph: {glyph_id} (type: {glyph_type.value})")
return glyph_state
def execute_operation(self, operation_type: str, glyph_id: str,
**kwargs) -> RuneOperationResult:
"""
Execute a Rune Protocol operation on a Glyph.
Args:
operation_type: Type of operation to execute
glyph_id: Target Glyph ID
**kwargs: Additional operation parameters
Returns:
RuneOperationResult with operation results
"""
if glyph_id not in self.glyphs:
raise ValueError(f"Glyph {glyph_id} not found")
if operation_type not in self.operators:
raise ValueError(f"Unknown operation type: {operation_type}")
start_time = time.time()
glyph_state = self.glyphs[glyph_id]
operator = self.operators[operation_type]
# Record initial state
initial_coherence = glyph_state.coherence_pressure
initial_loops = glyph_state.self_reference_depth
# Execute operation
try:
updated_state = operator.operate(glyph_state, **kwargs)
self.glyphs[glyph_id] = updated_state
# Calculate changes
coherence_change = updated_state.coherence_pressure - initial_coherence
self_reference_loops = updated_state.self_reference_depth - initial_loops
# Check for emergence
emergence_result = self.emergence_detector.detect_emergence([updated_state])
emergence_detected = emergence_result['emergence_detected']
# Calculate NRCI
nrci_score = self._calculate_operation_nrci(glyph_state, updated_state)
# Update coherence pressure state
self._update_coherence_pressure_state(updated_state.coherence_pressure)
operation_time = time.time() - start_time
# Extract specific metadata from the updated_state for the RuneOperationResult
operation_specific_metadata = {}
if operation_type == 'quantify':
# FIX: Copy quantified_value from updated_state.metadata to RuneOperationResult.metadata
operation_specific_metadata['quantified_value'] = updated_state.metadata.get('quantified_value')
elif operation_type == 'correlate': # In case correlate also uses this single-glyph path
operation_specific_metadata['correlation_coefficient'] = updated_state.metadata.get('correlation_coefficient')
elif operation_type == 'self_reference':
operation_specific_metadata['self_reference_applied'] = updated_state.metadata.get('self_reference_applied')
operation_specific_metadata['feedback_strength'] = updated_state.metadata.get('feedback_strength')
# Create result
result = RuneOperationResult(
operation_type=operation_type,
input_glyphs=[glyph_id],
output_glyph=glyph_id,
coherence_change=coherence_change,
self_reference_loops=self_reference_loops,
emergence_detected=emergence_detected,
operation_time=operation_time,
nrci_score=nrci_score,
metadata={
**operation_specific_metadata, # Add specific metadata here
'emergence_result': emergence_result,
'initial_state_norm': np.linalg.norm(glyph_state.state_vector) if glyph_state.state_vector.size > 0 else 0.0,
'final_state_norm': np.linalg.norm(updated_state.state_vector) if updated_state.state_vector.size > 0 else 0.0
}
)
# Record operation
self.operation_history.append(result)
self.logger.info(f"Operation {operation_type} on {glyph_id}: "
f"NRCI={nrci_score:.6f}, "
f"Emergence={emergence_detected}, "
f"Time={operation_time:.3f}s")
return result
except Exception as e:
self.logger.error(f"Operation {operation_type} failed on {glyph_id}: {e}")
raise
def execute_multi_glyph_operation(self, operation_type: str,
glyph_ids: List[str], **kwargs) -> RuneOperationResult:
"""
Execute operation involving multiple Glyphs.
Args:
operation_type: Type of operation
glyph_ids: List of Glyph IDs to operate on
**kwargs: Additional parameters
Returns:
RuneOperationResult
"""
if not glyph_ids:
raise ValueError("No Glyph IDs provided")
# Validate all Glyphs exist
for glyph_id in glyph_ids:
if glyph_id not in self.glyphs:
raise ValueError(f"Glyph {glyph_id} not found")
start_time = time.time()
if operation_type == "correlate" and len(glyph_ids) >= 2:
# Correlate operation between two Glyphs
glyph1 = self.glyphs[glyph_ids[0]]
glyph2 = self.glyphs[glyph_ids[1]]
operator = self.operators['correlate']
updated_state = operator.operate(glyph1, glyph2, **kwargs)
# Update first Glyph with correlation result
self.glyphs[glyph_ids[0]] = updated_state
# Extract specific metadata from the updated_state for the RuneOperationResult
operation_specific_metadata = {}
if operation_type == 'correlate':
# FIX: Copy correlation_coefficient from updated_state.metadata to RuneOperationResult.metadata
operation_specific_metadata['correlation_coefficient'] = updated_state.metadata.get('correlation_coefficient')
# Check for emergence across all involved Glyphs
all_states = [self.glyphs[gid] for gid in glyph_ids]
emergence_result = self.emergence_detector.detect_emergence(all_states)
# Calculate NRCI
nrci_score = self._calculate_operation_nrci(glyph1, updated_state)
operation_time = time.time() - start_time
result = RuneOperationResult(
operation_type=operation_type,
input_glyphs=glyph_ids,
output_glyph=glyph_ids[0],
coherence_change=updated_state.coherence_pressure - glyph1.coherence_pressure,
self_reference_loops=0,
emergence_detected=emergence_result['emergence_detected'],
operation_time=operation_time,
nrci_score=nrci_score,
metadata={
**operation_specific_metadata, # Add specific metadata here
'emergence_result': emergence_result
}
)
self.operation_history.append(result)
return result
else:
raise ValueError(f"Multi-Glyph operation {operation_type} not supported or "
f"requires at least 2 glyphs for 'correlate'.")
def get_system_state(self) -> Dict:
"""Get current state of the Rune Protocol system."""
glyph_states = list(self.glyphs.values())
# System-wide emergence analysis
emergence_result = self.emergence_detector.detect_emergence(glyph_states)
# Calculate system metrics
total_coherence_pressure = sum(g.coherence_pressure for g in glyph_states)
avg_activation = np.mean([g.activation_level for g in glyph_states]) if glyph_states else 0.0
max_self_ref_depth = max([g.self_reference_depth for g in glyph_states]) if glyph_states else 0
return {
'glyph_count': len(self.glyphs),
'total_coherence_pressure': total_coherence_pressure,
'average_activation': avg_activation,
'max_self_reference_depth': max_self_ref_depth,
'coherence_pressure_state': {
'current': self.coherence_pressure_state.current_pressure,
'target': self.coherence_pressure_state.target_pressure,
'stability': self.coherence_pressure_state.stability_index,
'mitigation_active': self.coherence_pressure_state.mitigation_active
},
'emergence_status': emergence_result,
'operation_count': len(self.operation_history),
'system_time': time.time()
}
def _calculate_operation_nrci(self, initial_state: GlyphState,
final_state: GlyphState) -> float:
"""Calculate NRCI for a Rune operation."""
# NRCI based on coherence preservation and enhancement
# Ensure non-zero denominator for variance calculation
initial_mean_sq = np.mean(initial_state.state_vector)**2
initial_coherence = 1.0 - np.var(initial_state.state_vector) / (initial_mean_sq + 1e-10)
final_mean_sq = np.mean(final_state.state_vector)**2