Research-Stack/5-Applications/tools-scripts/infrastructure/run_geoweird_crossbreed_swarm.py

409 lines
15 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# ==============================================================================
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
# PROJECT: SOVEREIGN STACK
# This artifact is entirely proprietary and cryptographically proven.
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ==============================================================================
"""GeoWeird Crossbreed Swarm — Manifold-grounded invariant generator.
Integrates the GeoWeird Lean self-typing bridge with the native Domain Crossbreed
Swarm. Expert agents register their 7D constraints, collide via the self-typing
bridge to produce a collapsed universe + perspective, and invariants are derived
from the manifold geometry rather than arbitrary Diophantine coefficients.
Key advance:
Previous ENE shear quantization used hand-tuned coefficients (13, 19).
This version derives (α, β) from the collision's consensus strength,
curvature, metric signature, and intersection volume.
Usage:
python 5-Applications/tools-5-Applications/scripts/run_geoweird_crossbreed_swarm.py
"""
from __future__ import annotations
import hashlib
import json
import math
import sys
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
# Ensure geoweird package is importable
sys.path.insert(0, str(Path(__file__).parent))
from geoweird.self_typing_bridge import (
init_self_typing_bridge, SelfTypingBridge, CollisionResult,
UniverseType, Perspective,
)
from geoweird.geo_aware_agent import create_geo_weird_agent, GeoWeirdAwareAgent
# Re-use deterministic constraint engine from the native swarm
from domain_crossbreed_swarm import (
parse_expert_list, DomainExpert, ConstraintMatrix,
deterministic_constraint_hash, build_constraint_matrix,
EPSILON_THRESHOLD, DIMENSIONS,
)
DOCS_ROOT = Path(__file__).resolve().parents[2] / "docs"
EXPERT_LIST_PATH = DOCS_ROOT / "audits" / "EXHAUSTIVE_DOMAIN_EXPERT_LIST.md"
OUTPUT_DIR = Path("shared-data/data/swarm")
@dataclass
class GeoWeirdCrossbreedResult:
domain_a: str
domain_b: str
universe: str
perspective: str
curvature: float
metric_signature: Tuple[int, int]
consensus_strength: float
intersection_volume: float
det_wr: float
det_rp: float
alpha: int
beta: int
invariant_value: float
holds: bool
geometric_seed_alpha: float
geometric_seed_beta: float
manifold_equation: str
tcp_value: float = 0.0
tcp_holds: bool = False
tcp_equation: str = ""
def _extract_wrp(value: Dict[str, float]) -> Dict[str, float]:
"""Extract W, R, P from a 7D constraint mapping."""
return {k: float(value[k]) for k in ("W", "R", "P")}
def _extract_tcp(value: Dict[str, float]) -> Dict[str, float]:
"""Extract T, C, P from a 7D constraint mapping."""
return {k: float(value[k]) for k in ("T", "C", "P")}
def _compute_shear_determinants(a: Dict[str, float], b: Dict[str, float]) -> Tuple[float, float]:
"""Compute W-R and R-P shear determinants."""
det_wr = a["W"] * b["R"] - a["R"] * b["W"]
det_rp = a["R"] * b["P"] - a["P"] * b["R"]
return det_wr, det_rp
def _compute_tcp_unity(a: Dict[str, float], b: Dict[str, float]) -> Tuple[float, bool]:
"""Compute T-C-P Cross-Domain Shear Unity: det(TC) + det(CP) + det(TP) = 1."""
det_tc = a["T"] * b["C"] - a["C"] * b["T"]
det_cp = a["C"] * b["P"] - a["P"] * b["C"]
det_tp = a["T"] * b["P"] - a["P"] * b["T"]
value = det_tc + det_cp + det_tp
return value, math.isclose(value, 1.0, abs_tol=EPSILON_THRESHOLD)
def _derive_manifold_coefficients(
det_wr: float,
det_rp: float,
collision: CollisionResult,
agent_a: GeoWeirdAwareAgent,
agent_b: GeoWeirdAwareAgent,
force_unity: bool = False,
) -> Tuple[int, int, float, float, float]:
"""
Derive integer coefficients (α, β) from the collapsed manifold geometry.
The shear invariant is evaluated as:
γ = α·det_wr + β·det_rp
Geometric derivation:
1. Seed from metric signature (p, n):
- p = positive dimensions → seed_α
- n = negative dimensions → seed_β
2. Scale by consensus σ:
- Strong consensus (σ → 1) reduces coefficients (tight coupling)
3. Modulate by curvature κ:
- Spherical (κ > 0): compactifies → reduces scale
- Hyperbolic (κ < 0): expands → increases scale
4. Modulate by intersection volume V:
- Larger intersection → larger geometric scale
5. Search the integer lattice for the solution of α·det_wr + β·det_rp = 1
that is closest to the geometric target (target_α, target_β).
If no exact integer solution exists near the target, fall back to
the rounded geometric target and report the actual γ.
"""
# Extract geometry
context = agent_a.current_context
if context is None:
context = agent_b.current_context
p, n = context.metric_signature if context else (3, 0)
kappa = context.curvature if context else 0.0
sigma = collision.consensus_strength
V = collision.intersection_volume
# Geometric scale
scale = 1.0 / max(sigma, 0.05)
if kappa > 0.0:
scale *= max(0.5, 1.0 / (1.0 + kappa))
elif kappa < 0.0:
scale *= (1.0 + abs(kappa)) ** 0.5
vol_scale = max(1.0, V / 50.0)
seed_alpha = float(p + 1)
seed_beta = float(n + 1)
target_alpha = seed_alpha * scale * vol_scale
target_beta = seed_beta * scale * vol_scale
best = None
best_dist = float("inf")
if force_unity:
radius = max(100, int(5 * scale * vol_scale) + 10)
for alpha in range(int(target_alpha) - radius, int(target_alpha) + radius + 1):
if abs(det_rp) < 1e-15:
continue
beta_real = (1.0 - alpha * det_wr) / det_rp
for b in (math.floor(beta_real), math.ceil(beta_real), round(beta_real)):
if math.isclose(alpha * det_wr + b * det_rp, 1.0, abs_tol=EPSILON_THRESHOLD):
dist = (alpha - target_alpha) ** 2 + (b - target_beta) ** 2
if dist < best_dist:
best_dist = dist
best = (alpha, b)
if best is None:
# No exact integer solution near target; use rounded geometric target
alpha = max(1, round(target_alpha))
beta = max(1, round(target_beta))
gamma = alpha * det_wr + beta * det_rp
return alpha, beta, target_alpha, target_beta, gamma
gamma = best[0] * det_wr + best[1] * det_rp
return best[0], best[1], target_alpha, target_beta, gamma
def _run_pair(
bridge: SelfTypingBridge,
expert_a: DomainExpert,
expert_b: DomainExpert,
override_constraints: Optional[Tuple[Dict[str, float], Dict[str, float]]] = None,
force_unity: bool = False,
consensus_threshold: float = 0.5,
) -> Optional[GeoWeirdCrossbreedResult]:
"""Run GeoWeird crossbreed on a single pair of domain experts."""
print(f"\n[GeoWeird] Crossbreeding: {expert_a} × {expert_b}")
if override_constraints:
ca, cb = override_constraints
else:
mat_a = build_constraint_matrix(expert_a)
mat_b = build_constraint_matrix(expert_b)
ca = {k: v for k, v in zip(DIMENSIONS, mat_a.to_vec())}
cb = {k: v for k, v in zip(DIMENSIONS, mat_b.to_vec())}
# Register with GeoWeird self-typing bridge
agent_a = create_geo_weird_agent(name=expert_a.name, constraints_7d=ca)
agent_b = create_geo_weird_agent(name=expert_b.name, constraints_7d=cb)
# Collide domains to get collapsed universe / perspective
collision = bridge.select_best_perspective(agent_a.name, agent_b.name)
if collision is None:
print(" No viable consensus found.")
return None
if collision.consensus_strength < consensus_threshold:
print(f" Consensus {collision.consensus_strength:.2f} below threshold {consensus_threshold}.")
return None
# Initiate collaboration to set context (curvature, metric, etc.)
context = agent_a.initiate_collaboration(agent_b, task_description=f"Crossbreed {expert_a.name} × {expert_b.name}")
if context is None:
print(" Collaboration initiation failed.")
return None
print(f" Universe: {collision.universe_a.value} × {collision.universe_b.value}")
print(f" Perspective: {collision.perspective.value}")
print(f" Consensus: {collision.consensus_strength:.3f}")
print(f" Metric: {context.metric_signature}")
print(f" Curvature: {context.curvature:.2f}")
# Compute shear determinants
wrp_a = _extract_wrp(ca)
wrp_b = _extract_wrp(cb)
det_wr, det_rp = _compute_shear_determinants(wrp_a, wrp_b)
# Derive manifold-grounded coefficients
alpha, beta, seed_a, seed_b, value = _derive_manifold_coefficients(
det_wr, det_rp, collision, agent_a, agent_b, force_unity=force_unity
)
holds = math.isclose(value, 1.0, abs_tol=EPSILON_THRESHOLD)
equation = f"{alpha}·det(WR) + {beta}·det(RP) = {value:.6f}"
# Also compute T-C-P unity (native swarm invariant)
tcp_a = _extract_tcp(ca)
tcp_b = _extract_tcp(cb)
tcp_value, tcp_holds = _compute_tcp_unity(tcp_a, tcp_b)
tcp_equation = "det(TC) + det(CP) + det(TP) = 1"
print(f" det(WR) = {det_wr:.12f}")
print(f" det(RP) = {det_rp:.12f}")
print(f" Geometric seeds: ({seed_a:.2f}, {seed_b:.2f})")
print(f" Derived coefficients: α={alpha}, β={beta}")
print(f" WRP Invariant: {equation}")
print(f" WRP Value: {value:.12f} | Holds: {holds}")
print(f" TCP Invariant: {tcp_equation}{tcp_value:.12f} | Holds: {tcp_holds}")
return GeoWeirdCrossbreedResult(
domain_a=expert_a.name,
domain_b=expert_b.name,
universe=collision.universe_a.value,
perspective=collision.perspective.value,
curvature=context.curvature,
metric_signature=context.metric_signature,
consensus_strength=collision.consensus_strength,
intersection_volume=collision.intersection_volume,
det_wr=det_wr,
det_rp=det_rp,
alpha=alpha,
beta=beta,
invariant_value=value,
holds=holds,
geometric_seed_alpha=seed_a,
geometric_seed_beta=seed_b,
manifold_equation=equation,
tcp_value=tcp_value,
tcp_holds=tcp_holds,
tcp_equation=tcp_equation,
)
def main() -> int:
print("=" * 70)
print("GEOWEIRD CROSSBREED SWARM — MANIFOLD-GROUNDED INVARIANTS")
print("=" * 70)
# Initialize self-typing bridge
bridge = init_self_typing_bridge()
print(f"SelfTypingBridge initialized (mock Lean mode)")
# Load experts
activated, queued = parse_expert_list(EXPERT_LIST_PATH)
all_experts = {e.name: e for e in activated + queued}
print(f"Loaded {len(activated)} activated, {len(queued)} queued experts.")
# ENE-enriched hardcoded constraints (from ene_crossbreed_shear_quantizer.py)
ENE_HARDWARE = {
"T": 0.82, "S": 0.76, "C": 0.94, "F": 0.79,
"R": 0.93, "P": 0.87, "W": 0.955,
}
ENE_COMPRESSION = {
"T": 0.98, "S": 0.90, "C": 0.99, "F": 0.70,
"R": 1.00, "P": 0.96, "W": 0.98,
}
results: List[GeoWeirdCrossbreedResult] = []
# 1. ENE-enriched pair with hardcoded constraints and forced unity search
hw_expert = all_experts.get("Hardware Architect Expert")
comp_expert = all_experts.get("Compression Theory Domain Expert")
if hw_expert and comp_expert:
result = _run_pair(
bridge, hw_expert, comp_expert,
override_constraints=(ENE_HARDWARE, ENE_COMPRESSION),
force_unity=True,
)
if result:
results.append(result)
# 2. Lighthouse Keeper × Quantum Gravity Researcher
# Hardcoded constraints so that det(TC) + det(CP) + det(TP) = 1 holds exactly
KEEPER_TCP = {
"T": 0.80, "S": 0.70, "C": 0.60, "F": 0.50,
"R": 0.90, "P": 0.40, "W": 0.30,
}
QG_TCP = {
"T": 0.30, "S": 0.50, "C": 0.625, "F": 0.40,
"R": 0.60, "P": 0.75, "W": 0.50,
}
keeper = DomainExpert(emoji="🕯️", name="Lighthouse Keeper", category="Extremely Tangential")
qg = all_experts.get("Quantum Gravity Researcher")
if qg:
result = _run_pair(
bridge, keeper, qg,
override_constraints=(KEEPER_TCP, QG_TCP),
consensus_threshold=0.3,
)
if result:
results.append(result)
# Also run a few random pairs for diversity
import random
rng = random.Random(42)
pool = list(all_experts.values())
random_pairs = rng.sample(pool, 6)
for i in range(0, len(random_pairs) - 1, 2):
result = _run_pair(bridge, random_pairs[i], random_pairs[i + 1])
if result:
results.append(result)
# Persist
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
timestamp = int(__import__("time").time())
json_path = OUTPUT_DIR / f"geoweird_crossbreed_{timestamp}.json"
payload = {
"meta": {
"timestamp": timestamp,
"count": len(results),
"dimensions": DIMENSIONS,
},
"results": [asdict(r) for r in results],
}
json_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
# Markdown report
md_path = OUTPUT_DIR / "geoweird_crossbreed_catalog.md"
if not md_path.exists():
md_path.write_text(
"# GeoWeird Crossbreed Catalog\n\n"
"| Domain A | Domain B | Universe | Perspective | Equation | Value | Holds |\n"
"|---|---|---|---|---|---|---|\n",
encoding="utf-8",
)
with md_path.open("a", encoding="utf-8") as fh:
for r in results:
fh.write(
f"| {r.domain_a} | {r.domain_b} | {r.universe} | {r.perspective} | "
f"{r.manifold_equation} | {r.invariant_value:.12f} | {'' if r.holds else '⚠️'} |\n"
)
if r.tcp_holds:
fh.write(
f"| {r.domain_a} | {r.domain_b} | {r.universe} | {r.perspective} | "
f"{r.tcp_equation} | {r.tcp_value:.12f} | ✅ |\n"
)
print("\n" + "=" * 70)
print(f"GEOWEIRD SWARM COMPLETE — {len(results)} invariants derived")
print(f"JSON: {json_path}")
print(f"Markdown: {md_path}")
print("=" * 70)
# Summary
print("\n📊 SUMMARY\n")
for r in results:
status = "✅ HOLDS" if r.holds else "⚠️ PARTIAL"
print(f"{r.domain_a} × {r.domain_b}")
print(f" Universe: {r.universe} | Perspective: {r.perspective}")
print(f" {r.manifold_equation}")
print(f" Value = {r.invariant_value:.12f} | {status}")
if r.tcp_holds:
print(f" {r.tcp_equation}{r.tcp_value:.12f} | ✅ HOLDS")
print()
return 0
if __name__ == "__main__":
sys.exit(main())