|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Example demonstrating ZHIR threshold verification (∂EPI/∂t > ξ). |
| 3 | +
|
| 4 | +This example shows how the canonical mutation threshold requirement works |
| 5 | +in practice, including cases where it's met and where it generates warnings. |
| 6 | +
|
| 7 | +References: |
| 8 | +- AGENTS.md §11 (Mutation operator) |
| 9 | +- TNFR.pdf §2.2.11 (ZHIR physics) |
| 10 | +""" |
| 11 | + |
| 12 | +from tnfr.structural import create_nfr |
| 13 | +from tnfr.operators.definitions import Dissonance, Mutation, Coherence |
| 14 | + |
| 15 | + |
| 16 | +def example_low_threshold(): |
| 17 | + """Mutation without sufficient velocity generates warning.""" |
| 18 | + print("\n=== Example 1: Low Velocity (∂EPI/∂t < ξ) ===") |
| 19 | + print("Creating node with low structural change velocity...") |
| 20 | + |
| 21 | + G, node = create_nfr("test_low", epi=0.3, vf=1.0) |
| 22 | + G.graph["ZHIR_THRESHOLD_XI"] = 0.1 # Canonical threshold |
| 23 | + G.graph["VALIDATE_OPERATOR_PRECONDITIONS"] = True # Enable validation |
| 24 | + |
| 25 | + # Build EPI history with low velocity |
| 26 | + # ∂EPI/∂t ≈ 0.30 - 0.29 = 0.01 < ξ=0.1 |
| 27 | + G.nodes[node]["epi_history"] = [0.28, 0.29, 0.30] |
| 28 | + |
| 29 | + print(f"EPI history: {G.nodes[node]['epi_history']}") |
| 30 | + print(f"∂EPI/∂t ≈ {0.30 - 0.29:.3f} < ξ={G.graph['ZHIR_THRESHOLD_XI']}") |
| 31 | + print("\nApplying ZHIR (Mutation)...") |
| 32 | + |
| 33 | + # Apply mutation - will generate warning |
| 34 | + Mutation()(G, node) |
| 35 | + |
| 36 | + print(f"✓ Mutation applied") |
| 37 | + print(f"⚠ Warning flag set: {G.nodes[node].get('_zhir_threshold_warning', False)}") |
| 38 | + print("→ Mutation may lack structural justification") |
| 39 | + |
| 40 | + |
| 41 | +def example_high_threshold(): |
| 42 | + """Mutation with sufficient velocity succeeds cleanly.""" |
| 43 | + print("\n=== Example 2: High Velocity (∂EPI/∂t > ξ) ===") |
| 44 | + print("Creating node with high structural change velocity...") |
| 45 | + |
| 46 | + G, node = create_nfr("test_high", epi=0.5, vf=1.0) |
| 47 | + G.graph["ZHIR_THRESHOLD_XI"] = 0.1 # Canonical threshold |
| 48 | + G.graph["VALIDATE_OPERATOR_PRECONDITIONS"] = True # Enable validation |
| 49 | + |
| 50 | + # Build EPI history with high velocity |
| 51 | + # ∂EPI/∂t ≈ 0.50 - 0.38 = 0.12 > ξ=0.1 |
| 52 | + G.nodes[node]["epi_history"] = [0.25, 0.38, 0.50] |
| 53 | + |
| 54 | + print(f"EPI history: {G.nodes[node]['epi_history']}") |
| 55 | + print(f"∂EPI/∂t ≈ {0.50 - 0.38:.3f} > ξ={G.graph['ZHIR_THRESHOLD_XI']}") |
| 56 | + print("\nApplying ZHIR (Mutation)...") |
| 57 | + |
| 58 | + # Apply mutation - will succeed without warning |
| 59 | + Mutation()(G, node) |
| 60 | + |
| 61 | + print(f"✓ Mutation applied") |
| 62 | + print(f"✓ Threshold met: {G.nodes[node].get('_zhir_threshold_met', False)}") |
| 63 | + print("→ Phase transformation justified by structural velocity") |
| 64 | + |
| 65 | + |
| 66 | +def example_canonical_sequence(): |
| 67 | + """OZ → ZHIR sequence generates sufficient threshold.""" |
| 68 | + print("\n=== Example 3: Canonical Sequence (OZ → ZHIR) ===") |
| 69 | + print("Canonical sequence: IL → OZ → ZHIR") |
| 70 | + print("OZ (Dissonance) elevates ΔNFR, increasing structural velocity...") |
| 71 | + |
| 72 | + G, node = create_nfr("test_canonical", epi=0.4, vf=1.0) |
| 73 | + G.graph["ZHIR_THRESHOLD_XI"] = 0.1 |
| 74 | + G.graph["COLLECT_OPERATOR_METRICS"] = True |
| 75 | + G.graph["VALIDATE_OPERATOR_PRECONDITIONS"] = True # Enable validation |
| 76 | + |
| 77 | + # Initialize with moderate history |
| 78 | + G.nodes[node]["epi_history"] = [0.35, 0.38, 0.40] |
| 79 | + |
| 80 | + print(f"Initial EPI history: {G.nodes[node]['epi_history']}") |
| 81 | + print("\nApplying sequence: IL → OZ → ZHIR...") |
| 82 | + |
| 83 | + # Apply canonical sequence |
| 84 | + Coherence()(G, node) # Stabilize |
| 85 | + Dissonance()(G, node) # Increase ΔNFR (elevates velocity) |
| 86 | + Mutation()(G, node) # Phase transformation |
| 87 | + |
| 88 | + print(f"✓ Sequence completed") |
| 89 | + |
| 90 | + # Check metrics |
| 91 | + if "operator_metrics" in G.graph: |
| 92 | + zhir_metrics = [m for m in G.graph["operator_metrics"] if m.get("glyph") == "ZHIR"] |
| 93 | + if zhir_metrics: |
| 94 | + m = zhir_metrics[-1] |
| 95 | + print(f"\nZHIR Metrics:") |
| 96 | + print(f" ∂EPI/∂t = {m.get('depi_dt', 0):.3f}") |
| 97 | + print(f" Threshold ξ = {m.get('threshold_xi', 0):.3f}") |
| 98 | + print(f" Threshold met: {m.get('threshold_met', False)}") |
| 99 | + print(f" Ratio: {m.get('threshold_ratio', 0):.2f}x") |
| 100 | + |
| 101 | + if m.get('threshold_met'): |
| 102 | + print("→ OZ successfully elevated velocity above threshold") |
| 103 | + |
| 104 | + |
| 105 | +def example_metrics(): |
| 106 | + """Show detailed threshold metrics.""" |
| 107 | + print("\n=== Example 4: Detailed Threshold Metrics ===") |
| 108 | + print("Configuring detailed metrics collection...") |
| 109 | + |
| 110 | + G, node = create_nfr("test_metrics", epi=0.6, vf=1.0) |
| 111 | + G.graph["ZHIR_THRESHOLD_XI"] = 0.1 |
| 112 | + G.graph["COLLECT_OPERATOR_METRICS"] = True |
| 113 | + G.graph["VALIDATE_OPERATOR_PRECONDITIONS"] = True # Enable validation |
| 114 | + |
| 115 | + # Build history with known velocity: 0.30 velocity / 0.1 threshold = 3.0x |
| 116 | + G.nodes[node]["epi_history"] = [0.10, 0.30, 0.60] |
| 117 | + |
| 118 | + print(f"EPI history: {G.nodes[node]['epi_history']}") |
| 119 | + print(f"∂EPI/∂t ≈ {0.60 - 0.30:.3f} (3.0x threshold)") |
| 120 | + print("\nApplying ZHIR...") |
| 121 | + |
| 122 | + Mutation()(G, node) |
| 123 | + |
| 124 | + # Display full metrics |
| 125 | + if "operator_metrics" in G.graph: |
| 126 | + m = G.graph["operator_metrics"][-1] |
| 127 | + print(f"\nFull ZHIR Metrics:") |
| 128 | + print(f" Operator: {m['operator']}") |
| 129 | + print(f" Glyph: {m['glyph']}") |
| 130 | + print(f" Phase shift: {m['theta_shift']:.3f}") |
| 131 | + print(f" Delta EPI: {m['delta_epi']:.3f}") |
| 132 | + print(f" ---") |
| 133 | + print(f" ∂EPI/∂t: {m['depi_dt']:.3f}") |
| 134 | + print(f" Threshold ξ: {m['threshold_xi']:.3f}") |
| 135 | + print(f" Threshold met: {m['threshold_met']}") |
| 136 | + print(f" Threshold ratio: {m['threshold_ratio']:.2f}x") |
| 137 | + print(f" Exceeded by: {m['threshold_exceeded_by']:.3f}") |
| 138 | + print(f" ---") |
| 139 | + print(f" Validated: {m['threshold_validated']}") |
| 140 | + print(f" Warning: {m['threshold_warning']}") |
| 141 | + |
| 142 | + |
| 143 | +def main(): |
| 144 | + """Run all examples.""" |
| 145 | + print("=" * 70) |
| 146 | + print("ZHIR (Mutation) Threshold Verification Examples") |
| 147 | + print("=" * 70) |
| 148 | + print("\nCanonical Requirement: θ → θ' when ∂EPI/∂t > ξ") |
| 149 | + print("(Phase transformation requires sufficient structural velocity)") |
| 150 | + |
| 151 | + example_low_threshold() |
| 152 | + example_high_threshold() |
| 153 | + example_canonical_sequence() |
| 154 | + example_metrics() |
| 155 | + |
| 156 | + print("\n" + "=" * 70) |
| 157 | + print("Summary") |
| 158 | + print("=" * 70) |
| 159 | + print("• ZHIR threshold verification ensures mutations are justified") |
| 160 | + print("• Default threshold ξ=0.1 represents minimum velocity for phase change") |
| 161 | + print("• OZ (Dissonance) sequences naturally elevate velocity above threshold") |
| 162 | + print("• Warnings are logged (not blocking) for backward compatibility") |
| 163 | + print("• Metrics include full threshold verification telemetry") |
| 164 | + print("\nConfiguration:") |
| 165 | + print(" G.graph['ZHIR_THRESHOLD_XI'] = 0.1 # Default threshold") |
| 166 | + print(" G.graph['COLLECT_OPERATOR_METRICS'] = True # Enable telemetry") |
| 167 | + print("=" * 70) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + main() |
0 commit comments