-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself_optimizing_engine.py
More file actions
1356 lines (1192 loc) · 53 KB
/
Copy pathself_optimizing_engine.py
File metadata and controls
1356 lines (1192 loc) · 53 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
"""
TNFR Self-Optimizing Mathematical Engine
This module implements self-optimization that emerges naturally from analyzing
the mathematical structure of the nodal equation ∂EPI/∂t = νf · ΔNFR(t).
Mathematical Foundation:
The nodal equation reveals natural optimization landscapes:
1. **Gradient Flows**: ΔNFR naturally defines optimization directions
2. **Energy Functionals**: EPI configurations have natural energy measures
3. **Constraint Manifolds**: Grammar rules create constraint manifolds
4. **Variational Principles**: Operator sequences minimize action functionals
5. **Learning Dynamics**: Repeated patterns improve through experience
6. **Adaptive Algorithms**: The system learns optimal strategies automatically
Self-Optimization Mechanisms:
- Automatic cache strategy learning based on mathematical importance
- Dynamic operator sequence optimization using variational principles
- Adaptive precision management based on mathematical requirements
- Self-tuning computational backend selection
- Emergent load balancing through mathematical analysis
- Natural parallelization discovery via spectral decomposition
Status: CANONICAL SELF-OPTIMIZING ENGINE
"""
import hashlib
import json
import re
import threading
import time
from collections import defaultdict, deque
from dataclasses import dataclass
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from statistics import fmean
from typing import Any, Mapping, Sequence
from ..alias import get_attr
from ..constants.aliases import ALIAS_DNFR, ALIAS_VF
# Import canonical constants for Phase 6 magic number elimination
from ..constants.canonical import (
NODAL_OPT_COUPLING_CANONICAL, # γ/(π+e) ≈ 0.0985 (0.1 → canonical)
)
from ..constants.canonical import PI # π ≈ 3.1416 (3.0 → canonical)
from ..constants.canonical import ( # PHASE 6 EXTENDED: Additional constants for remaining magic numbers
SELF_OPT_CACHE_CONTRACTION_CANONICAL,
SELF_OPT_CACHE_EXPANSION_CANONICAL,
SELF_OPT_CACHE_HIGH_FRACTION_CANONICAL,
SELF_OPT_CACHE_LOW_FRACTION_CANONICAL,
SELF_OPT_CHIRALITY_THRESHOLD_CANONICAL,
SELF_OPT_COMPRESSION_HIGH_CANONICAL,
SELF_OPT_COUPLING_LOW_CANONICAL,
SELF_OPT_DENSITY_DENSE_CANONICAL,
SELF_OPT_DENSITY_SPARSE_CANONICAL,
SELF_OPT_ENERGY_HIGH_CANONICAL,
SELF_OPT_IMPROVEMENT_SIGNIFICANT_CANONICAL,
SELF_OPT_SPEEDUP_HIGH_CANONICAL,
SELF_OPT_SPEEDUP_LOW_CANONICAL,
SELF_OPT_SYMMETRY_THRESHOLD_CANONICAL,
)
from ..errors import TNFRValueError
from ..mathematics.unified_numerical import np
from ..operators.grammar import glyph_function_name, validate_sequence
try:
import networkx as nx
HAS_NETWORKX = True
except ImportError:
HAS_NETWORKX = False
nx = None
HAS_SCIPY = True # Assume available for mathematical analysis
def _extract_scalar_epi(val: Any) -> float:
"""Extract scalar magnitude from potentially complex/dict EPI value."""
if isinstance(val, (int, float)):
return float(val)
if isinstance(val, complex):
return float(np.abs(val))
if isinstance(val, dict):
if "continuous" in val:
c = val["continuous"]
if isinstance(c, (tuple, list)) and len(c) > 0:
v = c[0]
return float(np.abs(v)) if isinstance(v, complex) else float(v)
return 0.0
# Import Unified Fields (New Nov 2025)
try:
from ..physics.fields import compute_unified_telemetry
HAS_UNIFIED_FIELDS = True
except ImportError:
HAS_UNIFIED_FIELDS = False
# Import Structural Integrity Monitor (closed-loop conservation)
try:
from ..physics.integrity import StructuralIntegrityMonitor
HAS_INTEGRITY_MONITOR = True
except ImportError:
HAS_INTEGRITY_MONITOR = False
# Import conservation functions for closed-loop optimization (P5)
try:
from ..physics.conservation import (
capture_conservation_snapshot,
compute_lyapunov_derivative,
detect_grammar_violations_from_conservation,
verify_conservation_balance,
)
HAS_CONSERVATION = True
except ImportError:
HAS_CONSERVATION = False
try:
from ..metrics.common import compute_coherence
from ..metrics.sense_index import compute_Si
HAS_METRIC_OPERATORS = True
except ImportError: # pragma: no cover - optional dependency in trimmed builds
HAS_METRIC_OPERATORS = False
compute_coherence = None # type: ignore
compute_Si = None # type: ignore
# Import TNFR engines
try:
from ..engines.pattern_discovery.mathematical_patterns import (
EmergentPatternType,
TNFREmergentPatternEngine,
)
from .optimization_orchestrator import (
OptimizationStrategy,
TNFROptimizationOrchestrator,
)
from .unified_backend import TNFRUnifiedBackend
HAS_ENGINES = True
except ImportError:
HAS_ENGINES = False
HAS_MATH_BACKENDS = True # Assume available
_SAFE_LABEL_RE = re.compile(r"[^A-Za-z0-9._-]+")
_DEFAULT_OUTPUT_DIR = Path("results") / "self_optimization"
# --- Self-optimization safety thresholds ---
_MIN_CONSERVATION_QUALITY = 0.7 # below → add stabilizers
_MAX_CHARGE_DRIFT = 0.1 # above → Noether charge drift correction
_MAX_VIOLATION_RATE = 0.2 # above → grammar review needed
_MIN_EPI_VARIANCE = 0.01 # below → variance too low, optimize
def _sanitize_label(value: Any | None, default: str) -> str:
"""Convert arbitrary identifiers to filesystem-safe labels."""
if value is None:
text = default
else:
text = str(value).strip()
if not text:
text = default
sanitized = _SAFE_LABEL_RE.sub("_", text)
return sanitized[:64] or default
def _mean_numeric(values: Sequence[float]) -> float | None:
"""Compute mean for numeric sequences with graceful fallback."""
data = [float(v) for v in values if isinstance(v, (int, float))]
if not data:
return None
try:
return fmean(data)
except Exception:
return float(sum(data) / len(data))
def _sense_index_mean(payload: Any) -> float | None:
"""Reduce compute_Si outputs (dict, array) to a scalar average."""
if payload is None:
return None
if isinstance(payload, dict):
return _mean_numeric(list(payload.values()))
try:
array = np.asarray(payload, dtype=float)
except Exception:
return None
if array.size == 0:
return None
return float(np.mean(array))
def _json_safe(value: Any) -> Any:
"""Convert complex objects (NumPy, mappings) to JSON-safe structures."""
if isinstance(value, (str, int, float, bool)) or value is None:
return value
if isinstance(value, np.generic):
return value.item()
if isinstance(value, np.ndarray):
return value.tolist()
if isinstance(value, Mapping):
return {str(k): _json_safe(v) for k, v in value.items()}
if isinstance(value, (list, tuple, set, frozenset)):
return [_json_safe(v) for v in value]
if hasattr(value, "__dict__"):
return {str(k): _json_safe(v) for k, v in vars(value).items()}
return repr(value)
def _canonicalize_sequence_tokens(sequence: Sequence[Any]) -> list[str]:
"""Normalize operator or glyph tokens into canonical operator names."""
tokens: list[str] = []
for entry in sequence:
if entry is None:
continue
candidate = getattr(entry, "name", entry)
text = str(candidate).strip()
if not text:
continue
canonical = glyph_function_name(text, default=None)
normalized = canonical or text
tokens.append(normalized.lower())
return tokens
class OptimizationObjective(Enum):
"""Self-optimization objectives."""
MINIMIZE_COMPUTATION_TIME = "minimize_time"
MAXIMIZE_CACHE_EFFICIENCY = "maximize_cache"
MINIMIZE_MEMORY_USAGE = "minimize_memory"
MAXIMIZE_ACCURACY = "maximize_accuracy"
MINIMIZE_ENERGY = "minimize_energy"
MAXIMIZE_THROUGHPUT = "maximize_throughput"
BALANCE_ALL = "balance_all"
class LearningStrategy(Enum):
"""Learning strategies for optimization."""
GRADIENT_DESCENT = "gradient_descent"
EVOLUTIONARY = "evolutionary"
REINFORCEMENT = "reinforcement"
BAYESIAN_OPTIMIZATION = "bayesian"
MATHEMATICAL_ANALYSIS = "mathematical"
HYBRID = "hybrid"
@dataclass
class OptimizationExperience:
"""Experience record for learning."""
graph_properties: dict[str, Any]
operation_type: str
strategy_used: str
parameters: dict[str, Any]
performance_metrics: dict[str, float]
timestamp: float
success: bool
mathematical_signature: dict[str, Any] | None = None
@dataclass
class OptimizationPolicy:
"""Learned optimization policy."""
policy_name: str
objective: OptimizationObjective
conditions: dict[str, Any] # When to apply this policy
actions: dict[str, Any] # What to do
confidence: float
success_rate: float
average_improvement: float
applications_count: int = 0
@dataclass
class SelfOptimizationResult:
"""Result of self-optimization analysis."""
learned_policies: list[OptimizationPolicy]
optimization_improvements: dict[str, float]
recommended_strategies: list[str]
mathematical_insights: dict[str, Any]
predicted_speedups: dict[str, float]
adaptive_configurations: dict[str, Any]
execution_time: float
conservation_feedback: dict[str, float] | None = None
class TNFRSelfOptimizingEngine:
"""
Self-optimizing engine that learns optimal strategies from mathematical structure.
This engine discovers optimization patterns by analyzing the mathematical
properties of the nodal equation and learning from experience.
"""
def __init__(
self,
learning_strategy: LearningStrategy = LearningStrategy.MATHEMATICAL_ANALYSIS,
optimization_objective: OptimizationObjective = OptimizationObjective.BALANCE_ALL,
max_experience_history: int = 1000,
):
self.learning_strategy = learning_strategy
self.optimization_objective = optimization_objective
self.max_experience_history = max_experience_history
# Learning state
self.experience_history: deque = deque(maxlen=max_experience_history)
self.learned_policies: list[OptimizationPolicy] = []
self.mathematical_insights: dict[str, Any] = {}
# Performance tracking
self.optimization_attempts = 0
self.successful_optimizations = 0
self.cumulative_improvements = defaultdict(float)
# Adaptive configuration
self.adaptive_config = {
"cache_size_mb": 256.0,
"precision": "float64",
"backend_preference": "numpy",
"parallel_threshold": 50,
"spectral_threshold": 20,
}
# Thread safety
self._lock = threading.Lock()
# Initialize engines if available
if HAS_ENGINES:
self.unified_backend = TNFRUnifiedBackend()
self.orchestrator = TNFROptimizationOrchestrator()
self.pattern_engine = TNFREmergentPatternEngine()
else:
self.unified_backend = None
self.orchestrator = None
self.pattern_engine = None
def analyze_mathematical_optimization_landscape(
self, G: Any, operation_type: str = "general"
) -> dict[str, Any]:
"""
Analyze mathematical structure to discover optimization opportunities.
Uses the mathematical properties of the nodal equation to identify
natural optimization strategies.
"""
insights = {}
if not HAS_NETWORKX or G is None:
return insights
# Basic graph properties
num_nodes = len(G.nodes())
num_edges = len(G.edges())
density = (
(2 * num_edges) / (num_nodes * (num_nodes - 1)) if num_nodes > 1 else 0
)
# Unified Field Analysis (New Nov 2025)
unified_insights = {}
if HAS_UNIFIED_FIELDS:
try:
# Compute full unified telemetry
telemetry = compute_unified_telemetry(G)
# Extract Complex Field
complex_data = telemetry.get("complex_field", {})
psi_mag_array = complex_data.get("magnitude", np.array([]))
psi_scalar = np.mean(psi_mag_array) if len(psi_mag_array) > 0 else 0.0
# Extract Emergent Fields
emergent_data = telemetry.get("emergent_fields", {})
chi_array = emergent_data.get("chirality", np.array([]))
sb_array = emergent_data.get("symmetry_breaking", np.array([]))
cc_array = emergent_data.get("coherence_coupling", np.array([]))
chi_scalar = np.mean(np.abs(chi_array)) if len(chi_array) > 0 else 0.0
sb_scalar = np.mean(sb_array) if len(sb_array) > 0 else 0.0
cc_scalar = np.mean(cc_array) if len(cc_array) > 0 else 0.0
# Extract Tensor Invariants
tensor_data = telemetry.get("tensor_invariants", {})
ed_array = tensor_data.get("energy_density", np.array([]))
tc_array = tensor_data.get("topological_charge", np.array([]))
ed_scalar = np.mean(ed_array) if len(ed_array) > 0 else 0.0
tc_scalar = np.mean(np.abs(tc_array)) if len(tc_array) > 0 else 0.0
unified_insights = {
"psi_magnitude": float(psi_scalar),
"chirality": float(chi_scalar),
"symmetry_breaking": float(sb_scalar),
"coherence_coupling": float(cc_scalar),
"energy_density": float(ed_scalar),
"topological_charge": float(tc_scalar),
}
insights["unified_field_analysis"] = unified_insights
except Exception as e:
# Fallback if computation fails
insights["unified_field_error"] = str(e)
# Conservation Integrity Feedback (closed-loop)
if HAS_INTEGRITY_MONITOR:
monitor = StructuralIntegrityMonitor.get(G) if G is not None else None
if monitor is not None:
fv = monitor.feedback_vector()
insights["conservation_feedback"] = fv
# Mathematical structure analysis
insights["graph_structure"] = {
"nodes": num_nodes,
"edges": num_edges,
"density": density,
"is_connected": nx.is_connected(G) if HAS_NETWORKX else False,
"avg_degree": 2 * num_edges / num_nodes if num_nodes > 0 else 0,
}
# Spectral properties for optimization
if self.pattern_engine:
pattern_result = self.pattern_engine.discover_all_patterns(G)
# Extract optimization hints from discovered patterns
optimization_hints = []
for pattern in pattern_result.discovered_patterns:
if pattern.compression_ratio > SELF_OPT_COMPRESSION_HIGH_CANONICAL:
optimization_hints.append(
f"use_compression_{pattern.pattern_type.value}"
)
if pattern.prediction_horizon > PI:
optimization_hints.append(
f"use_prediction_{pattern.pattern_type.value}"
)
if pattern.pattern_type == EmergentPatternType.EIGENMODE_RESONANCE:
optimization_hints.append("use_spectral_methods")
if pattern.pattern_type == EmergentPatternType.FRACTAL_SCALING:
optimization_hints.append("use_hierarchical_methods")
insights["pattern_optimization_hints"] = optimization_hints
insights["mathematical_patterns"] = len(pattern_result.discovered_patterns)
insights["compression_potential"] = pattern_result.compression_potential
# Nodal equation analysis
epi_values = [
_extract_scalar_epi(G.nodes[node].get("EPI", 0.0)) for node in G.nodes()
]
vf_values = [get_attr(G.nodes[node], ALIAS_VF, 1.0) for node in G.nodes()]
dnfr_values = [get_attr(G.nodes[node], ALIAS_DNFR, 0.0) for node in G.nodes()]
# Mathematical properties for optimization
epi_variance = np.var(epi_values)
vf_range = np.max(vf_values) - np.min(vf_values) if vf_values else 0
dnfr_magnitude = np.mean(np.abs(dnfr_values)) if dnfr_values else 0
# Optimization recommendations based on mathematical properties
recommendations = []
# Unified Field Recommendations (New Nov 2025)
if "unified_field_analysis" in insights:
ufa = insights["unified_field_analysis"]
# Chirality-based optimization
if abs(ufa.get("chirality", 0)) > SELF_OPT_CHIRALITY_THRESHOLD_CANONICAL:
recommendations.append("use_chiral_optimization")
# Symmetry breaking handling
if ufa.get("symmetry_breaking", 0) > SELF_OPT_SYMMETRY_THRESHOLD_CANONICAL:
recommendations.append("use_phase_transition_handling")
# Coherence coupling optimization
if ufa.get("coherence_coupling", 0) < SELF_OPT_COUPLING_LOW_CANONICAL:
recommendations.append("enhance_coherence_coupling")
# Topological charge handling
if abs(ufa.get("topological_charge", 0)) > NODAL_OPT_COUPLING_CANONICAL:
recommendations.append("topological_defect_correction")
# Energy density optimization
if ufa.get("energy_density", 0) > SELF_OPT_ENERGY_HIGH_CANONICAL:
recommendations.append("high_energy_stabilization")
# Conservation-based recommendations (closed-loop)
cf = insights.get("conservation_feedback")
if cf is not None:
if cf.get("conservation_quality", 1.0) < _MIN_CONSERVATION_QUALITY:
recommendations.append("conservation_quality_low_stabilize")
if cf.get("energy_derivative", 0.0) > 0:
recommendations.append("lyapunov_unstable_add_IL")
if cf.get("charge_drift", 0.0) > _MAX_CHARGE_DRIFT:
recommendations.append("noether_charge_drift_correction")
if cf.get("violation_rate", 0.0) > _MAX_VIOLATION_RATE:
recommendations.append("high_violation_rate_grammar_review")
if epi_variance < _MIN_EPI_VARIANCE:
recommendations.append("low_variance_epi_optimization")
if vf_range < NODAL_OPT_COUPLING_CANONICAL:
recommendations.append("uniform_vf_optimization")
if dnfr_magnitude > 1.0:
recommendations.append("high_dnfr_stabilization")
if num_nodes > 100:
recommendations.append("large_graph_optimization")
if density > SELF_OPT_DENSITY_DENSE_CANONICAL:
recommendations.append("dense_graph_optimization")
elif density < SELF_OPT_DENSITY_SPARSE_CANONICAL:
recommendations.append("sparse_graph_optimization")
insights["nodal_equation_analysis"] = {
"epi_variance": epi_variance,
"vf_range": vf_range,
"dnfr_magnitude": dnfr_magnitude,
"optimization_recommendations": recommendations,
}
return insights
def learn_from_experience(self, experience: OptimizationExperience) -> None:
"""
Learn optimization strategies from performance experience.
Uses mathematical analysis to extract general principles.
"""
with self._lock:
self.experience_history.append(experience)
self.optimization_attempts += 1
if experience.success:
self.successful_optimizations += 1
# Analyze experience for patterns
if len(self.experience_history) >= 10: # Minimum data for learning
self._extract_optimization_policies()
self._update_adaptive_configuration()
def _extract_optimization_policies(self) -> None:
"""Extract general optimization policies from experience."""
# Group experiences by similar conditions
condition_groups = defaultdict(list)
for exp in self.experience_history:
if exp.success:
# Create condition signature
graph_size = exp.graph_properties.get("nodes", 0)
density = exp.graph_properties.get("density", 0.0)
operation = exp.operation_type
# Discretize conditions for pattern recognition
size_bucket = (
"small"
if graph_size < 20
else "medium" if graph_size < 100 else "large"
)
density_bucket = (
"sparse"
if density < SELF_OPT_DENSITY_SPARSE_CANONICAL
else (
"medium"
if density < SELF_OPT_DENSITY_DENSE_CANONICAL
else "dense"
)
)
condition_key = (size_bucket, density_bucket, operation)
condition_groups[condition_key].append(exp)
# Extract policies from groups with sufficient data
new_policies = []
for condition_key, experiences in condition_groups.items():
if len(experiences) >= 3: # Minimum for reliable pattern
size_bucket, density_bucket, operation = condition_key
# Find best strategy for this condition
strategy_performance = defaultdict(list)
for exp in experiences:
strategy = exp.strategy_used
improvement = exp.performance_metrics.get("speedup_factor", 1.0)
strategy_performance[strategy].append(improvement)
# Select best strategy
best_strategy = None
best_avg_improvement = 0
for strategy, improvements in strategy_performance.items():
avg_improvement = np.mean(improvements)
if avg_improvement > best_avg_improvement:
best_avg_improvement = avg_improvement
best_strategy = strategy
if (
best_strategy
and best_avg_improvement
> SELF_OPT_IMPROVEMENT_SIGNIFICANT_CANONICAL
): # Significant improvement
policy = OptimizationPolicy(
policy_name=f"{size_bucket}_{density_bucket}_{operation}_policy",
objective=self.optimization_objective,
conditions={
"graph_size_bucket": size_bucket,
"density_bucket": density_bucket,
"operation_type": operation,
},
actions={
"recommended_strategy": best_strategy,
"expected_improvement": best_avg_improvement,
},
confidence=min(len(experiences) / 10.0, 1.0),
success_rate=len(experiences)
/ max(
1,
len(
[
e
for e in self.experience_history
if self._matches_conditions(e, condition_key)
]
),
),
average_improvement=best_avg_improvement,
)
new_policies.append(policy)
# Update learned policies
self.learned_policies.extend(new_policies)
# Remove outdated policies (keep only best 20)
if len(self.learned_policies) > 20:
self.learned_policies.sort(
key=lambda p: p.confidence * p.average_improvement, reverse=True
)
self.learned_policies = self.learned_policies[:20]
def _matches_conditions(
self, experience: OptimizationExperience, condition_key: tuple
) -> bool:
"""Check if experience matches condition key."""
size_bucket, density_bucket, operation = condition_key
graph_size = experience.graph_properties.get("nodes", 0)
density = experience.graph_properties.get("density", 0.0)
exp_size_bucket = (
"small" if graph_size < 20 else "medium" if graph_size < 100 else "large"
)
exp_density_bucket = (
"sparse"
if density < SELF_OPT_DENSITY_SPARSE_CANONICAL
else "medium" if density < SELF_OPT_DENSITY_DENSE_CANONICAL else "dense"
)
return (
exp_size_bucket == size_bucket
and exp_density_bucket == density_bucket
and experience.operation_type == operation
)
def _update_adaptive_configuration(self) -> None:
"""Update adaptive configuration based on learned patterns."""
if not self.experience_history:
return
# Analyze recent performance
recent_experiences = list(self.experience_history)[-50:] # Last 50 experiences
successful_experiences = [e for e in recent_experiences if e.success]
if not successful_experiences:
return
# Update cache size based on memory vs performance tradeoff
memory_usage = [
e.performance_metrics.get("memory_used_mb", 0)
for e in successful_experiences
]
speedups = [
e.performance_metrics.get("speedup_factor", 1.0)
for e in successful_experiences
]
if len(memory_usage) > 0 and len(speedups) > 0:
# Simple heuristic: increase cache if low memory usage but good speedup
avg_memory = np.mean(memory_usage)
avg_speedup = np.mean(speedups)
if (
avg_memory
< self.adaptive_config["cache_size_mb"]
* SELF_OPT_CACHE_LOW_FRACTION_CANONICAL
and avg_speedup > SELF_OPT_SPEEDUP_HIGH_CANONICAL
):
self.adaptive_config[
"cache_size_mb"
] *= SELF_OPT_CACHE_EXPANSION_CANONICAL
elif (
avg_memory
> self.adaptive_config["cache_size_mb"]
* SELF_OPT_CACHE_HIGH_FRACTION_CANONICAL
and avg_speedup < SELF_OPT_SPEEDUP_LOW_CANONICAL
):
self.adaptive_config[
"cache_size_mb"
] *= SELF_OPT_CACHE_CONTRACTION_CANONICAL
# Update backend preference
backend_performance = defaultdict(list)
for exp in successful_experiences:
backend = exp.parameters.get("backend", "numpy")
speedup = exp.performance_metrics.get("speedup_factor", 1.0)
backend_performance[backend].append(speedup)
if backend_performance:
best_backend = max(
backend_performance.keys(),
key=lambda b: np.mean(backend_performance[b]),
)
self.adaptive_config["backend_preference"] = best_backend
# Track conservation health across experiences (P5)
conservation_drifts = [
e.performance_metrics["conservation_charge_drift"]
for e in recent_experiences
if "conservation_charge_drift" in e.performance_metrics
]
if conservation_drifts:
self.adaptive_config["mean_conservation_drift"] = float(
np.mean(conservation_drifts)
)
def recommend_optimization_strategy(
self,
G: Any,
operation_type: str = "general",
current_performance: dict[str, float] | None = None,
) -> SelfOptimizationResult:
"""
Recommend optimization strategy based on mathematical analysis and learning.
"""
start_time = time.perf_counter()
# Analyze mathematical structure
mathematical_insights = self.analyze_mathematical_optimization_landscape(
G, operation_type
)
# Find matching learned policies
matching_policies = []
if HAS_NETWORKX and G:
num_nodes = len(G.nodes())
num_edges = len(G.edges())
density = (
(2 * num_edges) / (num_nodes * (num_nodes - 1)) if num_nodes > 1 else 0
)
size_bucket = (
"small" if num_nodes < 20 else "medium" if num_nodes < 100 else "large"
)
density_bucket = (
"sparse"
if density < SELF_OPT_DENSITY_SPARSE_CANONICAL
else "medium" if density < SELF_OPT_DENSITY_DENSE_CANONICAL else "dense"
)
for policy in self.learned_policies:
conditions = policy.conditions
if (
conditions.get("graph_size_bucket") == size_bucket
and conditions.get("density_bucket") == density_bucket
and conditions.get("operation_type") == operation_type
):
matching_policies.append(policy)
# Generate recommendations
recommended_strategies = []
predicted_speedups = {}
# From learned policies
for policy in matching_policies:
strategy = policy.actions.get("recommended_strategy")
if strategy:
recommended_strategies.append(strategy)
predicted_speedups[strategy] = policy.average_improvement
# From mathematical analysis
math_recommendations = mathematical_insights.get(
"nodal_equation_analysis", {}
).get("optimization_recommendations", [])
recommended_strategies.extend(math_recommendations)
# From pattern analysis
pattern_hints = mathematical_insights.get("pattern_optimization_hints", [])
recommended_strategies.extend(pattern_hints)
# Remove duplicates while preserving order
recommended_strategies = list(dict.fromkeys(recommended_strategies))
# Conservation-aware strategy reordering (P5: closed-loop)
# When conservation is stressed, prefer safe computational strategies
# to avoid aggressive optimizations that may degrade structural integrity.
cf = mathematical_insights.get("conservation_feedback")
if cf is not None:
cq = cf.get("conservation_quality", 1.0)
de_dt = cf.get("energy_derivative", 0.0)
if cq < _MIN_CONSERVATION_QUALITY or de_dt > 0:
safe = []
other = []
for s in recommended_strategies:
if any(
kw in s.lower()
for kw in ("cache", "structural", "stabiliz", "memo")
):
safe.append(s)
else:
other.append(s)
recommended_strategies = safe + other
# Calculate optimization improvements
optimization_improvements = {}
if current_performance:
for strategy, speedup in predicted_speedups.items():
optimization_improvements[strategy] = (speedup - 1.0) / speedup
execution_time = time.perf_counter() - start_time
return SelfOptimizationResult(
learned_policies=matching_policies,
optimization_improvements=optimization_improvements,
recommended_strategies=recommended_strategies,
mathematical_insights=mathematical_insights,
predicted_speedups=predicted_speedups,
adaptive_configurations=dict(self.adaptive_config),
execution_time=execution_time,
conservation_feedback=cf,
)
def optimize_automatically(
self, G: Any, operation_type: str = "general", **kwargs
) -> dict[str, Any]:
"""
Automatically apply best optimization strategy.
Uses learned policies and mathematical analysis to select and apply
the optimal strategy.
"""
exec_kwargs = dict(kwargs)
dry_run = bool(exec_kwargs.pop("dry_run", False))
capture_snapshots = bool(exec_kwargs.pop("capture_snapshots", dry_run))
seed_value = exec_kwargs.pop("seed", exec_kwargs.pop("random_seed", None))
node_label = (
exec_kwargs.pop("node", None)
or exec_kwargs.pop("node_id", None)
or exec_kwargs.pop("target_node", None)
or exec_kwargs.pop("focus_node", None)
)
output_dir = exec_kwargs.pop("output_dir", _DEFAULT_OUTPUT_DIR)
operator_sequence = exec_kwargs.pop("operator_sequence", None)
glyph_sequence = exec_kwargs.pop("glyph_sequence", None)
sequence_context = exec_kwargs.pop("sequence_context", None)
# Get recommendations
recommendations = self.recommend_optimization_strategy(G, operation_type)
baseline_snapshot = (
self._capture_structural_snapshot(G) if capture_snapshots else None
)
validation_report = self._prepare_sequence_validation(
operator_sequence,
glyph_sequence,
sequence_context,
G=G,
node=node_label,
)
if dry_run:
payload = self._build_dry_run_payload(
recommendations,
baseline_snapshot,
validation_report,
operation_type,
seed_value,
node_label,
)
safe_seed = _sanitize_label(seed_value, "unseeded")
safe_node = _sanitize_label(node_label, "global")
snapshot_path, signature = self._persist_dry_run_payload(
payload,
output_dir,
safe_seed,
safe_node,
)
snapshots = (
{
"before": baseline_snapshot,
"after": baseline_snapshot,
}
if baseline_snapshot
else None
)
return {
"dry_run": True,
"snapshot_path": str(snapshot_path),
"signature": signature,
"recommendations": recommendations,
"learning_updated": False,
"telemetry_snapshots": snapshots,
"validation": validation_report,
}
# Apply best strategy
if recommendations.recommended_strategies and self.orchestrator:
best_strategy = recommendations.recommended_strategies[0]
# Map strategy name to OptimizationStrategy enum
strategy_mapping = {
"spectral_methods": OptimizationStrategy.SPECTRAL_FFT,
"vectorized": OptimizationStrategy.NODAL_VECTORIZED,
"cache": OptimizationStrategy.ADELIC_CACHE,
"structural": OptimizationStrategy.STRUCTURAL_MEMO,
"hybrid": OptimizationStrategy.HYBRID,
}
# Find matching strategy
optimization_strategy = OptimizationStrategy.AUTO
for name_part, strategy in strategy_mapping.items():
if name_part in best_strategy.lower():
optimization_strategy = strategy
break
# Conservation pre-check (P5: capture baseline conserved quantities)
conservation_before = None
if HAS_CONSERVATION and G is not None:
try:
conservation_before = capture_conservation_snapshot(G)
except Exception:
conservation_before = None
# Execute optimization
try:
profile = self.orchestrator.analyze_optimization_profile(
G, operation_type
)
result = self.orchestrator.execute_optimization(
G, operation_type, optimization_strategy, **exec_kwargs
)
# Conservation post-check (P5: verify conservation balance)
conservation_result = None
conservation_healthy = True
if (
conservation_before is not None
and HAS_CONSERVATION
and G is not None
):
try:
conservation_after = capture_conservation_snapshot(G)
balance = verify_conservation_balance(
conservation_before, conservation_after
)
lyapunov = compute_lyapunov_derivative(
conservation_before, conservation_after
)
violations = detect_grammar_violations_from_conservation(
balance
)
conservation_result = {
"charge_drift": balance.charge_drift,
"rms_residual": balance.rms_residual,
"lyapunov_stable": lyapunov.is_stable,
"energy_derivative": lyapunov.energy_derivative,
"violations_detected": violations["violations_detected"],
"violation_types": violations.get("violation_types", []),
}
conservation_healthy = not violations["violations_detected"]
except Exception:
pass
# Record experience (P5: includes conservation metrics)
perf_metrics: dict[str, float] = {
"speedup_factor": result.speedup_factor,
"execution_time": result.execution_time,
"memory_used_mb": result.memory_used_mb,
"cache_hits": result.cache_hits,
}
if conservation_result is not None:
perf_metrics["conservation_charge_drift"] = conservation_result[
"charge_drift"
]
perf_metrics["conservation_energy_derivative"] = (
conservation_result["energy_derivative"]
)
perf_metrics["conservation_rms_residual"] = conservation_result[
"rms_residual"
]
experience = OptimizationExperience(
graph_properties={
"nodes": len(G.nodes()) if HAS_NETWORKX and G else 0,