|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Example demonstrating ZHIR bifurcation potential detection. |
| 3 | +
|
| 4 | +This example shows how ZHIR (Mutation) detects when structural acceleration |
| 5 | +∂²EPI/∂t² exceeds the bifurcation threshold τ, according to AGENTS.md §U4a. |
| 6 | +
|
| 7 | +The detection is conservative (Option B) - it sets telemetry flags and logs |
| 8 | +information but does not create structural variants. This enables validation |
| 9 | +of grammar U4a requirement: "If {OZ, ZHIR}, include {THOL, IL}". |
| 10 | +""" |
| 11 | + |
| 12 | +from tnfr.structural import create_nfr, run_sequence |
| 13 | +from tnfr.operators.definitions import Emission, Dissonance, Mutation, Coherence, Silence |
| 14 | +from tnfr.alias import get_attr |
| 15 | +from tnfr.constants.aliases import ALIAS_EPI |
| 16 | + |
| 17 | + |
| 18 | +def example_zhir_bifurcation_detection(): |
| 19 | + """Demonstrate ZHIR bifurcation potential detection.""" |
| 20 | + print("=" * 70) |
| 21 | + print("ZHIR Bifurcation Potential Detection Example") |
| 22 | + print("=" * 70) |
| 23 | + |
| 24 | + # Create a node with evolving EPI |
| 25 | + G, node = create_nfr("evolving_system", epi=0.3, vf=1.0, theta=0.5) |
| 26 | + |
| 27 | + # Build EPI history showing high acceleration |
| 28 | + # This simulates a system undergoing rapid structural reorganization |
| 29 | + # d²EPI/dt² = 0.60 - 2*0.40 + 0.30 = 0.60 - 0.80 + 0.30 = 0.10 |
| 30 | + G.nodes[node]["epi_history"] = [0.30, 0.40, 0.60] |
| 31 | + G.nodes[node]["glyph_history"] = [] |
| 32 | + |
| 33 | + # Configure bifurcation threshold |
| 34 | + # Default is 0.5, but we'll use 0.05 to demonstrate detection |
| 35 | + G.graph["BIFURCATION_THRESHOLD_TAU"] = 0.05 |
| 36 | + |
| 37 | + print(f"\nInitial state:") |
| 38 | + print(f" Node: {node}") |
| 39 | + print(f" EPI: {get_attr(G.nodes[node], ALIAS_EPI, 0.0):.3f}") |
| 40 | + print(f" EPI history: {G.nodes[node]['epi_history']}") |
| 41 | + print(f" Bifurcation threshold τ: {G.graph['BIFURCATION_THRESHOLD_TAU']}") |
| 42 | + |
| 43 | + # Calculate acceleration manually for demonstration |
| 44 | + history = G.nodes[node]["epi_history"] |
| 45 | + d2_epi_manual = abs(history[-1] - 2*history[-2] + history[-3]) |
| 46 | + print(f" Computed ∂²EPI/∂t²: {d2_epi_manual:.3f}") |
| 47 | + print(f" Will trigger detection: {d2_epi_manual > G.graph['BIFURCATION_THRESHOLD_TAU']}") |
| 48 | + |
| 49 | + # Apply canonical OZ → ZHIR sequence |
| 50 | + # OZ (Dissonance) destabilizes, ZHIR (Mutation) transforms |
| 51 | + print(f"\nApplying OZ → ZHIR sequence...") |
| 52 | + run_sequence(G, node, [Dissonance(), Mutation()]) |
| 53 | + |
| 54 | + # Check bifurcation detection |
| 55 | + print(f"\nAfter ZHIR:") |
| 56 | + if G.nodes[node].get("_zhir_bifurcation_potential"): |
| 57 | + print(f" ✓ Bifurcation potential DETECTED") |
| 58 | + print(f" ∂²EPI/∂t²: {G.nodes[node]['_zhir_d2epi']:.3f}") |
| 59 | + print(f" Threshold τ: {G.nodes[node]['_zhir_tau']:.3f}") |
| 60 | + print(f" Detection suggests: Apply THOL for controlled bifurcation") |
| 61 | + print(f" or IL for stabilization") |
| 62 | + else: |
| 63 | + print(f" ✗ No bifurcation potential detected") |
| 64 | + |
| 65 | + # Check graph-level events |
| 66 | + events = G.graph.get("zhir_bifurcation_events", []) |
| 67 | + print(f"\n Bifurcation events recorded: {len(events)}") |
| 68 | + if events: |
| 69 | + event = events[0] |
| 70 | + print(f" - Node: {event['node']}") |
| 71 | + print(f" - ∂²EPI/∂t²: {event['d2_epi']:.3f}") |
| 72 | + print(f" - Timestamp: {event['timestamp']}") |
| 73 | + |
| 74 | + # Verify no structural changes (Option B - detection only) |
| 75 | + print(f"\n Structural integrity (Option B):") |
| 76 | + print(f" - No new nodes created") |
| 77 | + print(f" - No new edges created") |
| 78 | + print(f" - Telemetry flags set for grammar validation") |
| 79 | + |
| 80 | + print(f"\n" + "=" * 70) |
| 81 | + print("Example complete!") |
| 82 | + print("=" * 70) |
| 83 | + |
| 84 | + |
| 85 | +def example_zhir_no_bifurcation(): |
| 86 | + """Demonstrate ZHIR with low acceleration (no bifurcation).""" |
| 87 | + print("\n" + "=" * 70) |
| 88 | + print("ZHIR with Low Acceleration (No Bifurcation)") |
| 89 | + print("=" * 70) |
| 90 | + |
| 91 | + # Create a node with low acceleration |
| 92 | + G, node = create_nfr("stable_system", epi=0.5, vf=1.0, theta=0.5) |
| 93 | + |
| 94 | + # Nearly linear EPI progression (low acceleration) |
| 95 | + # d²EPI/dt² = 0.50 - 2*0.49 + 0.48 = 0.50 - 0.98 + 0.48 = 0.00 |
| 96 | + G.nodes[node]["epi_history"] = [0.48, 0.49, 0.50] |
| 97 | + G.nodes[node]["glyph_history"] = [] |
| 98 | + |
| 99 | + G.graph["BIFURCATION_THRESHOLD_TAU"] = 0.05 |
| 100 | + |
| 101 | + print(f"\nInitial state:") |
| 102 | + print(f" Node: {node}") |
| 103 | + print(f" EPI history: {G.nodes[node]['epi_history']}") |
| 104 | + |
| 105 | + history = G.nodes[node]["epi_history"] |
| 106 | + d2_epi_manual = abs(history[-1] - 2*history[-2] + history[-3]) |
| 107 | + print(f" Computed ∂²EPI/∂t²: {d2_epi_manual:.3f}") |
| 108 | + print(f" Threshold τ: {G.graph['BIFURCATION_THRESHOLD_TAU']}") |
| 109 | + print(f" Will trigger detection: {d2_epi_manual > G.graph['BIFURCATION_THRESHOLD_TAU']}") |
| 110 | + |
| 111 | + # Apply ZHIR |
| 112 | + print(f"\nApplying ZHIR...") |
| 113 | + Mutation()(G, node) |
| 114 | + |
| 115 | + print(f"\nAfter ZHIR:") |
| 116 | + if G.nodes[node].get("_zhir_bifurcation_potential"): |
| 117 | + print(f" ✓ Bifurcation potential detected") |
| 118 | + else: |
| 119 | + print(f" ✗ No bifurcation potential detected (acceleration too low)") |
| 120 | + |
| 121 | + print(f"\n System remains in stable regime") |
| 122 | + print(f" No bifurcation handlers required") |
| 123 | + |
| 124 | + print(f"\n" + "=" * 70) |
| 125 | + |
| 126 | + |
| 127 | +def example_grammar_u4a_validation(): |
| 128 | + """Demonstrate Grammar U4a validation with bifurcation detection.""" |
| 129 | + print("\n" + "=" * 70) |
| 130 | + print("Grammar U4a Validation Support") |
| 131 | + print("=" * 70) |
| 132 | + |
| 133 | + print("\nGrammar U4a: If {OZ, ZHIR}, include {THOL, IL}") |
| 134 | + print("When ZHIR detects bifurcation potential, grammar validators") |
| 135 | + print("can check that THOL or IL is present in the sequence.") |
| 136 | + |
| 137 | + # Example 1: Valid sequence with stabilizer |
| 138 | + G1, node1 = create_nfr("system1", epi=0.5, vf=1.0) |
| 139 | + G1.nodes[node1]["epi_history"] = [0.30, 0.40, 0.60] # High acceleration |
| 140 | + G1.nodes[node1]["glyph_history"] = [] |
| 141 | + G1.graph["BIFURCATION_THRESHOLD_TAU"] = 0.05 |
| 142 | + |
| 143 | + print(f"\nExample 1: OZ → ZHIR → IL (with stabilizer)") |
| 144 | + run_sequence(G1, node1, [Dissonance(), Mutation(), Coherence()]) |
| 145 | + |
| 146 | + if G1.nodes[node1].get("_zhir_bifurcation_potential"): |
| 147 | + print(f" ✓ Bifurcation detected") |
| 148 | + print(f" ✓ IL (Coherence) present - Grammar U4a satisfied") |
| 149 | + |
| 150 | + # Example 2: Would violate U4a (no stabilizer) |
| 151 | + G2, node2 = create_nfr("system2", epi=0.5, vf=1.0) |
| 152 | + G2.nodes[node2]["epi_history"] = [0.30, 0.40, 0.60] # High acceleration |
| 153 | + G2.nodes[node2]["glyph_history"] = [] |
| 154 | + G2.graph["BIFURCATION_THRESHOLD_TAU"] = 0.05 |
| 155 | + |
| 156 | + print(f"\nExample 2: OZ → ZHIR (no stabilizer)") |
| 157 | + run_sequence(G2, node2, [Dissonance(), Mutation()]) |
| 158 | + |
| 159 | + if G2.nodes[node2].get("_zhir_bifurcation_potential"): |
| 160 | + print(f" ⚠ Bifurcation detected") |
| 161 | + print(f" ⚠ No THOL/IL present - Grammar U4a should flag this") |
| 162 | + print(f" ⚠ Uncontrolled bifurcation risk") |
| 163 | + |
| 164 | + print(f"\n" + "=" * 70) |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + example_zhir_bifurcation_detection() |
| 169 | + example_zhir_no_bifurcation() |
| 170 | + example_grammar_u4a_validation() |
0 commit comments