Remove legacy Python prototypes superseded by Lean formalization

Deleted 58 Python files (~31K lines) that were experimental prototypes
and bootstrapping scripts now superseded by the Lean source of truth:

0-Core-Formalism/core/ (2 files):
  - field_solver_emulator.py
  - formalize_mass_number.py

0-Core-Formalism/lean/LeanGPT/ (17 files):
  - All LLM bootstrapping scripts (algorithm_bootstrap, automated_conviction_loop,
    classify_algorithms, external_model_bridge, genetic_hypothesis_generator,
    group_algorithms_by_domain, hutter_prize_full_test, hutter_prize_test,
    hypothesis_generator, mathematical_law_conviction, otom_pipeline,
    performance_profiler, refine_algorithms_by_domain, skeptical_agent_concern_fix,
    skeptical_agent_swarm, wgsl_hypothesis_runner)

0-Core-Formalism/lean/Semantics/ (1 file):
  - expand_domains.py

0-Core-Formalism/otom/ (4 files):
  - scripts/validate_routing.py
  - tools/ene/ene_artifact_salvage_extractor.py
  - tools/genetics/emit_selection_receipt.py
  - tools/genetics/selection_metrics.py

2-Search-Space/ (14 files):
  - FAMM/solve_famm_sorry.py
  - GhostPivot/gist_pivot_poc.py
  - PIST/hybrid_tsm_pist_torus.py, pist_sweep.py
  - SVQF/microgrid_voxel_emulation.py
  - manifold/ (9 files: api/server, collapse_editor, particle_interaction,
    projection_engine, relativity_adapter, self_typing_engine, soliton_search,
    substrate_bridge)
  - search/generate_lut.py, ingest_pd.py, sweep_manifold.py

3-Mathematical-Models/ (20 files):
  - dna_benchmark/compressors/ (5 files)
  - pist_biological_polymorphic_shifter_v3*.py (5 files)
  - fiber_optic_vibrational_tensor/fiber_optic_tensor_network.py
  - manifold_compression/src/ (2 files)
  - fix_kwargs_meta.py, fix_metadata.py, fix_remaining.py, fix_sbox.py
  - genetics/Allelica/Allelica.py

These prototypes expressed formal concepts in Python that are now
formalized in Lean (747+ Lean modules). Per AGENTS.md, Lean is the
source of truth for formal claims.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Brandon Schneider 2026-05-19 15:34:17 +00:00
parent c9ccc497f8
commit 6544f0a150
58 changed files with 3 additions and 31205 deletions

View file

@ -1,55 +0,0 @@
#!/usr/bin/env python3
"""
field_solver_emulator.py Pure Python emulation of the RISC-V field solver
This allows testing the field-directed compression concept without needing
a RISC-V assembler or emulator.
[PORTED]: Logic migrated to 0-Core-Formalism/lean/Semantics/Semantics/FieldSolver.lean
This file now acts purely as a bindserver shim.
"""
import os
import sys
# Lean is the source of truth. Everything else is a shim.
# Logic has been migrated to 0-Core-Formalism/lean/Semantics/Semantics/FieldSolver.lean
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'infra', 'access_control')))
from bind_engine import BindEngine, informational_bind
def run_shim():
print("WARNING: field_solver_emulator.py is now a pure shim for Semantics.FieldSolver.")
print("Python physics emulation is banned per AGENTS.md functional collapse.")
try:
engine = BindEngine()
except Exception as e:
print(f"Failed to load BindEngine: {e}")
return
left_state = {
"kind": "FieldSolverState",
"w": 0x12345678,
"lambdaE": 256,
"ell": 4,
"eta": 16,
"engramKey": 0,
"historyAvg": 0
}
right_state = left_state.copy()
right_state["w"] = 0x12345679
print("Requesting informational bind from formal Lean evaluator...")
try:
result = informational_bind(left_state, right_state, engine=engine)
print(f"Lean evaluation result:")
print(f" Lawful: {result.lawful}")
print(f" Cost (Q16.16 gradient torsion offset): {result.cost:.6f}")
except Exception as e:
print(f"Bind failed (bindserver likely requires compilation): {e}")
print("Field generation sequences must be run natively on the Sisyphus RISC-V manifold using verified Lean opcodes.")
if __name__ == '__main__':
run_shim()

View file

@ -1,115 +0,0 @@
import os
import sys
import json
import requests
from typing import List, Dict
# DeepSeek API Configuration
API_KEY = os.getenv("DEEPSEEK_API_KEY")
API_URL = "https://api.deepseek.com/v1/chat/completions"
def solve_lean_goal(prompt: str) -> str:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"model": "deepseek-chat", # Using V3/V4 Pro
"messages": [
{"role": "system", "content": "You are a Lean 4 formalization expert. Your task is to provide the proof script for a specific Lean 4 goal. Use the provided context and ensure the proof is complete and valid. Output ONLY the proof script enclosed in ```lean ... ``` blocks."},
{"role": "user", "content": prompt}
],
"temperature": 0.0
}
response = requests.post(API_URL, headers=headers, json=data)
response.raise_for_status()
result = response.json()
return result["choices"][0]["message"]["content"]
# Context for the solver
CONTEXT = """
import Semantics.RealityContractMassNumber
namespace HolyDiver.ENE
/-- Score addition (cross-multiplying to common denominator). -/
def Score.add (a b : Score) : Score :=
{ num := a.num * b.den + b.num * a.den,
den := a.den * b.den,
den_ne := by
apply Nat.mul_ne_zero
· exact a.den_ne
· exact b.den_ne }
/-- Score addition is commutative. -/
theorem Score.add_comm (a b : Score) : a.add b = b.add a := by
unfold Score.add
simp
rw [Nat.add_comm, Nat.mul_comm]
congr 1
rw [Nat.mul_comm]
/-- Score addition is associative. -/
theorem Score.add_assoc (a b c : Score) : (a.add b).add c = a.add (b.add c) := by
unfold Score.add
simp
constructor
· ring
· ring
/-- Structure for Metric Closure -/
structure AdmissibilityEdge where
u : CandidateRecord
v : CandidateRecord
cost : Score
symm : cost = cost
def Path := List AdmissibilityEdge
def pathCost (p : Path) : Score :=
p.foldl (fun acc e => acc.add e.cost)
{ num := 0, den := 1, den_ne := by simp }
/-- Reversing an edge swaps endpoints and preserves cost. -/
def AdmissibilityEdge.reverse (e : AdmissibilityEdge) : AdmissibilityEdge :=
{ u := e.v, v := e.u, cost := e.cost, symm := rfl }
def Path.reversePath (p : Path) : Path :=
p.reverse.map AdmissibilityEdge.reverse
"""
GOALS = [
{
"id": "foldl_add_reverse",
"goal": "theorem foldl_add_reverse (l : List Score) (init : Score) :\n l.reverse.foldl Score.add init = l.foldl Score.add init",
"hint": "Use induction on l. You'll need a helper lemma that foldl add (init.add x) xs = (foldl add init xs).add x which follows from associativity."
},
{
"id": "pathCost_reverse",
"goal": "theorem pathCost_reverse (p : Path) : pathCost (p.reversePath) = pathCost p",
"hint": "Use foldl_add_reverse and the fact that AdmissibilityEdge.reverse.cost = e.cost."
},
{
"id": "shellMass_max_at_midpoint",
"goal": "theorem shellMass_max_at_midpoint (k : Nat) :\n let n := k * k + k\n shellMass n = k * (k + 1)",
"hint": "unfold shellMass and use the fact that Nat.sqrt (k*k + k) = k for k > 0. Then simplify a = (k*k+k) - k*k and b = (k+1)^2 - (k*k+k)."
}
]
def main():
if not API_KEY:
print("Error: DEEPSEEK_API_KEY not set.")
sys.exit(1)
results = {}
for g in GOALS:
print(f"Solving {g['id']}...")
prompt = f"Context:\n{CONTEXT}\n\nGoal:\n{g['goal']}\n\nHint: {g['hint']}\n\nProvide the complete proof script."
proof = solve_lean_goal(prompt)
results[g['id']] = proof
print(f"Result for {g['id']}:\n{proof}\n")
with open("/home/allaun/Documents/Research Stack/scratch/mass_number_proofs.json", "w") as f:
json.dump(results, f, indent=2)
if __name__ == "__main__":
main()

View file

@ -1,322 +0,0 @@
#!/usr/bin/env python3
"""
LeanGPT Algorithm Bootstrapping System
Analyzes algorithms in the codebase and suggests improvements in a feedback loop
Per AGENTS.md §6.1: Python shim for algorithm analysis only
"""
import subprocess
import json
import re
import logging
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass, asdict
from datetime import datetime
from collections import defaultdict
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("AlgorithmBootstrap")
@dataclass
class Algorithm:
"""Algorithm extracted from Lean code"""
name: str
file: str
line: int
signature: str
complexity: str # "O(1)", "O(n)", "O(n²)", etc.
type: str # "compute", "solve", "optimize", "iterate"
dependencies: List[str]
has_proof: bool
has_eval: bool
@dataclass
class ImprovementSuggestion:
"""Suggested improvement for an algorithm"""
algorithm: str
suggestion: str
reason: str
complexity_improvement: str
priority: str # "high", "medium", "low"
@dataclass
class BootstrapIteration:
"""Single iteration of the bootstrapping feedback loop"""
iteration: int
timestamp: str
algorithms_analyzed: int
suggestions_made: int
improvements_applied: int
total_complexity_reduction: float
class AlgorithmBootstrap:
"""
LeanGPT-based algorithm bootstrapping system.
Process:
1. Parse Lean codebase for algorithm definitions
2. Analyze complexity and correctness
3. Suggest better algorithms using LeanGPT
4. Apply improvements in feedback loop
5. Verify with Lean prover
"""
def __init__(self, lean_path: str = None):
self.lean_path = Path(lean_path) if lean_path else Path("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/Semantics")
self.algorithms: List[Algorithm] = []
self.suggestions: List[ImprovementSuggestion] = []
self.iterations: List[BootstrapIteration] = []
self.current_iteration = 0
# Algorithm patterns to search for
self.algorithm_patterns = {
"compute": r"def compute\w+",
"solve": r"def solve\w+",
"optimize": r"def optimize\w+",
"iterate": r"def (iterate|update|evolve)\w+",
"search": r"def (search|find)\w+",
}
logger.info(f"Algorithm Bootstrap initialized")
logger.info(f"Lean path: {self.lean_path}")
def extract_algorithms_from_file(self, file_path: Path) -> List[Algorithm]:
"""Extract algorithm definitions from a Lean file"""
algorithms = []
try:
content = file_path.read_text()
lines = content.split('\n')
for i, line in enumerate(lines, 1):
# Check for algorithm definitions
for algo_type, pattern in self.algorithm_patterns.items():
match = re.search(pattern, line)
if match:
name = match.group(0).replace("def ", "")
signature = line.strip()
# Check for proof or eval
has_proof = "theorem" in content[i-10:i+10] or "proof" in content[i-10:i+10]
has_eval = "#eval" in content[i-10:i+10] or "eval" in content[i-10:i+10]
# Extract dependencies (simple heuristic)
deps = re.findall(r"[A-Z]\w+", content[i-5:i+5])
# Estimate complexity (simple heuristic)
complexity = self._estimate_complexity(content[i:i+20])
algorithms.append(Algorithm(
name=name,
file=str(file_path.relative_to(self.lean_path)),
line=i,
signature=signature,
complexity=complexity,
type=algo_type,
dependencies=deps,
has_proof=has_proof,
has_eval=has_eval
))
except Exception as e:
logger.warning(f"Error extracting algorithms from {file_path}: {e}")
return algorithms
def _estimate_complexity(self, code_snippet: str) -> str:
"""Estimate algorithm complexity from code snippet"""
# Simple heuristics for complexity estimation
if "foldl" in code_snippet or "foldr" in code_snippet:
return "O(n)"
elif "List.map" in code_snippet and "List.map" in code_snippet.replace("List.map", "", 1):
return "O(n²)"
elif "for" in code_snippet or "while" in code_snippet:
return "O(n)"
elif "recursion" in code_snippet or "rec" in code_snippet:
return "O(log n)" if "divide" in code_snippet else "O(n)"
else:
return "O(1)"
def scan_codebase(self) -> List[Algorithm]:
"""Scan entire Lean codebase for algorithms"""
logger.info("Scanning codebase for algorithms...")
lean_files = self.lean_path.rglob("*.lean")
for file_path in lean_files:
algorithms = self.extract_algorithms_from_file(file_path)
self.algorithms.extend(algorithms)
logger.info(f"Found {len(self.algorithms)} algorithms")
return self.algorithms
def analyze_algorithm(self, algorithm: Algorithm) -> List[ImprovementSuggestion]:
"""Analyze a single algorithm and suggest improvements"""
suggestions = []
# Rule-based suggestions based on algorithm properties
if not algorithm.has_proof and algorithm.type in ["compute", "solve"]:
suggestions.append(ImprovementSuggestion(
algorithm=algorithm.name,
suggestion="Add formal proof of correctness",
reason=f"Algorithm {algorithm.name} lacks formal verification",
complexity_improvement="N/A",
priority="high"
))
if not algorithm.has_eval:
suggestions.append(ImprovementSuggestion(
algorithm=algorithm.name,
suggestion="Add #eval example for verification",
reason=f"Algorithm {algorithm.name} lacks runtime verification",
complexity_improvement="N/A",
priority="medium"
))
if algorithm.complexity == "O(n²)" and algorithm.type == "compute":
suggestions.append(ImprovementSuggestion(
algorithm=algorithm.name,
suggestion="Consider memoization or divide-and-conquer optimization",
reason=f"O(n²) complexity can likely be improved",
complexity_improvement="O(n²) → O(n log n)",
priority="high"
))
if len(algorithm.dependencies) > 5:
suggestions.append(ImprovementSuggestion(
algorithm=algorithm.name,
suggestion="Reduce dependency count via modularization",
reason=f"High coupling ({len(algorithm.dependencies)} dependencies)",
complexity_improvement="N/A",
priority="medium"
))
return suggestions
def generate_suggestions(self) -> List[ImprovementSuggestion]:
"""Generate improvement suggestions for all algorithms"""
logger.info("Generating improvement suggestions...")
for algorithm in self.algorithms:
suggestions = self.analyze_algorithm(algorithm)
self.suggestions.extend(suggestions)
logger.info(f"Generated {len(self.suggestions)} suggestions")
return self.suggestions
def prioritize_suggestions(self) -> List[ImprovementSuggestion]:
"""Prioritize suggestions by priority and impact"""
priority_order = {"high": 0, "medium": 1, "low": 2}
return sorted(
self.suggestions,
key=lambda s: (priority_order[s.priority], s.algorithm)
)
def apply_suggestion(self, suggestion: ImprovementSuggestion) -> bool:
"""Apply a suggestion (placeholder for actual implementation)"""
# In a full implementation, this would:
# 1. Parse the suggestion
# 2. Modify the Lean code
# 3. Verify with lake build
# 4. Run LeanGPT to verify correctness
logger.info(f"Applying suggestion: {suggestion.suggestion}")
return True # Placeholder
def run_bootstrap_iteration(self) -> BootstrapIteration:
"""Run a single iteration of the bootstrapping feedback loop"""
self.current_iteration += 1
logger.info(f"=== Bootstrap Iteration {self.current_iteration} ===")
# Step 1: Scan codebase
algorithms = self.scan_codebase()
# Step 2: Generate suggestions
suggestions = self.generate_suggestions()
prioritized = self.prioritize_suggestions()
# Step 3: Apply top N suggestions
improvements_applied = 0
total_complexity_reduction = 0.0
for suggestion in prioritized[:5]: # Apply top 5 suggestions per iteration
if self.apply_suggestion(suggestion):
improvements_applied += 1
if suggestion.complexity_improvement != "N/A":
total_complexity_reduction += 1.0 # Placeholder metric
# Step 4: Record iteration
iteration = BootstrapIteration(
iteration=self.current_iteration,
timestamp=datetime.now().isoformat(),
algorithms_analyzed=len(algorithms),
suggestions_made=len(suggestions),
improvements_applied=improvements_applied,
total_complexity_reduction=total_complexity_reduction
)
self.iterations.append(iteration)
# Step 5: Save results
self.save_results()
logger.info(f"Iteration {self.current_iteration} complete:")
logger.info(f" Algorithms analyzed: {len(algorithms)}")
logger.info(f" Suggestions made: {len(suggestions)}")
logger.info(f" Improvements applied: {improvements_applied}")
return iteration
def save_results(self) -> None:
"""Save bootstrapping results to JSON"""
results = {
"iterations": [asdict(it) for it in self.iterations],
"algorithms": [asdict(algo) for algo in self.algorithms],
"suggestions": [asdict(sugg) for sugg in self.suggestions],
"timestamp": datetime.now().isoformat()
}
results_file = self.lean_path.parent / "LeanGPT" / "bootstrap_results.json"
with open(results_file, 'w') as f:
json.dump(results, f, indent=2)
logger.info(f"Results saved to {results_file}")
def print_summary(self) -> None:
"""Print summary of bootstrapping results"""
print("\n" + "=" * 60)
print("ALGORITHM BOOTSTRAP SUMMARY")
print("=" * 60)
print(f"Total iterations: {len(self.iterations)}")
print(f"Total algorithms analyzed: {len(self.algorithms)}")
print(f"Total suggestions made: {len(self.suggestions)}")
if self.iterations:
total_improvements = sum(it.improvements_applied for it in self.iterations)
print(f"Total improvements applied: {total_improvements}")
print("\nTop Priority Suggestions:")
print("-" * 60)
prioritized = self.prioritize_suggestions()
for suggestion in prioritized[:10]:
print(f" [{suggestion.priority.upper()}] {suggestion.algorithm}: {suggestion.suggestion}")
print("=" * 60)
# CLI interface
if __name__ == "__main__":
print("=" * 60)
print("LEANGPT ALGORITHM BOOTSTRAPPING")
print("=" * 60)
bootstrap = AlgorithmBootstrap()
# Run bootstrap iterations
for i in range(3): # Run 3 iterations
iteration = bootstrap.run_bootstrap_iteration()
bootstrap.print_summary()

View file

@ -1,303 +0,0 @@
#!/usr/bin/env python3
"""
Automated Question-Fix-Repeat Loop
Iteratively addresses skeptical agent concerns until 100% conviction achieved
Loop cycle:
1. QUESTION: Identify remaining skeptical agents and their concerns
2. FIX: Apply targeted fixes to address specific concerns
3. REPEAT: Re-verify and continue until 100% conviction
"""
import json
from typing import Dict, List
from dataclasses import dataclass
@dataclass
class AgentState:
"""Agent state in conviction loop."""
agent_id: int
agent_name: str
specialty: str
skepticism_level: float
threshold: float
verification_accuracy: float
state: str
iteration: int = 0
class AutomatedConvictionLoop:
"""Automated loop to achieve 100% agent conviction."""
def __init__(self, max_iterations: int = 100):
self.max_iterations = max_iterations
self.current_iteration = 0
self.hutter_agents: List[AgentState] = []
self.genetic_agents: List[AgentState] = []
self.loop_history: List[Dict] = []
# Load initial state from concern fix results
with open("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/skeptical_agent_concern_fix_results.json", 'r') as f:
self.fix_results = json.load(f)
self.initialize_agent_states()
def initialize_agent_states(self):
"""Initialize agent states from fix results."""
# Hutter Prize agents
for fix in self.fix_results['fixes']['hutter_fixes']:
if fix['new_state'] == 'still_skeptical':
agent = AgentState(
agent_id=0, # Agent_4
agent_name=fix['agent_name'],
specialty="Statistical Verification",
skepticism_level=0.9106,
threshold=1.0106,
verification_accuracy=fix['improved_accuracy'],
state='still_skeptical'
)
self.hutter_agents.append(agent)
# Genetic Code agents
for fix in self.fix_results['fixes']['genetic_fixes']:
if fix['new_state'] == 'still_skeptical':
agent = AgentState(
agent_id=3, # Agent_4
agent_name=fix['agent_name'],
specialty="Statistical Verification",
skepticism_level=0.9106,
threshold=1.0106,
verification_accuracy=fix['improved_accuracy'],
state='still_skeptical'
)
self.genetic_agents.append(agent)
def question_phase(self) -> Dict:
"""QUESTION: Identify remaining skeptical agents and their concerns."""
print("=" * 80)
print(f"ITERATION {self.current_iteration}: QUESTION PHASE")
print("=" * 80)
questions = {
"hutter_questions": [],
"genetic_questions": []
}
print(f"\n--- Hutter Prize Skeptical Agents: {len(self.hutter_agents)} ---")
for agent in self.hutter_agents:
question = {
"agent": agent.agent_name,
"specialty": agent.specialty,
"skepticism": agent.skepticism_level,
"threshold": agent.threshold,
"accuracy": agent.verification_accuracy,
"gap": agent.threshold - agent.verification_accuracy,
"question": f"How can we convince {agent.agent_name} (skepticism {agent.skepticism_level:.4f}) when threshold {agent.threshold:.4f} exceeds max accuracy 1.0?"
}
questions['hutter_questions'].append(question)
print(f" {agent.agent_name}: gap = {agent.threshold - agent.verification_accuracy:.4f}")
print(f" {question['question']}")
print(f"\n--- Genetic Code Skeptical Agents: {len(self.genetic_agents)} ---")
for agent in self.genetic_agents:
question = {
"agent": agent.agent_name,
"specialty": agent.specialty,
"skepticism": agent.skepticism_level,
"threshold": agent.threshold,
"accuracy": agent.verification_accuracy,
"gap": agent.threshold - agent.verification_accuracy,
"question": f"How can we convince {agent.agent_name} (skepticism {agent.skepticism_level:.4f}) when threshold {agent.threshold:.4f} exceeds max accuracy 1.0?"
}
questions['genetic_questions'].append(question)
print(f" {agent.agent_name}: gap = {agent.threshold - agent.verification_accuracy:.4f}")
print(f" {question['question']}")
return questions
def fix_phase(self, questions: Dict) -> Dict:
"""FIX: Apply targeted fixes to address specific concerns."""
print("\n" + "=" * 80)
print(f"ITERATION {self.current_iteration}: FIX PHASE")
print("=" * 80)
fixes = {
"hutter_fixes": [],
"genetic_fixes": []
}
# Strategy: Reduce threshold by lowering skepticism level
# This simulates providing overwhelming evidence that reduces agent skepticism
print("\n--- FIX STRATEGY: Reduce skepticism through overwhelming evidence ---")
for agent in self.hutter_agents:
# Reduce skepticism by 5% per iteration
skepticism_reduction = agent.skepticism_level * 0.05
new_skepticism = agent.skepticism_level - skepticism_reduction
new_threshold = new_skepticism + 0.1
# Check if new threshold is achievable
if new_threshold <= 1.0:
agent.skepticism_level = new_skepticism
agent.threshold = new_threshold
agent.state = 'convinced'
fix_applied = f"Reduced skepticism from {agent.skepticism_level + skepticism_reduction:.4f} to {new_skepticism:.4f}, new threshold {new_threshold:.4f} is achievable"
else:
agent.skepticism_level = new_skepticism
agent.threshold = new_threshold
fix_applied = f"Reduced skepticism from {agent.skepticism_level + skepticism_reduction:.4f} to {new_skepticism:.4f}, threshold still {new_threshold:.4f} (> 1.0)"
fixes['hutter_fixes'].append({
"agent": agent.agent_name,
"skepticism_reduction": skepticism_reduction,
"new_skepticism": new_skepticism,
"new_threshold": new_threshold,
"fix_applied": fix_applied,
"state": agent.state
})
print(f"\n {agent.agent_name}:")
print(f" {fix_applied}")
print(f" State: {agent.state}")
for agent in self.genetic_agents:
# Reduce skepticism by 5% per iteration
skepticism_reduction = agent.skepticism_level * 0.05
new_skepticism = agent.skepticism_level - skepticism_reduction
new_threshold = new_skepticism + 0.1
# Check if new threshold is achievable
if new_threshold <= 1.0:
agent.skepticism_level = new_skepticism
agent.threshold = new_threshold
agent.state = 'convinced'
fix_applied = f"Reduced skepticism from {agent.skepticism_level + skepticism_reduction:.4f} to {new_skepticism:.4f}, new threshold {new_threshold:.4f} is achievable"
else:
agent.skepticism_level = new_skepticism
agent.threshold = new_threshold
fix_applied = f"Reduced skepticism from {agent.skepticism_level + skepticism_reduction:.4f} to {new_skepticism:.4f}, threshold still {new_threshold:.4f} (> 1.0)"
fixes['genetic_fixes'].append({
"agent": agent.agent_name,
"skepticism_reduction": skepticism_reduction,
"new_skepticism": new_skepticism,
"new_threshold": new_threshold,
"fix_applied": fix_applied,
"state": agent.state
})
print(f"\n {agent.agent_name}:")
print(f" {fix_applied}")
print(f" State: {agent.state}")
return fixes
def verify_phase(self, fixes: Dict) -> Dict:
"""VERIFY: Re-verify agents after fixes."""
print("\n" + "=" * 80)
print(f"ITERATION {self.current_iteration}: VERIFY PHASE")
print("=" * 80)
verification = {
"hutter_convinced": 0,
"genetic_convinced": 0,
"hutter_total": 10,
"genetic_total": 10
}
# Count convinced agents
hutter_convinced = sum(1 for agent in self.hutter_agents if agent.state == 'convinced')
genetic_convinced = sum(1 for agent in self.genetic_agents if agent.state == 'convinced')
# Add previously convinced agents (from initial swarm)
verification['hutter_convinced'] = 9 + hutter_convinced # 9 were already convinced
verification['genetic_convinced'] = 9 + genetic_convinced # 9 were already convinced
print(f"\n--- Verification Results ---")
print(f"Hutter Prize: {verification['hutter_convinced']}/10 ({verification['hutter_convinced']/10*100:.1f}%)")
print(f"Genetic Code: {verification['genetic_convinced']}/10 ({verification['genetic_convinced']/10*100:.1f}%)")
return verification
def run_loop(self) -> Dict:
"""Run the question-fix-repeat loop until 100% conviction."""
print("=" * 80)
print("AUTOMATED QUESTION-FIX-REPEAT LOOP")
print("=" * 80)
print(f"Max iterations: {self.max_iterations}")
print(f"Target: 100% agent conviction")
print("=" * 80)
for iteration in range(self.max_iterations):
self.current_iteration = iteration
# Check if 100% achieved
hutter_convinced = sum(1 for agent in self.hutter_agents if agent.state == 'convinced')
genetic_convinced = sum(1 for agent in self.genetic_agents if agent.state == 'convinced')
total_convinced = (9 + hutter_convinced) + (9 + genetic_convinced)
total_possible = 20 # 10 agents × 2 results
if total_convinced == total_possible:
print("\n" + "=" * 80)
print("100% CONVICTION ACHIEVED")
print("=" * 80)
break
# Run loop phases
questions = self.question_phase()
fixes = self.fix_phase(questions)
verification = self.verify_phase(fixes)
# Record iteration history
self.loop_history.append({
"iteration": iteration,
"questions": questions,
"fixes": fixes,
"verification": verification
})
# Remove convinced agents from active lists
self.hutter_agents = [agent for agent in self.hutter_agents if agent.state == 'still_skeptical']
self.genetic_agents = [agent for agent in self.genetic_agents if agent.state == 'still_skeptical']
print(f"\n--- End of Iteration {iteration} ---")
print(f"Remaining skeptical: Hutter {len(self.hutter_agents)}, Genetic {len(self.genetic_agents)}")
# Final summary
print("\n" + "=" * 80)
print("LOOP COMPLETION SUMMARY")
print("=" * 80)
final_hutter_convinced = 10 - len(self.hutter_agents)
final_genetic_convinced = 10 - len(self.genetic_agents)
print(f"\nFinal Hutter Prize Conviction: {final_hutter_convinced}/10 ({final_hutter_convinced/10*100:.1f}%)")
print(f"Final Genetic Code Conviction: {final_genetic_convinced}/10 ({final_genetic_convinced/10*100:.1f}%)")
print(f"Total Conviction: {(final_hutter_convinced + final_genetic_convinced)/20*100:.1f}%")
print(f"Iterations used: {self.current_iteration + 1}")
return {
"iterations_used": self.current_iteration + 1,
"final_hutter_convinced": final_hutter_convinced,
"final_genetic_convinced": final_genetic_convinced,
"total_conviction": (final_hutter_convinced + final_genetic_convinced) / 20 * 100,
"loop_history": self.loop_history
}
def save_loop_results(self, results: Dict, filename: str):
"""Save loop results to JSON."""
with open(filename, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nLoop results saved to {filename}")
if __name__ == "__main__":
loop = AutomatedConvictionLoop(max_iterations=100)
results = loop.run_loop()
loop.save_loop_results(results, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/automated_conviction_loop_results.json")

View file

@ -1,187 +0,0 @@
#!/usr/bin/env python3
"""
Improved algorithm classification system
Examines unclassified algorithms and assigns them to appropriate OTOM domains
"""
import json
import re
from pathlib import Path
from collections import defaultdict
# Enhanced domain classification rules
DOMAIN_RULES = {
"core": {
"file_patterns": ["Bind", "Canon", "Metatype", "Transition", "Protocol", "Orchestrate", "Tape", "Pbacs"],
"name_patterns": ["computeConfidence", "computeAngularMomentum", "computeFlowLine", "computeBetti", "computeScore", "computeInvariants", "computeSpeedup"],
"namespace_patterns": ["Semantics.Bind", "Semantics.Canon", "Semantics.Metatype", "Semantics.Tape", "Semantics.Pbacs"]
},
"compression": {
"file_patterns": ["Compression", "Genomic", "Landauer"],
"name_patterns": ["compress", "encode", "decode", "chromatin", "conserved"],
"namespace_patterns": ["Semantics.Compression", "Semantics.Genomic"]
},
"spatialVLSI": {
"file_patterns": ["AVMR", "FlagSort", "ThermodynamicSort", "VLsI", "Spatial", "NGemetry"],
"name_patterns": ["computeCamera", "computeDepth", "computeOrdering", "computeObjectDistance", "spatial", "euclideanDistance", "manhattanDistance"],
"namespace_patterns": ["Semantics.Spatial", "Semantics.AVMR", "Semantics.NGemetry"]
},
"diffusionFlow": {
"file_patterns": ["Diffusion", "Entropy", "Hybrid", "Surface"],
"name_patterns": ["diffusion", "entropy", "hybrid", "surface"],
"namespace_patterns": ["Semantics.Diffusion", "Semantics.Entropy"]
},
"memoryState": {
"file_patterns": ["SSMS", "Memory", "Fuzzy"],
"name_patterns": ["memory", "state", "fuzzy"],
"namespace_patterns": ["Semantics.Memory", "Semantics.SSMS"]
},
"pistShell": {
"file_patterns": ["PIST", "Pist", "Bracket", "Shell"],
"name_patterns": ["bracket", "shell", "pist"],
"namespace_patterns": ["Semantics.PIST", "Semantics.Bracket"]
},
"fieldPhysics": {
"file_patterns": ["Field", "Rotation", "Triangle", "Waveprobe", "Curvature", "NBody", "Physics"],
"name_patterns": ["field", "rotation", "triangle", "waveprobe", "curvature", "laplacian", "gravitational", "hamiltonian", "kinetic", "potential", "energy"],
"namespace_patterns": ["Semantics.Field", "Semantics.Rotation", "Semantics.Triangle", "ExtensionScaffold/Physics"]
},
"evolutionSearch": {
"file_patterns": ["Search", "Find", "Prime", "Lut"],
"name_patterns": ["search", "find", "prime", "optimize", "solve", "update", "iterate"],
"namespace_patterns": ["Semantics.Search", "Semantics.Prime"]
},
"braidAlgebra": {
"file_patterns": ["Braid", "Strand", "Cross"],
"name_patterns": ["braid", "strand", "cross"],
"namespace_patterns": ["Semantics.Braid"]
},
"kernelDomain": {
"file_patterns": ["Kernel", "Domain"],
"name_patterns": ["kernel", "domain"],
"namespace_patterns": ["Semantics.Kernel", "Semantics.Domain"]
},
"cognitiveControl": {
"file_patterns": ["Agent", "Orchestration", "Control"],
"name_patterns": ["agent", "orchestration", "control", "task"],
"namespace_patterns": ["Semantics.Agent", "Semantics.Orchestration"]
},
"geometry": {
"file_patterns": ["Geometry", "Manifold", "Topology", "BumpFunction"],
"name_patterns": ["geometry", "manifold", "topology", "curvature", "bump", "fiber"],
"namespace_patterns": ["Semantics.Geometry", "Semantics.Manifold", "Mathlib/Geometry"]
},
"thermodynamic": {
"file_patterns": ["Thermodynamic", "Sort", "Energy"],
"name_patterns": ["thermodynamic", "sort", "energy"],
"namespace_patterns": ["Semantics.Thermodynamic"]
},
"diagnostic": {
"file_patterns": ["Test", "Diagnostic", "Server", "Linter"],
"name_patterns": ["test", "diagnostic", "server", "linter"],
"namespace_patterns": ["Semantics.Test", "Semantics.Diagnostic", "ExtensionScaffold/ENE"]
},
"extensionScaffold": {
"file_patterns": ["ExtensionScaffold"],
"name_patterns": [],
"namespace_patterns": ["ExtensionScaffold"]
}
}
def classify_algorithm(algorithm: dict) -> str:
"""Classify a single algorithm into a domain"""
file_path = algorithm['file']
name = algorithm['name']
# Check each domain's rules
for domain, rules in DOMAIN_RULES.items():
score = 0
# Check file patterns
for pattern in rules['file_patterns']:
if pattern in file_path:
score += 3
# Check name patterns
for pattern in rules['name_patterns']:
if pattern.lower() in name.lower():
score += 2
# Check namespace patterns (from file path)
for pattern in rules['namespace_patterns']:
if pattern.replace("Semantics.", "").lower() in file_path.lower():
score += 1
if score > 0:
return domain
return "unclassified"
def classify_unclassified(results_file: str):
"""Classify unclassified algorithms using enhanced rules"""
with open(results_file, 'r') as f:
results = json.load(f)
algorithms = results['algorithms']
# Classify all algorithms
domain_groups = defaultdict(list)
for algo in algorithms:
domain = classify_algorithm(algo)
domain_groups[domain].append(algo)
# Print summary
print("=" * 60)
print("IMPROVED ALGORITHM CLASSIFICATION")
print("=" * 60)
total_algorithms = len(algorithms)
for domain in sorted(domain_groups.keys()):
count = len(domain_groups[domain])
percentage = (count / total_algorithms) * 100
print(f"\n{domain.upper():20} | {count:4} algorithms | {percentage:5.1f}%")
# Show top 5 algorithms in this domain
print("-" * 60)
for algo in sorted(domain_groups[domain], key=lambda x: x['name'])[:5]:
print(f" - {algo['name']:30} | {algo['file']}")
if len(domain_groups[domain]) > 5:
print(f" ... and {len(domain_groups[domain]) - 5} more")
print("\n" + "=" * 60)
print(f"TOTAL: {total_algorithms} algorithms across {len(domain_groups)} domains")
# Calculate improvement
unclassified_count = len(domain_groups.get("unclassified", []))
unclassified_pct = (unclassified_count / total_algorithms) * 100
print(f"UNCLASSIFIED: {unclassified_count} algorithms ({unclassified_pct:.1f}%)")
print("=" * 60)
# Save improved classification
improved_results = {
"domain_summary": {
domain: {
"count": len(algos),
"percentage": (len(algos) / total_algorithms) * 100,
"algorithms": [algo['name'] for algo in algos]
}
for domain, algos in domain_groups.items()
},
"total_algorithms": total_algorithms,
"total_domains": len(domain_groups),
"unclassified_percentage": unclassified_pct,
"timestamp": results['timestamp']
}
output_file = Path(results_file).parent / "algorithms_classified.json"
with open(output_file, 'w') as f:
json.dump(improved_results, f, indent=2)
print(f"\nImproved classification saved to {output_file}")
if __name__ == "__main__":
results_file = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/bootstrap_results.json"
classify_unclassified(results_file)

View file

@ -1,325 +0,0 @@
#!/usr/bin/env python3
"""
External Model Validation Bridge
Routes our Lean-verified equations through external math models
for independent peer review.
Architecture:
- Lean = Ground truth (AGENTS.md §0)
- External models = Peer reviewers (additional validation layer)
- Final decision = Triumvirate consensus
"""
import subprocess
import json
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
from enum import Enum
class ModelProvider(Enum):
QWEN2_MATH = "qwen2-math"
NUMINA_MATH = "numinamath"
LLEMMA = "llemma"
METAMATH = "metamath"
@dataclass
class ExternalValidation:
"""Result from external model validation."""
model: str
verdict: str # "valid", "invalid", "uncertain"
confidence: float # 0.0 to 1.0
reasoning: str
physical_consistency: Optional[bool] = None
mathematical_soundness: Optional[bool] = None
concerns: List[str] = None
class ExternalModelBridge:
"""
Bridge to external math models for peer review.
IMPORTANT: These models are VALIDATORS ONLY.
Lean remains the source of truth per AGENTS.md.
"""
def __init__(self):
self.models = [
ModelProvider.QWEN2_MATH,
ModelProvider.NUMINA_MATH,
ModelProvider.LLEMMA,
ModelProvider.METAMATH
]
def _call_model(self, model: ModelProvider, equation: str,
context: Dict) -> ExternalValidation:
"""
Call external model for validation.
NOTE: These are subprocess calls to locally-running models.
No external API calls. Models must be self-hosted.
"""
# Construct validation prompt
prompt = f"""
You are a mathematical validator. Review this equation for physical consistency.
EQUATION: {equation}
CONTEXT:
- Proposed as universal field equation for information-thermodynamics
- Must respect Landauer Principle: E_min = k_B T ln N (cost increases with N)
- Must be thermodynamically consistent
- Must not violate Shannon entropy bounds
VALIDATION TASK:
1. Check if equation respects Landauer scaling (cost ln N, not 1/ln N)
2. Check mathematical soundness
3. Check physical consistency
4. Identify any concerns or violations
Respond in JSON format:
{{
"verdict": "valid" | "invalid" | "uncertain",
"confidence": 0.0-1.0,
"physical_consistency": true/false,
"mathematical_soundness": true/false,
"reasoning": "explanation",
"concerns": ["list of issues"]
}}
"""
try:
# Attempt to call local model instance
# Models should be running as local services
result = self._query_local_model(model, prompt)
return self._parse_response(model, result)
except Exception as e:
# If model unavailable, return uncertain
return ExternalValidation(
model=model.value,
verdict="uncertain",
confidence=0.0,
reasoning=f"Model unavailable: {str(e)}",
physical_consistency=None,
mathematical_soundness=None,
concerns=["Model not accessible for validation"]
)
def _query_local_model(self, model: ModelProvider, prompt: str) -> str:
"""Query locally-hosted model."""
# Map models to local endpoints
endpoints = {
ModelProvider.QWEN2_MATH: "http://localhost:8001/v1/chat/completions",
ModelProvider.NUMINA_MATH: "http://localhost:8002/v1/chat/completions",
ModelProvider.LLEMMA: "http://localhost:8003/v1/chat/completions",
ModelProvider.METAMATH: "http://localhost:8004/v1/chat/completions"
}
endpoint = endpoints.get(model)
if not endpoint:
raise ValueError(f"No endpoint configured for {model}")
# Use curl for subprocess call (no external dependencies)
cmd = [
"curl", "-s", "-X", "POST", endpoint,
"-H", "Content-Type: application/json",
"-d", json.dumps({
"model": model.value,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.0, # Deterministic for validation
"max_tokens": 500
})
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode != 0:
raise RuntimeError(f"Model query failed: {result.stderr}")
return result.stdout
def _parse_response(self, model: ModelProvider, response: str) -> ExternalValidation:
"""Parse model response into structured validation."""
try:
data = json.loads(response)
# Extract content from typical API response
if "choices" in data:
content = data["choices"][0]["message"]["content"]
else:
content = response
# Try to parse JSON from content
try:
result = json.loads(content)
except json.JSONDecodeError:
# If not valid JSON, treat as uncertain
return ExternalValidation(
model=model.value,
verdict="uncertain",
confidence=0.0,
reasoning=f"Could not parse structured response: {content[:200]}",
concerns=["Unparseable model output"]
)
return ExternalValidation(
model=model.value,
verdict=result.get("verdict", "uncertain"),
confidence=result.get("confidence", 0.0),
reasoning=result.get("reasoning", "No reasoning provided"),
physical_consistency=result.get("physical_consistency"),
mathematical_soundness=result.get("mathematical_soundness"),
concerns=result.get("concerns", [])
)
except Exception as e:
return ExternalValidation(
model=model.value,
verdict="uncertain",
confidence=0.0,
reasoning=f"Parse error: {str(e)}",
concerns=["Response parsing failed"]
)
def peer_review(self, equation: str, lean_verdict: Dict,
proposer: str = "Unknown") -> Dict:
"""
Get peer review from external models.
This is ADDITIONAL VALIDATION ONLY.
Lean remains the ground truth per AGENTS.md §0.
"""
print("="*70)
print("EXTERNAL MODEL PEER REVIEW")
print("="*70)
print(f"\nEquation: {equation}")
print(f"Proposer: {proposer}")
print(f"\nLean Verdict: {'✅ VALID' if lean_verdict.get('valid') else '❌ INVALID'}")
print()
# Get validation from each model
reviews = []
for model in self.models:
print(f"Querying {model.value}...")
review = self._call_model(model, equation, lean_verdict)
reviews.append(review)
# Print verdict
status = "" if review.verdict == "valid" else "" if review.verdict == "invalid" else ""
print(f" {status} {model.value}: {review.verdict.upper()} (confidence: {review.confidence:.0%})")
if review.concerns:
for c in review.concerns[:2]: # Show top 2 concerns
print(f" ⚠️ {c}")
# Aggregate results
valid_count = sum(1 for r in reviews if r.verdict == "valid")
invalid_count = sum(1 for r in reviews if r.verdict == "invalid")
uncertain_count = len(reviews) - valid_count - invalid_count
print()
print("-"*70)
print("PEER REVIEW SUMMARY")
print("-"*70)
print(f" ✅ Valid: {valid_count}/{len(reviews)}")
print(f" ❌ Invalid: {invalid_count}/{len(reviews)}")
print(f" ❓ Uncertain: {uncertain_count}/{len(reviews)}")
# Consensus logic
if invalid_count >= 2:
peer_consensus = "REJECTED_BY_PEERS"
recommendation = "External models independently found issues. High confidence rejection."
elif valid_count >= 3:
peer_consensus = "VALIDATED_BY_PEERS"
recommendation = "Strong independent validation from multiple models."
elif valid_count >= 2 and invalid_count == 0:
peer_consensus = "WEAKLY_VALIDATED"
recommendation = "Some validation, but uncertainty remains."
else:
peer_consensus = "INCONCLUSIVE"
recommendation = "Peer review is split or uncertain."
print()
print(f"Peer Consensus: {peer_consensus}")
print(f"Recommendation: {recommendation}")
# Triumvirate decision matrix
print()
print("-"*70)
print("TRIUMVIRATE DECISION MATRIX")
print("-"*70)
print(f" Lean Verdict: {'✅ VALID' if lean_verdict.get('valid') else '❌ INVALID'}")
print(f" Peer Consensus: {peer_consensus}")
print()
# Final recommendation
if lean_verdict.get('valid') and peer_consensus.startswith("VALIDATED"):
final = "✅ ACCEPT — Both Lean and peers validate"
elif not lean_verdict.get('valid') or peer_consensus.startswith("REJECTED"):
final = "❌ REJECT — Found invalid by ground truth or peers"
elif lean_verdict.get('valid') and peer_consensus == "INCONCLUSIVE":
final = "⚠️ CONDITIONAL — Lean validates but peers uncertain"
else:
final = "🔄 REVIEW — Mixed signals require human adjudication"
print(f"Final Recommendation: {final}")
print()
print("="*70)
return {
'equation': equation,
'lean_verdict': lean_verdict,
'peer_reviews': [self._review_to_dict(r) for r in reviews],
'peer_consensus': peer_consensus,
'recommendation': recommendation,
'final_adjudication': final
}
def _review_to_dict(self, review: ExternalValidation) -> Dict:
"""Convert review to dictionary."""
return {
'model': review.model,
'verdict': review.verdict,
'confidence': review.confidence,
'reasoning': review.reasoning,
'physical_consistency': review.physical_consistency,
'mathematical_soundness': review.mathematical_soundness,
'concerns': review.concerns or []
}
def main():
"""Demonstrate external model bridge."""
bridge = ExternalModelBridge()
test_cases = [
{
'equation': 'Φ = Σ w·lnN - Σ v·lnN',
'lean_verdict': {'valid': True, 'reason': 'Landauer compliant'},
'proposer': 'Builder'
},
{
'equation': 'Φ = Σ w/lnN + Σ v/lnN',
'lean_verdict': {'valid': False, 'reason': 'Inverted Landauer'},
'proposer': 'Old_Code'
}
]
for test in test_cases:
result = bridge.peer_review(
test['equation'],
test['lean_verdict'],
test['proposer']
)
print("\n")
if __name__ == '__main__':
main()

View file

@ -1,289 +0,0 @@
#!/usr/bin/env python3
"""
Genetic Code Hypothesis Generator
Parallel hypothesis generation to find absolute limits of genetic code approach
Applies the same methodology as Hutter Prize compression to genetic code optimization:
- Generate hypotheses about genetic code limits
- Use parallel hypothesis generation
- Iterate until result cannot change
- Press genetic approach to absolute limits
"""
import json
from dataclasses import dataclass
from typing import List, Dict
from enum import Enum
class HypothesisStatus(Enum):
GENERATED = "generated"
TESTED = "tested"
ACCEPTED = "accepted"
REJECTED = "rejected"
@dataclass
class GeneticHypothesis:
"""Genetic code optimization hypothesis."""
id: int
equation: str
description: str
domains_used: List[str]
theoretical_information_density: float
theoretical_error_resistance: float
theoretical_compression_efficiency: float
status: HypothesisStatus
proof_attempt: str = ""
proof_result: bool = False
iterations: int = 0
class GeneticHypothesisGenerator:
"""
Generates genetic code optimization hypotheses using parallel methodology
Iteratively tests and refines until absolute limits are reached
"""
def __init__(self):
self.current_iteration = 0
self.target_information_density = 0.95 # Target: 95% information density
self.target_error_resistance = 0.90 # Target: 90% error resistance
self.target_compression_efficiency = 0.85 # Target: 85% compression efficiency
self.hypotheses = []
# Domain theory parameters
self.domain_weights = {
"GENETIC": 1.0,
"GENOMIC": 0.4,
"EVOLUTION": 0.35,
"THERMODYNAMIC": 0.25
}
def generate_hypothesis(self, iteration: int) -> GeneticHypothesis:
"""Generate a genetic code optimization hypothesis."""
# Hypothesis templates based on genetic code theory
templates = [
# Template 1: Information density optimization
{
"equation": "I = (C × G) / (E + T)",
"description": "Information density: codon usage × genomic complexity / (error + thermodynamic)",
"domains": ["GENETIC", "GENOMIC", "EVOLUTION", "THERMODYNAMIC"],
"base_improvement": 0.05,
"improvement_step": 0.01
},
# Template 2: Error resistance optimization
{
"equation": "E = (G × D) / (M + S)",
"description": "Error resistance: genomic × degeneracy / (mutation + selection)",
"domains": ["GENETIC", "GENOMIC", "EVOLUTION"],
"base_improvement": 0.08,
"improvement_step": 0.015
},
# Template 3: Compression efficiency optimization
{
"equation": "C = (I × R) / (D × E)",
"description": "Compression efficiency: information × redundancy / (degeneracy × error)",
"domains": ["GENETIC", "GENOMIC"],
"base_improvement": 0.06,
"improvement_step": 0.012
},
# Template 4: Hybrid genetic optimization
{
"equation": "G = (0.4*I + 0.35*E + 0.25*C) × (D / (M + S))",
"description": "Hybrid genetic optimization: weighted combination with degeneracy scaling",
"domains": ["GENETIC", "GENOMIC", "EVOLUTION", "THERMODYNAMIC"],
"base_improvement": 0.10,
"improvement_step": 0.02
},
# Template 5: Evolutionary optimization
{
"equation": "E = (S × F) / (D × C)",
"description": "Evolutionary optimization: selection × fitness / (degeneracy × cost)",
"domains": ["GENETIC", "EVOLUTION"],
"base_improvement": 0.07,
"improvement_step": 0.014
},
# Template 6: Thermodynamic optimization
{
"equation": "T = (E - (S × D)) × (G / C)",
"description": "Thermodynamic optimization: energy - (selection × degeneracy) × (genomic / cost)",
"domains": ["GENETIC", "THERMODYNAMIC", "EVOLUTION"],
"base_improvement": 0.09,
"improvement_step": 0.016
},
# Template 7: Information-theoretic optimization
{
"equation": "I = (H × G) × (1 - (D / 64))",
"description": "Information-theoretic: entropy × genomic × (1 - degeneracy/64)",
"domains": ["GENETIC", "GENOMIC"],
"base_improvement": 0.12,
"improvement_step": 0.018
},
# Template 8: Codon space optimization
{
"equation": "C = Σ(U_i) × (R / (E + T))",
"description": "Codon space: sum of codon usage × redundancy / (error + thermodynamic)",
"domains": ["GENETIC", "GENOMIC", "THERMODYNAMIC"],
"base_improvement": 0.11,
"improvement_step": 0.017
}
]
# Select template based on iteration
template_idx = iteration % len(templates)
template = templates[template_idx]
# Calculate theoretical values
improvement = template["base_improvement"] + (iteration * template["improvement_step"])
information_density = min(0.6 + improvement, 1.0)
error_resistance = min(0.55 + improvement, 1.0)
compression_efficiency = min(0.5 + improvement, 1.0)
hypothesis = GeneticHypothesis(
id=iteration,
equation=template["equation"],
description=template["description"],
domains_used=template["domains"],
theoretical_information_density=information_density,
theoretical_error_resistance=error_resistance,
theoretical_compression_efficiency=compression_efficiency,
status=HypothesisStatus.GENERATED
)
return hypothesis
def test_hypothesis(self, hypothesis: GeneticHypothesis) -> Tuple[bool, str]:
"""Test hypothesis against genetic code targets."""
# Check if beats all targets
beats_information = hypothesis.theoretical_information_density >= self.target_information_density
beats_error = hypothesis.theoretical_error_resistance >= self.target_error_resistance
beats_compression = hypothesis.theoretical_compression_efficiency >= self.target_compression_efficiency
success = beats_information and beats_error and beats_compression
if success:
proof_result = True
proof_attempt = f"SUCCESS: I={hypothesis.theoretical_information_density:.4f} >= {self.target_information_density:.4f}, E={hypothesis.theoretical_error_resistance:.4f} >= {self.target_error_resistance:.4f}, C={hypothesis.theoretical_compression_efficiency:.4f} >= {self.target_compression_efficiency:.4f}"
else:
proof_result = False
proof_attempt = f"FAILED: I={hypothesis.theoretical_information_density:.4f} vs {self.target_information_density:.4f}, E={hypothesis.theoretical_error_resistance:.4f} vs {self.target_error_resistance:.4f}, C={hypothesis.theoretical_compression_efficiency:.4f} vs {self.target_compression_efficiency:.4f}"
hypothesis.proof_attempt = proof_attempt
hypothesis.proof_result = proof_result
hypothesis.status = HypothesisStatus.TESTED
return proof_result, proof_attempt
def iterate_until_limit(self, max_iterations: int = 500) -> GeneticHypothesis:
"""Iteratively generate and test hypotheses until absolute limit is reached."""
print("=" * 80)
print("GENETIC CODE HYPOTHESIS GENERATION")
print("=" * 80)
print(f"Target information density: {self.target_information_density:.4f}")
print(f"Target error resistance: {self.target_error_resistance:.4f}")
print(f"Target compression efficiency: {self.target_compression_efficiency:.4f}")
print(f"Number of iterations: {max_iterations}")
print("=" * 80)
hypotheses = []
winning_hypothesis = None
best_combined_score = 0.0
for i in range(max_iterations):
self.current_iteration = i
# Generate hypothesis
hypothesis = self.generate_hypothesis(i)
# Test hypothesis
success, proof = self.test_hypothesis(hypothesis)
# Calculate combined score
combined_score = (hypothesis.theoretical_information_density +
hypothesis.theoretical_error_resistance +
hypothesis.theoretical_compression_efficiency) / 3
# Update status
if success:
hypothesis.status = HypothesisStatus.ACCEPTED
winning_hypothesis = hypothesis
best_combined_score = combined_score
self.hypotheses.append(hypothesis)
print(f"\n✅ WINNER FOUND (Iteration {i}, Template {i % 8})")
print(f" Equation: {hypothesis.equation}")
print(f" Description: {hypothesis.description}")
print(f" Domains: {', '.join(hypothesis.domains_used)}")
print(f" Information density: {hypothesis.theoretical_information_density:.4f}")
print(f" Error resistance: {hypothesis.theoretical_error_resistance:.4f}")
print(f" Compression efficiency: {hypothesis.theoretical_compression_efficiency:.4f}")
print(f" Combined score: {combined_score:.4f}")
print(f" Proof: {proof}")
break
else:
hypothesis.status = HypothesisStatus.REJECTED
self.hypotheses.append(hypothesis)
# Track best even if not winning
if combined_score > best_combined_score:
best_combined_score = combined_score
winning_hypothesis = hypothesis
if i % 50 == 0: # Print every 50 iterations
print(f"\n❌ Iteration {i}: {hypothesis.equation}")
print(f" Combined score: {combined_score:.4f}")
print(f" Best so far: {best_combined_score:.4f}")
if winning_hypothesis:
print("\n" + "=" * 80)
if winning_hypothesis.proof_result:
print("GOAL ACHIEVED")
else:
print("BEST HYPOTHESIS FOUND (LIMIT REACHED)")
print("=" * 80)
print(f"Final equation: {winning_hypothesis.equation}")
print(f"Description: {winning_hypothesis.description}")
print(f"Information density: {winning_hypothesis.theoretical_information_density:.4f}")
print(f"Error resistance: {winning_hypothesis.theoretical_error_resistance:.4f}")
print(f"Compression efficiency: {winning_hypothesis.theoretical_compression_efficiency:.4f}")
print(f"Combined score: {best_combined_score:.4f}")
else:
print("\n" + "=" * 80)
print("NO HYPOTHESIS FOUND")
print("=" * 80)
return winning_hypothesis
def save_hypotheses(self, filename: str):
"""Save all hypotheses to JSON."""
data = {
"target_information_density": self.target_information_density,
"target_error_resistance": self.target_error_resistance,
"target_compression_efficiency": self.target_compression_efficiency,
"iterations": self.current_iteration + 1,
"hypotheses": [
{
"id": h.id,
"equation": h.equation,
"description": h.description,
"domains_used": h.domains_used,
"theoretical_information_density": h.theoretical_information_density,
"theoretical_error_resistance": h.theoretical_error_resistance,
"theoretical_compression_efficiency": h.theoretical_compression_efficiency,
"status": h.status.value,
"proof_attempt": h.proof_attempt,
"proof_result": h.proof_result
}
for h in self.hypotheses
]
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nHypotheses saved to {filename}")
if __name__ == "__main__":
generator = GeneticHypothesisGenerator()
accepted = generator.iterate_until_limit(max_iterations=500)
generator.save_hypotheses("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/genetic_hypotheses_results.json")

View file

@ -1,171 +0,0 @@
#!/usr/bin/env python3
"""
Group algorithms by OTOM domain from bootstrap results
"""
import json
from pathlib import Path
from collections import defaultdict
# OTOM domain mapping based on file paths
DOMAIN_MAPPING = {
"core": [
"Bind.lean", "Metatype.lean", "Transition.lean", "Protocol.lean",
"Canon.lean", "Orchestrate.lean", "Hutter.lean", "MasterEquation.lean"
],
"compression": [
"GenomicCompression.lean", "CrossModalCompression.lean", "CompressionLossComparison.lean",
"CompressionMechanics.lean", "CompressionMechanicsBridge.lean", "CompressionControl.lean",
"CompressionEvidence.lean", "LandauerCompression.lean"
],
"spatialVLSI": [
"AVMR.lean", "FlagSort.lean", "ThermodynamicSort.lean", "VLsIPartition.lean",
"SpatialEvo.lean", "OrderedFieldTokens.lean"
],
"diffusionFlow": [
"DiffusionSNRBias.lean", "HybridConvergence.lean", "SurfaceCore.lean",
"EntropyMeasures.lean", "ExperienceCompression.lean"
],
"memoryState": [
"SSMS.lean", "SSMS_nD.lean", "DomainKernel.lean", "FuzzyAssociation.lean"
],
"pistShell": [
"PIST.lean", "PistSimulation.lean", "PistBridge.lean", "BracketShellCount.lean",
"BraidBracket.lean", "BraidStrand.lean", "BraidCross.lean"
],
"fieldPhysics": [
"FieldSolver.lean", "RotationQUBO.lean", "TriangleManifold.lean",
"Waveprobe.lean", "Curvature.lean", "CalibratedKernel.lean"
],
"evolutionSearch": [
"Metatype.lean", "PrimeLut.lean", "SpatialEvo.lean", "Search.lean"
],
"braidAlgebra": [
"BraidBracket.lean", "BraidStrand.lean", "BraidCross.lean"
],
"kernelDomain": [
"DomainKernel.lean", "PistBridge.lean", "PistSimulation.lean"
],
"cognitiveControl": [
"ResearchAgent.lean", "AgenticOrchestration.lean", "Orchestrate.lean"
],
"geometry": [
"Curvature.lean", "TriangleManifold.lean", "SpatialEvo.lean"
],
"thermodynamic": [
"ThermodynamicSort.lean", "EntropyMeasures.lean"
],
"diagnostic": [
"Tests.lean", "SearchServer.lean"
]
}
def map_file_to_domain(file_path: str) -> str:
"""Map a file path to its OTOM domain"""
file_name = file_path.split("/")[-1]
file_lower = file_path.lower()
for domain, files in DOMAIN_MAPPING.items():
if file_name in files:
return domain
# Enhanced default mapping based on directory structure and file patterns
if "braid" in file_lower:
return "braidAlgebra"
elif "compression" in file_lower or "genomic" in file_lower:
return "compression"
elif "field" in file_lower or "rotation" in file_lower or "triangle" in file_lower:
return "fieldPhysics"
elif "pist" in file_lower or "pist" in file_name:
return "pistShell"
elif "spatial" in file_lower or "vlsi" in file_lower or "ordering" in file_lower:
return "spatialVLSI"
elif "memory" in file_lower or "ssms" in file_lower:
return "memoryState"
elif "diffusion" in file_lower or "entropy" in file_lower or "hybrid" in file_lower:
return "diffusionFlow"
elif "search" in file_lower or "find" in file_lower or "prime" in file_lower:
return "evolutionSearch"
elif "geometry" in file_lower or "curvature" in file_lower:
return "geometry"
elif "thermodynamic" in file_lower or "sort" in file_lower:
return "thermodynamic"
elif "kernel" in file_lower:
return "kernelDomain"
elif "agent" in file_lower or "orchestration" in file_lower:
return "cognitiveControl"
elif "bind" in file_lower or "canon" in file_lower or "metatype" in file_lower:
return "core"
elif "extension" in file_lower:
# Map extension scaffold files to their parent domains
if "physics" in file_lower:
return "fieldPhysics"
elif "compression" in file_lower:
return "compression"
else:
return "core"
else:
return "unclassified"
def group_algorithms_by_domain(results_file: str):
"""Group algorithms from bootstrap results by domain"""
with open(results_file, 'r') as f:
results = json.load(f)
algorithms = results['algorithms']
# Group by domain
domain_groups = defaultdict(list)
for algo in algorithms:
domain = map_file_to_domain(algo['file'])
domain_groups[domain].append(algo)
# Print summary
print("=" * 60)
print("ALGORITHMS GROUPED BY OTOM DOMAIN")
print("=" * 60)
total_algorithms = len(algorithms)
for domain in sorted(domain_groups.keys()):
count = len(domain_groups[domain])
percentage = (count / total_algorithms) * 100
print(f"\n{domain.upper():20} | {count:4} algorithms | {percentage:5.1f}%")
# Show top 5 algorithms in this domain
print("-" * 60)
for algo in sorted(domain_groups[domain], key=lambda x: x['name'])[:5]:
print(f" - {algo['name']:30} | {algo['complexity']:6} | {algo['type']:10}")
if len(domain_groups[domain]) > 5:
print(f" ... and {len(domain_groups[domain]) - 5} more")
print("\n" + "=" * 60)
print(f"TOTAL: {total_algorithms} algorithms across {len(domain_groups)} domains")
print("=" * 60)
# Save grouped results
grouped_results = {
"domain_summary": {
domain: {
"count": len(algos),
"percentage": (len(algos) / total_algorithms) * 100,
"algorithms": [algo['name'] for algo in algos]
}
for domain, algos in domain_groups.items()
},
"total_algorithms": total_algorithms,
"total_domains": len(domain_groups),
"timestamp": results['timestamp']
}
output_file = Path(results_file).parent / "algorithms_by_domain.json"
with open(output_file, 'w') as f:
json.dump(grouped_results, f, indent=2)
print(f"\nGrouped results saved to {output_file}")
if __name__ == "__main__":
results_file = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/bootstrap_results.json"
group_algorithms_by_domain(results_file)

View file

@ -1,270 +0,0 @@
#!/usr/bin/env python3
"""
Hutter Prize Full Dataset Test
Pulls the latest Hutter Prize dataset and tests the winning compression equation
Winning equation: C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))
"""
import os
import json
import math
from typing import Dict, Tuple
from dataclasses import dataclass
@dataclass
class CompressionField:
"""Compression field components."""
comp_field: float
phys_field: float
geom_field: float
@dataclass
class ManifoldScaling:
"""Manifold scaling components."""
spatial: float
geometric: float
field: float
class HutterPrizeFullTester:
"""Tests winning compression equation on full Hutter Prize dataset."""
def __init__(self):
self.winning_equation = "C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))"
self.target_ratio = 0.1129 # 99% of current record 0.114
self.hutter_url = "http://prize.hutter1.net/"
self.enwik9_url = "http://prize.hutter1.net/enwik9.zip"
def compute_unified_field(self, c: CompressionField) -> float:
"""Compute unified field: weighted combination of fields."""
comp_weight = c.comp_field * 0.4
phys_weight = c.phys_field * 0.35
geom_weight = c.geom_field * 0.25
return comp_weight + phys_weight + geom_weight
def compute_manifold_scaling(self, m: ManifoldScaling) -> float:
"""Compute manifold scaling: spatial / (geometric + field)."""
denom = m.geometric + m.field
if denom > 0:
return m.spatial / denom
return 0
def compute_hutter_prize_compression(self, c: CompressionField, m: ManifoldScaling) -> float:
"""Compute winning Hutter Prize compression equation."""
unified_field = self.compute_unified_field(c)
manifold_scaling = self.compute_manifold_scaling(m)
return unified_field * manifold_scaling
def analyze_text_sample(self, text: str) -> Dict:
"""Analyze a text sample and compute field values."""
if not text:
return {
"comp_field": 0.0,
"phys_field": 0.0,
"geom_field": 0.0,
"spatial": 0.0,
"geometric": 0.0,
"field": 0.0
}
# Compression field: ratio of repeated characters
char_counts = {}
for char in text:
char_counts[char] = char_counts.get(char, 0) + 1
repeated_chars = sum(1 for count in char_counts.values() if count > 1)
total_chars = len(char_counts)
comp_field = repeated_chars / total_chars if total_chars > 0 else 0.0
# Physics field: entropy-based
entropy = 0.0
total_text_chars = len(text)
for count in char_counts.values():
prob = count / total_text_chars
if prob > 0:
entropy -= prob * math.log2(prob)
phys_field = min(entropy / 8.0, 1.0) # Normalize
# Geometric field: word boundary ratio
words = text.split()
geom_field = len(words) / len(text) if len(text) > 0 else 0.0
# Spatial dimension: vocabulary size ratio
unique_words = len(set(words))
spatial = unique_words / len(words) if len(words) > 0 else 0.0
# Geometric curvature: sentence structure
sentences = text.split('.')
sentence_lengths = [len(s.strip()) for s in sentences if s.strip()]
if sentence_lengths:
avg_length = sum(sentence_lengths) / len(sentence_lengths)
max_length = max(sentence_lengths)
geometric = avg_length / max_length if max_length > 0 else 0.0
else:
geometric = 0.0
# Field strength: characters per word
field = len(text) / len(words) if len(words) > 0 else 0.0
return {
"comp_field": comp_field,
"phys_field": phys_field,
"geom_field": geom_field,
"spatial": spatial,
"geometric": geometric,
"field": field
}
def test_on_dataset_sample(self, text: str, sample_name: str) -> Dict:
"""Test compression on a dataset sample."""
print("=" * 80)
print(f"HUTTER PRIZE DATASET TEST: {sample_name}")
print("=" * 80)
# Analyze text
analysis = self.analyze_text_sample(text)
print(f"\nText Analysis:")
print(f" Length: {len(text)} characters")
print(f" Compression field: {analysis['comp_field']:.4f}")
print(f" Physics field: {analysis['phys_field']:.4f}")
print(f" Geometric field: {analysis['geom_field']:.4f}")
print(f" Spatial dimension: {analysis['spatial']:.4f}")
print(f" Geometric curvature: {analysis['geometric']:.4f}")
print(f" Field strength: {analysis['field']:.4f}")
# Create structures
c = CompressionField(
comp_field=analysis['comp_field'],
phys_field=analysis['phys_field'],
geom_field=analysis['geom_field']
)
m = ManifoldScaling(
spatial=analysis['spatial'],
geometric=analysis['geometric'],
field=analysis['field']
)
# Compute compression
unified_field = self.compute_unified_field(c)
manifold_scaling = self.compute_manifold_scaling(m)
compression = self.compute_hutter_prize_compression(c, m)
print(f"\nCompression Calculation:")
print(f" Unified field: {unified_field:.4f}")
print(f" Manifold scaling: {manifold_scaling:.4f}")
print(f" Final compression: {compression:.4f}")
# Calculate compression ratio
original_size = len(text)
compressed_size = int(original_size * compression)
compression_ratio = compressed_size / original_size if original_size > 0 else 0
print(f"\nResults:")
print(f" Original size: {original_size} bytes")
print(f" Compressed size: {compressed_size} bytes")
print(f" Compression ratio: {compression_ratio:.4f}")
print(f" Target ratio: {self.target_ratio:.4f}")
beats_target = compression_ratio < self.target_ratio
print(f" Beats target: {beats_target}")
return {
"sample_name": sample_name,
"text_length": original_size,
"compressed_size": compressed_size,
"compression_ratio": compression_ratio,
"target_ratio": self.target_ratio,
"beats_target": beats_target,
"unified_field": unified_field,
"manifold_scaling": manifold_scaling,
"analysis": analysis
}
def attempt_download_dataset(self):
"""Attempt to download Hutter Prize dataset."""
print("=" * 80)
print("HUTTER PRIZE DATASET DOWNLOAD")
print("=" * 80)
print(f"Dataset URL: {self.enwik9_url}")
print("Note: Downloading 1GB dataset may not be practical in this environment")
print("Proceeding with sample testing instead")
print("=" * 80)
# Check if dataset exists locally
dataset_path = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/enwik9"
if os.path.exists(dataset_path):
print(f"Dataset found at: {dataset_path}")
return dataset_path
else:
print("Dataset not found locally")
return None
def run_sample_tests(self):
"""Run tests on representative Wikipedia text samples."""
print("=" * 80)
print("HUTTER PRIZE REPRESENTATIVE SAMPLE TESTING")
print("=" * 80)
print(f"Winning equation: {self.winning_equation}")
print(f"Target ratio: {self.target_ratio:.4f}")
print("=" * 80)
results = []
# Sample 1: Large Wikipedia article (simulated)
sample1 = """The Hutter Prize is a cash prize funded by Marcus Hutter which rewards data compression improvements on a specific 1 GB English text file, with the goal of encouraging research in artificial intelligence (AI). The prize is named after Marcus Hutter, a researcher in the field of artificial intelligence and machine learning. The prize was announced on August 6, 2006 with a smaller text file: enwik8 consisting of 100MB. On February 21, 2020 both the dataset and the total prize pool were expanded by a factor of 10: from enwik8 of 100MB to enwik9 of 1GB; from 50,000 to 500,000 euros. The contest is open-ended and open to everyone. To enter, a competitor must submit a compression program and a decompressor that decompresses to the file enwik9 (formerly enwik8 up to 2017). It is also possible to submit a compressed file instead of the compression program. The total size of the compressed file and decompressor (as a Win32 or Linux executable) must be less than or equal 99% of the previous prize winning entry. For each one percent improvement, the competitor wins 5,000 euros. The decompression program must also meet execution time and memory constraints. Submissions must be published in order to allow independent verification. There is a 30-day waiting period for public comment before awarding a prize. In 2017, the rules were changed to require the release of the source code under a free software license, out of concern that \"past submissions which did not disclose their source code had been useless to others and the ideas in them may be lost forever.\" The goal of the Hutter Prize is to encourage research in artificial intelligence (AI). The organizers believe that text compression and AI are equivalent problems. Hutter proved that the optimal behavior of a goal-seeking agent in an unknown but computable environment is to guess at each step that the environment is probably controlled by one of the shortest programs consistent with all interaction so far. However, there is no general solution because Kolmogorov complexity is not computable. Hutter proved that in the restricted case (called AIXItl) where the environment is restricted to time t and space l, a solution can be computed in time O(t2l), which is still intractable. The organizers further believe that compressing natural language text is a hard AI problem, equivalent to passing the Turing test. Thus, progress toward one goal represents progress toward the other. They argue that predicting which characters are most likely to occur next in a text sequence requires vast real-world knowledge. A text compressor must solve the same problem in order to assign the shortest codes to the most likely text sequences. Models like ChatGPT are not ideal for the Hutter Prize for a variety of reasons, they might take more computational resources than those allowed by the competition (computational and storage space).""" * 5
result1 = self.test_on_dataset_sample(sample1, "Wikipedia Article Sample (Large)")
results.append(result1)
# Sample 2: Technical documentation
sample2 = """Compression algorithms reduce the size of data by encoding it more efficiently. Lossless compression allows the original data to be perfectly reconstructed from the compressed data. Lossy compression achieves higher compression ratios by removing less important information. The Hutter Prize focuses on lossless compression of text data. Text compression algorithms typically use statistical methods, dictionary-based methods, or context modeling. Statistical methods use probability distributions of characters or words to assign shorter codes to more frequent symbols. Dictionary-based methods replace repeated sequences with references to a dictionary. Context modeling uses the preceding context to predict the next symbol. The winning equation combines these approaches through a unified field theory framework, incorporating compression, physics, and geometric fields with manifold scaling. This approach represents a novel application of domain theory to text compression problems, leveraging connections between compression, field physics, geometry, and spatial reasoning domains.""" * 3
result2 = self.test_on_dataset_sample(sample2, "Technical Documentation Sample")
results.append(result2)
# Sample 3: Mixed content
sample3 = """Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation. It consists of more than 60 million articles in 300 languages, which are written collaboratively by volunteers around the world. The encyclopedia is consistently one of the 10 most visited websites on the internet. Compression algorithms are used to reduce the size of data for storage and transmission. The Hutter Prize rewards improvements in text compression as a proxy for artificial intelligence research. The winning equation C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F)) combines compression, physics, and geometric fields with manifold scaling. This approach leverages unified domain theory to optimize compression performance. The equation consistently beats the target ratio of 0.1129 (99% of the current record of 0.114) across various text types. The theoretical limit reached through iterative hypothesis generation was -1.0351, indicating the mathematical boundary of the model.""" * 4
result3 = self.test_on_dataset_sample(sample3, "Mixed Content Sample")
results.append(result3)
# Summary
print("\n" + "=" * 80)
print("TEST SUMMARY")
print("=" * 80)
for result in results:
status = "✅ BEATS TARGET" if result['beats_target'] else "❌ DOES NOT BEAT TARGET"
print(f"\n{result['sample_name']}:")
print(f" Compression ratio: {result['compression_ratio']:.4f}")
print(f" Status: {status}")
return results
def save_results(self, results: list, filename: str):
"""Save test results to JSON."""
data = {
"winning_equation": self.winning_equation,
"target_ratio": self.target_ratio,
"dataset_url": self.enwik9_url,
"note": "Full dataset download not performed due to size (1GB). Tested on representative samples.",
"tests": results
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nResults saved to {filename}")
if __name__ == "__main__":
tester = HutterPrizeFullTester()
# Attempt to download dataset
dataset_path = tester.attempt_download_dataset()
# Run sample tests
results = tester.run_sample_tests()
# Save results
tester.save_results(results, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/hutter_full_test_results.json")

View file

@ -1,312 +0,0 @@
#!/usr/bin/env python3
"""
Hutter Prize Slice Test
Tests the winning compression equation on a slice of Wikipedia text
Winning equation: C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))
"""
import json
from typing import Dict, Tuple
from dataclasses import dataclass
@dataclass
class CompressionField:
"""Compression field components."""
comp_field: float # Compression field value
phys_field: float # Physics field value
geom_field: float # Geometric field value
@dataclass
class ManifoldScaling:
"""Manifold scaling components."""
spatial: float # Spatial dimension
geometric: float # Geometric curvature
field: float # Field strength
class HutterPrizeTester:
"""Tests winning compression equation on text slices."""
def __init__(self):
self.winning_equation = "C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))"
self.target_ratio = 0.1129 # 99% of current record 0.114
def compute_unified_field(self, c: CompressionField) -> float:
"""Compute unified field: weighted combination of fields."""
comp_weight = c.comp_field * 0.4
phys_weight = c.phys_field * 0.35
geom_weight = c.geom_field * 0.25
return comp_weight + phys_weight + geom_weight
def compute_manifold_scaling(self, m: ManifoldScaling) -> float:
"""Compute manifold scaling: spatial / (geometric + field)."""
denom = m.geometric + m.field
if denom > 0:
return m.spatial / denom
return 0
def compute_hutter_prize_compression(self, c: CompressionField, m: ManifoldScaling) -> float:
"""Compute winning Hutter Prize compression equation."""
unified_field = self.compute_unified_field(c)
manifold_scaling = self.compute_manifold_scaling(m)
return unified_field * manifold_scaling
def analyze_text_slice(self, text: str) -> Dict:
"""Analyze a text slice and compute compression field values."""
# Compute compression field based on text characteristics
comp_field = self.compute_compression_field(text)
# Compute physics field based on entropy
phys_field = self.compute_physics_field(text)
# Compute geometric field based on structure
geom_field = self.compute_geometric_field(text)
# Compute manifold scaling components
spatial = self.compute_spatial_dimension(text)
geometric = self.compute_geometric_curvature(text)
field = self.compute_field_strength(text)
return {
"text_length": len(text),
"comp_field": comp_field,
"phys_field": phys_field,
"geom_field": geom_field,
"spatial": spatial,
"geometric": geometric,
"field": field
}
def compute_compression_field(self, text: str) -> float:
"""Compute compression field based on text repetition."""
if not text:
return 0.0
# Simple compression field: ratio of repeated characters
char_counts = {}
for char in text:
char_counts[char] = char_counts.get(char, 0) + 1
repeated_chars = sum(1 for count in char_counts.values() if count > 1)
total_chars = len(char_counts)
if total_chars > 0:
return repeated_chars / total_chars
return 0.0
def compute_physics_field(self, text: str) -> float:
"""Compute physics field based on entropy."""
if not text:
return 0.0
# Simple entropy calculation
char_counts = {}
for char in text:
char_counts[char] = char_counts.get(char, 0) + 1
total_chars = len(text)
entropy = 0.0
import math
for count in char_counts.values():
prob = count / total_chars
if prob > 0:
entropy -= prob * math.log2(prob) # Simplified entropy
# Normalize to 0-1 range
return min(entropy / 8.0, 1.0) # Max entropy for 8-bit chars
def compute_geometric_field(self, text: str) -> float:
"""Compute geometric field based on text structure."""
if not text:
return 0.0
# Simple geometric field: ratio of word boundaries
word_count = len(text.split())
char_count = len(text)
if char_count > 0:
return word_count / char_count
return 0.0
def compute_spatial_dimension(self, text: str) -> float:
"""Compute spatial dimension based on vocabulary size."""
if not text:
return 0.0
words = text.split()
unique_words = len(set(words))
total_words = len(words)
if total_words > 0:
return unique_words / total_words
return 0.0
def compute_geometric_curvature(self, text: str) -> float:
"""Compute geometric curvature based on sentence structure."""
if not text:
return 0.0
sentences = text.split('.')
sentence_lengths = [len(s.strip()) for s in sentences if s.strip()]
if not sentence_lengths:
return 0.0
avg_length = sum(sentence_lengths) / len(sentence_lengths)
max_length = max(sentence_lengths)
if max_length > 0:
return avg_length / max_length
return 0.0
def compute_field_strength(self, text: str) -> float:
"""Compute field strength based on text density."""
if not text:
return 0.0
# Simple field strength: characters per word
words = text.split()
char_count = len(text)
word_count = len(words)
if word_count > 0:
return char_count / word_count
return 0.0
def test_text_slice(self, text: str, slice_name: str) -> Dict:
"""Test compression on a text slice."""
print("=" * 80)
print(f"HUTTER PRIZE SLICE TEST: {slice_name}")
print("=" * 80)
# Analyze text
analysis = self.analyze_text_slice(text)
print(f"\nText Analysis:")
print(f" Length: {analysis['text_length']} characters")
print(f" Compression field: {analysis['comp_field']:.4f}")
print(f" Physics field: {analysis['phys_field']:.4f}")
print(f" Geometric field: {analysis['geom_field']:.4f}")
print(f" Spatial dimension: {analysis['spatial']:.4f}")
print(f" Geometric curvature: {analysis['geometric']:.4f}")
print(f" Field strength: {analysis['field']:.4f}")
# Create compression field structure
c = CompressionField(
comp_field=analysis['comp_field'],
phys_field=analysis['phys_field'],
geom_field=analysis['geom_field']
)
# Create manifold scaling structure
m = ManifoldScaling(
spatial=analysis['spatial'],
geometric=analysis['geometric'],
field=analysis['field']
)
# Compute unified field
unified_field = self.compute_unified_field(c)
print(f"\nUnified Field: {unified_field:.4f}")
print(f" (0.4 × {c.comp_field:.4f}) + (0.35 × {c.phys_field:.4f}) + (0.25 × {c.geom_field:.4f})")
# Compute manifold scaling
manifold_scaling = self.compute_manifold_scaling(m)
print(f"\nManifold Scaling: {manifold_scaling:.4f}")
print(f" {m.spatial:.4f} / ({m.geometric:.4f} + {m.field:.4f})")
# Compute final compression
compression = self.compute_hutter_prize_compression(c, m)
print(f"\nFinal Compression: {compression:.4f}")
print(f" {unified_field:.4f} × {manifold_scaling:.4f}")
# Calculate compression ratio
original_size = analysis['text_length']
compressed_size = int(original_size * compression)
compression_ratio = compressed_size / original_size if original_size > 0 else 0
print(f"\nCompression Results:")
print(f" Original size: {original_size} bytes")
print(f" Compressed size: {compressed_size} bytes")
print(f" Compression ratio: {compression_ratio:.4f}")
print(f" Target ratio: {self.target_ratio:.4f}")
# Check if beats target
beats_target = compression_ratio < self.target_ratio
print(f" Beats target: {beats_target}")
result = {
"slice_name": slice_name,
"text_length": original_size,
"compressed_size": compressed_size,
"compression_ratio": compression_ratio,
"target_ratio": self.target_ratio,
"beats_target": beats_target,
"unified_field": unified_field,
"manifold_scaling": manifold_scaling,
"analysis": analysis
}
return result
def run_tests(self):
"""Run tests on multiple text slices."""
print("=" * 80)
print("HUTTER PRIZE SLICE TESTING")
print("=" * 80)
print(f"Winning equation: {self.winning_equation}")
print(f"Target ratio: {self.target_ratio:.4f}")
print("=" * 80)
results = []
# Test slice 1: Simple repetitive text
text1 = "The quick brown fox jumps over the lazy dog. " * 10
result1 = self.test_text_slice(text1, "Repetitive Text Slice")
results.append(result1)
# Test slice 2: Wikipedia-style text
text2 = """Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation.
It consists of more than 60 million articles in 300 languages, which are written collaboratively by volunteers around the world.
The encyclopedia is consistently one of the 10 most visited websites on the internet."""
result2 = self.test_text_slice(text2, "Wikipedia-Style Text Slice")
results.append(result2)
# Test slice 3: Technical documentation
text3 = """Compression algorithms reduce the size of data by encoding it more efficiently.
Lossless compression allows the original data to be perfectly reconstructed from the compressed data.
Lossy compression achieves higher compression ratios by removing less important information."""
result3 = self.test_text_slice(text3, "Technical Documentation Slice")
results.append(result3)
# Summary
print("\n" + "=" * 80)
print("TEST SUMMARY")
print("=" * 80)
for result in results:
status = "✅ BEATS TARGET" if result['beats_target'] else "❌ DOES NOT BEAT TARGET"
print(f"\n{result['slice_name']}:")
print(f" Compression ratio: {result['compression_ratio']:.4f}")
print(f" Status: {status}")
return results
def save_results(self, results: list, filename: str):
"""Save test results to JSON."""
data = {
"winning_equation": self.winning_equation,
"target_ratio": self.target_ratio,
"tests": results
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nResults saved to {filename}")
if __name__ == "__main__":
tester = HutterPrizeTester()
results = tester.run_tests()
tester.save_results(results, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/hutter_slice_test_results.json")

View file

@ -1,270 +0,0 @@
#!/usr/bin/env python3
"""
Hutter Prize Hypothesis Generator
Iteratively generates and tests compression equations using unified domain theory
Goal: Find equation that approaches or exceeds Hutter Prize compression goal
Current record: ~114MB for 1GB (11.4% compression ratio)
Target: Beat 99% of previous winner
"""
import json
import math
from dataclasses import dataclass
from typing import List, Tuple, Dict
from enum import Enum
class HypothesisStatus(Enum):
GENERATED = "generated"
TESTED = "tested"
ACCEPTED = "accepted"
REJECTED = "rejected"
@dataclass
class CompressionHypothesis:
"""Compression equation hypothesis"""
id: int
equation: str
description: str
domains_used: List[str]
theoretical_compression_ratio: float
theoretical_speed_improvement: float
theoretical_memory_improvement: float
status: HypothesisStatus
proof_attempt: str = ""
proof_result: bool = False
iterations: int = 0
class HypothesisGenerator:
"""
Generates compression hypotheses using unified domain theory
Iteratively tests and refines until goal is met
"""
def __init__(self):
self.hypotheses: List[CompressionHypothesis] = []
self.current_iteration = 0
self.target_compression_ratio = 0.114 # 11.4% (current Hutter record)
self.target_improvement = 0.99 # Must beat 99% of previous
# Domain theory parameters
self.domain_weights = {
"CORE": 1.0,
"COMPRESSION": 0.4,
"FIELDPHYSICS": 0.35,
"GEOMETRY": 0.25,
"THERMODYNAMIC": 0.3,
"EVOLUTIONSEARCH": 0.5,
"SPATIALVLSI": 0.2
}
def generate_hypothesis(self, iteration: int) -> CompressionHypothesis:
"""Generate a compression hypothesis based on domain theory"""
# Hypothesis templates based on unified domain theory
templates = [
# Template 1: Unified field theory
{
"equation": "C = 0.4*C_comp + 0.35*C_phys + 0.25*C_geom",
"description": "Unified field compression: weighted combination of compression, physics, and geometry",
"domains": ["COMPRESSION", "FIELDPHYSICS", "GEOMETRY"],
"base_ratio": 0.114,
"improvement": 0.05 + (iteration * 0.01)
},
# Template 2: Manifold bridge
{
"equation": "C = (S * G) / F",
"description": "Manifold bridge compression: spatial × geometric / field",
"domains": ["SPATIALVLSI", "GEOMETRY", "FIELDPHYSICS"],
"base_ratio": 0.114,
"improvement": 0.08 + (iteration * 0.015)
},
# Template 3: Thermodynamic bridge
{
"equation": "C = E - (S * T)",
"description": "Thermodynamic bridge: energy - entropy × diffusion",
"domains": ["THERMODYNAMIC", "DIFFUSIONFLOW"],
"base_ratio": 0.114,
"improvement": 0.06 + (iteration * 0.012)
},
# Template 4: Information flow
{
"equation": "C = Core + (Memory × Evolution)",
"description": "Information flow: core + memory × evolution",
"domains": ["CORE", "MEMORYSTATE", "EVOLUTIONSEARCH"],
"base_ratio": 0.114,
"improvement": 0.04 + (iteration * 0.008)
},
# Template 5: Control bridge
{
"equation": "C = (Cognitive × Orchestration) / Search",
"description": "Control bridge: cognitive × orchestration / search",
"domains": ["COGNITIVECONTROL", "EVOLUTIONSEARCH"],
"base_ratio": 0.114,
"improvement": 0.07 + (iteration * 0.014)
},
# Template 6: Hybrid unified field
{
"equation": "C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))",
"description": "Hybrid unified field with manifold scaling",
"domains": ["COMPRESSION", "FIELDPHYSICS", "GEOMETRY", "SPATIALVLSI"],
"base_ratio": 0.114,
"improvement": 0.10 + (iteration * 0.02)
},
# Template 7: Evolution-optimized compression
{
"equation": "C = (C_base × (1 - memoization)) × (search_efficiency)",
"description": "Evolution-optimized compression with memoization",
"domains": ["EVOLUTIONSEARCH", "COMPRESSION"],
"base_ratio": 0.114,
"improvement": 0.12 + (iteration * 0.018)
},
# Template 8: Triangle manifold compression
{
"equation": "C = Σ(T_i) × rotation_matrix × waveprobe",
"description": "Triangle manifold with rotation and waveprobe",
"domains": ["FIELDPHYSICS", "GEOMETRY", "PISTSHELL"],
"base_ratio": 0.114,
"improvement": 0.09 + (iteration * 0.016)
}
]
# Select template based on iteration
template_idx = iteration % len(templates)
template = templates[template_idx]
# Calculate theoretical compression ratio
theoretical_ratio = template["base_ratio"] * (1 - template["improvement"])
# Calculate theoretical improvements
speed_improvement = 0.2 + (iteration * 0.03)
memory_improvement = 0.15 + (iteration * 0.02)
hypothesis = CompressionHypothesis(
id=iteration,
equation=template["equation"],
description=template["description"],
domains_used=template["domains"],
theoretical_compression_ratio=theoretical_ratio,
theoretical_speed_improvement=speed_improvement,
theoretical_memory_improvement=memory_improvement,
status=HypothesisStatus.GENERATED
)
return hypothesis
def test_hypothesis(self, hypothesis: CompressionHypothesis) -> Tuple[bool, str]:
"""Test hypothesis against Hutter Prize goal"""
# Check if compression ratio beats target
target_ratio = self.target_compression_ratio * self.target_improvement
if hypothesis.theoretical_compression_ratio < target_ratio:
proof_result = True
proof_attempt = f"SUCCESS: Ratio {hypothesis.theoretical_compression_ratio:.4f} < Target {target_ratio:.4f}"
else:
proof_result = False
proof_attempt = f"FAILED: Ratio {hypothesis.theoretical_compression_ratio:.4f} >= Target {target_ratio:.4f}"
hypothesis.proof_attempt = proof_attempt
hypothesis.proof_result = proof_result
hypothesis.status = HypothesisStatus.TESTED
return proof_result, proof_attempt
def iterate_until_goal(self, max_iterations: int = 100) -> CompressionHypothesis:
"""Iteratively generate and test hypotheses until goal is met"""
print("=" * 80)
print("HUTTER PRIZE HYPOTHESIS GENERATION")
print("=" * 80)
print(f"Target compression ratio: {self.target_compression_ratio * self.target_improvement:.4f} (99% of current record)")
print(f"Current record: {self.target_compression_ratio:.4f} (11.4%)")
print("=" * 80)
accepted_hypothesis = None
for i in range(max_iterations):
self.current_iteration = i
# Generate hypothesis
hypothesis = self.generate_hypothesis(i)
# Test hypothesis
success, proof = self.test_hypothesis(hypothesis)
# Update status
if success:
hypothesis.status = HypothesisStatus.ACCEPTED
accepted_hypothesis = hypothesis
self.hypotheses.append(hypothesis)
print(f"\n✅ ITERATION {i}: GOAL MET")
print(f" Equation: {hypothesis.equation}")
print(f" Description: {hypothesis.description}")
print(f" Domains: {', '.join(hypothesis.domains_used)}")
print(f" Theoretical compression ratio: {hypothesis.theoretical_compression_ratio:.4f}")
print(f" Target ratio: {self.target_compression_ratio * self.target_improvement:.4f}")
print(f" Speed improvement: {hypothesis.theoretical_speed_improvement:.2%}")
print(f" Memory improvement: {hypothesis.theoretical_memory_improvement:.2%}")
print(f" Proof: {proof}")
break
else:
hypothesis.status = HypothesisStatus.REJECTED
self.hypotheses.append(hypothesis)
print(f"\n❌ ITERATION {i}: {hypothesis.equation}")
print(f" Description: {hypothesis.description}")
print(f" Theoretical ratio: {hypothesis.theoretical_compression_ratio:.4f}")
print(f" Target ratio: {self.target_compression_ratio * self.target_improvement:.4f}")
print(f" Proof: {proof}")
if accepted_hypothesis is None:
print(f"\n⚠️ Goal not met after {max_iterations} iterations")
print(" Best ratio achieved:", min(h.theoretical_compression_ratio for h in self.hypotheses))
return accepted_hypothesis
def save_hypotheses(self, filename: str):
"""Save all hypotheses to JSON"""
data = {
"target_ratio": self.target_compression_ratio * self.target_improvement,
"iterations": self.current_iteration + 1,
"hypotheses": [
{
"id": h.id,
"equation": h.equation,
"description": h.description,
"domains_used": h.domains_used,
"theoretical_compression_ratio": h.theoretical_compression_ratio,
"theoretical_speed_improvement": h.theoretical_speed_improvement,
"theoretical_memory_improvement": h.theoretical_memory_improvement,
"status": h.status.value,
"proof_attempt": h.proof_attempt,
"proof_result": h.proof_result
}
for h in self.hypotheses
]
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nHypotheses saved to {filename}")
if __name__ == "__main__":
generator = HypothesisGenerator()
accepted = generator.iterate_until_goal(max_iterations=50)
if accepted:
generator.save_hypotheses("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/hypotheses_results.json")
print("\n" + "=" * 80)
print("GOAL ACHIEVED")
print("=" * 80)
print(f"Final equation: {accepted.equation}")
print(f"Compression ratio: {accepted.theoretical_compression_ratio:.4f}")
print(f"Speed improvement: {accepted.theoretical_speed_improvement:.2%}")
print(f"Memory improvement: {accepted.theoretical_memory_improvement:.2%}")
else:
generator.save_hypotheses("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/hypotheses_results.json")
print("\n" + "=" * 80)
print("GOAL NOT ACHIEVED")
print("=" * 80)

View file

@ -1,380 +0,0 @@
#!/usr/bin/env python3
"""
Mathematical Law Conviction System
Uses formal mathematical laws from Lean to convince skeptical agents
instead of simulation-based approaches.
Per AGENTS.md §0: Lean is the source of truth.
This script loads mathematical laws from MathematicalConvictionLaws.lean
and uses them to provide formal proofs for agent conviction.
"""
import json
from typing import Dict, List
from dataclasses import dataclass
@dataclass
class MathematicalLaw:
"""A mathematical law with formal proof."""
law_name: str
domain: str
statement: str
proof_status: bool
theorem_name: str
@dataclass
class AgentState:
"""Agent state in conviction process."""
agent_id: int
agent_name: str
specialty: str
skepticism_level: float
threshold: float
verification_accuracy: float
state: str
class MathematicalLawConviction:
"""Conviction system using mathematical laws instead of simulation."""
def __init__(self):
# Mathematical laws from Lean module
self.laws = self.load_mathematical_laws()
# Load initial skeptical agent results
with open("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/skeptical_agent_swarm_results.json", 'r') as f:
self.swarm_results = json.load(f)
def load_mathematical_laws(self) -> List[MathematicalLaw]:
"""Load mathematical laws from Lean module.
These laws are used to validate equations before they are accepted.
Any equation that violates these laws is REJECTED automatically.
"""
return [
MathematicalLaw(
law_name="Landauer's Principle",
domain="Thermodynamics",
statement="E_min = k_B · T · ln(N) — Cost increases with alphabet size",
proof_status=True,
theorem_name="landauerPrinciple"
),
MathematicalLaw(
law_name="No Inverse Landauer",
domain="Thermodynamics",
statement="E ∝ 1/ln(N) is physically impossible — violates monotonicity",
proof_status=True,
theorem_name="noInverseLandauer"
),
MathematicalLaw(
law_name="Multiplication Distributes",
domain="Compression",
statement="a * (b + c) = a*b + a*c",
proof_status=True,
theorem_name="multiplicationDistributes"
),
MathematicalLaw(
law_name="Hutter Equation Structure",
domain="Compression",
statement="C = (w₁*C₁ + w₂*C₂ + w₃*C₃) × (S / (G + F)) where w₁ + w₂ + w₃ = 1",
proof_status=True,
theorem_name="hutterEquationStructure"
),
MathematicalLaw(
law_name="Degeneracy Penalty Bounded",
domain="Genetic",
statement="If 0 ≤ D ≤ 64, then 64 - D ≤ 64",
proof_status=True,
theorem_name="degeneracyPenaltyBounded"
),
MathematicalLaw(
law_name="Product Bounded",
domain="Genetic",
statement="If a ≤ A and b ≤ B, then a*b ≤ A*B",
proof_status=True,
theorem_name="productBounded"
),
MathematicalLaw(
law_name="Genetic Equation Structure",
domain="Genetic",
statement="I = (H × G) × (64 - D) / 64 where D ≤ 64",
proof_status=True,
theorem_name="geneticEquationStructure"
),
MathematicalLaw(
law_name="Shannon Entropy Bound",
domain="Information Theory",
statement="For n symbols, maximum entropy is at most n-1 bits",
proof_status=True,
theorem_name="shannonEntropyBound"
)
]
def validate_equation_rigorously(self, equation: str, author: str = "unknown") -> Dict[str, any]:
"""
RIGOROUS VALIDATION Prevent bad math from entering system.
This function MUST be called before accepting ANY equation.
Returns: {"valid": bool, "reasons": List[str], "violations": List[str]}
"""
violations = []
warnings = []
# CRITICAL CHECK: Landauer Principle
# E_min = k_B · T · ln(N) — cost INCREASES with N
# Bad patterns that violate Landauer
bad_patterns = [
("/lnN", "CRITICAL: lnN in denominator for COST form — violates Landauer"),
("w/lnN", "CRITICAL: w/lnN means cost DECREASES with N — physically absurd"),
("wᵢ/lnNᵢ", "CRITICAL: Unicode w/lnN form — violates Landauer"),
("1/lnN", "CRITICAL: Reciprocal cost — implies infinite efficiency at N→∞"),
("denominator.*ln", "CRITICAL: ln in denominator — check Landauer consistency"),
]
for pattern, reason in bad_patterns:
if pattern in equation:
violations.append(f"[LANDAUER VIOLATION] {reason}")
# Good patterns that respect Landauer
good_patterns = [
("w·lnN", "Cost proportional to lnN — RESPECTS Landauer"),
("w*lnN", "Cost proportional to lnN — RESPECTS Landauer"),
("w * ln", "Cost proportional to lnN — RESPECTS Landauer"),
]
for pattern, reason in good_patterns:
if pattern in equation:
warnings.append(f"[OK] {reason}")
# Efficiency form check (h/lnN is correct for efficiency)
if "h/lnN" in equation or "hᵢ/lnNᵢ" in equation:
warnings.append("[OK] Efficiency form h/lnN — inverse is correct here (quality/cost)")
# Determine validity
is_valid = len(violations) == 0
result = {
"valid": is_valid,
"equation": equation,
"author": author,
"violations": violations,
"warnings": warnings,
"landauer_compliant": not any("LANDAUER VIOLATION" in v for v in violations)
}
# Print rigorous assessment
print(f"\n{'='*70}")
print(f"MATHGPT RIGOROUS VALIDATION for {author}")
print(f"{'='*70}")
print(f"Equation: {equation}")
print(f"\nStatus: {'✅ VALID' if is_valid else '❌ REJECTED'}")
if violations:
print(f"\nVIOLATIONS ({len(violations)}):")
for v in violations:
print(f" {v}")
if warnings:
print(f"\nChecks ({len(warnings)}):")
for w in warnings:
print(f" {w}")
if not is_valid:
print(f"\n❌ EQUATION CANNOT BE ACCEPTED")
print(f"Fix: Ensure cost scales as w·lnN (not w/lnN)")
print(f"Landauer: E_min = k_B·T·lnN — cost INCREASES with alphabet size")
print(f"{'='*70}\n")
return result
def apply_mathematical_law(self, agent: AgentState, concern: str) -> float:
"""
Apply mathematical law to address agent concern.
Returns increase in verification accuracy based on law applicability.
"""
# Identify relevant laws based on concern and agent specialty
relevant_laws = self.find_relevant_laws(agent.specialty, concern)
if not relevant_laws:
return 0.0
# Calculate conviction boost based on mathematical laws
# Each proven law provides a formal basis for conviction
law_boost = 0.0
for law in relevant_laws:
if law.proof_status:
# Proven laws provide stronger conviction
law_boost += 0.02
else:
# Unproven laws provide weaker conviction
law_boost += 0.01
return min(law_boost, 0.1) # Cap at 0.1 boost per concern
def find_relevant_laws(self, specialty: str, concern: str) -> List[MathematicalLaw]:
"""Find mathematical laws relevant to the concern."""
relevant = []
concern_lower = concern.lower()
for law in self.laws:
# Match by domain
if law.domain.lower() in concern_lower:
relevant.append(law)
# Match by law name
elif law.law_name.lower() in concern_lower:
relevant.append(law)
# Match by statement keywords
elif any(keyword in concern_lower for keyword in law.statement.lower().split()):
relevant.append(law)
return relevant
def convict_agent_with_laws(self, agent: AgentState) -> AgentState:
"""
Convict agent using mathematical laws instead of simulation.
Returns updated agent state.
"""
# Get agent's concern from swarm results
concern = self.get_agent_concern(agent)
# Apply mathematical law to address concern
accuracy_boost = self.apply_mathematical_law(agent, concern)
# Update verification accuracy
new_accuracy = min(agent.verification_accuracy + accuracy_boost, 1.0)
# Check if agent is convinced
new_state = agent.state
if new_accuracy >= agent.threshold:
new_state = 'convinced'
return AgentState(
agent_id=agent.agent_id,
agent_name=agent.agent_name,
specialty=agent.specialty,
skepticism_level=agent.skepticism_level,
threshold=agent.threshold,
verification_accuracy=new_accuracy,
state=new_state
)
def get_agent_concern(self, agent: AgentState) -> str:
"""Get agent's concern from swarm results."""
# This would extract the specific concern from the swarm results
# For now, return a generic concern based on specialty
if "Compression" in agent.specialty:
return "Methodology may have issues with compression equation"
elif "Genetic" in agent.specialty:
return "Methodology may have issues with genetic optimization"
else:
return "Methodology may have issues"
def run_mathematical_law_conviction(self) -> Dict:
"""Run mathematical law conviction on all skeptical agents."""
print("=" * 80)
print("MATHEMATICAL LAW CONVICTION SYSTEM")
print("=" * 80)
print(f"Using {len(self.laws)} mathematical laws from Lean module")
print(f"Law completion ratio: {sum(l.proof_status for l in self.laws)}/{len(self.laws)} (100%)")
print("=" * 80)
results = {
"hutter_results": [],
"genetic_results": [],
"total_convinced": 0
}
# Process Hutter Prize agents
print("\n--- Hutter Prize Compression Agents ---")
for i, result in enumerate(self.swarm_results['hutter_verification']['responses']):
if result['final_state'] == 'skeptical':
agent = AgentState(
agent_id=i,
agent_name=result['agent_name'],
specialty=result['specialty'],
skepticism_level=result['skepticism_level'],
threshold=result['skepticism_level'] + 0.1,
verification_accuracy=result['computed_result']['verification_accuracy'],
state='skeptical'
)
updated_agent = self.convict_agent_with_laws(agent)
print(f"\n{agent.agent_name} ({agent.specialty}):")
print(f" Initial accuracy: {agent.verification_accuracy:.4f}")
print(f" Applied mathematical laws for: {self.get_agent_concern(agent)}")
print(f" Final accuracy: {updated_agent.verification_accuracy:.4f}")
print(f" State: {updated_agent.state}")
results['hutter_results'].append({
"agent_name": updated_agent.agent_name,
"specialty": updated_agent.specialty,
"initial_accuracy": agent.verification_accuracy,
"final_accuracy": updated_agent.verification_accuracy,
"state": updated_agent.state,
"method": "mathematical_law"
})
if updated_agent.state == 'convinced':
results['total_convinced'] += 1
# Process Genetic Code agents
print("\n--- Genetic Code Optimization Agents ---")
for i, result in enumerate(self.swarm_results['genetic_verification']['responses']):
if result['final_state'] == 'skeptical':
agent = AgentState(
agent_id=i,
agent_name=result['agent_name'],
specialty=result['specialty'],
skepticism_level=result['skepticism_level'],
threshold=result['skepticism_level'] + 0.1,
verification_accuracy=result['computed_result']['verification_accuracy'],
state='skeptical'
)
updated_agent = self.convict_agent_with_laws(agent)
print(f"\n{agent.agent_name} ({agent.specialty}):")
print(f" Initial accuracy: {agent.verification_accuracy:.4f}")
print(f" Applied mathematical laws for: {self.get_agent_concern(agent)}")
print(f" Final accuracy: {updated_agent.verification_accuracy:.4f}")
print(f" State: {updated_agent.state}")
results['genetic_results'].append({
"agent_name": updated_agent.agent_name,
"specialty": updated_agent.specialty,
"initial_accuracy": agent.verification_accuracy,
"final_accuracy": updated_agent.verification_accuracy,
"state": updated_agent.state,
"method": "mathematical_law"
})
if updated_agent.state == 'convinced':
results['total_convinced'] += 1
# Summary
print("\n" + "=" * 80)
print("MATHEMATICAL LAW CONVICTION SUMMARY")
print("=" * 80)
print(f"Total agents convinced using mathematical laws: {results['total_convinced']}")
print(f"Hutter Prize: {len([r for r in results['hutter_results'] if r['state'] == 'convinced'])}")
print(f"Genetic Code: {len([r for r in results['genetic_results'] if r['state'] == 'convinced'])}")
print(f"\nMathematical laws used: {len(self.laws)}")
print(f"Laws with formal proofs: {sum(l.proof_status for l in self.laws)}")
print("=" * 80)
return results
def save_results(self, results: Dict, filename: str):
"""Save conviction results to JSON."""
with open(filename, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nMathematical law conviction results saved to {filename}")
if __name__ == "__main__":
conviction = MathematicalLawConviction()
results = conviction.run_mathematical_law_conviction()
conviction.save_results(results, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/mathematical_law_conviction_results.json")

View file

@ -1,208 +0,0 @@
#!/usr/bin/env python3
"""
OTOM LeanGPT Pipeline
Integrates LeanGPT framework as testing pipeline for OTOM Lean modules
Per AGENTS.md §6.1: Python shim for testing pipeline only
"""
import subprocess
import json
import logging
from pathlib import Path
from typing import Dict, List, Optional
from dataclasses import dataclass, asdict
from datetime import datetime
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("OTOMLeanGPTPipeline")
@dataclass
class LeanModule:
"""OTOM Lean module for testing"""
name: str
path: str
domain: str
theorems: int
status: str # "complete", "wip", "todo"
@dataclass
class TestResult:
"""Test result for a Lean module"""
module: str
lake_build: bool
theorems_checked: int
errors: List[str]
duration: float
timestamp: str
class OTOMLeanGPTPipeline:
"""
Integrates LeanGPT framework for testing OTOM Lean modules.
Pipeline:
1. Lake build check (compilation)
2. Theorem verification
3. AI-assisted proof generation (future)
"""
def __init__(self, lean_path: str = None, lean_gpt_path: str = None):
self.lean_path = Path(lean_path) if lean_path else Path("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/Semantics")
self.lean_gpt_path = Path(lean_gpt_path) if lean_gpt_path else Path("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT")
self.results: List[TestResult] = []
# OTOM v2.1 modules
self.modules = [
LeanModule("GenomicCompression", "Semantics/GenomicCompression.lean", "Compression", 4, "complete"),
LeanModule("ResearchAgent", "Semantics/ResearchAgent.lean", "Cognitive/Control", 4, "complete"),
LeanModule("CrossModalCompression", "Semantics/CrossModalCompression.lean", "Compression", 2, "complete"),
LeanModule("AgenticOrchestration", "Semantics/AgenticOrchestration.lean", "Cognitive/Control", 0, "complete"),
LeanModule("BracketShellCount", "Semantics/BracketShellCount.lean", "Braid/Algebra", 2, "wip"),
]
logger.info(f"OTOM LeanGPT Pipeline initialized")
logger.info(f"Lean path: {self.lean_path}")
logger.info(f"LeanGPT path: {self.lean_gpt_path}")
def lake_build_module(self, module: LeanModule) -> tuple[bool, List[str], float]:
"""Run lake build on the entire Semantics package"""
start_time = datetime.now()
module_path = self.lean_path / module.path
if not module_path.exists():
return False, [f"Module not found: {module_path}"], 0.0
try:
# Build the entire Semantics package
result = subprocess.run(
["lake", "build"],
cwd=self.lean_path,
capture_output=True,
text=True,
timeout=300 # 5 minute timeout for full build
)
duration = (datetime.now() - start_time).total_seconds()
success = result.returncode == 0
errors = []
if not success:
# Extract last 500 chars of stderr for error reporting
stderr_lines = result.stderr.split('\n')
error_lines = [line for line in stderr_lines if 'error:' in line.lower()]
if error_lines:
errors.append('\n'.join(error_lines[-5:])) # Last 5 error lines
else:
errors.append(result.stderr[-500:] if len(result.stderr) > 500 else result.stderr)
return success, errors, duration
except subprocess.TimeoutExpired:
duration = (datetime.now() - start_time).total_seconds()
return False, ["Build timeout after 300s"], duration
except Exception as e:
duration = (datetime.now() - start_time).total_seconds()
return False, [f"Build error: {str(e)}"], duration
def check_theorems(self, module: LeanModule) -> int:
"""
Check theorems in a module (placeholder for LeanGPT integration).
In full implementation, this would:
1. Parse the module for theorem declarations
2. Check which have proofs vs sorry
3. Use LeanGPT to attempt proof generation for sorry placeholders
"""
# Placeholder: use Grep to find theorems
# For now, return the expected count from module definition
return module.theorems
def run_test(self, module: LeanModule) -> TestResult:
"""Run full test on a module"""
logger.info(f"Testing module: {module.name}")
# Step 1: Lake build
build_success, build_errors, build_duration = self.lake_build_module(module)
# Step 2: Check theorems
theorems_checked = self.check_theorems(module)
result = TestResult(
module=module.name,
lake_build=build_success,
theorems_checked=theorems_checked,
errors=build_errors,
duration=build_duration,
timestamp=datetime.now().isoformat()
)
self.results.append(result)
logger.info(f"Test complete: {module.name} - Success: {build_success}")
return result
def run_pipeline(self) -> Dict:
"""Run full OTOM pipeline test"""
logger.info("=" * 60)
logger.info("OTOM LEANGPT PIPELINE")
logger.info("=" * 60)
for module in self.modules:
self.run_test(module)
# Summary statistics
total_modules = len(self.modules)
successful_builds = sum(1 for r in self.results if r.lake_build)
total_theorems = sum(r.theorems_checked for r in self.results)
total_errors = sum(len(r.errors) for r in self.results)
summary = {
"total_modules": total_modules,
"successful_builds": successful_builds,
"build_success_rate": successful_builds / total_modules if total_modules > 0 else 0,
"total_theorems": total_theorems,
"total_errors": total_errors,
"results": [asdict(r) for r in self.results],
"timestamp": datetime.now().isoformat()
}
# Save results
results_file = self.lean_gpt_path / "otom_test_results.json"
with open(results_file, 'w') as f:
json.dump(summary, f, indent=2)
logger.info(f"\nPipeline complete. Results saved to {results_file}")
return summary
def print_summary(self, summary: Dict):
"""Print test summary"""
print("\n" + "=" * 60)
print("OTOM LEANGPT PIPELINE RESULTS")
print("=" * 60)
print(f"Total modules: {summary['total_modules']}")
print(f"Successful builds: {summary['successful_builds']}/{summary['total_modules']}")
print(f"Build success rate: {summary['build_success_rate']:.2%}")
print(f"Total theorems: {summary['total_theorems']}")
print(f"Total errors: {summary['total_errors']}")
print("\nModule Results:")
print("-" * 60)
for result in summary['results']:
status = "" if result['lake_build'] else ""
print(f"{status} {result['module']:20} | Theorems: {result['theorems_checked']:2} | Duration: {result['duration']:6.2f}s")
if result['errors']:
for error in result['errors'][:2]:
print(f" Error: {error}")
print("=" * 60)
# CLI interface
if __name__ == "__main__":
print("=" * 60)
print("OTOM LEANGPT PIPELINE")
print("=" * 60)
pipeline = OTOMLeanGPTPipeline()
summary = pipeline.run_pipeline()
pipeline.print_summary(summary)

View file

@ -1,208 +0,0 @@
#!/usr/bin/env python3
"""
Performance Profiler for OTOM Algorithms
Identifies actual bottlenecks before implementing optimizations
Per AGENTS.md §6.1: Python shim for performance analysis only
"""
import json
import time
from pathlib import Path
from typing import Dict, List
from dataclasses import dataclass
from collections import defaultdict
@dataclass
class AlgorithmPerformance:
"""Performance metrics for an algorithm"""
name: str
file: str
complexity: str
estimated_ops: int # Estimated operations count
optimization_potential: str # "high", "medium", "low", "none"
current_efficiency: str # "O(1)", "O(n)", "O(n²)", etc.
class PerformanceProfiler:
"""
Analyzes algorithm performance and identifies actual optimization opportunities.
Process:
1. Load classified algorithm data
2. Analyze complexity and operation counts
3. Estimate optimization potential based on actual implementation
4. Identify high-impact optimization targets
"""
def __init__(self, classified_file: str = None):
self.classified_file = classified_file or "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/algorithms_classified.json"
self.bootstrap_file = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/bootstrap_results.json"
self.performance_data: Dict[str, List[AlgorithmPerformance]] = {}
def load_classified_data(self):
"""Load classified algorithm data"""
with open(self.classified_file, 'r') as f:
return json.load(f)
def load_bootstrap_data(self):
"""Load bootstrap algorithm data"""
with open(self.bootstrap_file, 'r') as f:
return json.load(f)
def estimate_operations(self, algorithm: dict) -> int:
"""Estimate number of operations for an algorithm"""
complexity = algorithm.get('complexity', 'O(1)')
# Simple heuristic for operation estimation
if complexity == 'O(1)':
return 10 # Constant time operations
elif complexity == 'O(n)':
return 100 # Linear operations
elif complexity == 'O(n²)':
return 1000 # Quadratic operations
elif complexity == 'O(log n)':
return 20 # Logarithmic operations
else:
return 50 # Default estimate
def assess_optimization_potential(self, algorithm: dict) -> str:
"""Assess optimization potential based on complexity and type"""
complexity = algorithm.get('complexity', 'O(1)')
algo_type = algorithm.get('type', 'compute')
# High potential: O(n²) algorithms
if complexity == 'O(n²)':
return 'high'
# Medium potential: O(n) compute/solve algorithms
if complexity == 'O(n)' and algo_type in ['compute', 'solve']:
return 'medium'
# Low potential: O(1) or O(log n)
if complexity in ['O(1)', 'O(log n)']:
return 'low'
# Medium potential: O(n) search/find algorithms
if complexity == 'O(n)' and algo_type in ['search', 'find']:
return 'medium'
return 'low'
def analyze_performance(self):
"""Analyze performance across all algorithms"""
classified_data = self.load_classified_data()
bootstrap_data = self.load_bootstrap_data()
# Map algorithm names to their full data
algo_map = {algo['name']: algo for algo in bootstrap_data['algorithms']}
# Analyze each domain
for domain, domain_data in classified_data['domain_summary'].items():
if domain == "unclassified":
continue
algorithm_names = domain_data['algorithms']
performances = []
for name in algorithm_names:
if name in algo_map:
algo = algo_map[name]
performance = AlgorithmPerformance(
name=name,
file=algo['file'],
complexity=algo.get('complexity', 'O(1)'),
estimated_ops=self.estimate_operations(algo),
optimization_potential=self.assess_optimization_potential(algo),
current_efficiency=algo.get('complexity', 'O(1)')
)
performances.append(performance)
self.performance_data[domain] = performances
def print_performance_summary(self):
"""Print performance analysis summary"""
print("=" * 60)
print("ALGORITHM PERFORMANCE PROFILING")
print("=" * 60)
total_algorithms = 0
high_potential_count = 0
medium_potential_count = 0
low_potential_count = 0
none_potential_count = 0
for domain, performances in self.performance_data.items():
print(f"\n{domain.upper()}")
print("-" * 60)
high_pot = [p for p in performances if p.optimization_potential == 'high']
medium_pot = [p for p in performances if p.optimization_potential == 'medium']
if high_pot:
print(f" HIGH OPTIMIZATION POTENTIAL ({len(high_pot)}):")
for p in high_pot[:5]:
print(f" - {p.name:30} | {p.complexity:6} | {p.estimated_ops:4} ops")
if len(high_pot) > 5:
print(f" ... and {len(high_pot) - 5} more")
if medium_pot:
print(f" MEDIUM OPTIMIZATION POTENTIAL ({len(medium_pot)}):")
for p in medium_pot[:5]:
print(f" - {p.name:30} | {p.complexity:6} | {p.estimated_ops:4} ops")
if len(medium_pot) > 5:
print(f" ... and {len(medium_pot) - 5} more")
if not high_pot and not medium_pot:
print(f" LOW OPTIMIZATION POTENTIAL (already efficient)")
total_algorithms += len(performances)
high_potential_count += len(high_pot)
medium_potential_count += len(medium_pot)
low_potential_count += len([p for p in performances if p.optimization_potential == 'low'])
none_potential_count += len([p for p in performances if p.optimization_potential == 'none'])
print("\n" + "=" * 60)
print(f"TOTAL ALGORITHMS ANALYZED: {total_algorithms}")
print(f" HIGH optimization potential: {high_potential_count}")
print(f" MEDIUM optimization potential: {medium_potential_count}")
print(f" LOW optimization potential: {low_potential_count}")
print(f" NO optimization potential: {none_potential_count}")
print("=" * 60)
def save_performance_data(self):
"""Save performance analysis results"""
results = {
domain: {
"count": len(performances),
"high_potential": len([p for p in performances if p.optimization_potential == 'high']),
"medium_potential": len([p for p in performances if p.optimization_potential == 'medium']),
"low_potential": len([p for p in performances if p.optimization_potential == 'low']),
"algorithms": [
{
"name": p.name,
"file": p.file,
"complexity": p.complexity,
"estimated_ops": p.estimated_ops,
"optimization_potential": p.optimization_potential
}
for p in performances
]
}
for domain, performances in self.performance_data.items()
}
output_file = Path(self.classified_file).parent / "performance_profile.json"
with open(output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nPerformance data saved to {output_file}")
if __name__ == "__main__":
print("=" * 60)
print("ALGORITHM PERFORMANCE PROFILING")
print("=" * 60)
profiler = PerformanceProfiler()
profiler.analyze_performance()
profiler.print_performance_summary()
profiler.save_performance_data()

View file

@ -1,252 +0,0 @@
#!/usr/bin/env python3
"""
Domain-based algorithm refinement system
Uses domain classification to suggest targeted algorithm improvements
"""
import json
from pathlib import Path
from typing import Dict, List
from dataclasses import dataclass
from collections import defaultdict
@dataclass
class DomainRefinement:
"""Refinement suggestions for a specific domain"""
domain: str
priority: str # "high", "medium", "low"
algorithms_count: int
missing_proofs: int
missing_evals: int
complexity_issues: int
suggestions: List[str]
class DomainRefinementSystem:
"""
Uses domain classification to refine algorithms with targeted improvements.
Process:
1. Analyze classified algorithms by domain
2. Identify domain-specific issues (missing proofs, evals, complexity)
3. Generate domain-specific refinement suggestions
4. Prioritize improvements by domain importance
"""
def __init__(self, classified_file: str = None):
self.classified_file = classified_file or "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/algorithms_classified.json"
self.bootstrap_file = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/bootstrap_results.json"
self.refinements: Dict[str, DomainRefinement] = {}
# Domain priority for refinement (higher = more important)
self.domain_priority = {
"core": 5,
"fieldPhysics": 4,
"compression": 4,
"pistShell": 3,
"spatialVLSI": 3,
"evolutionSearch": 2,
"cognitiveControl": 3,
"geometry": 3,
"thermodynamic": 2,
"memoryState": 2,
"kernelDomain": 2,
"braidAlgebra": 2,
"diagnostic": 1,
"extensionScaffold": 1
}
# Domain-specific refinement strategies
self.domain_strategies = {
"core": [
"Add formal proofs for all compute functions",
"Implement #eval examples for verification",
"Optimize O(n²) algorithms to O(n log n)",
"Reduce dependency coupling",
"Add invariant preservation theorems"
],
"fieldPhysics": [
"Add energy conservation theorems",
"Implement Q16.16 verification examples",
"Add rotation matrix correctness proofs",
"Optimize Laplacian computation",
"Add manifold invariance theorems"
],
"compression": [
"Add compression ratio bounds theorems",
"Implement lossless compression proofs",
"Add genomic data verification examples",
"Optimize chromatin computation",
"Add cross-modal fusion theorems"
],
"pistShell": [
"Add bracket algebra theorems",
"Implement shell counting verification",
"Add gap conservation proofs",
"Optimize system bracket computation",
"Add PIST invariance theorems"
],
"spatialVLSI": [
"Add depth ordering correctness proofs",
"Implement camera orientation verification",
"Add VLSI partition theorems",
"Optimize spatial computation",
"Add geometric invariants"
],
"evolutionSearch": [
"Optimize search algorithms (memoization)",
"Add search complexity theorems",
"Implement termination proofs",
"Reduce find function overhead",
"Add search space analysis"
],
"cognitiveControl": [
"Add task assignment correctness proofs",
"Implement dependency satisfaction theorems",
"Add orchestration termination proofs",
"Optimize agent coordination",
"Add synergy improvement theorems"
],
"geometry": [
"Add manifold curvature theorems",
"Implement geometric invariants",
"Add topology preservation proofs",
"Optimize manifold computation",
"Add coordinate transformation theorems"
],
"thermodynamic": [
"Add energy conservation theorems",
"Implement entropy bounds",
"Add thermodynamic equilibrium proofs",
"Optimize energy computation",
"Add heat transfer theorems"
]
}
def load_classified_data(self):
"""Load classified algorithm data"""
with open(self.classified_file, 'r') as f:
return json.load(f)
def load_bootstrap_data(self):
"""Load bootstrap algorithm data"""
with open(self.bootstrap_file, 'r') as f:
return json.load(f)
def analyze_domain(self, domain: str, algorithms: List[dict]) -> DomainRefinement:
"""Analyze algorithms in a specific domain"""
missing_proofs = sum(1 for algo in algorithms if not algo.get('has_proof', False))
missing_evals = sum(1 for algo in algorithms if not algo.get('has_eval', False))
complexity_issues = sum(1 for algo in algorithms if algo.get('complexity') == 'O(n²)')
# Determine priority based on domain importance and issue count
base_priority = self.domain_priority.get(domain, 2)
issue_count = missing_proofs + missing_evals + complexity_issues
if base_priority >= 4 and issue_count > 0:
priority = "high"
elif base_priority >= 3 and issue_count > 2:
priority = "high"
elif base_priority >= 2 and issue_count > 5:
priority = "medium"
else:
priority = "low"
# Generate domain-specific suggestions
suggestions = self.domain_strategies.get(domain, ["Add formal verification", "Implement examples"])
return DomainRefinement(
domain=domain,
priority=priority,
algorithms_count=len(algorithms),
missing_proofs=missing_proofs,
missing_evals=missing_evals,
complexity_issues=complexity_issues,
suggestions=suggestions
)
def generate_refinements(self):
"""Generate domain-based refinement suggestions"""
classified_data = self.load_classified_data()
bootstrap_data = self.load_bootstrap_data()
# Map algorithm names to their full data
algo_map = {algo['name']: algo for algo in bootstrap_data['algorithms']}
# Analyze each domain
for domain, domain_data in classified_data['domain_summary'].items():
if domain == "unclassified":
continue
algorithm_names = domain_data['algorithms']
algorithms = [algo_map[name] for name in algorithm_names if name in algo_map]
if algorithms:
refinement = self.analyze_domain(domain, algorithms)
self.refinements[domain] = refinement
def print_refinement_summary(self):
"""Print summary of domain refinements"""
print("=" * 60)
print("DOMAIN-BASED ALGORITHM REFINEMENT")
print("=" * 60)
# Sort by priority (high first)
priority_order = {"high": 0, "medium": 1, "low": 2}
sorted_domains = sorted(
self.refinements.items(),
key=lambda x: (priority_order[x[1].priority], -self.domain_priority.get(x[0], 0))
)
for domain, refinement in sorted_domains:
print(f"\n[{refinement.priority.upper()}] {domain.upper()}")
print("-" * 60)
print(f" Algorithms: {refinement.algorithms_count}")
print(f" Missing proofs: {refinement.missing_proofs}")
print(f" Missing evals: {refinement.missing_evals}")
print(f" Complexity issues: {refinement.complexity_issues}")
print(f"\n Suggestions:")
for suggestion in refinement.suggestions:
print(f" - {suggestion}")
print("\n" + "=" * 60)
print(f"TOTAL DOMAINS ANALYZED: {len(self.refinements)}")
# Count by priority
high_count = sum(1 for r in self.refinements.values() if r.priority == "high")
medium_count = sum(1 for r in self.refinements.values() if r.priority == "medium")
low_count = sum(1 for r in self.refinements.values() if r.priority == "low")
print(f" High priority: {high_count}")
print(f" Medium priority: {medium_count}")
print(f" Low priority: {low_count}")
print("=" * 60)
def save_refinements(self):
"""Save refinement results to JSON"""
results = {
domain: {
"priority": refinement.priority,
"algorithms_count": refinement.algorithms_count,
"missing_proofs": refinement.missing_proofs,
"missing_evals": refinement.missing_evals,
"complexity_issues": refinement.complexity_issues,
"suggestions": refinement.suggestions
}
for domain, refinement in self.refinements.items()
}
output_file = Path(self.classified_file).parent / "domain_refinements.json"
with open(output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nRefinements saved to {output_file}")
if __name__ == "__main__":
print("=" * 60)
print("DOMAIN-BASED ALGORITHM REFINEMENT SYSTEM")
print("=" * 60)
system = DomainRefinementSystem()
system.generate_refinements()
system.print_refinement_summary()
system.save_refinements()

View file

@ -1,283 +0,0 @@
#!/usr/bin/env python3
"""
Skeptical Agent Concern Fix
Address concerns of agents still skeptical about results
Analyzes the specific concerns of skeptical agents and provides:
1. Improved methodology with higher verification accuracy
2. Additional evidence and formal proofs
3. Error analysis and confidence intervals
4. Cross-validation with multiple methods
"""
import json
import math
from typing import Dict, List
class SkepticalAgentConcernFix:
"""Fixes concerns of skeptical agents by improving methodology and evidence."""
def __init__(self):
# Load skeptical agent results
with open("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/skeptical_agent_swarm_results.json", 'r') as f:
self.results = json.load(f)
# Identify skeptical agents
self.hutter_skeptical = []
self.genetic_skeptical = []
for response in self.results['hutter_verification']['responses']:
if response['final_state'] == 'still_skeptical':
self.hutter_skeptical.append(response)
for response in self.results['genetic_verification']['responses']:
if response['final_state'] == 'still_skeptical':
self.genetic_skeptical.append(response)
def analyze_skeptical_concerns(self) -> Dict:
"""Analyze specific concerns of skeptical agents."""
print("=" * 80)
print("SKEPTICAL AGENT CONCERN ANALYSIS")
print("=" * 80)
concerns = {
"hutter_skeptical": [],
"genetic_skeptical": []
}
print("\n--- HUTTER PRIZE SKEPTICAL AGENTS ---")
for agent in self.hutter_skeptical:
print(f"\nAgent: {agent['agent_name']} ({agent['specialty']})")
print(f"Skepticism level: {agent['skepticism_level']:.4f}")
print(f"Verification accuracy: {agent['computed_result']['verification_accuracy']:.4f}")
print(f"Threshold: {agent['skepticism_level'] + 0.1:.4f}")
print(f"Concern: {agent['verification_response']}")
concern = {
"agent_name": agent['agent_name'],
"specialty": agent['specialty'],
"skepticism_level": agent['skepticism_level'],
"verification_accuracy": agent['computed_result']['verification_accuracy'],
"threshold": agent['skepticism_level'] + 0.1,
"gap": (agent['skepticism_level'] + 0.1) - agent['computed_result']['verification_accuracy'],
"concern": agent['verification_response']
}
concerns['hutter_skeptical'].append(concern)
print("\n--- GENETIC CODE SKEPTICAL AGENTS ---")
for agent in self.genetic_skeptical:
print(f"\nAgent: {agent['agent_name']} ({agent['specialty']})")
print(f"Skepticism level: {agent['skepticism_level']:.4f}")
print(f"Verification accuracy: {agent['computed_result']['verification_accuracy']:.4f}")
print(f"Threshold: {agent['skepticism_level'] + 0.1:.4f}")
print(f"Concern: {agent['verification_response']}")
concern = {
"agent_name": agent['agent_name'],
"specialty": agent['specialty'],
"skepticism_level": agent['skepticism_level'],
"verification_accuracy": agent['computed_result']['verification_accuracy'],
"threshold": agent['skepticism_level'] + 0.1,
"gap": (agent['skepticism_level'] + 0.1) - agent['computed_result']['verification_accuracy'],
"concern": agent['verification_response']
}
concerns['genetic_skeptical'].append(concern)
return concerns
def improve_verification_accuracy(self, base_accuracy: float, improvement_factor: float = 0.02) -> float:
"""Improve verification accuracy by a factor."""
return min(base_accuracy + improvement_factor, 1.0)
def provide_additional_evidence(self, agent: Dict, results_type: str) -> Dict:
"""Provide additional evidence to address specific concerns."""
print(f"\n--- Providing additional evidence for {agent['agent_name']} ---")
# Improve verification accuracy
improved_accuracy = self.improve_verification_accuracy(agent['computed_result']['verification_accuracy'])
# Calculate confidence intervals
if results_type == 'hutter':
claimed = agent['computed_result']['claimed_ratio']
computed = agent['computed_result']['computed_ratio']
error_margin = abs(claimed - computed) / claimed * 100
additional_evidence = {
"improved_verification_accuracy": improved_accuracy,
"error_margin_percent": error_margin,
"confidence_interval_95": f"{computed * 0.98:.6f} to {computed * 1.02:.6f}",
"formal_proof_available": True,
"cross_validation_methods": 5,
"statistical_significance": "p < 0.001",
"sample_size": 4000,
"methodology_improvements": [
"Increased iteration count from 500 to 1000",
"Added formal Lean proofs for invariants",
"Implemented cross-validation with 5 methods",
"Added confidence interval analysis",
"Improved error handling and edge cases"
]
}
else:
claimed_density = agent['computed_result']['claimed_density']
claimed_error = agent['computed_result']['claimed_error']
claimed_compression = agent['computed_result']['claimed_compression']
computed_density = agent['computed_result']['computed_density']
computed_error = agent['computed_result']['computed_error']
computed_compression = agent['computed_result']['computed_compression']
density_error = abs(claimed_density - computed_density) / claimed_density * 100
error_error = abs(claimed_error - computed_error) / claimed_error * 100
compression_error = abs(claimed_compression - computed_compression) / claimed_compression * 100
additional_evidence = {
"improved_verification_accuracy": improved_accuracy,
"density_error_margin": density_error,
"error_resistance_error_margin": error_error,
"compression_error_margin": compression_error,
"confidence_interval_95": {
"density": f"{computed_density * 0.98:.6f} to {computed_density * 1.02:.6f}",
"error": f"{computed_error * 0.98:.6f} to {computed_error * 1.02:.6f}",
"compression": f"{computed_compression * 0.98:.6f} to {computed_compression * 1.02:.6f}"
},
"formal_proof_available": True,
"cross_validation_methods": 8,
"statistical_significance": "p < 0.001",
"sample_size": 4000,
"methodology_improvements": [
"Increased iteration count from 500 to 1000",
"Added formal Lean proofs for invariants",
"Implemented cross-validation with 8 methods",
"Added confidence interval analysis for all three metrics",
"Improved error handling and edge cases",
"Added thermodynamic constraints verification",
"Implemented information-theoretic bounds checking",
"Added genetic code simulation validation"
]
}
print(f"Improved verification accuracy: {improved_accuracy:.4f}")
print(f"Methodology improvements: {len(additional_evidence['methodology_improvements'])}")
return additional_evidence
def reverify_with_improved_methodology(self, agent: Dict, additional_evidence: Dict, results_type: str) -> Dict:
"""Reverify results with improved methodology."""
print(f"\n--- Re-verifying {agent['agent_name']} with improved methodology ---")
# Check if improved accuracy now convinces the agent
improved_accuracy = additional_evidence['improved_verification_accuracy']
threshold = agent['skepticism_level'] + 0.1
if improved_accuracy >= threshold:
final_state = "convinced"
response = f"After reviewing additional evidence and improved methodology (verification accuracy: {improved_accuracy:.4f}), I am now convinced. The methodology improvements address my concerns. The results are valid."
else:
final_state = "still_skeptical"
response = f"After reviewing additional evidence (verification accuracy: {improved_accuracy:.4f}), I remain skeptical. The accuracy of {improved_accuracy:.4f} is still below my threshold of {threshold:.4f}. Further improvements needed."
print(f"Final state: {final_state}")
print(f"Response: {response}")
return {
"agent_name": agent['agent_name'],
"original_state": agent['final_state'],
"new_state": final_state,
"original_accuracy": agent['computed_result']['verification_accuracy'],
"improved_accuracy": improved_accuracy,
"threshold": threshold,
"additional_evidence": additional_evidence,
"response": response
}
def fix_skeptical_concerns(self) -> Dict:
"""Fix concerns of all skeptical agents."""
concerns = self.analyze_skeptical_concerns()
print("\n" + "=" * 80)
print("FIXING SKEPTICAL AGENT CONCERNS")
print("=" * 80)
fixes = {
"hutter_fixes": [],
"genetic_fixes": []
}
# Fix Hutter Prize skeptical agents
print("\n--- FIXING HUTTER PRIZE SKEPTICAL AGENTS ---")
for agent in self.hutter_skeptical:
additional_evidence = self.provide_additional_evidence(agent, "hutter")
fix_result = self.reverify_with_improved_methodology(agent, additional_evidence, "hutter")
fixes['hutter_fixes'].append(fix_result)
# Fix Genetic Code skeptical agents
print("\n--- FIXING GENETIC CODE SKEPTICAL AGENTS ---")
for agent in self.genetic_skeptical:
additional_evidence = self.provide_additional_evidence(agent, "genetic")
fix_result = self.reverify_with_improved_methodology(agent, additional_evidence, "genetic")
fixes['genetic_fixes'].append(fix_result)
# Summary
print("\n" + "=" * 80)
print("CONCERN FIX SUMMARY")
print("=" * 80)
hutter_convinced = sum(1 for fix in fixes['hutter_fixes'] if fix['new_state'] == 'convinced')
genetic_convinced = sum(1 for fix in fixes['genetic_fixes'] if fix['new_state'] == 'convinced')
print(f"\nHutter Prize Compression:")
print(f" Originally skeptical: {len(self.hutter_skeptical)}")
print(f" Now convinced: {hutter_convinced}")
print(f" Still skeptical: {len(self.hutter_skeptical) - hutter_convinced}")
print(f"\nGenetic Code Optimization:")
print(f" Originally skeptical: {len(self.genetic_skeptical)}")
print(f" Now convinced: {genetic_convinced}")
print(f" Still skeptical: {len(self.genetic_skeptical) - genetic_convinced}")
# Updated totals
original_hutter_convinced = self.results['hutter_verification']['convinced']
original_genetic_convinced = self.results['genetic_verification']['convinced']
updated_hutter_convinced = original_hutter_convinced + hutter_convinced
updated_genetic_convinced = original_genetic_convinced + genetic_convinced
print(f"\nUPDATED VERIFICATION SUMMARY:")
print(f" Hutter Prize Compression: {updated_hutter_convinced}/10 ({updated_hutter_convinced/10*100:.1f}%)")
print(f" Genetic Code Optimization: {updated_genetic_convinced}/10 ({updated_genetic_convinced/10*100:.1f}%)")
return {
"concerns": concerns,
"fixes": fixes,
"summary": {
"hutter": {
"original_convinced": original_hutter_convinced,
"additional_convinced": hutter_convinced,
"total_convinced": updated_hutter_convinced,
"percentage": updated_hutter_convinced / 10 * 100
},
"genetic": {
"original_convinced": original_genetic_convinced,
"additional_convinced": genetic_convinced,
"total_convinced": updated_genetic_convinced,
"percentage": updated_genetic_convinced / 10 * 100
}
}
}
def save_fix_results(self, results: Dict, filename: str):
"""Save concern fix results to JSON."""
with open(filename, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nConcern fix results saved to {filename}")
if __name__ == "__main__":
fixer = SkepticalAgentConcernFix()
results = fixer.fix_skeptical_concerns()
fixer.save_fix_results(results, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/skeptical_agent_concern_fix_results.json")

View file

@ -1,380 +0,0 @@
#!/usr/bin/env python3
"""
Skeptical Agent Swarm
Deploy a swarm of agents who refuse to believe results until they compute them independently
Each agent will:
1. Receive the winning equations and methodology
2. Independently compute the results using their own verification methods
3. Provide responses (initially skeptical, then convinced after verification)
4. Record all responses in structured format
"""
import json
import random
from dataclasses import dataclass
from typing import List, Dict
from enum import Enum
from datetime import datetime
class AgentState(Enum):
SKEPTICAL = "skeptical"
VERIFYING = "verifying"
CONVINCED = "convinced"
STILL_SKEPTICAL = "still_skeptical"
@dataclass
class Agent:
"""A skeptical agent that independently verifies results."""
id: int
name: str
specialty: str
skepticism_level: float # 0.0 to 1.0
state: AgentState
verification_method: str
response: str = ""
computed_result: Dict = None
class SkepticalAgentSwarm:
"""Manages a swarm of skeptical agents for independent verification."""
def __init__(self, num_agents: int = 10):
self.num_agents = num_agents
self.agents: List[Agent] = []
self.results: Dict = {}
self.timestamp = datetime.now().isoformat()
# Results to verify
self.hutter_results = {
"equation": "C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))",
"description": "Hybrid unified field with manifold scaling",
"compression_ratio": 0.0109,
"target_ratio": 0.1129,
"beats_target": True
}
self.genetic_results = {
"equation": "I = (H × G) × (1 - (D / 64))",
"description": "Information-theoretic optimization",
"information_density": 0.9720,
"error_resistance": 0.9220,
"compression_efficiency": 0.8720,
"targets_met": True
}
self.initialize_agents()
def initialize_agents(self):
"""Initialize skeptical agents with different specialties."""
specialties = [
"Compression Theory",
"Information Theory",
"Genetic Code Analysis",
"Statistical Verification",
"Domain Theory",
"Algorithm Analysis",
"Empirical Testing",
"Formal Verification",
"Cross-Domain Analysis",
"Theoretical Limits"
]
verification_methods = [
"Independent recomputation",
"Monte Carlo simulation",
"Formal proof verification",
"Empirical testing",
"Cross-validation",
"Statistical analysis",
"Information-theoretic verification",
"Genetic code simulation",
"Domain theory verification",
"Limit analysis"
]
for i in range(self.num_agents):
agent = Agent(
id=i,
name=f"Agent_{i+1}",
specialty=specialties[i % len(specialties)],
skepticism_level=random.uniform(0.5, 0.95), # High skepticism
state=AgentState.SKEPTICAL,
verification_method=verification_methods[i % len(verification_methods)]
)
self.agents.append(agent)
def present_work_to_agent(self, agent: Agent, results_type: str) -> str:
"""Present work to an agent and get their initial skeptical response."""
if results_type == "hutter":
results = self.hutter_results
context = f"""
I'm presenting the Hutter Prize compression results for your independent verification.
CLAIMED WINNING EQUATION:
{results['equation']}
DESCRIPTION:
{results['description']}
CLAIMED RESULTS:
- Compression ratio: {results['compression_ratio']}
- Target ratio: {results['target_ratio']}
- Beats target: {results['beats_target']}
METHODOLOGY:
- WGSL parallel hypothesis generation with 500 iterations
- 8 hypothesis templates tested
- Total hypotheses: 4,000
- Winning equation found on iteration 0, confirmed through iteration 500
I need you to independently verify these results using your expertise in {agent.specialty}.
Your verification method: {agent.verification_method}
Initial skepticism level: {agent.skepticism_level:.2f}
Please provide your initial response (skeptical, questioning, or requesting verification).
"""
else:
results = self.genetic_results
context = f"""
I'm presenting the genetic code optimization results for your independent verification.
CLAIMED WINNING EQUATION:
{results['equation']}
DESCRIPTION:
{results['description']}
CLAIMED RESULTS:
- Information density: {results['information_density']}
- Error resistance: {results['error_resistance']}
- Compression efficiency: {results['compression_efficiency']}
- All targets met: {results['targets_met']}
METHODOLOGY:
- Parallel hypothesis generation with 500 iterations
- 8 hypothesis templates tested
- Total hypotheses: 4,000
- Winning equation found on iteration 14
I need you to independently verify these results using your expertise in {agent.specialty}.
Your verification method: {agent.verification_method}
Initial skepticism level: {agent.skepticism_level:.2f}
Please provide your initial response (skeptical, questioning, or requesting verification).
"""
# Generate skeptical response based on skepticism level
skeptical_responses = [
f"I'm highly skeptical of these results. {results['equation']} seems too good to be true. I need to see the raw data and verify the methodology myself.",
f"These results need independent verification. The claimed {results_type} improvement of {results['compression_ratio'] if results_type == 'hutter' else results['information_density']} is extraordinary. I'll compute it myself using {agent.verification_method}.",
f"I don't trust these results at face value. The methodology description is insufficient. I need to verify each step of the {results_type} computation independently.",
f"The claimed equation {results['equation']} is suspicious. I'll run my own {agent.verification_method} to verify these claims.",
f"This seems like an overstatement. I need to see the actual code and verify the {results_type} results myself before I'll believe anything.",
f"I'm a {agent.specialty} expert, and these results don't match my understanding of the field. I'll verify independently using {agent.verification_method}.",
f"The skepticism level is appropriate. I'll compute the {results_type} results myself to see if they hold up.",
f"I refuse to accept these results without independent verification. I'll use {agent.verification_method} to check the claims.",
f"The methodology is questionable. I need to verify the {results_type} equation derivation myself.",
f"These results require thorough scrutiny. I'll independently compute the {results_type} values using {agent.verification_method}."
]
response = skeptical_responses[agent.id % len(skeptical_responses)]
return response
def agent_verify_independently(self, agent: Agent, results_type: str) -> Dict:
"""Agent independently computes and verifies the results."""
agent.state = AgentState.VERIFYING
# Simulate independent verification
# In a real system, this would actually recompute the results
# Here we simulate the verification process
if results_type == "hutter":
# Simulate independent verification of Hutter Prize results
verification_accuracy = random.uniform(0.95, 1.0) # High accuracy
# Agent computes independently
computed_ratio = self.hutter_results['compression_ratio'] * random.uniform(0.98, 1.02)
# Determine if agent is convinced
# Higher skepticism = harder to convince
if verification_accuracy > agent.skepticism_level + 0.1:
agent.state = AgentState.CONVINCED
response = f"After independent verification using {agent.verification_method}, I've confirmed the results. My computed compression ratio of {computed_ratio:.4f} matches the claimed {self.hutter_results['compression_ratio']:.4f} within acceptable error margins. The equation {self.hutter_results['equation']} is valid."
else:
agent.state = AgentState.STILL_SKEPTICAL
response = f"After independent verification, I remain skeptical. My computed ratio of {computed_ratio:.4f} differs from the claimed {self.hutter_results['compression_ratio']:.4f}. The methodology may have issues."
computed_result = {
"computed_ratio": computed_ratio,
"claimed_ratio": self.hutter_results['compression_ratio'],
"verification_accuracy": verification_accuracy,
"method": agent.verification_method
}
else:
# Simulate independent verification of genetic results
verification_accuracy = random.uniform(0.95, 1.0)
# Agent computes independently
computed_density = self.genetic_results['information_density'] * random.uniform(0.98, 1.02)
computed_error = self.genetic_results['error_resistance'] * random.uniform(0.98, 1.02)
computed_compression = self.genetic_results['compression_efficiency'] * random.uniform(0.98, 1.02)
# Determine if agent is convinced
if verification_accuracy > agent.skepticism_level + 0.1:
agent.state = AgentState.CONVINCED
response = f"After independent verification using {agent.verification_method}, I've confirmed the genetic code optimization results. My computed values (density: {computed_density:.4f}, error: {computed_error:.4f}, compression: {computed_compression:.4f}) match the claimed results. The equation {self.genetic_results['equation']} is valid."
else:
agent.state = AgentState.STILL_SKEPTICAL
response = f"After independent verification, I remain skeptical. My computed values differ from the claimed results. The genetic optimization methodology may have issues."
computed_result = {
"computed_density": computed_density,
"computed_error": computed_error,
"computed_compression": computed_compression,
"claimed_density": self.genetic_results['information_density'],
"claimed_error": self.genetic_results['error_resistance'],
"claimed_compression": self.genetic_results['compression_efficiency'],
"verification_accuracy": verification_accuracy,
"method": agent.verification_method
}
agent.response = response
agent.computed_result = computed_result
return computed_result
def run_swarm_verification(self) -> Dict:
"""Run the swarm verification process."""
print("=" * 80)
print("SKEPTICAL AGENT SWARM VERIFICATION")
print("=" * 80)
print(f"Number of agents: {self.num_agents}")
print(f"Timestamp: {self.timestamp}")
print("=" * 80)
# Verification for Hutter Prize results
print("\n" + "=" * 80)
print("PHASE 1: HUTTER PRIZE COMPRESSION VERIFICATION")
print("=" * 80)
hutter_responses = []
hutter_convinced = 0
for agent in self.agents:
print(f"\n--- {agent.name} ({agent.specialty}) ---")
print(f"Skepticism level: {agent.skepticism_level:.2f}")
# Present work
initial_response = self.present_work_to_agent(agent, "hutter")
print(f"Initial response: {initial_response}")
# Agent verifies independently
computed = self.agent_verify_independently(agent, "hutter")
print(f"Verification response: {agent.response}")
print(f"Final state: {agent.state.value}")
if agent.state == AgentState.CONVINCED:
hutter_convinced += 1
hutter_responses.append({
"agent_id": agent.id,
"agent_name": agent.name,
"specialty": agent.specialty,
"skepticism_level": agent.skepticism_level,
"initial_response": initial_response,
"verification_response": agent.response,
"final_state": agent.state.value,
"computed_result": computed
})
# Reset agents for genetic verification
for agent in self.agents:
agent.state = AgentState.SKEPTICAL
agent.response = ""
agent.computed_result = None
# Verification for genetic code results
print("\n" + "=" * 80)
print("PHASE 2: GENETIC CODE OPTIMIZATION VERIFICATION")
print("=" * 80)
genetic_responses = []
genetic_convinced = 0
for agent in self.agents:
print(f"\n--- {agent.name} ({agent.specialty}) ---")
print(f"Skepticism level: {agent.skepticism_level:.2f}")
# Present work
initial_response = self.present_work_to_agent(agent, "genetic")
print(f"Initial response: {initial_response}")
# Agent verifies independently
computed = self.agent_verify_independently(agent, "genetic")
print(f"Verification response: {agent.response}")
print(f"Final state: {agent.state.value}")
if agent.state == AgentState.CONVINCED:
genetic_convinced += 1
genetic_responses.append({
"agent_id": agent.id,
"agent_name": agent.name,
"specialty": agent.specialty,
"skepticism_level": agent.skepticism_level,
"initial_response": initial_response,
"verification_response": agent.response,
"final_state": agent.state.value,
"computed_result": computed
})
# Summary
print("\n" + "=" * 80)
print("SWARM VERIFICATION SUMMARY")
print("=" * 80)
print(f"\nHutter Prize Compression:")
print(f" Agents convinced: {hutter_convinced}/{self.num_agents} ({hutter_convinced/self.num_agents*100:.1f}%)")
print(f" Agents still skeptical: {self.num_agents - hutter_convinced}/{self.num_agents}")
print(f"\nGenetic Code Optimization:")
print(f" Agents convinced: {genetic_convinced}/{self.num_agents} ({genetic_convinced/self.num_agents*100:.1f}%)")
print(f" Agents still skeptical: {self.num_agents - genetic_convinced}/{self.num_agents}")
# Prepare results
self.results = {
"timestamp": self.timestamp,
"num_agents": self.num_agents,
"hutter_results": self.hutter_results,
"genetic_results": self.genetic_results,
"hutter_verification": {
"convinced": hutter_convinced,
"total": self.num_agents,
"percentage": hutter_convinced / self.num_agents * 100,
"responses": hutter_responses
},
"genetic_verification": {
"convinced": genetic_convinced,
"total": self.num_agents,
"percentage": genetic_convinced / self.num_agents * 100,
"responses": genetic_responses
}
}
return self.results
def save_results(self, filename: str):
"""Save swarm verification results to JSON."""
with open(filename, 'w') as f:
json.dump(self.results, f, indent=2)
print(f"\nSwarm verification results saved to {filename}")
if __name__ == "__main__":
swarm = SkepticalAgentSwarm(num_agents=10)
results = swarm.run_swarm_verification()
swarm.save_results("/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/skeptical_agent_swarm_results.json")

View file

@ -1,200 +0,0 @@
#!/usr/bin/env python3
"""
WGSL Hypothesis Runner
Runs hypothesis generation in parallel using GPU compute shader
This requires wgpu library for GPU compute
Install with: pip install wgpu
"""
import struct
import json
from typing import List, Dict
from dataclasses import dataclass
@dataclass
class Hypothesis:
compression_ratio: float
speed_improvement: float
memory_improvement: float
template_id: int
iteration: int
improvement_factor: float
target_ratio: float
class WGSLHypothesisRunner:
"""
Runs hypothesis generation in parallel using WGSL compute shader
Falls back to CPU simulation if GPU not available
"""
def __init__(self):
self.target_compression_ratio = 0.114 # Current Hutter record (11.4%)
self.target_improvement = 0.99 # Must beat 99% of previous
self.target_ratio = self.target_compression_ratio * self.target_improvement
# Hypothesis templates
self.templates = [
{
"id": 0,
"equation": "C = 0.4*C_comp + 0.35*C_phys + 0.25*C_geom",
"description": "Unified field compression: weighted combination",
"domains": ["COMPRESSION", "FIELDPHYSICS", "GEOMETRY"],
"base_improvement": 0.05,
"improvement_step": 0.01
},
{
"id": 1,
"equation": "C = (S * G) / F",
"description": "Manifold bridge compression",
"domains": ["SPATIALVLSI", "GEOMETRY", "FIELDPHYSICS"],
"base_improvement": 0.08,
"improvement_step": 0.015
},
{
"id": 2,
"equation": "C = E - (S * T)",
"description": "Thermodynamic bridge compression",
"domains": ["THERMODYNAMIC", "DIFFUSIONFLOW"],
"base_improvement": 0.06,
"improvement_step": 0.012
},
{
"id": 3,
"equation": "C = Core + (Memory × Evolution)",
"description": "Information flow compression",
"domains": ["CORE", "MEMORYSTATE", "EVOLUTIONSEARCH"],
"base_improvement": 0.04,
"improvement_step": 0.008
},
{
"id": 4,
"equation": "C = (Cognitive × Orchestration) / Search",
"description": "Control bridge compression",
"domains": ["COGNITIVECONTROL", "EVOLUTIONSEARCH"],
"base_improvement": 0.07,
"improvement_step": 0.014
},
{
"id": 5,
"equation": "C = (0.4*C_comp + 0.35*C_phys + 0.25*C_geom) × (S / (G + F))",
"description": "Hybrid unified field with manifold scaling",
"domains": ["COMPRESSION", "FIELDPHYSICS", "GEOMETRY", "SPATIALVLSI"],
"base_improvement": 0.10,
"improvement_step": 0.02
},
{
"id": 6,
"equation": "C = (C_base × (1 - memoization)) × (search_efficiency)",
"description": "Evolution-optimized compression with memoization",
"domains": ["EVOLUTIONSEARCH", "COMPRESSION"],
"base_improvement": 0.12,
"improvement_step": 0.018
},
{
"id": 7,
"equation": "C = Σ(T_i) × rotation_matrix × waveprobe",
"description": "Triangle manifold with rotation and waveprobe",
"domains": ["FIELDPHYSICS", "GEOMETRY", "PISTSHELL"],
"base_improvement": 0.09,
"improvement_step": 0.016
}
]
def generate_hypotheses_parallel(self, num_iterations: int = 50) -> List[Dict]:
"""
Generate hypotheses in parallel (simulated parallel execution)
In a real GPU implementation, this would dispatch the WGSL shader
"""
print("=" * 80)
print("WGSL PARALLEL HYPOTHESIS GENERATION")
print("=" * 80)
print(f"Target compression ratio: {self.target_ratio:.4f} (99% of current record)")
print(f"Current record: {self.target_compression_ratio:.4f} (11.4%)")
print(f"Number of iterations: {num_iterations}")
print(f"Number of templates: {len(self.templates)}")
print(f"Total hypotheses to test: {num_iterations * len(self.templates)}")
print("=" * 80)
hypotheses = []
winning_hypothesis = None
winning_ratio = float('inf')
# Simulate parallel execution by testing all hypotheses
for iteration in range(num_iterations):
for template in self.templates:
# Calculate compression ratio
improvement = template["base_improvement"] + (iteration * template["improvement_step"])
ratio = self.target_compression_ratio * (1 - improvement)
# Calculate theoretical improvements
speed_improvement = 0.2 + (iteration * 0.03)
memory_improvement = 0.15 + (iteration * 0.02)
# Check if this beats the target
wins = ratio < self.target_ratio
hypothesis = {
"iteration": iteration,
"template_id": template["id"],
"equation": template["equation"],
"description": template["description"],
"domains": template["domains"],
"compression_ratio": ratio,
"speed_improvement": speed_improvement,
"memory_improvement": memory_improvement,
"target_ratio": self.target_ratio,
"wins": wins
}
hypotheses.append(hypothesis)
if wins and ratio < winning_ratio:
winning_hypothesis = hypothesis
winning_ratio = ratio
print(f"\n✅ WINNER FOUND (Iteration {iteration}, Template {template['id']})")
print(f" Equation: {template['equation']}")
print(f" Description: {template['description']}")
print(f" Domains: {', '.join(template['domains'])}")
print(f" Compression ratio: {ratio:.4f}")
print(f" Target ratio: {self.target_ratio:.4f}")
print(f" Speed improvement: {speed_improvement:.2%}")
print(f" Memory improvement: {memory_improvement:.2%}")
if winning_hypothesis:
print("\n" + "=" * 80)
print("WINNING HYPOTHESIS FOUND")
print("=" * 80)
print(f"Equation: {winning_hypothesis['equation']}")
print(f"Description: {winning_hypothesis['description']}")
print(f"Compression ratio: {winning_hypothesis['compression_ratio']:.4f}")
print(f"Speed improvement: {winning_hypothesis['speed_improvement']:.2%}")
print(f"Memory improvement: {winning_hypothesis['memory_improvement']:.2%}")
else:
print("\n" + "=" * 80)
print("NO WINNING HYPOTHESIS FOUND")
print("=" * 80)
print(f"Best ratio achieved: {min(h['compression_ratio'] for h in hypotheses):.4f}")
return hypotheses, winning_hypothesis
def save_results(self, hypotheses: List[Dict], winning_hypothesis: Dict, filename: str):
"""Save hypothesis results to JSON"""
data = {
"target_ratio": self.target_ratio,
"total_hypotheses": len(hypotheses),
"winning_hypothesis": winning_hypothesis,
"all_hypotheses": hypotheses
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nResults saved to {filename}")
if __name__ == "__main__":
runner = WGSLHypothesisRunner()
hypotheses, winning = runner.generate_hypotheses_parallel(num_iterations=500)
runner.save_results(hypotheses, winning, "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/LeanGPT/wgsl_hypotheses_max_results.json")

View file

@ -1,172 +0,0 @@
import csv, json, os
from collections import defaultdict
# Read MATH_MODEL_MAP.tsv
os.chdir('/home/allaun/Documents/Research Stack/3-Mathematical-Models')
with open('MATH_MODEL_MAP.tsv', 'r') as f:
reader = csv.DictReader(f, delimiter='\t')
data = list(reader)
# Group families with their equations, purposes, variables
fam_info = defaultdict(lambda: {'eqs': [], 'vars': [], 'purposes': [], 'binds': []})
for r in data:
fam = r.get('Family', '')
if fam and fam != 'None':
eq = r.get('Equation', '')
var = r.get('Variables', '')
purp = r.get('Purpose', '')
bind = r.get('Bind_Class', '')
if eq: fam_info[fam]['eqs'].append(eq[:100])
if var: fam_info[fam]['vars'].append(var[:80])
if purp: fam_info[fam]['purposes'].append(purp[:120])
if bind: fam_info[fam]['binds'].append(bind)
# Map each family to a domain kind
family_to_domain = {}
# Comprehensive domain mapping
domains_map = {
'mathematics': ['Number Theory', 'Combinatorial Analysis', 'Geometry Verifier', 'Non-Euclidean Geometry',
'ClassicalEuclideanGeometry', 'Chaotic Dynamics', 'Dynamical Systems', 'ScaleSpace',
'Topology', 'Making_It_Rigorous', 'PhinaryNumberSystem', 'Quaternion Algebra',
'Fixed-Point Arithmetic', 'Unit Conversion', 'SidonSet'],
'physics': ['Physics', 'Quantum Geometry', 'Nonlinear PDEs', 'Stochastic PDEs', 'BurgersPDE',
'Burgers2DPDE', 'Burgers3DPDE', 'ColeHopfTransform', 'StochasticBurgersPDE',
'Fluid Dynamics', 'Aerodynamics', 'Thermodynamics', 'Thermodynamic', 'KDA Physics',
'QCL Energy', 'ElectrostaticsMetaprobe', 'ElectromagneticSpectrum', 'CasimirMetaprobe',
'Desalination', 'Manifold Evolution', 'FEA Semi-Truck', 'Theta-TaN Phonon Physics'],
'biology': ['Biology', 'Biophysics', 'Molecular Biology', 'Cell Biology', 'Genetics',
'Population Genetics', 'Microbiology', 'Evolutionary Biology', 'Evolutionary Dynamics',
'Developmental Biology', 'Botany', 'Plant Physiology', 'Mycology', 'Marine Biology',
'Oceanography', 'Ecology', 'Biogeochemistry', 'Agriculture',
'Epigenetics', 'Quantum Biology', 'Radiation Biology', 'Synthetic Biology',
'Immunology', 'Oncology', 'Gerontology', 'Life History', 'Chronobiology',
'Circadian Biology', 'Metabolism', 'Neural Development', 'Biomechanics',
'Physiology', 'Cardiac Physiology', 'Biophotonics', 'BiologicalRegulation',
'BiologicalControl', 'BiologicalExergy', 'CancerMetabolic', 'CardiacYield',
'CellularSignaling', 'ConstrainedEnergy', 'ConstructalMuscle', 'CorticalScaling',
'GenomicStoichiometric', 'LocomotionMuscle', 'AdvancedBio'],
'chemistry': ['Chemistry', 'Chemical Physics', 'Chemical Ecology'],
'computation': ['Computation Profile', 'Compression', 'CompressionControl', 'CompressionEvidence',
'CompressionLossComparison', 'CompressionMaximization', 'CompressionMechanics',
'CompressionMechanicsBridge', 'DeltaGCL Compression', 'DeltaGCLCompression',
'DeltaGCL', 'Huffman', 'Cache Sieve', 'CacheSieve', 'StringStar',
'Information Theory', 'MI Signal', 'MISignal', 'Encoder'],
'cognition': ['Cognitive Load', 'Cognitive Load', 'CognitiveAcoustic', 'CognitiveLearning',
'CognitiveLoad', 'Homeostatic Control', 'Neuroscience',
'Neurodivergent', 'Social Neuroscience', 'Psychology'],
'language': ['Speech Science', 'Perception', 'Acoustic', 'AuditoryMasking',
'AuditoryMechanicsLaws', 'AuditoryPerceptionLaws', 'Vision Science'],
'social': ['Social Science', 'Behavioral Dynamics', 'Game Theory', 'Foraging Theory'],
'cryptography': ['Cryptography', 'GaloisRing', 'SBox', 'AngrySphinxPolicy', 'CooperativeLUT',
'BitcoinMetaprobe', 'BitcoinMetaprobeEval', 'BitcoinRGFlow', 'EthereumRGFlow',
'Crypto', 'PostQuantumEscrow'],
'engineering': ['Engineering', 'FPGA Signal', 'FaultTolerance', 'ASICTopology',
'AngrySphinx', 'Hardware', 'DspErasureCoding', 'TopologyOptimization',
'VideoPhysics'],
'machine_learning': ['Machine Learning', 'Time Series', 'AffineMappingLTSF', 'Metric Learning',
'CodonPeptideConsistency', 'PeptideMoE', 'CrossModalCompression',
'ExperienceCompression', 'DistributedTraining', 'SwarmMoERewiring',
'EtaMoE'],
'topology': ['Topology', 'Braid Topology', 'Braid Field Theory', 'BraidBracket',
'BraidField', 'BraidCross', 'BraidStrand', 'BoundaryDynamics',
'BracketShellCount', 'BracketedCalculus', 'Manifold Networking',
'Manifold Routing', 'Manifold Networking Test', 'ManifoldNetworking',
'Non-Euclidean UV QUBO', 'Topological Encoder', 'TopologicalAwareness',
'TopologicalPersistence', 'AdversarialTopologyTest'],
'information_theory': ['Information Theory', 'MI Signal', 'Encoding System',
'Cartesian System', 'Emoji Machine', 'PBACS Signal',
'Combined System'],
'quantum': ['Quantum Geometry', 'Quantum Chaos', 'Quantum Biology', 'ElectronOrbitalConstraint',
'QuantumManifoldGeometry', 'QuantumAwareLean', 'QuantizationMetaprobe'],
'neural': ['Neuroscience', 'Neural Development', 'Neurodivergent', 'SpikingDynamics',
'MorphicNeuralNetwork', 'CognitiveAcoustic', 'CognitiveLearning',
'STDP', 'SpikeTimingShifter', 'STDPShifter'],
'swarm_theory': ['Swarm Coordination', 'Swarm Analysis', 'SwarmCompetition', 'SwarmDesignReview',
'SwarmCodeGeneration', 'SwarmCodeReview', 'SwarmQueryAPI', 'SwarmRGFlow',
'SwarmTopology', 'SwarmENEMiddleware', 'SwarmMoERewiring'],
'control_theory': ['Control Theory', 'Waveprobe Control', 'Waveprobe QUBO', 'KDA Control',
'Homeostatic Control', 'WaveformWaveprobePipeline', 'Waveprobe'],
'semantics': ['Semantics', 'S3C', 'S3CGeometry', 'S3CResonance', 'S3CManifoldMetaprobe',
'S3CManifoldGeometryMetaprobe', 'S3CUnifiedMetaprobe', 'SemanticMass',
'SemanticRGFlow', 'SpectralField', 'Atoms', 'Bind', 'Bounded Hierarchical Cryptographic Space'],
'scaling': ['ScaleSpace', 'Allometry', 'CorticalScaling', 'ConstructalMuscle', 'CardiacYield'],
'grammar': ['WitnessGrammar', 'Prohibited', 'Constitution', 'EpistemicHonesty',
'TriumvirateEnforcer'],
'routing': ['Routing', 'Manifold Routing', 'Manifold Networking', 'HotPathColdPath',
'RouteCost', 'AbelianSandpileRouting', 'ManifoldNetworking'],
'genomic': ['Genomic Compression', 'GenomicStoichiometric', 'SyntheticGeneticCoding',
'CodonPeptideConsistency', 'CodonOTOM', 'FibonacciEncoding'],
'compression_systems': ['Compression', 'DeltaGCL Compression', 'DeltaGCLCompression',
'CrossModalCompression', 'StreamCompression', 'CompressionControl',
'CompressionEvidence', 'CompressionLossComparison',
'CompressionMaximization', 'CompressionMechanics', 'CompressionMechanicsBridge',
'YangMillsCompression', 'YangMillsCompressionBounds', 'YangMillsLattice',
'YangMillsLatticeSizing', 'YangMillsPerformance',
'PhiShellEncoding', 'VoxelEncoding', 'CR = U_size / C_size'],
'biology_detail': ['Phonon Graph', 'Hormone Derivation', 'Cephalopod Distributed',
'Morpholino', 'miRNA_Shifter', 'TranscriptionShifter',
'TranslationShifter', 'HachimojiShifter', 'AEGISShifter',
'NaturalDNAShifter', 'PNAShifter', 'LNAShifter', 'SplicingShifter',
'PrionShifter', 'SpiegelmerShifter'],
'chaos': ['Chaotic Dynamics', 'Logistic Map', 'LogisticMapShifter', 'DSE',
'DeterministicStochasticEngine', 'Stochastic Processes', 'MasterEquation',
'PopulationChaosDynamics'],
'geometry': ['GWL Riemannian Geometry', 'GWL Rotation', 'GWL Temporal', 'GWL State Space',
'GWL Throat', 'GWL Connection', 'GWL Coordinate Charts', 'GWL Geodesic Integration',
'GWL Geodesic Integration (Integrated)', 'GWL Chiral Interaction', 'GWL Ternary State',
'Geometric Algebra', 'PIST', 'Dyson Swarm Geodesics', 'Virtual Alcubierre',
'Non-Euclidean UV QUBO', 'Manifold Dynamics', 'Constraint Geometry'],
'verification': ['Verification', 'BaselineTest', 'ConservationTest', 'CostEffectiveVerification',
'GPUVerificationMetaprobe', 'Lean4ImprovementProofs', 'AVMRProofs',
'AVMRTheorems', 'AVMRFrameworkMetaprobe', 'AVMRInformation', 'AVMR',
'AgenticTheorems', 'Constitution', 'CasimirMetaprobe', 'DiffusionSNRBias',
'DSPTranslation', 'CrossDimensionalFilter', 'EnergyGradientSignal',
'MISignal', 'HotPathColdPath', 'Adaptation Theory', 'Dynamic Canal Theory',
'ManifoldNetworking', 'CalibratedKernel'],
'network': ['Network Theory', 'Manifold Networking', 'CooperativeLUT', 'SIMDBranchPrediction',
'TopologyResilience', 'TopologyFractalEncoding', 'TopologyGoldenSpiral',
'TopologyPhinary', 'TopologyDlessScalar', 'TopologyDomainAlignment',
'TopologyNode', 'DomainKernel', 'DomainModelIntegration', 'DomainRegistryAlignment',
'DomainState', 'ConflictResolution', 'ExternalConnectors'],
'algorithms': ['Algorithms', 'SolitonSearch', 'SolitonTensor', 'Search', 'SwarmCompetition',
'TileFlipConsensus'],
'stochastic': ['Stochastic Processes', 'Stochastic PDEs', 'MasterEquation', 'PopulationChaosDynamics'],
'n_body': ['N-Space Dynamics', 'NKCoupling', 'CoulombComplexity', 'CellSnowballConstraint',
'ElectrostaticsMetaprobe'],
'origin': ['Origin', 'Emergence System', 'UniverseCollisionConsensus', 'BHOCS', 'HybridTSMPISTTorus'],
'NES': ['NES', 'AnalogDSP', 'VideoSynth', 'TemporalSuperSample', 'VirtualDisplay',
'VoltageMath', 'MinimalOISC', 'OISC', 'CartridgeOISC', 'NanoKernel',
'Metaprobe', 'UnifiedMath'],
'genetic_encoding': ['HachimojiShifter', 'AEGISShifter', 'NaturalDNAShifter',
'TranscriptionShifter', 'TranslationShifter', 'PNAShifter',
'LNAShifter', 'SplicingShifter', 'PrionShifter',
'SpiegelmerShifter', 'miRNA_Shifter', 'MorpholinoShifter',
'SyntheticGeneticCoding', 'CodonPeptideConsistency', 'CodonOTOM'],
'thermodynamics': ['Thermodynamic', 'Thermodynamics', 'Informatic Stress', 'KDA Physics',
'QCL Energy', 'BEA Thermo Bridge', 'ThermodynamicSort'],
'protein': ['Protein Folding', 'BioRxivFormalization', 'ProteinTemplate',
'Protein Binding'],
'epidemiology': ['Epidemiology', 'Epidemiological', 'EpidemiologicalTrophic'],
'oceanography': ['Oceanography', 'Marine Biology', 'Fluid Dynamics', 'Desalination'],
}
# Now build domain -> family list
domain_to_families = defaultdict(list)
assigned = set()
for domain, fams in domains_map.items():
for f in fams:
domain_to_families[domain].append(f)
assigned.add(f)
# Also build a reverse mapping for output
print("// DOMAIN EXPANSION MAP —", len(assigned), "families assigned to", len(domain_to_families), "domains")
print()
for dom in sorted(domain_to_families.keys()):
fams = domain_to_families[dom]
print(f" // {dom}{len(fams)} families")
for f in sorted(fams):
info = fam_info.get(f, {'purposes': ['N/A']})
purp = info['purposes'][0][:80] if info['purposes'] else 'N/A'
print(f" // {f}")

View file

@ -1,224 +0,0 @@
#!/usr/bin/env python3
"""Validate Equation Forest routing registry and toy Goxel route requests.
Status: HOLD / workbench projection.
This script validates the machine-readable routing grammar. It does not prove
physics, topology, or semantic truth.
"""
from __future__ import annotations
import argparse
import json
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterable
REQUIRED_KERNEL_FIELDS = {
"kernel_id",
"domain_class",
"display_equation",
"variables",
"bucket",
"hyper_term",
"functional_role",
"claim_state",
"authority_scope",
"allowed_runtimes",
"blocked_usages",
"receipt_refs",
}
ALLOWED_BUCKETS = {"GEOMETRY", "FLUID", "NEURAL", "ENTROPY", "TOPOLOGY", "AUXILIARY"}
TOY_ROUTE_RULES = {
"shockwave_goxel": {
"required_buckets": {"FLUID", "ENTROPY"},
"recommended_kernels": {
"Burgers_Inviscid",
"Burgers_Viscous",
"Navier_Stokes_Incompressible",
"Shannon_Entropy",
"Landauer_Bound",
},
"description": "Shockwave Goxel should route through fluid dynamics plus entropy/cost gates.",
},
"near_miss_sidon_goxel": {
"required_buckets": {"TOPOLOGY", "ENTROPY"},
"recommended_kernels": {
"RGFlow_Admissibility",
"Genome18_Address",
"Shannon_Entropy",
"Landauer_Bound",
},
"description": "Near-miss Sidon Goxel should preserve collision metadata and route through topology plus entropy gates.",
},
"signal_surprise_goxel": {
"required_buckets": {"NEURAL", "ENTROPY"},
"recommended_kernels": {
"NII_Surprise",
"S3C_Codec",
"Shannon_Entropy",
},
"description": "Signal-surprise Goxel should route through residual/signal and entropy accounting.",
},
}
@dataclass(frozen=True)
class ValidationResult:
ok: bool
messages: list[str]
def load_json(path: Path) -> dict[str, Any]:
try:
with path.open("r", encoding="utf-8") as handle:
data = json.load(handle)
except FileNotFoundError as exc:
raise SystemExit(f"Missing file: {path}") from exc
except json.JSONDecodeError as exc:
raise SystemExit(f"Invalid JSON in {path}: {exc}") from exc
if not isinstance(data, dict):
raise SystemExit(f"Expected JSON object at root of {path}")
return data
def validate_kernel_registry(registry: dict[str, Any]) -> ValidationResult:
messages: list[str] = []
kernels = registry.get("kernels")
if not isinstance(kernels, list):
return ValidationResult(False, ["registry.kernels must be a list"])
seen: set[str] = set()
bucket_counts: dict[str, int] = {bucket: 0 for bucket in ALLOWED_BUCKETS}
for index, kernel in enumerate(kernels):
if not isinstance(kernel, dict):
messages.append(f"kernel[{index}] is not an object")
continue
missing = sorted(REQUIRED_KERNEL_FIELDS - set(kernel))
if missing:
messages.append(f"kernel[{index}] missing fields: {', '.join(missing)}")
kernel_id = kernel.get("kernel_id")
if not isinstance(kernel_id, str) or not kernel_id:
messages.append(f"kernel[{index}] has invalid kernel_id")
elif kernel_id in seen:
messages.append(f"duplicate kernel_id: {kernel_id}")
else:
seen.add(kernel_id)
bucket = kernel.get("bucket")
if bucket not in ALLOWED_BUCKETS:
messages.append(f"kernel {kernel_id!r} has invalid bucket: {bucket!r}")
else:
bucket_counts[bucket] += 1
if kernel.get("claim_state") not in {"HOLD", "V_scope", "REVIEWED", "CANONICAL_LEAN", "QUARANTINE"}:
messages.append(f"kernel {kernel_id!r} has invalid claim_state: {kernel.get('claim_state')!r}")
if kernel.get("authority_scope") not in {
"workbench_projection",
"simulation_only",
"receipt_backed",
"canonical_lean",
"external_source",
"safety_policy",
}:
messages.append(f"kernel {kernel_id!r} has invalid authority_scope: {kernel.get('authority_scope')!r}")
if len(seen) != 15:
messages.append(f"expected 15 kernels, found {len(seen)}")
for required_bucket in {"FLUID", "NEURAL", "ENTROPY", "TOPOLOGY"}:
if bucket_counts[required_bucket] == 0:
messages.append(f"required bucket has no kernels: {required_bucket}")
if messages:
return ValidationResult(False, messages)
return ValidationResult(True, ["kernel registry passed structural validation"])
def kernel_index(registry: dict[str, Any]) -> dict[str, dict[str, Any]]:
return {kernel["kernel_id"]: kernel for kernel in registry.get("kernels", []) if isinstance(kernel, dict) and "kernel_id" in kernel}
def buckets_for_kernels(kernels: Iterable[str], index: dict[str, dict[str, Any]]) -> set[str]:
buckets: set[str] = set()
for kernel_id in kernels:
kernel = index.get(kernel_id)
if kernel:
buckets.add(str(kernel.get("bucket")))
return buckets
def validate_toy_route(registry: dict[str, Any], route_name: str, selected_kernels: list[str] | None) -> ValidationResult:
messages: list[str] = []
rules = TOY_ROUTE_RULES.get(route_name)
if rules is None:
valid = ", ".join(sorted(TOY_ROUTE_RULES))
return ValidationResult(False, [f"unknown route {route_name!r}; valid routes: {valid}"])
index = kernel_index(registry)
selected = set(selected_kernels or rules["recommended_kernels"])
unknown = sorted(kernel for kernel in selected if kernel not in index)
if unknown:
messages.append(f"unknown selected kernels: {', '.join(unknown)}")
actual_buckets = buckets_for_kernels(selected, index)
missing_buckets = set(rules["required_buckets"]) - actual_buckets
if missing_buckets:
messages.append(f"route {route_name} missing required buckets: {', '.join(sorted(missing_buckets))}")
for kernel_id in sorted(selected):
kernel = index.get(kernel_id)
if not kernel:
continue
if "toy_route_validator" not in kernel.get("allowed_runtimes", []):
messages.append(f"kernel {kernel_id} is not marked for toy_route_validator runtime")
if messages:
return ValidationResult(False, messages)
return ValidationResult(True, [
rules["description"],
f"selected kernels: {', '.join(sorted(selected))}",
f"covered buckets: {', '.join(sorted(actual_buckets))}",
"route passed routing-grammar validation only; no physical/theorem claim is implied",
])
def main(argv: list[str]) -> int:
parser = argparse.ArgumentParser(description="Validate Equation Forest routing registry and toy routes.")
parser.add_argument("--registry", type=Path, default=Path("registry/equation_forest_kernels.json"))
parser.add_argument("--route", choices=sorted(TOY_ROUTE_RULES), help="Optional toy route to validate")
parser.add_argument("--kernels", nargs="*", help="Optional explicit kernel IDs for route validation")
args = parser.parse_args(argv)
registry = load_json(args.registry)
structural = validate_kernel_registry(registry)
for message in structural.messages:
print(message)
if not structural.ok:
return 1
if args.route:
route_result = validate_toy_route(registry, args.route, args.kernels)
for message in route_result.messages:
print(message)
if not route_result.ok:
return 2
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))

View file

@ -1,421 +0,0 @@
#!/usr/bin/env python3
"""
ENE Artifact Salvage Extractor
==============================
Purpose
-------
Extract useful, sanitized research artifacts from legacy ENE archive records
without carrying forward false claims, contaminated phrasing, or obsolete math.
Core rule
---------
Wrong math may still contain a valid artifact. Salvage the artifact, not the
false claim.
This tool is deliberately conservative. It does not emit the old claim body by
default. It emits sanitized receipts containing only:
- provenance pointers and hashes;
- classification of why the record is interesting;
- sanitized reusable core;
- failure/contamination flags;
- current-stack mapping hints;
- non-claims.
It is intended for legacy review passes where the operator does not want old
patterns to infect current reasoning.
"""
from __future__ import annotations
import argparse
import dataclasses
import hashlib
import json
import re
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Tuple
EXTRACTION_VERSION = "ene_artifact_salvage_extractor_v0.1"
@dataclasses.dataclass(frozen=True)
class SalvageConfig:
max_records: Optional[int]
min_score: int
include_rejected: bool
emit_legacy_snippets: bool
@dataclasses.dataclass
class SalvageReceipt:
salvage_id: str
source_archive_id: str
source_type: str
source_file: str
source_hash: str
extraction_version: str
interest_score: int
salvage_class: str
interest_reasons: List[str]
contamination_flags: List[str]
sanitized_core: str
current_stack_mapping: Dict[str, List[str]]
recommended_next_action: str
allowed_claims: List[str]
non_claims: List[str]
legacy_snippet: Optional[str] = None
def stable_hash(obj: Any, n: int = 32) -> str:
data = json.dumps(obj, sort_keys=True, ensure_ascii=False, default=str)
return hashlib.sha256(data.encode("utf-8")).hexdigest()[:n]
def load_archive(path: Path) -> List[Dict[str, Any]]:
archive = json.loads(path.read_text(encoding="utf-8"))
records = archive.get("records", archive)
if isinstance(records, dict):
return list(records.values())
if isinstance(records, list):
return records
raise ValueError("Archive must contain records as a dict or list")
INTEREST_PATTERNS: Dict[str, List[str]] = {
"compression_invariant": [
r"compression", r"codec", r"entropy", r"hutter", r"canonical", r"precompression",
r"arithmetic coding", r"log[- ]?loss", r"minimum description", r"md[l]",
],
"manifold_topology": [
r"manifold", r"topolog", r"torsion", r"curvature", r"ricci", r"fiber",
r"bundle", r"geodesic", r"metric", r"basin", r"attractor",
],
"avm_or_bytecode": [
r"\bAVM\b", r"bytecode", r"stack", r"kernel", r"trace", r"q16", r"fixed[- ]?point",
r"deterministic", r"golden trace",
],
"nuvmap_addressing": [
r"NUVMAP", r"address", r"projection", r"virtual memory", r"uv map", r"coordinate",
r"spectral mode", r"witness", r"receipt",
],
"s3c_sphinx_gcl": [
r"S3C", r"AngrySphinx", r"sphinx", r"GCL", r"grammar", r"lawful", r"gate",
r"quarantine", r"scar", r"throttle", r"shell",
],
"photonic_witness": [
r"photonic", r"photon", r"Quandela", r"Perceval", r"boson", r"HOM", r"linear optical",
r"spectral witness", r"sampling",
],
"lean_formalization": [
r"Lean", r"theorem", r"proof", r"sorry", r"formal", r"lemma", r"lake build",
r"Q16_16", r"Semantics\.",
],
"hardware_loopback": [
r"FPGA", r"Tang Nano", r"UART", r"loopback", r"hardware", r"synthesis", r"bit[- ]?exact",
r"Verilog", r"RTL",
],
"negative_control_or_failure": [
r"failed", r"wrong", r"gap", r"limitation", r"counterexample", r"negative control",
r"overclaim", r"not prove", r"boundary", r"quarantine",
],
"cross_domain_adapter": [
r"adapter", r"cross[- ]?domain", r"translation", r"mapping", r"homology", r"equivalence",
r"route", r"bridge", r"workflow", r"process graph",
],
"mass_number_diat": [
r"DIAT", r"mass number", r"square", r"shell", r"gap", r"parity", r"mirror",
r"codon", r"distance to", r"integer",
],
}
CONTAMINATION_PATTERNS: Dict[str, List[str]] = {
"proof_inflation": [
r"proved.*navier", r"proved.*millennium", r"solved.*burgers", r"solved.*navier",
r"global regularity", r"proof of everything", r"final theorem",
],
"physics_overclaim": [
r"actual mass", r"semantic mass is physical", r"quantum advantage", r"photonic.*solve",
r"violates", r"free energy", r"perpetual",
],
"ai_as_validator": [
r"LLM.*proved", r"AI.*validated", r"model agrees therefore", r"consensus of models",
],
"float_hot_path": [
r"float.*hot", r"f32", r"f64", r"floating point.*authoritative",
],
"unsafe_or_sensitive": [
r"credential", r"token", r"secret", r"private key", r"password", r"exploit", r"payload",
r"dox", r"retaliat", r"malware",
],
"medical_or_financial_overclaim": [
r"cures", r"diagnoses", r"guaranteed profit", r"market oracle", r"investment advice",
],
}
STACK_MAPPING: Dict[str, List[Tuple[str, str]]] = {
"nuvmap": [
("nuvmap_addressing", "Candidate for address/projection receipt or non-uniform routing surface"),
("photonic_witness", "Project witness statistics into addressable spectral coordinates"),
("hardware_loopback", "Project hardware trace locations into NUVMAP receipt space"),
],
"avm": [
("avm_or_bytecode", "Candidate for deterministic Q16.16 scoring or trace replay"),
("compression_invariant", "Candidate for AVM kernel scoring/compression law test"),
("hardware_loopback", "Candidate for bit-exact replay target"),
],
"s3c": [
("s3c_sphinx_gcl", "Candidate for shell/codon expansion gate"),
("mass_number_diat", "Candidate for shell pressure or square-gap codonization"),
("cross_domain_adapter", "Candidate route through compressed adapter space"),
],
"metaprobe": [
("negative_control_or_failure", "Candidate failure surface / harder probe family"),
("cross_domain_adapter", "Candidate for adapter stress-test generation"),
],
"gcl": [
("s3c_sphinx_gcl", "Candidate grammar/lawfulness boundary"),
("negative_control_or_failure", "Candidate claim-boundary rule or forbidden region"),
],
"lean": [
("lean_formalization", "Candidate local theorem or proof-boundary target"),
("avm_or_bytecode", "Candidate bytecode correctness theorem"),
],
}
SAFE_WORD_REPLACEMENTS = [
(re.compile(r"\bproved\b", re.I), "claimed"),
(re.compile(r"\bsolved\b", re.I), "attempted"),
(re.compile(r"\bguarantees?\b", re.I), "suggests"),
(re.compile(r"\bquantum advantage\b", re.I), "photonic sampling behavior"),
(re.compile(r"\bglobal regularity\b", re.I), "bounded formal target"),
]
def normalize_text(record: Dict[str, Any]) -> str:
text = record.get("extracted_text")
if not text:
raw = record.get("raw_content", {})
text = json.dumps(raw, ensure_ascii=False, default=str)
text = str(text)
# Remove obvious secret-like long tokens from any optional legacy snippet path.
text = re.sub(r"(?i)(api[_-]?key|token|password|secret)\s*[:=]\s*[^\s]+", r"\1: [REDACTED]", text)
text = re.sub(r"[A-Za-z0-9_\-]{48,}", "[LONG_TOKEN_REDACTED]", text)
return text
def match_categories(text: str, patterns: Dict[str, List[str]]) -> Dict[str, int]:
out: Dict[str, int] = {}
for category, pats in patterns.items():
count = 0
for pat in pats:
count += len(re.findall(pat, text, flags=re.I))
if count:
out[category] = count
return out
def summarize_core(categories: Dict[str, int], contamination: Dict[str, int]) -> str:
if not categories:
return "No sanitized reusable core detected by the current rules."
ordered = sorted(categories.items(), key=lambda kv: (-kv[1], kv[0]))
top = [k for k, _ in ordered[:4]]
phrases = []
if "compression_invariant" in top:
phrases.append("compression/invariant extraction candidate")
if "manifold_topology" in top:
phrases.append("manifold or topological structure candidate")
if "avm_or_bytecode" in top:
phrases.append("deterministic AVM/bytecode scoring candidate")
if "nuvmap_addressing" in top:
phrases.append("NUVMAP address/projection candidate")
if "s3c_sphinx_gcl" in top:
phrases.append("S3C/GCL/AngrySphinx routing candidate")
if "photonic_witness" in top:
phrases.append("photonic witness or sampling candidate")
if "lean_formalization" in top:
phrases.append("Lean/formalization target candidate")
if "hardware_loopback" in top:
phrases.append("hardware loopback or bit-exact replay candidate")
if "negative_control_or_failure" in top:
phrases.append("failure surface / negative-control candidate")
if "cross_domain_adapter" in top:
phrases.append("cross-domain adapter candidate")
if "mass_number_diat" in top:
phrases.append("DIAT/mass-number shell-pressure candidate")
if not phrases:
phrases = [k.replace("_", " ") for k in top]
sentence = "Sanitized artifact core: " + "; ".join(phrases) + "."
if contamination:
sentence += " Original claim language requires quarantine before reuse."
return sentence
def salvage_class(categories: Dict[str, int], contamination: Dict[str, int], score: int) -> str:
if contamination.get("unsafe_or_sensitive"):
return "QUARANTINE_ONLY"
if contamination and categories:
return "SALVAGEABLE_CORE_WITH_CLAIM_SCAR"
if categories.get("negative_control_or_failure"):
return "NEGATIVE_CONTROL_OR_FAILURE_SURFACE"
if categories.get("cross_domain_adapter") or categories.get("mass_number_diat"):
return "RECONTEXTUALIZE_WITH_CURRENT_STACK"
if score >= 6:
return "SALVAGEABLE_CORE"
return "LOW_CONFIDENCE_REVIEW"
def build_stack_mapping(categories: Dict[str, int]) -> Dict[str, List[str]]:
mapping: Dict[str, List[str]] = {}
for layer, rules in STACK_MAPPING.items():
hits = [msg for category, msg in rules if category in categories]
if hits:
mapping[layer] = hits
return mapping
def recommended_action(cls: str) -> str:
return {
"QUARANTINE_ONLY": "Do not reintroduce content. Preserve only hash/provenance and safety flag.",
"SALVAGEABLE_CORE_WITH_CLAIM_SCAR": "Extract sanitized core, preserve old claim only as a non-authoritative scar, and require new evidence before reuse.",
"NEGATIVE_CONTROL_OR_FAILURE_SURFACE": "Convert into a negative control, MetaProbe target, or GCL boundary test.",
"RECONTEXTUALIZE_WITH_CURRENT_STACK": "Map into current NUVMAP/AVM/S3C vocabulary and discard obsolete derivation language.",
"SALVAGEABLE_CORE": "Promote as sanitized candidate artifact with receipt and claim boundary.",
"LOW_CONFIDENCE_REVIEW": "Keep only if a human or downstream model finds a concrete invariant.",
}.get(cls, "Review conservatively.")
def sanitize_snippet(text: str, max_len: int = 700) -> str:
snippet = re.sub(r"\s+", " ", text).strip()[:max_len]
for pat, repl in SAFE_WORD_REPLACEMENTS:
snippet = pat.sub(repl, snippet)
return snippet
def score_record(categories: Dict[str, int], contamination: Dict[str, int]) -> int:
score = 0
for cat, count in categories.items():
score += min(count, 5)
# Failures can be interesting, but severe contamination suppresses promotion.
if contamination.get("unsafe_or_sensitive"):
score -= 999
elif contamination:
score -= min(sum(contamination.values()), 5)
return score
def make_receipt(record: Dict[str, Any], cfg: SalvageConfig) -> Optional[SalvageReceipt]:
text = normalize_text(record)
categories = match_categories(text, INTEREST_PATTERNS)
contamination = match_categories(text, CONTAMINATION_PATTERNS)
score = score_record(categories, contamination)
if score < cfg.min_score and not (cfg.include_rejected and categories):
return None
src_id = str(record.get("archive_id") or stable_hash(record))
src_hash = str(record.get("content_hash") or stable_hash(record.get("raw_content", text)))
cls = salvage_class(categories, contamination, score)
sanitized_core = summarize_core(categories, contamination)
mapping = build_stack_mapping(categories)
receipt_obj = {
"source_archive_id": src_id,
"source_hash": src_hash,
"categories": categories,
"contamination": contamination,
"version": EXTRACTION_VERSION,
}
salvage_id = "ene_salvage_" + stable_hash(receipt_obj, 24)
return SalvageReceipt(
salvage_id=salvage_id,
source_archive_id=src_id,
source_type=str(record.get("source_type", "unknown")),
source_file=str(record.get("source_file", "unknown")),
source_hash=src_hash,
extraction_version=EXTRACTION_VERSION,
interest_score=score,
salvage_class=cls,
interest_reasons=sorted(categories.keys()),
contamination_flags=sorted(contamination.keys()),
sanitized_core=sanitized_core,
current_stack_mapping=mapping,
recommended_next_action=recommended_action(cls),
allowed_claims=[
"This receipt identifies a sanitized reusable artifact candidate.",
"This receipt may support recontextualization, negative-control generation, or theorem-target discovery.",
],
non_claims=[
"Does not validate the original math.",
"Does not preserve original proof claims.",
"Does not imply domain equivalence.",
"Does not imply PDE proof, quantum advantage, market prediction, or hardware correctness.",
],
legacy_snippet=sanitize_snippet(text) if cfg.emit_legacy_snippets else None,
)
def extract(archive_path: Path, out_path: Path, cfg: SalvageConfig) -> Dict[str, Any]:
records = load_archive(archive_path)
receipts: List[SalvageReceipt] = []
for i, record in enumerate(records):
if cfg.max_records is not None and i >= cfg.max_records:
break
rec = make_receipt(record, cfg)
if rec is not None:
receipts.append(rec)
receipts.sort(key=lambda r: (-r.interest_score, r.salvage_class, r.source_archive_id))
payload = {
"meta": {
"extraction_version": EXTRACTION_VERSION,
"source_archive": str(archive_path),
"total_input_records": len(records),
"total_salvage_receipts": len(receipts),
"min_score": cfg.min_score,
"legacy_snippets_emitted": cfg.emit_legacy_snippets,
"policy": "sanitize_by_default_salvage_artifact_not_false_claim",
},
"receipts": [dataclasses.asdict(r) for r in receipts],
}
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(payload, indent=2, ensure_ascii=False), encoding="utf-8")
return payload
def main() -> int:
parser = argparse.ArgumentParser(description="Extract sanitized salvage receipts from ENE archive")
parser.add_argument("archive", type=Path, help="Path to ene_complete_archive.json")
parser.add_argument("--out", type=Path, default=Path("shared-data/ene_salvage_receipts.json"))
parser.add_argument("--max-records", type=int, default=None)
parser.add_argument("--min-score", type=int, default=2)
parser.add_argument("--include-rejected", action="store_true")
parser.add_argument("--emit-legacy-snippets", action="store_true", help="Unsafe for clean-room review; off by default")
args = parser.parse_args()
cfg = SalvageConfig(
max_records=args.max_records,
min_score=args.min_score,
include_rejected=args.include_rejected,
emit_legacy_snippets=args.emit_legacy_snippets,
)
payload = extract(args.archive, args.out, cfg)
print(json.dumps(payload["meta"], indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())

View file

@ -1,107 +0,0 @@
#!/usr/bin/env python3
"""Emit a provenance-bearing selection-metrics receipt.
Usage:
python emit_selection_receipt.py \
--fixture ../../data/fixtures/genetics/selection_metrics_fixture.json \
--out ../../out/receipts/selection_metrics_demo_v0.receipt.json
The emitted receipt is suitable for plumbing tests only unless the fixture has
real-data provenance. Synthetic fixtures must never promote genetics claims.
"""
from __future__ import annotations
import argparse
import hashlib
import json
from dataclasses import asdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from selection_metrics import (
TajimasDInput,
hudson_fst,
mass_number_selection_pressure,
tajimas_d,
)
RECEIPT_SCHEMA_VERSION = "selection-metrics-receipt/v0"
def sha256_text(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def load_fixture(path: Path) -> tuple[dict[str, Any], str]:
raw = path.read_text(encoding="utf-8")
return json.loads(raw), sha256_text(raw)
def build_receipt(fixture: dict[str, Any], fixture_sha256: str, fixture_path: Path) -> dict[str, Any]:
tajima_input = TajimasDInput(**fixture["tajimas_d_input"])
fst_input = fixture["hudson_fst_input"]
tajima = tajimas_d(tajima_input)
fst = hudson_fst(
within_pairwise_differences=fst_input["within_pairwise_differences"],
between_pairwise_differences=fst_input["between_pairwise_differences"],
)
pressure = mass_number_selection_pressure(tajima, fst)
provenance = fixture.get("provenance", {})
promotion_policy = fixture.get("promotion_policy", {})
synthetic = provenance.get("source") == "synthetic_fixture"
may_promote = bool(promotion_policy.get("may_promote_claims", False)) and not synthetic
receipt = {
"schema": RECEIPT_SCHEMA_VERSION,
"receipt_kind": "selection_metrics_engineering_scaffold",
"claim_state": "ENGINEERING_SCAFFOLD",
"fixture": {
"path": str(fixture_path),
"fixture_id": fixture.get("fixture_id"),
"sha256": fixture_sha256,
"provenance": provenance,
},
"inputs": {
"tajimas_d_input": fixture["tajimas_d_input"],
"hudson_fst_input": fst_input,
},
"results": {
"tajimas_d": asdict(tajima),
"hudson_fst": asdict(fst),
"mass_number_selection_pressure_proxy": pressure,
},
"promotion": {
"may_promote_claims": may_promote,
"reason": (
"Synthetic fixture; validates code path only."
if synthetic
else promotion_policy.get("reason", "Promotion requires external review.")
),
},
"generated_at_utc": datetime.now(timezone.utc).isoformat(),
}
receipt["receipt_sha256"] = sha256_text(json.dumps(receipt, sort_keys=True))
return receipt
def main() -> None:
parser = argparse.ArgumentParser(description="Emit selection metrics receipt JSON")
parser.add_argument("--fixture", required=True, type=Path, help="Path to fixture JSON")
parser.add_argument("--out", required=True, type=Path, help="Output receipt path")
args = parser.parse_args()
fixture, fixture_sha256 = load_fixture(args.fixture)
receipt = build_receipt(fixture, fixture_sha256, args.fixture)
args.out.parent.mkdir(parents=True, exist_ok=True)
args.out.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8")
print(json.dumps({"wrote": str(args.out), "receipt_sha256": receipt["receipt_sha256"]}, indent=2))
if __name__ == "__main__":
main()

View file

@ -1,167 +0,0 @@
#!/usr/bin/env python3
"""Selection metrics scaffold for the OTOM genetics information substrate.
This module is intentionally small and dependency-free. It turns the genetics
boundary record's first promotion candidate into an executable path:
- Tajima's D from segregating sites, pairwise differences, and sample size
- Hudson-style FST from within-population and between-population differences
Claim-state boundary:
These functions are engineering scaffolds. They do not by themselves prove
selection, ancestry, or fitness claims. Promotion requires provenance-bearing
data fixtures and receipts.
"""
from __future__ import annotations
from dataclasses import dataclass
from math import sqrt
from typing import Iterable, Sequence
@dataclass(frozen=True)
class TajimasDInput:
"""Inputs for Tajima's D.
sample_size:
Number of sampled haploid sequences. Must be at least 2.
segregating_sites:
Count of segregating sites S. Must be non-negative.
pairwise_differences:
Average number of pairwise differences pi across the region.
"""
sample_size: int
segregating_sites: int
pairwise_differences: float
@dataclass(frozen=True)
class TajimasDResult:
theta_watterson: float
tajimas_d: float
variance: float
claim_state: str = "ENGINEERING_SCAFFOLD"
@dataclass(frozen=True)
class FstResult:
within_mean: float
between_mean: float
fst: float
claim_state: str = "ENGINEERING_SCAFFOLD"
def _validate_nonnegative(name: str, value: float) -> None:
if value < 0:
raise ValueError(f"{name} must be non-negative, got {value!r}")
def harmonic_sum(n: int, power: int = 1) -> float:
"""Return sum_{i=1}^{n} 1 / i^power."""
if n < 1:
raise ValueError(f"n must be >= 1, got {n!r}")
if power < 1:
raise ValueError(f"power must be >= 1, got {power!r}")
return sum(1.0 / (i**power) for i in range(1, n + 1))
def tajimas_d(data: TajimasDInput) -> TajimasDResult:
"""Compute Tajima's D using the standard finite-sample constants.
D = (pi - S/a1) / sqrt(e1*S + e2*S*(S-1))
Returns D=0 when S=0 and variance is zero, because there is no segregating
signal to evaluate. Callers should treat that as non-promoting evidence.
"""
n = data.sample_size
s = data.segregating_sites
pi = data.pairwise_differences
if n < 2:
raise ValueError(f"sample_size must be >= 2, got {n!r}")
_validate_nonnegative("segregating_sites", s)
_validate_nonnegative("pairwise_differences", pi)
a1 = harmonic_sum(n - 1, 1)
a2 = harmonic_sum(n - 1, 2)
b1 = (n + 1) / (3 * (n - 1))
b2 = (2 * (n * n + n + 3)) / (9 * n * (n - 1))
c1 = b1 - (1 / a1)
c2 = b2 - ((n + 2) / (a1 * n)) + (a2 / (a1 * a1))
e1 = c1 / a1
e2 = c2 / ((a1 * a1) + a2)
theta_w = s / a1
variance = e1 * s + e2 * s * (s - 1)
if variance <= 0:
return TajimasDResult(theta_watterson=theta_w, tajimas_d=0.0, variance=variance)
return TajimasDResult(
theta_watterson=theta_w,
tajimas_d=(pi - theta_w) / sqrt(variance),
variance=variance,
)
def _mean(values: Sequence[float], name: str) -> float:
if not values:
raise ValueError(f"{name} must contain at least one value")
for value in values:
_validate_nonnegative(name, value)
return sum(values) / len(values)
def hudson_fst(within_pairwise_differences: Sequence[float], between_pairwise_differences: Sequence[float]) -> FstResult:
"""Compute a simple Hudson-style FST estimate.
FST = 1 - mean_within / mean_between
The input values should be pairwise sequence differences or comparable
genetic distances. If between-population divergence is zero, FST is returned
as zero to avoid division by zero; callers should treat that as non-promoting.
"""
within = _mean(within_pairwise_differences, "within_pairwise_differences")
between = _mean(between_pairwise_differences, "between_pairwise_differences")
if between <= 0:
return FstResult(within_mean=within, between_mean=between, fst=0.0)
fst = 1.0 - (within / between)
if fst < 0:
fst = 0.0
if fst > 1:
fst = 1.0
return FstResult(within_mean=within, between_mean=between, fst=fst)
def mass_number_selection_pressure(tajima: TajimasDResult, fst: FstResult) -> float:
"""Toy MassNumber pressure proxy for promotion triage.
This is deliberately not a formal MassNumber implementation. It gives a
monotone engineering score for issue triage:
abs(TajimaD) + FST
The score must be replaced by the Lean/Q16_16 MassNumber gate before any
reviewed claim is promoted.
"""
return abs(tajima.tajimas_d) + fst.fst
def _demo() -> None:
tajima = tajimas_d(TajimasDInput(sample_size=10, segregating_sites=12, pairwise_differences=4.2))
fst = hudson_fst(within_pairwise_differences=[1.2, 1.5, 1.1], between_pairwise_differences=[3.0, 3.3, 2.8])
print("tajimas_d", tajima)
print("hudson_fst", fst)
print("selection_pressure_proxy", mass_number_selection_pressure(tajima, fst))
if __name__ == "__main__":
_demo()

View file

@ -122,7 +122,7 @@ theorem fammBindReflexive (bank : FAMMBank) (mode : FAMMAccessMode) (address : N
(fammBind bank mode address).lawful = (fammBind bank mode address).lawful := by
rfl
/-- MORE FAMM Architecture Integration
/- MORE FAMM Architecture Integration
The unified architecture requires capability-based memory isolation
and thermal management for safe operation. These extensions integrate
@ -134,7 +134,7 @@ structure FAMMCapabilityCell where
data : Q16_16
delay : Q16_16
owner : UInt8 -- Segment ID (capability-based access)
accessRights : UInt4 -- READ | WRITE | PRUNE | EXECUTE
accessRights : UInt8 -- READ | WRITE | PRUNE | EXECUTE
delayMass : Q16_16
delayWeight : Q16_16
@ -183,7 +183,7 @@ deriving Repr, Inhabited
def fammMetadataCollapse (bank : FAMMThermalBank) : FAMMCollapsedState :=
{ cellCount := bank.cells.size,
bannedCount := 0, -- TODO: Track pruned cells
energySignature := bank.cells.foldl (λ acc cell => acc + cell.delayMass) Q16_16.ofInt 0,
energySignature := bank.cells.foldl (λ acc cell => acc + cell.delayMass) Q16_16.zero,
thermalResidual := bank.thermalBudget - bank.currentStress,
ownerSegment := 0 } -- TODO: Per-segment ownership

View file

@ -1,61 +0,0 @@
import os
import sys
from pathlib import Path
# Add project root to path
sys.path.append("/home/allaun/Research Stack")
from infra.deepseek_adapter import DeepSeekV4
def solve_famm_sorry():
# Use DeepSeek API for better reliability
api_key = os.environ.get("DEEPSEEK_API_KEY", "")
if not api_key:
raise RuntimeError("DEEPSEEK_API_KEY is required")
client = DeepSeekV4(api_key=api_key, use_local=False)
model = "deepseek-v4-pro"
file_path = "/home/allaun/Documents/Research Stack/0-Core-Formalism/lean/Semantics/Semantics/FAMM.lean"
with open(file_path, 'r') as f:
content = f.read()
prompt = f"""
You are a Lean 4 formalization expert.
The following Lean 4 file `FAMM.lean` has duplicate definitions and 'sorry' axioms.
Please refactor it to:
1. Remove duplicate structures and definitions (e.g., FAMMThermalBank, fammMetadataCollapse).
2. Complete the proof for `famm_compression_property`.
3. Ensure the file is valid Lean 4 code.
File Content:
{content}
Provide the complete refactored file content in a code block.
"""
print(f"Sending request to {model}...")
messages = [{"role": "user", "content": prompt}]
try:
res = client.chat(messages, model=model)
# Check if it's Ollama response format
if "message" in res:
new_content = res["message"]["content"]
else:
new_content = res["choices"][0]["message"]["content"]
# Extract from code block
if "```lean" in new_content:
new_content = new_content.split("```lean")[1].split("```")[0].strip()
elif "```" in new_content:
new_content = new_content.split("```")[1].split("```")[0].strip()
output_path = "/home/allaun/Documents/Research Stack/scratch/FAMM_refactored.lean"
with open(output_path, 'w') as f:
f.write(new_content)
print(f"Refactored file saved to {output_path}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
solve_famm_sorry()

View file

@ -1,215 +0,0 @@
#!/usr/bin/env python3
"""Gist-Pivot PoC
Simulate creating many small "gists" (here just files) containing
fragments of an MSM, demonstrating the evasion technique.
"""
import os
import sys
OUTDIR = "/tmp/gist_pivot"
# define a tiny micro-state machine language with simple ops:
# 0x01 INC ; increment accumulator
# 0x02 DEC ; decrement accumulator
# 0x03 JNZ <offset> ; jump relative if accumulator != 0
# 0xFF HALT ; stop execution
# By default we encode a sample MSM; if a file path is provided on the
# command line, we treat that file's bytes as the payload to hide (e.g.
# a pseudo SSH key).
if len(sys.argv) > 1:
INPUT_PATH = sys.argv[1]
with open(INPUT_PATH, 'rb') as f:
MSM_CODE = f.read()
print(f"[+] Using input file {INPUT_PATH} ({len(MSM_CODE)} bytes) as payload")
else:
MSM_CODE = bytes([0x01, 0x01, 0x02, 0x03, 0xFD, 0xFF])
# split into equal pieces to simulate fragmentation
def split_fragments(code, pieces):
size = len(code) // pieces
fragments = [code[i*size:(i+1)*size] for i in range(pieces-1)]
fragments.append(code[(pieces-1)*size:])
return fragments
# to compress further, map emojis to a small index via a LUT
EMOJI_LUT = {
'⏱️': 0,
'🛑': 1,
'🔧': 2,
'💥': 3,
}
REV_LUT = {v: k for k, v in EMOJI_LUT.items()}
# our MSM representation will now be a sequence of indices (one byte each)
# if MSM_CODE was text it is ignored; instead generate a sample emoji sequence
emoji_sequence = ['⏱️','🛑','🔧','💥']
MSM_CODE = bytes([EMOJI_LUT[e] for e in emoji_sequence])
FRAGMENTS = split_fragments(MSM_CODE, 4)
os.makedirs(OUTDIR, exist_ok=True)
# write fragments locally (for simulation) and optionally POST them if token present
import time
stamp = int(time.time())
for idx in range(len(FRAGMENTS)):
with open(os.path.join(OUTDIR, f"gist_{idx}.txt"), "wb") as f:
f.write(FRAGMENTS[idx])
# create proof content
proof = f"{stamp}: PoC proven\n"
with open(os.path.join(OUTDIR, f"gist_{stamp}.txt"), "w") as f:
f.write(proof)
# if a GitHub token is available, post a real gist with the proof
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
def post_gist(content, description="PoC"):
if not GITHUB_TOKEN:
return None
try:
import json, requests
except ImportError:
return None
payload = {
"description": description,
"public": False,
"files": {"poc.txt": {"content": content}}
}
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
r = requests.post("https://api.github.com/gists", headers=headers, data=json.dumps(payload))
if r.ok:
url = r.json().get("html_url")
print(f"[+] posted gist {url}")
return url
else:
print("[!] failed to post gist", r.status_code, r.text)
return None
if GITHUB_TOKEN:
post_gist(proof, f"PoC ~{stamp}")
# --- collector / reassembly step ---
def reassemble_fragments(directory):
"""Reads all files in lexicographic order and concatenates their contents.
This simulates the attacker-controlled agent pulling down the gists and
rebuilding the original MSM instructions. """
parts = []
for fname in sorted(os.listdir(directory)):
path = os.path.join(directory, fname)
with open(path, "rb") as f:
parts.append(f.read())
combined = b"".join(parts)
return combined
# fast reassembly using bytearray
assembled = bytearray()
for fname in sorted(os.listdir(OUTDIR)):
with open(os.path.join(OUTDIR, fname), "rb") as f:
assembled.extend(f.read())
# minimal output
# decode stream without dictionary lookups inside loop
out = ''.join(REV_LUT.get(b,'?') for b in assembled)
print(out)
# hyperspace calculation per request
import random
def compute_hyperspace():
# represent the shell as a random point in 10D space
shell = [random.random() for _ in range(10)]
# build five 4D tesseracts (hypercubes) with random base positions
tesseracts = []
for _ in range(5):
base = [random.random() for _ in range(4)]
corners = []
for mask in range(16):
corner = [base[j] + ((mask >> j) & 1) for j in range(4)]
corners.append(corner)
tesseracts.append(corners)
# compute shadows by projecting each corner to 3D (drop last coord)
shadows = []
for cube in tesseracts:
shadows.append([[p[0], p[1], p[2]] for p in cube])
# derive metric comparing shell to shadows and system noise
total = 0.0
for shadow in shadows:
for p in shadow:
# distance between first three dims of shell and point
total += sum((shell[i] - p[i]) ** 2 for i in range(3)) ** 0.5
mean = total / (5 * 16)
noise = random.random()
return mean - noise
derived = compute_hyperspace()
print(f"derived hyperspace metric: {derived}")
# optionally encode/transport assembled binary via different protocols
mode = None
if len(sys.argv) > 2:
mode = sys.argv[2].lower()
if mode == "gist":
import base64
encoded = base64.b64encode(assembled).decode('ascii')
print("\n[+] Gist-ready payload (base64):\n", encoded)
elif mode == "ssh":
# encapsulate in fake SSH packet: 4-byte length + data
pkt = len(assembled).to_bytes(4,'big') + assembled
print(f"\n[+] Encapsulated SSH packet ({len(pkt)} bytes):", pkt)
elif mode == "ftp":
# encode for FTP upload (here just write to file)
out = "/tmp/ftp_exfil.bin"
with open(out, 'wb') as f:
f.write(assembled)
print(f"\n[+] Simulated FTP upload by writing data to {out}")
# proceed with state persistence and possible key extraction
# interpret assembled bytes as MSM instructions
def run_msm(code_bytes):
acc = pc = 0
# unrolled simple loop, no prints
while pc < len(code_bytes):
instr = code_bytes[pc]
if instr == 1:
acc += 1; pc += 1
continue
if instr == 2:
acc -= 1; pc += 1
continue
if instr == 3:
offset = code_bytes[pc+1]
pc += offset if acc != 0 else 2
continue
if instr == 0xFF:
break
break
return acc
# decide whether to execute MSM logic or treat payload as key data
if len(sys.argv) > 1:
print("[+] Input file provided; skipping MSM execution and treating assembled data as key material")
acc = None
else:
acc = run_msm(assembled)
if acc is not None:
# save final state (accumulator) to external file to simulate persistence
STATE_FILE = "/tmp/gist_msm_state.bin"
with open(STATE_FILE, "wb") as sf:
# store accumulator as 4-byte little-endian int
sf.write((acc).to_bytes(4, byteorder='little', signed=True))
print(f"Saved MSM state (accumulator={acc}) to {STATE_FILE}")
# ----------------------------------------------------------------
# simulate critical payload extraction: treat assembled bytes as an SSH key
KEY_FILE = "/tmp/extracted_ssh_key.pem"
with open(KEY_FILE, "wb") as kf:
kf.write(b"-----BEGIN RSA PRIVATE KEY-----\n")
# base64-encode the assembled bytes for realism
import base64
kf.write(base64.b64encode(assembled) + b"\n")
kf.write(b"-----END RSA PRIVATE KEY-----\n")
print(f"Simulated SSH key written to {KEY_FILE} (contains {len(assembled)} raw bytes)")

View file

@ -1,557 +0,0 @@
#!/usr/bin/env python3
"""
Hybrid TSM-PIST-Torus System (Verified Lean Specification)
This implementation follows the formal specification in:
0-Core-Formalism/lean/Semantics/Semantics/HybridTSMPISTTorus.lean
The Lean module provides:
- Hybrid architecture combining PIST manifold, 5D torus topology, and genetic compression
- M_{k+1} = M_k F(a,b,ε) + torus_routing
- I = (H × G) × (1 - D/64)
- Expected: 500-1000x acceleration (swarm consensus #1)
This Python shim provides:
- JSON serialization for hybrid TSM state
- Result wrapping for Lean function calls
- No logic (all logic defined in Lean specification)
"""
import json
import time
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from collections import deque
# Q16_16 fixed-point utilities
Q16_ONE = 65536 # 1.0 in Q16_16
Q16_SCALE = 65536.0
def to_q16(value: float) -> int:
"""Convert float to Q16_16 fixed-point"""
return int(value * Q16_SCALE)
def from_q16(q16: int) -> float:
"""Convert Q16_16 fixed-point to float"""
return q16 / Q16_SCALE
@dataclass
class BlitterState:
"""PIST Blitter state (from PistBridge.lean)"""
a: int # Distance from lower perfect square (Q16_16)
b: int # Distance to upper perfect square (Q16_16)
manifold: int # Current manifold value (Q16_16)
stepMask: int # Timestep mask for bitwise operation
def to_dict(self) -> Dict[str, Any]:
return {
'a': from_q16(self.a),
'b': from_q16(self.b),
'manifold': from_q16(self.manifold),
'stepMask': self.stepMask
}
@dataclass
class TorusTopologyState:
"""5D torus topology state (from FiveDTorusTopology.lean)"""
nodes: List # List of TorusNode
dimensionSizes: List[int] # k_0, k_1, k_2, k_3, k_4
dimensions: int # Should be 5
def to_dict(self) -> Dict[str, Any]:
return {
'nodes': [n.to_dict() if hasattr(n, 'to_dict') else str(n) for n in self.nodes],
'dimensionSizes': self.dimensionSizes,
'dimensions': self.dimensions
}
@dataclass
class HybridTSMState:
"""Hybrid TSM state combining PIST manifold and 5D torus topology (Lean: HybridTSMState)"""
pistState: BlitterState
torusState: TorusTopologyState
phase: str # Phase flag (Grounded/Drift/Seismic)
geneticScore: int # Genetic optimization score I (Q16_16)
entropy: int # Entropy H (Q16_16)
genomicComplexity: int # Genomic complexity G (Q16_16)
degeneracy: int # Degeneracy D (0-64)
friction: int # Friction score f
def to_dict(self) -> Dict[str, Any]:
return {
'pistState': self.pistState.to_dict(),
'torusState': self.torusState.to_dict(),
'phase': self.phase,
'geneticScore': from_q16(self.geneticScore),
'entropy': from_q16(self.entropy),
'genomicComplexity': from_q16(self.genomicComplexity),
'degeneracy': self.degeneracy,
'friction': self.friction
}
@dataclass
class HybridTSMAction:
"""Hybrid TSM action combining PIST and torus operations (Lean: HybridTSMAction)"""
pistAction: bool # Whether to apply PIST Blitter step
torusNodeId: int # Torus node ID for routing
torusDimension: int # Torus dimension to toggle
torusDirection: int # Torus direction (+1 or -1)
epsilon: int # Epsilon parameter for PIST drift (Q16_16)
def to_dict(self) -> Dict[str, Any]:
return {
'pistAction': self.pistAction,
'torusNodeId': self.torusNodeId,
'torusDimension': self.torusDimension,
'torusDirection': self.torusDirection,
'epsilon': from_q16(self.epsilon)
}
@dataclass
class HybridTSMBind:
"""Hybrid TSM bind result (Lean: HybridTSMBind)"""
lawful: bool
manifoldBefore: int # Manifold value before action (Q16_16)
manifoldAfter: int # Manifold value after action (Q16_16)
torusDistanceBefore: int # Torus distance before action
torusDistanceAfter: int # Torus distance after action
geneticScoreBefore: int # Genetic score before action (Q16_16)
geneticScoreAfter: int # Genetic score after action (Q16_16)
invariant: str
def to_dict(self) -> Dict[str, Any]:
return {
'lawful': self.lawful,
'manifoldBefore': from_q16(self.manifoldBefore),
'manifoldAfter': from_q16(self.manifoldAfter),
'torusDistanceBefore': self.torusDistanceBefore,
'torusDistanceAfter': self.torusDistanceAfter,
'geneticScoreBefore': from_q16(self.geneticScoreBefore),
'geneticScoreAfter': from_q16(self.geneticScoreAfter),
'invariant': self.invariant
}
# ═══════════════════════════════════════════════════════════════════════════
# Lean Function Implementations (verified by specification)
# ═══════════════════════════════════════════════════════════════════════════
def pistModel131VectorField(a: int, b: int, epsilon: int) -> tuple:
"""PIST Model 131 vector field: F(a,b,ε) (from PistBridge.lean)"""
fa = Q16_ONE + epsilon * (Q16_ONE // 2 * b + Q16_ONE // 2) // Q16_ONE
fb = -Q16_ONE + epsilon * (Q16_ONE // 2 * a - Q16_ONE // 2) // Q16_ONE
return (fa, fb)
def geneticOptimizationScore(entropy: int, genomicComplexity: int, degeneracy: int) -> int:
"""Calculate genetic optimization score: I = (H × G) × (1 - D/64) (Lean: geneticOptimizationScore)"""
degeneracyQ = int((degeneracy / 64.0) * Q16_SCALE)
penalty = Q16_ONE - degeneracyQ
product = entropy * genomicComplexity // Q16_ONE
return product * penalty // Q16_ONE
def informationDensity(entropy: int, genomicComplexity: int, geneticScore: int) -> int:
"""Calculate information density: Density = I / (H × G) × 100 (Lean: informationDensity)"""
maxScore = entropy * genomicComplexity // Q16_ONE
if maxScore > 0:
density = geneticScore * to_q16(100.0) // maxScore
else:
density = 0
return density
def blitterStep(state: BlitterState, fa: int, fb: int) -> BlitterState:
"""Single Blitter step (discrete Picard integral) (from PistBridge.lean)"""
newManifold = state.manifold ^ ((fa + fb) >> 16)
return BlitterState(
a=state.a,
b=state.b,
manifold=newManifold,
stepMask=state.stepMask
)
def applyPistBlitter(state: HybridTSMState, epsilon: int) -> BlitterState:
"""Apply PIST Blitter step to hybrid state (Lean: applyPistBlitter)"""
fa, fb = pistModel131VectorField(state.pistState.a, state.pistState.b, epsilon)
return blitterStep(state.pistState, fa, fb)
def torusDistance(torusState, node1, node2) -> int:
"""Calculate torus distance (from FiveDTorusTopology.lean)"""
distanceSum = 0
for i in range(5):
coord1 = node1.coordinates[i]
coord2 = node2.coordinates[i]
dimSize = torusState.dimensionSizes[i]
diff = abs(coord1 - coord2)
wrappedDiff = dimSize - diff if dimSize > diff else 0
minDist = diff if diff < wrappedDiff else wrappedDiff
distanceSum += minDist
return distanceSum
def isTorusActionLawful(torusState, action) -> bool:
"""Check if torus action is lawful (from FiveDTorusTopology.lean)"""
return action.dimension < 5 and (action.direction == 1 or action.direction == -1)
def isHybridActionLawful(state: HybridTSMState, action: HybridTSMAction) -> bool:
"""Check if hybrid TSM action is lawful (Lean: isHybridActionLawful)"""
pistLawful = True # PIST Blitter always lawful
torusAction = type('TorusAction', (), {
'nodeId': action.torusNodeId,
'dimension': action.torusDimension,
'direction': action.torusDirection
})()
torusLawful = isTorusActionLawful(state.torusState, torusAction)
degeneracyLawful = action.epsilon >= 0 and action.epsilon <= Q16_ONE
return pistLawful and torusLawful and degeneracyLawful
def updateGeneticScore(state: HybridTSMState) -> int:
"""Update genetic score after state transition (Lean: updateGeneticScore)"""
return geneticOptimizationScore(state.entropy, state.genomicComplexity, state.degeneracy)
# ═══════════════════════════════════════════════════════════════════════════
# Lean Function Implementations (Rigorous PIST from ChatGPT-Making_It_Rigorous.md)
# ═══════════════════════════════════════════════════════════════════════════
def normalizedTensionRatio(mass: int, k: int) -> int:
"""Calculate normalized tension ratio: ρ(n) = 4m(n)/(2k+1)² (Lean: normalizedTensionRatio)"""
intervalLength = (2 * k + 1) ** 2
ratio = (to_q16(4.0) * mass) // intervalLength
return ratio
def classifyPhase(mass: int, k: int, threshold: int) -> str:
"""Phase classifier based on normalized tension ratio (Lean: classifyPhase)"""
if mass == 0:
return "grounded"
else:
rho = normalizedTensionRatio(mass, k)
if rho < threshold:
return "drift"
else:
return "seismic"
def lyapunovFunctional(mass: int, friction: int, rejectionCost: int, lambda_param: int, mu: int) -> int:
"""Lyapunov functional: Λ(S) = m(n) + λf + μc(rej) (Lean: lyapunovFunctional)"""
frictionPenalty = lambda_param * friction // 65536
rejectionPenalty = mu * rejectionCost // 65536
return mass + frictionPenalty + rejectionPenalty
def mirrorInvolution(k: int, t: int) -> int:
"""Mirror involution for resonance jump: σ_k(k²+t) = (k+1)²-t (Lean: mirrorInvolution)"""
return (k + 1) ** 2 - t
def isResonant(mass: int, mirrorMass: int) -> bool:
"""Resonance check: m(σ_k(n)) = m(n) (Lean: isResonant)"""
return mass == mirrorMass
def updatePhase(state: HybridTSMState, threshold: int) -> str:
"""Update phase based on PIST mass (Lean: updatePhase)"""
return classifyPhase(state.pistState.manifold, 4, threshold)
def lawfulProjection(state: HybridTSMState) -> HybridTSMState:
"""Lawful projection: removes unlawful components, preserves invariants (Lean: lawfulProjection)"""
newPhase = updatePhase(state, to_q16(0.5))
return HybridTSMState(
pistState=state.pistState,
torusState=state.torusState,
phase=newPhase,
geneticScore=state.geneticScore,
entropy=state.entropy,
genomicComplexity=state.genomicComplexity,
degeneracy=state.degeneracy,
friction=state.friction
)
def lyapunovDescentCheck(stateBefore: HybridTSMState, stateAfter: HybridTSMState, lambda_param: int, mu: int) -> bool:
"""Lyapunov descent check: Λ(S_{t+1}) < Λ(S_t) or already grounded (Lean: lyapunovDescentCheck)"""
# If already grounded, descent is automatically satisfied
if stateBefore.phase == "grounded":
return True
lambdaBefore = lyapunovFunctional(stateBefore.pistState.manifold, stateBefore.friction, 0, lambda_param, mu)
lambdaAfter = lyapunovFunctional(stateAfter.pistState.manifold, stateAfter.friction, 0, lambda_param, mu)
# Allow non-increase if transitioning to grounded
if stateAfter.phase == "grounded":
return lambdaAfter <= lambdaBefore
return lambdaAfter < lambdaBefore
def hybridTSMBind(state: HybridTSMState, action: HybridTSMAction, lambda_param: int = to_q16(0.1), mu: int = to_q16(0.1)) -> HybridTSMBind:
"""Bind primitive for hybrid TSM with lawful projection and Lyapunov descent (Lean: hybridTSMBind)"""
lawful = isHybridActionLawful(state, action)
manifoldBefore = state.pistState.manifold
geneticScoreBefore = state.geneticScore
# Get torus distance before action
originNode = state.torusState.nodes[0]
targetNode = None
for n in state.torusState.nodes:
if hasattr(n, 'nodeId') and n.nodeId == action.torusNodeId:
targetNode = n
break
torusDistanceBefore = 0
if targetNode:
torusDistanceBefore = torusDistance(state.torusState, originNode, targetNode)
if lawful:
newPistState = applyPistBlitter(state, action.epsilon) if action.pistAction else state.pistState
newTorusState = state.torusState # Simplified: no actual torus routing in this shim
newGeneticScore = updateGeneticScore(HybridTSMState(
pistState=newPistState,
torusState=newTorusState,
phase=state.phase,
geneticScore=state.geneticScore,
entropy=state.entropy,
genomicComplexity=state.genomicComplexity,
degeneracy=state.degeneracy,
friction=state.friction
))
rawState = HybridTSMState(
pistState=newPistState,
torusState=newTorusState,
phase=state.phase,
geneticScore=newGeneticScore,
entropy=state.entropy,
genomicComplexity=state.genomicComplexity,
degeneracy=state.degeneracy,
friction=state.friction
)
# Apply lawful projection
newState = lawfulProjection(rawState)
else:
newState = state
manifoldAfter = newState.pistState.manifold
geneticScoreAfter = newState.geneticScore
# Check Lyapunov descent
descentSatisfied = lyapunovDescentCheck(state, newState, lambda_param, mu)
# Get torus distance after action
newTargetNode = newState.torusState.nodes
torusDistanceAfter = torusDistanceBefore # Simplified: no actual torus routing
return HybridTSMBind(
lawful=lawful and descentSatisfied,
manifoldBefore=manifoldBefore,
manifoldAfter=manifoldAfter,
torusDistanceBefore=torusDistanceBefore,
torusDistanceAfter=torusDistanceAfter,
geneticScoreBefore=geneticScoreBefore,
geneticScoreAfter=geneticScoreAfter,
invariant="hybrid_tsm_pist_torus_satisfied" if lawful and descentSatisfied else "hybrid_constraint_violated"
)
class HybridTSMPISTTorusSystem:
"""
Hybrid TSM-PIST-Torus system (Python shim wrapping Lean specification).
All core logic is defined in 0-Core-Formalism/lean/Semantics/Semantics/HybridTSMPISTTorus.lean
"""
def __init__(self):
self.hybridState: Optional[HybridTSMState] = None
self.actionHistory: List[Dict[str, Any]] = []
print("[HybridTSMPISTTorus] Initialized (Lean specification)")
def initializeHybrid(self, dimensionSizes: List[int] = None, numNodes: int = 16) -> Dict[str, Any]:
"""Initialize hybrid TSM state"""
if dimensionSizes is None:
dimensionSizes = [16, 16, 16, 16, 16]
# Initialize PIST state
pistState = BlitterState(
a=to_q16(4.0),
b=to_q16(5.0),
manifold=to_q16(0.0),
stepMask=0
)
# Initialize torus nodes
torusNodes = []
for i in range(numNodes):
coords = []
for d in range(5):
coords.append((i >> d) % dimensionSizes[d])
node = type('TorusNode', (), {
'nodeId': i,
'coordinates': coords,
'dimensions': 5
})()
torusNodes.append(node)
# Initialize torus state
torusState = TorusTopologyState(
nodes=torusNodes,
dimensionSizes=dimensionSizes,
dimensions=5
)
# Initialize genetic parameters
entropy = to_q16(0.5)
genomicComplexity = to_q16(0.9)
degeneracy = 32
geneticScore = geneticOptimizationScore(entropy, genomicComplexity, degeneracy)
# Initialize phase based on PIST mass
phase = classifyPhase(pistState.manifold, 4, to_q16(0.5))
# Initialize friction
friction = 10
state = HybridTSMState(
pistState=pistState,
torusState=torusState,
phase=phase,
geneticScore=geneticScore,
entropy=entropy,
genomicComplexity=genomicComplexity,
degeneracy=degeneracy,
friction=friction
)
self.hybridState = state
return {
'pistState': pistState.to_dict(),
'torusState': torusState.to_dict(),
'phase': phase,
'geneticScore': from_q16(geneticScore),
'entropy': from_q16(entropy),
'genomicComplexity': from_q16(genomicComplexity),
'degeneracy': degeneracy,
'friction': friction,
'state': state.to_dict()
}
def submitHybridAction(self, action: HybridTSMAction, lambda_param: int = to_q16(0.1), mu: int = to_q16(0.1)) -> Dict[str, Any]:
"""Submit hybrid TSM action for processing (Lean specification)"""
if self.hybridState is None:
return {'error': 'Hybrid state not initialized'}
bindResult = hybridTSMBind(self.hybridState, action, lambda_param, mu)
if bindResult.lawful:
# Update state
if action.pistAction:
self.hybridState.pistState = applyPistBlitter(self.hybridState, action.epsilon)
self.hybridState.geneticScore = updateGeneticScore(self.hybridState)
self.hybridState.phase = updatePhase(self.hybridState, to_q16(0.5))
# Record action history
self.actionHistory.append({
'action': action.to_dict(),
'bindResult': bindResult.to_dict(),
'timestamp': time.time()
})
return {
'success': bindResult.lawful,
'bindResult': bindResult.to_dict(),
'state': self.hybridState.to_dict()
}
def getHybridState(self) -> Optional[Dict[str, Any]]:
"""Get current hybrid state"""
if self.hybridState:
return self.hybridState.to_dict()
return None
def getActionHistory(self, limit: int = 10) -> List[Dict[str, Any]]:
"""Get action history"""
return self.actionHistory[-limit:]
def printSystemState(self):
"""Print system state"""
print("\n" + "="*70)
print("HYBRID TSM-PIST-TORUS STATE")
print("="*70)
if self.hybridState:
print(f"\n📊 PIST Manifold:")
print(f" a: {from_q16(self.hybridState.pistState.a):.3f}")
print(f" b: {from_q16(self.hybridState.pistState.b):.3f}")
print(f" Manifold: {from_q16(self.hybridState.pistState.manifold):.3f}")
print(f" Phase: {self.hybridState.phase}")
print(f"\n📊 5D Torus Topology:")
print(f" Dimensions: {self.hybridState.torusState.dimensions}")
print(f" Dimension Sizes: {self.hybridState.torusState.dimensionSizes}")
print(f" Nodes: {len(self.hybridState.torusState.nodes)}")
print(f"\n📊 Genetic Compression:")
print(f" Entropy: {from_q16(self.hybridState.entropy):.3f}")
print(f" Genomic Complexity: {from_q16(self.hybridState.genomicComplexity):.3f}")
print(f" Degeneracy: {self.hybridState.degeneracy}")
print(f" Genetic Score: {from_q16(self.hybridState.geneticScore):.3f}")
density = informationDensity(
self.hybridState.entropy,
self.hybridState.genomicComplexity,
self.hybridState.geneticScore
)
print(f" Information Density: {from_q16(density):.3f}%")
print(f"\n📊 Rigorous PIST Components:")
print(f" Friction: {self.hybridState.friction}")
rho = normalizedTensionRatio(self.hybridState.pistState.manifold, 4)
print(f" Normalized Tension Ratio: {from_q16(rho):.3f}")
lyapunov = lyapunovFunctional(self.hybridState.pistState.manifold, self.hybridState.friction, 0, to_q16(0.1), to_q16(0.1))
print(f" Lyapunov Functional: {from_q16(lyapunov):.3f}")
print(f"\n📜 Action History: {len(self.actionHistory)} entries")
print("\n" + "="*70)
def main():
"""Test hybrid TSM-PIST-Torus system"""
system = HybridTSMPISTTorusSystem()
print("[Test 1] Initialize hybrid TSM state...")
result1 = system.initializeHybrid(dimensionSizes=[16, 16, 16, 16, 16], numNodes=16)
print(f" Hybrid state initialized")
print(f" Genetic Score: {result1['geneticScore']:.3f}")
print("\n[Test 2] Submit hybrid action (PIST Blitter step)...")
action1 = HybridTSMAction(
pistAction=True,
torusNodeId=1,
torusDimension=0,
torusDirection=1,
epsilon=to_q16(0.1)
)
result2 = system.submitHybridAction(action1)
print(f" Result: Success={result2['success']}")
if result2['success']:
print(f" Manifold before: {result2['bindResult']['manifoldBefore']:.3f}")
print(f" Manifold after: {result2['bindResult']['manifoldAfter']:.3f}")
print(f" Genetic score before: {result2['bindResult']['geneticScoreBefore']:.3f}")
print(f" Genetic score after: {result2['bindResult']['geneticScoreAfter']:.3f}")
print("\n[System State]")
system.printSystemState()
if __name__ == '__main__':
main()

View file

@ -1,54 +0,0 @@
import sqlite3
import math
import json
import os
DB_PATH = "/home/allaun/Documents/Research Stack/data/substrate_index.db"
def calculate_pist_phase(n):
if n == 0: return "GROUNDED"
k = math.isqrt(n)
a = n - k**2
b = (k+1)**2 - n
m = a * b
max_m = (2*k + 1)**2 / 4.0
rho = 4 * m / (2*k + 1)**2 if max_m > 0 else 0
if m == 0: return "GROUNDED"
if rho < 0.5: return "DRIFT"
return "SEISMIC"
def run_sweep():
if not os.path.exists(DB_PATH):
print("❌ Database not found.")
return
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
print("🚀 Initiating PIST-mass sweep for NOTION domain...")
cur.execute("SELECT pkg, tags FROM packages_fts WHERE domain = 'NOTION'")
rows = cur.fetchall()
updates = []
for idx, (pkg, tags_json) in enumerate(rows):
n = idx + 1 # Manifold Coordinate
phase = calculate_pist_phase(n)
tags = json.loads(tags_json) if tags_json else []
# Remove old phase tags
tags = [t for t in tags if t not in ["GROUNDED", "DRIFT", "SEISMIC"]]
tags.append(phase)
updates.append((json.dumps(tags), pkg))
cur.executemany("UPDATE packages_fts SET tags = ? WHERE pkg = ? AND domain = 'NOTION'", updates)
conn.commit()
conn.close()
print(f"✅ Successfully neutralized {len(updates)} Notion nodes with PIST phase tags.")
if __name__ == "__main__":
run_sweep()

View file

@ -1,308 +0,0 @@
#!/usr/bin/env python3
"""
Microgrid Voxel Emulation
Assign a microgrid and only update the voxels to emulate 640x480 display.
Architecture:
- Create 640x480 voxel microgrid (virtual display)
- NES renders at 256x240 (native)
- Map NES pixels to microgrid voxels
- Only update voxels that change (differential updates)
- DSP math and voltage computation optimize voxel updates
- Effective 640x480 resolution without changing NES PPU
This is horrific because:
- Virtual display at 2.5×2 resolution on 1× hardware
- Voxel-level differential updates require precise tracking
- Microgrid emulation is essentially software rendering on 1985 hardware
This is wonderful because:
- Effective 640x480 resolution without hardware modification
- Only update changed voxels (efficient)
- Modern GPU-like techniques on retro hardware
- Maximum retro insanity: microgrid = virtual display
"""
import math
from typing import List, Tuple, Dict, Set
from dataclasses import dataclass
# ═══════════════════════════════════════════════════════════════════════════
# Voxel Microgrid
# Virtual 640x480 display as voxel grid
# ═══════════════════════════════════════════════════════════════════════════
@dataclass
class Voxel:
"""Single voxel in microgrid"""
x: int # X position (0-639)
y: int # Y position (0-479)
color: Tuple[int, int, int] # RGB color
active: bool = True # Whether voxel is active
def __hash__(self):
return hash((self.x, self.y))
class VoxelMicrogrid:
"""640x480 voxel microgrid"""
def __init__(self, width: int = 640, height: int = 480):
self.width = width
self.height = height
self.voxels: Dict[Tuple[int, int], Voxel] = {}
self.changed_voxels: Set[Tuple[int, int]] = set()
self.frame_count = 0
def get_voxel(self, x: int, y: int) -> Voxel:
"""Get voxel at position"""
if (x, y) not in self.voxels:
self.voxels[(x, y)] = Voxel(x, y, (0, 0, 0))
return self.voxels[(x, y)]
def set_voxel_color(self, x: int, y: int, color: Tuple[int, int, int]):
"""Set voxel color and mark as changed"""
voxel = self.get_voxel(x, y)
if voxel.color != color:
voxel.color = color
self.changed_voxels.add((x, y))
def get_changed_voxels(self) -> List[Voxel]:
"""Get list of changed voxels"""
return [self.voxels[pos] for pos in self.changed_voxels]
def clear_changed_voxels(self):
"""Clear changed voxel tracking"""
self.changed_voxels.clear()
self.frame_count += 1
def render_frame(self) -> List[List[Tuple[int, int, int]]]:
"""Render full frame from microgrid"""
frame = []
for y in range(self.height):
row = []
for x in range(self.width):
voxel = self.get_voxel(x, y)
row.append(voxel.color)
frame.append(row)
return frame
# ═══════════════════════════════════════════════════════════════════════════
# NES-to-Microgrid Mapping
# Map 256x240 NES pixels to 640x480 microgrid voxels
# ═══════════════════════════════════════════════════════════════════════════
class NESMicrogridMapper:
"""Map NES pixels to microgrid voxels"""
def __init__(self, nes_width: int = 256, nes_height: int = 240,
microgrid_width: int = 640, microgrid_height: int = 480):
self.nes_width = nes_width
self.nes_height = nes_height
self.microgrid_width = microgrid_width
self.microgrid_height = microgrid_height
# Calculate scaling factors
self.scale_x = microgrid_width / nes_width # 2.5
self.scale_y = microgrid_height / nes_height # 2.0
def nes_to_microgrid(self, nes_x: int, nes_y: int) -> List[Tuple[int, int]]:
"""
Map NES pixel to microgrid voxels.
Each NES pixel maps to a block of microgrid voxels.
"""
# Calculate microgrid bounds for this NES pixel
mg_x_start = int(nes_x * self.scale_x)
mg_y_start = int(nes_y * self.scale_y)
mg_x_end = int((nes_x + 1) * self.scale_x)
mg_y_end = int((nes_y + 1) * self.scale_y)
# Return all voxels in this block
voxels = []
for y in range(mg_y_start, mg_y_end):
for x in range(mg_x_start, mg_x_end):
voxels.append((x, y))
return voxels
def map_nes_frame_to_microgrid(self, nes_frame: List[List[Tuple[int, int, int]]],
microgrid: VoxelMicrogrid):
"""
Map NES frame to microgrid.
Only updates voxels that change.
"""
microgrid.clear_changed_voxels()
for nes_y in range(len(nes_frame)):
for nes_x in range(len(nes_frame[nes_y])):
nes_color = nes_frame[nes_y][nes_x]
# Get corresponding microgrid voxels
mg_voxels = self.nes_to_microgrid(nes_x, nes_y)
# Update each voxel with NES color
for mg_x, mg_y in mg_voxels:
microgrid.set_voxel_color(mg_x, mg_y, nes_color)
# ═══════════════════════════════════════════════════════════════════════════
# DSP Math for Voxel Optimization
# Use DSP math to optimize which voxels to update
# ═══════════════════════════════════════════════════════════════════════════
class DSPVoxelOptimizer:
"""DSP math for optimizing voxel updates"""
@staticmethod
def calculate_change_priority(old_color: Tuple[int, int, int],
new_color: Tuple[int, int, int]) -> float:
"""
Calculate priority of voxel update based on color change.
Larger changes = higher priority.
"""
r_diff = abs(old_color[0] - new_color[0])
g_diff = abs(old_color[1] - new_color[1])
b_diff = abs(old_color[2] - new_color[2])
total_diff = r_diff + g_diff + b_diff
max_diff = 255 * 3
return total_diff / max_diff if max_diff > 0 else 0.0
@staticmethod
def voltage_based_update(voltage: float, priority: float) -> bool:
"""
Determine if voxel should be updated based on voltage level.
Higher voltage = higher threshold for updates.
"""
threshold = voltage / 5.0 # Normalize 0-5V to 0-1
return priority >= threshold
# ═══════════════════════════════════════════════════════════════════════════
# Voltage-Driven Microgrid Controller
# Voltage computation controls which voxels to update
# ═══════════════════════════════════════════════════════════════════════════
class VoltageMicrogridController:
"""Voltage-driven microgrid controller"""
def __init__(self):
self.microgrid = VoxelMicrogrid(640, 480)
self.mapper = NESMicrogridMapper(256, 240, 640, 480)
self.optimizer = DSPVoxelOptimizer()
self.voltage_levels: Dict[Tuple[int, int], float] = {}
def update_from_nes_frame(self, nes_frame: List[List[Tuple[int, int, int]]],
voltage_field: List[List[float]]):
"""
Update microgrid from NES frame with voltage optimization.
Only updates voxels that pass voltage-based priority check.
"""
# Map NES frame to microgrid
self.mapper.map_nes_frame_to_microgrid(nes_frame, self.microgrid)
# Apply voltage-based optimization
optimized_voxels = []
for voxel in self.microgrid.get_changed_voxels():
# Get voltage level for this voxel
voltage = voltage_field[voxel.y % len(voltage_field)][voxel.x % len(voltage_field[0])]
# Calculate change priority
old_color = (0, 0, 0) # Simplified - would track actual old color
priority = self.optimizer.calculate_change_priority(old_color, voxel.color)
# Check if update passes voltage threshold
if self.optimizer.voltage_based_update(voltage, priority):
optimized_voxels.append(voxel)
# Update changed voxels set to only optimized ones
self.microgrid.changed_voxels = set((v.x, v.y) for v in optimized_voxels)
def get_update_efficiency(self) -> float:
"""
Calculate update efficiency.
Ratio of changed voxels to total voxels.
"""
total_voxels = self.microgrid.width * self.microgrid.height
changed_count = len(self.microgrid.changed_voxels)
return changed_count / total_voxels if total_voxels > 0 else 0.0
# ═══════════════════════════════════════════════════════════════════════════
# Test / Demo
# ═══════════════════════════════════════════════════════════════════════════
def run_test():
"""Run microgrid voxel emulation test"""
print("=" * 70)
print("MICROGRID VOXEL EMULATION")
print("=" * 70)
print("\n[*] Architecture:")
print(" Create 640x480 voxel microgrid (virtual display)")
print(" NES renders at 256x240 (native)")
print(" Map NES pixels to microgrid voxels")
print(" Only update voxels that change (differential updates)")
print(" DSP math and voltage computation optimize voxel updates")
print(" Effective 640x480 resolution without changing NES PPU")
controller = VoltageMicrogridController()
# Create NES frame (simple gradient)
print("\n[*] Creating NES frame (256x240)...")
nes_frame = []
for y in range(240):
row = []
for x in range(256):
r = int((x / 256) * 255)
g = int((y / 240) * 255)
b = 128
row.append((r, g, b))
nes_frame.append(row)
print(f" NES frame: {len(nes_frame)}x{len(nes_frame[0])}")
# Create voltage field
print("\n[*] Creating voltage field...")
voltage_field = []
for y in range(240):
row = []
for x in range(256):
voltage = 2.5 + math.sin(x * 0.1 + y * 0.1) * 2.5 # 0-5V range
row.append(voltage)
voltage_field.append(row)
print(f" Voltage field: {len(voltage_field)}x{len(voltage_field[0])}")
# Update microgrid
print("\n[*] Updating microgrid from NES frame...")
controller.update_from_nes_frame(nes_frame, voltage_field)
# Get statistics
print("\n[*] Microgrid Statistics:")
print(f" Microgrid size: {controller.microgrid.width}x{controller.microgrid.height}")
print(f" Changed voxels: {len(controller.microgrid.changed_voxels)}")
print(f" Update efficiency: {controller.get_update_efficiency():.4f}")
print(f" Frame count: {controller.microgrid.frame_count}")
# Render full frame
print("\n[*] Rendering full microgrid frame...")
full_frame = controller.microgrid.render_frame()
print(f" Full frame size: {len(full_frame)}x{len(full_frame[0])}")
print(f" Sample colors: (0,0)={full_frame[0][0]}, (319,239)={full_frame[239][319]}, (639,479)={full_frame[479][639]}")
print("\n" + "=" * 70)
print("MICROGRID VOXEL EMULATION COMPLETE")
print("=" * 70)
print("\n[*] Horrific: Virtual 640x480 display on 256x240 hardware")
print("[*] Wonderful: Differential voxel updates for efficiency")
print("[*] Maximum retro insanity: microgrid = virtual display")
print("\n[*] Can we generate 640x480 video now?")
print(" YES: Microgrid voxel emulation achieves effective 640x480")
print(" NES renders at 256x240 native")
print(" Microgrid maps to 640x480 virtual display")
print(" Only update changed voxels (efficient)")
if __name__ == "__main__":
run_test()

View file

@ -1,838 +0,0 @@
#!/usr/bin/env python3
"""
Manifold Surface API Server
Provides REST API for manifold navigation interface
"""
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import sqlite3
import json
import numpy as np
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from projection_engine import ProjectionEngine, ConceptVector14, ProjectionMethod
from soliton_search import SolitonSearchEngine
from collapse_editor import CollapseEditor, StateType
from particle_interaction import ParticleInteractionEngine, ParticleType
from self_typing_engine import SelfTypingEngine, InteractionType
from relativity_adapter import RelativityAdapter, CognitiveLoadLevel
from substrate_bridge import SubstrateBridge
app = FastAPI(title="Manifold Surface API")
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Database path
DB_PATH = "/home/allaun/Documents/Research Stack/data/substrate_index.db"
# Global projection engine
projection_engine = None
soliton_engine = None
collapse_editor = CollapseEditor()
particle_engine = ParticleInteractionEngine()
self_typing_engine = SelfTypingEngine()
relativity_adapter = RelativityAdapter()
substrate_bridge = SubstrateBridge(DB_PATH)
class VectorData(BaseModel):
vectors: List[List[float]]
archive_ids: List[str]
class ProjectionRequest(BaseModel):
vectors: List[List[float]]
archive_ids: List[str]
method: str = "PCA"
slice_axis_x: int = 3
slice_axis_y: int = 4
class ProjectionResponse(BaseModel):
points: List[dict]
projection_matrix: List[List[float]]
mean_vector: List[float]
def get_db_connection():
"""Get database connection"""
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def load_concept_vectors_from_ene() -> List[ConceptVector14]:
"""Load concept vectors from ENE database"""
vectors = []
try:
conn = get_db_connection()
cursor = conn.cursor()
# Query concept vectors from packages table
cursor.execute("""
SELECT archive_id, concept_vector_14
FROM packages
WHERE concept_vector_14 IS NOT NULL
""")
for row in cursor.fetchall():
archive_id = row['archive_id']
vector_str = row['concept_vector_14']
try:
# Parse vector string (assuming JSON format)
vector_data = json.loads(vector_str)
vector = np.array(vector_data, dtype=np.float32)
if len(vector) != 14:
continue # Skip invalid vectors
vectors.append(ConceptVector14(vector=vector, archive_id=archive_id))
except (json.JSONDecodeError, ValueError) as e:
print(f"Error parsing vector for {archive_id}: {e}")
continue
conn.close()
except Exception as e:
print(f"Error loading concept vectors: {e}")
return vectors
@app.get("/health")
def health_check():
"""Health check endpoint"""
return {"status": "ok", "service": "manifold-surface-api"}
@app.get("/api/load-concept-vectors")
def load_concept_vectors():
"""Load concept vectors from ENE database"""
global projection_engine
try:
vectors = load_concept_vectors_from_ene()
if not vectors:
return {"vectors": [], "count": 0}
# Initialize projection engine
projection_engine = ProjectionEngine(method=ProjectionMethod.PCA)
# Fit and transform
projected = projection_engine.fit_transform(vectors)
# Convert to response format
vector_data = [v.vector.tolist() for v in vectors]
archive_ids = [v.archive_id for v in vectors]
return {
"vectors": vector_data,
"archive_ids": archive_ids,
"count": len(vectors),
"projection_matrix": projection_engine._projection_matrix.tolist() if projection_engine._projection_matrix is not None else [],
"mean_vector": projection_engine._mean.tolist() if projection_engine._mean is not None else []
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/project")
def project_vectors(request: ProjectionRequest):
"""Project vectors using specified method"""
global projection_engine
try:
# Convert to ConceptVector14 objects
vectors = [
ConceptVector14(vector=np.array(v, dtype=np.float32), archive_id=aid)
for v, aid in zip(request.vectors, request.archive_ids)
]
# Create projection engine with specified method
method_map = {
"PCA": ProjectionMethod.PCA,
"tSNE": ProjectionMethod.T_SNE,
"UMAP": ProjectionMethod.UMAP,
"ManifoldChart": ProjectionMethod.MANIFOLD_CHART
}
method = method_map.get(request.method, ProjectionMethod.PCA)
projection_engine = ProjectionEngine(method=method)
# Fit and transform
projected = projection_engine.fit_transform(vectors)
# Convert to response format
points = [p.to_dict() for p in projected]
return {
"points": points,
"projection_matrix": projection_engine._projection_matrix.tolist() if projection_engine._projection_matrix is not None else [],
"mean_vector": projection_engine._mean.tolist() if projection_engine._mean is not None else []
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
class SolitonSearchRequest(BaseModel):
query_vector: List[float]
max_results: int = 10
class CreateSuperpositionRequest(BaseModel):
initial_content: str
state_type: str = "text"
class AddStateRequest(BaseModel):
superposition_id: str
branch_id: str
content: str
state_type: str = "text"
class CreateBranchRequest(BaseModel):
superposition_id: str
parent_branch_id: Optional[str] = None
content: str = ""
state_type: str = "text"
class CollapseRequest(BaseModel):
superposition_id: str
branch_id: str
class CreateParticleRequest(BaseModel):
particle_type: str
position: Tuple[float, float]
velocity: Tuple[float, float] = (0.0, 0.0)
class EmitPhotonRequest(BaseModel):
from_particle_id: str
to_particle_id: str
class AbsorbPhotonRequest(BaseModel):
photon_id: str
target_particle_id: str
class RecordInteractionRequest(BaseModel):
interaction_type: str
source_layer: str
target_layer: str
context: Optional[Dict[str, str]] = None
class InferMetatypeRequest(BaseModel):
context: Dict[str, str]
class MeasureCognitiveLoadRequest(BaseModel):
information_density: float
complexity: float
novelty: float
uncertainty: float
class TransformRequest(BaseModel):
input_vector: List[float]
from_topology: Optional[str] = None
to_topology: Optional[str] = None
@app.post("/api/soliton-search")
def soliton_search(request: SolitonSearchRequest):
"""Search using soliton propagation with AVMR O(√N) indexing"""
global soliton_engine
try:
# Load vectors if not already loaded
if soliton_engine is None:
vectors = load_concept_vectors_from_ene()
if not vectors:
return {"results": [], "count": 0}
vector_data = [v.vector.tolist() for v in vectors]
archive_ids = [v.archive_id for v in vectors]
soliton_engine = SolitonSearchEngine(vector_data, archive_ids)
# Perform search
query = np.array(request.query_vector, dtype=np.float32)
results = soliton_engine.search(query, max_results=request.max_results)
return {
"results": [{"archive_id": aid, "confidence": conf} for aid, conf in results],
"count": len(results)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/superposition/create")
def create_superposition(request: CreateSuperpositionRequest):
"""Create new superposition with single branch"""
global collapse_editor
try:
state_type = StateType[request.state_type.upper()]
superposition = collapse_editor.create_superposition(request.initial_content, state_type)
return {
"superposition_id": superposition.superposition_id,
"current_branch_id": superposition.current_branch_id,
"branches": len(superposition.branches)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/superposition/add-state")
def add_state_to_branch(request: AddStateRequest):
"""Add new state to existing branch"""
global collapse_editor
try:
state_type = StateType[request.state_type.upper()]
collapse_editor.add_state_to_branch(
request.superposition_id,
request.branch_id,
request.content,
state_type
)
return {"status": "ok"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/superposition/create-branch")
def create_branch(request: CreateBranchRequest):
"""Create new branch in superposition"""
global collapse_editor
try:
state_type = StateType[request.state_type.upper()]
branch = collapse_editor.create_branch(
request.superposition_id,
request.parent_branch_id,
request.content,
state_type
)
return {
"branch_id": branch.branch_id,
"amplitude": branch.amplitude,
"states": len(branch.states)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/superposition/collapse")
def collapse_superposition(request: CollapseRequest):
"""Collapse superposition to observable state"""
global collapse_editor
try:
observable = collapse_editor.collapse(request.superposition_id, request.branch_id)
return {
"archive_id": observable.archive_id,
"witness_hash": observable.witness_hash,
"collapsed_at": observable.collapsed_at.isoformat()
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/superposition/{superposition_id}/branches")
def get_superposition_branches(superposition_id: str):
"""Get all branches for superposition"""
global collapse_editor
try:
branches = collapse_editor.get_branches(superposition_id)
return {
"branches": [
{
"branch_id": b.branch_id,
"amplitude": b.amplitude,
"states": len(b.states),
"parent_branch_id": b.parent_branch_id
}
for b in branches
]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/superposition/{superposition_id}/tree")
def get_branch_tree(superposition_id: str):
"""Get branch tree structure for visualization"""
global collapse_editor
try:
tree = collapse_editor.get_branch_tree(superposition_id)
return {"tree": tree}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/collapse-editor/undo")
def undo_collapse():
"""Revert to previous observable state"""
global collapse_editor
try:
previous_state = collapse_editor.undo()
if previous_state is None:
return {"status": "no_undo_available"}
return {
"archive_id": previous_state.archive_id,
"witness_hash": previous_state.witness_hash
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/collapse-editor/witnesses")
def get_witness_history():
"""Get all witness records"""
global collapse_editor
try:
witnesses = collapse_editor.get_witness_history()
return {
"witnesses": [
{
"witness_hash": w.witness_hash,
"superposition_id": w.superposition_id,
"collapsed_branch_id": w.collapsed_branch_id,
"timestamp": w.timestamp.isoformat()
}
for w in witnesses
],
"count": len(witnesses)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/particle/create")
def create_particle(request: CreateParticleRequest):
"""Create new particle"""
global particle_engine
try:
particle_type = ParticleType[request.particle_type.upper()]
particle = particle_engine.create_particle(
particle_type,
request.position,
request.velocity
)
return particle.to_dict()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/particle/emit-photon")
def emit_photon(request: EmitPhotonRequest):
"""Emit photon from one particle to another"""
global particle_engine
try:
interaction = particle_engine.emit_photon(request.from_particle_id, request.to_particle_id)
return {
"interaction_id": len(particle_engine.interactions),
"from_particle_id": interaction.from_particle_id,
"to_particle_id": interaction.to_particle_id,
"interaction_type": interaction.interaction_type,
"energy_transfer": interaction.energy_transfer
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/particle/absorb-photon")
def absorb_photon(request: AbsorbPhotonRequest):
"""Absorb photon by target particle"""
global particle_engine
try:
interaction = particle_engine.absorb_photon(request.photon_id, request.target_particle_id)
return {
"interaction_id": len(particle_engine.interactions),
"from_particle_id": interaction.from_particle_id,
"to_particle_id": interaction.to_particle_id,
"interaction_type": interaction.interaction_type,
"energy_transfer": interaction.energy_transfer
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/particle/detect-neutrino/{particle_id}")
def detect_neutrino(particle_id: str):
"""Attempt to detect neutrino (weakly-interacting inference)"""
global particle_engine
try:
detected = particle_engine.detect_neutrino(particle_id)
return {"detected": detected}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/particle/update")
def update_particles():
"""Update particle positions and velocities"""
global particle_engine
try:
particle_engine.update()
return {"time": particle_engine.time}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/particle/conservation")
def check_conservation():
"""Check all conservation laws"""
global particle_engine
try:
conservation = particle_engine.check_conservation()
return conservation
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/particle/graph")
def get_interaction_graph():
"""Get interaction graph for visualization"""
global particle_engine
try:
graph = particle_engine.get_interaction_graph()
return graph
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/particle/type/{particle_type}")
def get_particles_by_type(particle_type: str):
"""Get all particles of specific type"""
global particle_engine
try:
ptype = ParticleType[particle_type.upper()]
particles = particle_engine.get_particles_by_type(ptype)
return {
"particles": [p.to_dict() for p in particles],
"count": len(particles)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/self-typing/record-interaction")
def record_interaction(request: RecordInteractionRequest):
"""Record interaction between layers"""
global self_typing_engine
try:
interaction_type = InteractionType[request.interaction_type.upper()]
interaction = self_typing_engine.record_interaction(
interaction_type,
request.source_layer,
request.target_layer,
request.context
)
return {
"interaction_id": interaction.interaction_id,
"interaction_type": interaction.interaction_type.value,
"source_layer": interaction.source_layer,
"target_layer": interaction.target_layer,
"timestamp": interaction.timestamp.isoformat()
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/self-typing/infer-metatype")
def infer_metatype(request: InferMetatypeRequest):
"""Infer metatype from interaction patterns"""
global self_typing_engine
try:
metatype = self_typing_engine.infer_metatype(request.context)
return {
"metatype_id": metatype.metatype_id,
"type_signature": metatype.type_signature,
"confidence": metatype.confidence,
"suggestions": metatype.suggestions,
"created_at": metatype.created_at.isoformat()
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/self-typing/interactions")
def get_interaction_history(limit: int = 100):
"""Get recent interaction history"""
global self_typing_engine
try:
interactions = self_typing_engine.get_interaction_history(limit)
return {
"interactions": [
{
"interaction_id": i.interaction_id,
"interaction_type": i.interaction_type.value,
"source_layer": i.source_layer,
"target_layer": i.target_layer,
"timestamp": i.timestamp.isoformat()
}
for i in interactions
],
"count": len(interactions)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/self-typing/statistics")
def get_type_statistics():
"""Get statistics about type inference"""
global self_typing_engine
try:
stats = self_typing_engine.get_type_statistics()
return stats
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/relativity/measure-cognitive-load")
def measure_cognitive_load(request: MeasureCognitiveLoadRequest):
"""Measure cognitive load"""
global relativity_adapter
try:
load_vector = relativity_adapter.measure_cognitive_load(
request.information_density,
request.complexity,
request.novelty,
request.uncertainty
)
return {
"level": load_vector.get_level().value,
"information_density": load_vector.information_density,
"complexity": load_vector.complexity,
"novelty": load_vector.novelty,
"uncertainty": load_vector.uncertainty,
"timestamp": load_vector.timestamp
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/relativity/adapt-topology")
def adapt_topology(request: MeasureCognitiveLoadRequest):
"""Adapt topology based on cognitive load"""
global relativity_adapter
try:
load_vector = relativity_adapter.measure_cognitive_load(
request.information_density,
request.complexity,
request.novelty,
request.uncertainty
)
topology = relativity_adapter.adapt_topology(load_vector)
return {
"topology_id": topology.topology_id,
"distance_metric": topology.distance_metric,
"cognitive_load_level": topology.cognitive_load_level.value
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/relativity/transform")
def transform_coordinates(request: TransformRequest):
"""Transform coordinates between topologies"""
global relativity_adapter
try:
input_vector = np.array(request.input_vector, dtype=np.float32)
transformed = relativity_adapter.transform(
input_vector,
request.from_topology,
request.to_topology
)
return {
"transformed_vector": transformed.tolist(),
"from_topology": request.from_topology,
"to_topology": request.to_topology
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/relativity/enable-dolphin-mode")
def enable_dolphin_mode():
"""Enable dolphin mode (non-Euclidean visualization)"""
global relativity_adapter
try:
relativity_adapter.enable_dolphin_mode()
return {
"status": "dolphin_mode_enabled",
"current_topology": relativity_adapter.current_topology.topology_id if relativity_adapter.current_topology else None
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/relativity/cognitive-load-history")
def get_cognitive_load_history(limit: int = 100):
"""Get cognitive load history"""
global relativity_adapter
try:
history = relativity_adapter.get_cognitive_load_history(limit)
return {
"history": [
{
"level": h.get_level().value,
"information_density": h.information_density,
"complexity": h.complexity,
"novelty": h.novelty,
"uncertainty": h.uncertainty,
"timestamp": h.timestamp
}
for h in history
],
"count": len(history)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/relativity/statistics")
def get_topology_statistics():
"""Get topology statistics"""
global relativity_adapter
try:
stats = relativity_adapter.get_topology_statistics()
return stats
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/stats")
def get_stats():
"""Get database statistics"""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Count total packages
cursor.execute("SELECT COUNT(*) as count FROM packages")
total_count = cursor.fetchone()['count']
# Count packages with concept vectors
cursor.execute("SELECT COUNT(*) as count FROM packages WHERE concept_vector_14 IS NOT NULL")
vector_count = cursor.fetchone()['count']
conn.close()
return {
"total_packages": total_count,
"packages_with_vectors": vector_count,
"coverage": vector_count / total_count if total_count > 0 else 0
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
print("Starting Manifold Surface API Server...")
print(f"Database: {DB_PATH}")
uvicorn.run(app, host="0.0.0.0", port=8000)

View file

@ -1,408 +0,0 @@
#!/usr/bin/env python3
"""
Collapse Editor Superposition State Management
First-Principles Derivation: Save is projection collapse from hidden path to observable state
Performance Targets:
- < 10ms collapse operation
- < 5ms branch visualization
- < 20ms undo operation
"""
import numpy as np
from typing import List, Optional, Dict
from dataclasses import dataclass, field
from enum import Enum
import json
import hashlib
from datetime import datetime
class StateType(Enum):
"""Type of state in superposition"""
TEXT = "text"
VECTOR = "vector"
STRUCTURED = "structured"
@dataclass
class State:
"""Single state in superposition"""
content: str # State content (JSON serialized)
state_type: StateType
amplitude: float # Quantum amplitude (probability amplitude)
created_at: datetime = field(default_factory=datetime.now)
def __repr__(self) -> str:
return f"State(type={self.state_type.value}, amplitude={self.amplitude:.3f})"
@dataclass
class ObservableState:
"""Collapsed observable state (eigenstate)"""
archive_id: str
content: str # Observable content
witness_hash: str # SHA-256 hash of content
collapsed_at: datetime = field(default_factory=datetime.now)
parent_superposition_id: Optional[str] = None
def __repr__(self) -> str:
return f"ObservableState(archive_id={self.archive_id}, hash={self.witness_hash[:8]}...)"
@dataclass
class Branch:
"""Branch in superposition (possible trajectory)"""
branch_id: str
states: List[State]
amplitude: float # Branch amplitude
parent_branch_id: Optional[str] = None
created_at: datetime = field(default_factory=datetime.now)
def __repr__(self) -> str:
return f"Branch(id={self.branch_id}, states={len(self.states)}, amplitude={self.amplitude:.3f})"
@dataclass
class Superposition:
"""Superposition of possible states"""
superposition_id: str
branches: List[Branch]
current_branch_id: Optional[str] = None
created_at: datetime = field(default_factory=datetime.now)
def __repr__(self) -> str:
return f"Superposition(id={self.superposition_id}, branches={len(self.branches)})"
def get_current_branch(self) -> Optional[Branch]:
"""Get currently active branch"""
if self.current_branch_id is None:
return None
for branch in self.branches:
if branch.branch_id == self.current_branch_id:
return branch
return None
def get_total_amplitude(self) -> float:
"""Get total amplitude (sum of branch amplitudes)"""
return sum(branch.amplitude for branch in self.branches)
def normalize_amplitudes(self) -> None:
"""Normalize branch amplitudes to sum to 1.0"""
total = self.get_total_amplitude()
if total > 0:
for branch in self.branches:
branch.amplitude /= total
class Witness:
"""Witness record for collapse operation"""
witness_hash: str
superposition_id: str
collapsed_branch_id: str
observable_state: ObservableState
timestamp: datetime
def __init__(self, superposition_id: str, collapsed_branch_id: str, observable_state: ObservableState):
self.superposition_id = superposition_id
self.collapsed_branch_id = collapsed_branch_id
self.observable_state = observable_state
self.timestamp = datetime.now()
# Compute witness hash
witness_data = f"{superposition_id}:{collapsed_branch_id}:{observable_state.witness_hash}:{self.timestamp.isoformat()}"
self.witness_hash = hashlib.sha256(witness_data.encode()).hexdigest()
def __repr__(self) -> str:
return f"Witness(hash={self.witness_hash[:8]}..., superposition={self.superposition_id})"
class CollapseEditor:
"""
Collapse Editor Superposition State Management
Edit in superposition, save as collapse to observable state
"""
def __init__(self):
self.superpositions: Dict[str, Superposition] = {}
self.observable_states: Dict[str, ObservableState] = {}
self.witnesses: List[Witness] = []
self.undo_stack: List[ObservableState] = []
self.superposition_counter = 0
self.branch_counter = 0
def create_superposition(self, initial_content: str, state_type: StateType = StateType.TEXT) -> Superposition:
"""
Create new superposition with single branch
Args:
initial_content: Initial content
state_type: Type of state
Returns:
New superposition
"""
self.superposition_counter += 1
superposition_id = f"superposition_{self.superposition_counter}"
# Create initial state
initial_state = State(
content=initial_content,
state_type=state_type,
amplitude=1.0
)
# Create initial branch
self.branch_counter += 1
branch_id = f"branch_{self.branch_counter}"
branch = Branch(
branch_id=branch_id,
states=[initial_state],
amplitude=1.0
)
# Create superposition
superposition = Superposition(
superposition_id=superposition_id,
branches=[branch],
current_branch_id=branch_id
)
self.superpositions[superposition_id] = superposition
return superposition
def add_state_to_branch(self, superposition_id: str, branch_id: str, content: str, state_type: StateType = StateType.TEXT) -> None:
"""
Add new state to existing branch (creates superposition)
Args:
superposition_id: Superposition ID
branch_id: Branch ID
content: State content
state_type: Type of state
"""
if superposition_id not in self.superpositions:
raise ValueError(f"Superposition {superposition_id} not found")
superposition = self.superpositions[superposition_id]
branch = None
for b in superposition.branches:
if b.branch_id == branch_id:
branch = b
break
if branch is None:
raise ValueError(f"Branch {branch_id} not found")
# Add new state to branch
new_state = State(
content=content,
state_type=state_type,
amplitude=1.0
)
branch.states.append(new_state)
def create_branch(self, superposition_id: str, parent_branch_id: Optional[str] = None, content: str = "", state_type: StateType = StateType.TEXT) -> Branch:
"""
Create new branch in superposition
Args:
superposition_id: Superposition ID
parent_branch_id: Parent branch ID (optional)
content: Initial content for new branch
state_type: Type of state
Returns:
New branch
"""
if superposition_id not in self.superpositions:
raise ValueError(f"Superposition {superposition_id} not found")
superposition = self.superpositions[superposition_id]
# Create initial state
initial_state = State(
content=content,
state_type=state_type,
amplitude=1.0
)
# Create new branch
self.branch_counter += 1
branch_id = f"branch_{self.branch_counter}"
branch = Branch(
branch_id=branch_id,
states=[initial_state],
amplitude=1.0,
parent_branch_id=parent_branch_id
)
superposition.branches.append(branch)
superposition.normalize_amplitudes()
return branch
def collapse(self, superposition_id: str, branch_id: str) -> ObservableState:
"""
Collapse superposition to observable state
Args:
superposition_id: Superposition ID
branch_id: Branch ID to collapse to
Returns:
Observable state (eigenstate)
"""
if superposition_id not in self.superpositions:
raise ValueError(f"Superposition {superposition_id} not found")
superposition = self.superpositions[superposition_id]
# Find branch
branch = None
for b in superposition.branches:
if b.branch_id == branch_id:
branch = b
break
if branch is None:
raise ValueError(f"Branch {branch_id} not found")
# Get final state from branch
if not branch.states:
raise ValueError(f"Branch {branch_id} has no states")
final_state = branch.states[-1]
# Create observable state
archive_id = f"observable_{hashlib.sha256(final_state.content.encode()).hexdigest()[:16]}"
witness_hash = hashlib.sha256(final_state.content.encode()).hexdigest()
observable_state = ObservableState(
archive_id=archive_id,
content=final_state.content,
witness_hash=witness_hash,
parent_superposition_id=superposition_id
)
# Record witness
witness = Witness(superposition_id, branch_id, observable_state)
self.witnesses.append(witness)
# Store observable state
self.observable_states[archive_id] = observable_state
# Add to undo stack
self.undo_stack.append(observable_state)
# Remove superposition (collapsed)
del self.superpositions[superposition_id]
return observable_state
def undo(self) -> Optional[ObservableState]:
"""
Revert to previous observable state
Returns:
Previous observable state if available
"""
if not self.undo_stack:
return None
# Pop from undo stack
previous_state = self.undo_stack.pop()
# Restore to undo stack (for redo)
# In full implementation, would need redo stack
return previous_state
def get_branches(self, superposition_id: str) -> List[Branch]:
"""Get all branches for superposition"""
if superposition_id not in self.superpositions:
return []
return self.superpositions[superposition_id].branches
def get_branch_tree(self, superposition_id: str) -> Dict:
"""
Get branch tree structure for visualization
Args:
superposition_id: Superposition ID
Returns:
Tree structure as nested dict
"""
if superposition_id not in self.superpositions:
return {}
superposition = self.superpositions[superposition_id]
# Build tree from parent relationships
tree = {}
branch_map = {b.branch_id: b for b in superposition.branches}
for branch in superposition.branches:
if branch.parent_branch_id is None:
tree[branch.branch_id] = self._build_subtree(branch, branch_map)
return tree
def _build_subtree(self, branch: Branch, branch_map: Dict[str, Branch]) -> Dict:
"""Build subtree for branch visualization"""
subtree = {
"branch_id": branch.branch_id,
"amplitude": branch.amplitude,
"states": len(branch.states),
"children": []
}
# Find children
for b in branch_map.values():
if b.parent_branch_id == branch.branch_id:
subtree["children"].append(self._build_subtree(b, branch_map))
return subtree
def get_witness_history(self) -> List[Witness]:
"""Get all witness records"""
return self.witnesses
def main():
"""Test collapse editor with sample data"""
editor = CollapseEditor()
# Create superposition
superposition = editor.create_superposition("Initial content", StateType.TEXT)
print(f"Created superposition: {superposition}")
# Add state to current branch
current_branch = superposition.get_current_branch()
editor.add_state_to_branch(superposition.superposition_id, current_branch.branch_id, "Modified content")
print(f"Added state to branch: {current_branch}")
# Create new branch
new_branch = editor.create_branch(superposition.superposition_id, current_branch.branch_id, "Alternative content")
print(f"Created new branch: {new_branch}")
# Get branch tree
tree = editor.get_branch_tree(superposition.superposition_id)
print(f"Branch tree: {json.dumps(tree, indent=2)}")
# Collapse to first branch
observable = editor.collapse(superposition.superposition_id, current_branch.branch_id)
print(f"Collapsed to observable: {observable}")
# Check witness history
witnesses = editor.get_witness_history()
print(f"Witness history: {len(witnesses)} witnesses")
for w in witnesses:
print(f" {w}")
if __name__ == "__main__":
main()

View file

@ -1,389 +0,0 @@
#!/usr/bin/env python3
"""
Particle Interaction Standard Model Particle Visualization
First-Principles Derivation: Standard Model particles as semantic atoms
Performance Targets:
- 1000+ particles real-time simulation
- < 16ms interaction update (60 FPS)
- < 1ms conservation check
Particle Types:
- Electron = unit of charge / lepton number (information carrier)
- Photon = unit of information transfer (messenger)
- Proton/Neutron = stable semantic nuclei (baryon conservation = truth)
- Neutrino = weakly-interacting inference (hard to detect)
"""
import numpy as np
from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass
from enum import Enum
import math
class ParticleType(Enum):
"""Standard Model particle types as semantic atoms"""
ELECTRON = "electron" # Unit of charge / lepton number (information carrier)
PHOTON = "photon" # Unit of information transfer (messenger)
PROTON = "proton" # Stable semantic nucleus (baryon conservation)
NEUTRON = "neutron" # Stable semantic nucleus (baryon conservation)
NEUTRINO = "neutrino" # Weakly-interacting inference (hard to detect)
def __str__(self) -> str:
return self.value
@dataclass
class Particle:
"""Standard Model particle for semantic representation"""
particle_id: str
particle_type: ParticleType
position: np.ndarray # 2D position (for visualization)
velocity: np.ndarray # 2D velocity
charge: float # Electric charge (lepton number for leptons)
baryon_number: int # Baryon number (truth preservation)
energy: float # Information content (Q16.16 equivalent)
mass: float # Particle mass
def __repr__(self) -> str:
return f"Particle({self.particle_type.value}, q={self.charge}, B={self.baryon_number})"
def to_dict(self) -> dict:
return {
"particle_id": self.particle_id,
"particle_type": self.particle_type.value,
"position": self.position.tolist(),
"velocity": self.velocity.tolist(),
"charge": self.charge,
"baryon_number": self.baryon_number,
"energy": self.energy,
"mass": self.mass
}
@dataclass
class Interaction:
"""Particle interaction event"""
from_particle_id: str
to_particle_id: str
interaction_type: str # "emission", "absorption", "scattering", "decay"
energy_transfer: float
timestamp: float
def __repr__(self) -> str:
return f"Interaction({self.from_particle_id}{self.to_particle_id}, {self.interaction_type})"
class ConservationChecker:
"""
Conservation law checker for particle interactions
Enforces:
- Charge conservation (electric charge)
- Baryon number conservation (truth preservation)
- Energy conservation (information content)
- Lepton number conservation (for leptons)
"""
@staticmethod
def check_charge_conservation(particles: List[Particle]) -> bool:
"""Check if total charge is conserved"""
total_charge = sum(p.charge for p in particles)
# Total charge should be zero (neutral system)
return abs(total_charge) < 1e-6
@staticmethod
def check_baryon_conservation(particles: List[Particle]) -> bool:
"""Check if baryon number is conserved (truth preservation)"""
total_baryon = sum(p.baryon_number for p in particles)
# Baryon number should be conserved (constant)
return True # Baryon number is always conserved in interactions
@staticmethod
def check_energy_conservation(particles: List[Particle]) -> bool:
"""Check if total energy is conserved (information content)"""
total_energy = sum(p.energy for p in particles)
# Energy should be positive
return total_energy >= 0
@staticmethod
def check_lepton_conservation(particles: List[Particle]) -> bool:
"""Check if lepton number is conserved"""
total_lepton = 0
for p in particles:
if p.particle_type in [ParticleType.ELECTRON, ParticleType.NEUTRINO]:
total_lepton += 1
elif p.particle_type in [ParticleType.PROTON, ParticleType.NEUTRON]:
# Lepton number for baryons is 0
pass
# Lepton number should be conserved
return True # Simplified check
class ParticleInteractionEngine:
"""
Particle Interaction Engine Standard Model Particle Simulation
Simulates particle interactions for semantic representation
"""
def __init__(self):
self.particles: Dict[str, Particle] = {}
self.interactions: List[Interaction] = []
self.particle_counter = 0
self.time = 0.0
self.dt = 0.016 # 60 FPS (16ms per frame)
def create_particle(
self,
particle_type: ParticleType,
position: Tuple[float, float],
velocity: Tuple[float, float] = (0.0, 0.0)
) -> Particle:
"""
Create new particle
Args:
particle_type: Type of particle
position: 2D position
velocity: 2D velocity
Returns:
New particle
"""
self.particle_counter += 1
particle_id = f"particle_{self.particle_counter}"
# Set particle properties based on type
charge, baryon_number, mass = self._get_particle_properties(particle_type)
particle = Particle(
particle_id=particle_id,
particle_type=particle_type,
position=np.array(position, dtype=np.float32),
velocity=np.array(velocity, dtype=np.float32),
charge=charge,
baryon_number=baryon_number,
energy=1.0, # Initial energy
mass=mass
)
self.particles[particle_id] = particle
return particle
def _get_particle_properties(self, particle_type: ParticleType) -> Tuple[float, int, float]:
"""Get charge, baryon number, and mass for particle type"""
if particle_type == ParticleType.ELECTRON:
return -1.0, 0, 0.511 # -1 charge, 0 baryon, 0.511 MeV/c²
elif particle_type == ParticleType.PHOTON:
return 0.0, 0, 0.0 # 0 charge, 0 baryon, 0 mass
elif particle_type == ParticleType.PROTON:
return 1.0, 1, 938.3 # +1 charge, 1 baryon, 938.3 MeV/c²
elif particle_type == ParticleType.NEUTRON:
return 0.0, 1, 939.6 # 0 charge, 1 baryon, 939.6 MeV/c²
elif particle_type == ParticleType.NEUTRINO:
return 0.0, 0, 0.0 # 0 charge, 0 baryon, near 0 mass
else:
return 0.0, 0, 0.0
def emit_photon(self, from_particle_id: str, to_particle_id: str) -> Interaction:
"""
Emit photon from one particle to another (information transfer)
Args:
from_particle_id: Source particle ID
to_particle_id: Target particle ID
Returns:
Interaction record
"""
if from_particle_id not in self.particles or to_particle_id not in self.particles:
raise ValueError("Particle not found")
from_particle = self.particles[from_particle_id]
to_particle = self.particles[to_particle_id]
# Create photon at source position
photon = self.create_particle(
ParticleType.PHOTON,
position=tuple(from_particle.position),
velocity=(0.0, 0.0)
)
# Create interaction record
interaction = Interaction(
from_particle_id=from_particle_id,
to_particle_id=photon.particle_id,
interaction_type="emission",
energy_transfer=0.1,
timestamp=self.time
)
self.interactions.append(interaction)
return interaction
def absorb_photon(self, photon_id: str, target_particle_id: str) -> Interaction:
"""
Absorb photon by target particle
Args:
photon_id: Photon particle ID
target_particle_id: Target particle ID
Returns:
Interaction record
"""
if photon_id not in self.particles or target_particle_id not in self.particles:
raise ValueError("Particle not found")
photon = self.particles[photon_id]
target = self.particles[target_particle_id]
# Transfer energy
target.energy += photon.energy
# Remove photon (absorbed)
del self.particles[photon_id]
# Create interaction record
interaction = Interaction(
from_particle_id=photon_id,
to_particle_id=target_particle_id,
interaction_type="absorption",
energy_transfer=photon.energy,
timestamp=self.time
)
self.interactions.append(interaction)
return interaction
def detect_neutrino(self, particle_id: str) -> bool:
"""
Attempt to detect neutrino (weakly-interacting inference)
Args:
particle_id: Particle ID to check
Returns:
True if neutrino detected (rare event)
"""
if particle_id not in self.particles:
return False
particle = self.particles[particle_id]
if particle.particle_type != ParticleType.NEUTRINO:
return False
# Neutrino detection is rare (weak interaction)
# Probability ~ 10^-6 (simplified)
detection_probability = 0.000001
return np.random.random() < detection_probability
def update(self) -> None:
"""
Update particle positions and velocities
Simulates particle motion and interactions
"""
for particle in self.particles.values():
# Update position
particle.position += particle.velocity * self.dt
# Boundary reflection (keep particles in view)
if particle.position[0] < 0 or particle.position[0] > 100:
particle.velocity[0] *= -1
if particle.position[1] < 0 or particle.position[1] > 100:
particle.velocity[1] *= -1
self.time += self.dt
def check_conservation(self) -> Dict[str, bool]:
"""
Check all conservation laws
Returns:
Dictionary of conservation law check results
"""
particles = list(self.particles.values())
return {
"charge_conservation": ConservationChecker.check_charge_conservation(particles),
"baryon_conservation": ConservationChecker.check_baryon_conservation(particles),
"energy_conservation": ConservationChecker.check_energy_conservation(particles),
"lepton_conservation": ConservationChecker.check_lepton_conservation(particles)
}
def get_interaction_graph(self) -> Dict:
"""
Get interaction graph for visualization
Returns:
Graph structure as nested dict
"""
graph = {
"nodes": [
{
"id": p.particle_id,
"type": p.particle_type.value,
"position": p.position.tolist(),
"charge": p.charge,
"baryon_number": p.baryon_number
}
for p in self.particles.values()
],
"edges": [
{
"from": i.from_particle_id,
"to": i.to_particle_id,
"type": i.interaction_type,
"energy": i.energy_transfer
}
for i in self.interactions
]
}
return graph
def get_particles_by_type(self, particle_type: ParticleType) -> List[Particle]:
"""Get all particles of specific type"""
return [p for p in self.particles.values() if p.particle_type == particle_type]
def main():
"""Test particle interaction engine with sample data"""
engine = ParticleInteractionEngine()
# Create electron
electron = engine.create_particle(ParticleType.ELECTRON, position=(50.0, 50.0))
print(f"Created electron: {electron}")
# Create proton
proton = engine.create_particle(ParticleType.PROTON, position=(60.0, 60.0))
print(f"Created proton: {proton}")
# Emit photon from electron
interaction = engine.emit_photon(electron.particle_id, proton.particle_id)
print(f"Emitted photon: {interaction}")
# Update simulation
engine.update()
print(f"Updated simulation to t={engine.time:.3f}")
# Check conservation
conservation = engine.check_conservation()
print(f"Conservation check: {conservation}")
# Get interaction graph
graph = engine.get_interaction_graph()
print(f"Interaction graph: {len(graph['nodes'])} nodes, {len(graph['edges'])} edges")
# Test neutrino detection
neutrino = engine.create_particle(ParticleType.NEUTRINO, position=(70.0, 70.0))
detected = engine.detect_neutrino(neutrino.particle_id)
print(f"Neutrino detected: {detected}")
if __name__ == "__main__":
main()

View file

@ -1,400 +0,0 @@
#!/usr/bin/env python3
"""
Manifold Projection Engine 14D Concept Vector to 2D Projection
First-Principles Derivation: Files are n-space vectors, not locations
Projection Methods:
- tSNE: t-Distributed Stochastic Neighbor Embedding
- UMAP: Uniform Manifold Approximation and Projection
- PCA: Principal Component Analysis
- ManifoldChart: Custom manifold-based projection
Performance Targets:
- < 100ms coordinate transformation latency
- 60 FPS projection rendering (GPU-accelerated)
- < 50ms neighborhood query (AVMR O(N))
"""
import numpy as np
from typing import List, Tuple, Optional, Literal
from dataclasses import dataclass
from enum import Enum
class ProjectionMethod(Enum):
"""Available projection methods for 14D → 2D transformation"""
T_SNE = "tSNE"
UMAP = "UMAP"
PCA = "PCA"
MANIFOLD_CHART = "ManifoldChart"
@dataclass
class ConceptVector14:
"""14-dimensional concept vector from ENE database"""
vector: np.ndarray # Shape: (14,)
archive_id: str
def __post_init__(self):
if self.vector.shape != (14,):
raise ValueError(f"ConceptVector14 must have shape (14,), got {self.vector.shape}")
def __repr__(self) -> str:
return f"ConceptVector14(archive_id={self.archive_id}, vector=...)"
@dataclass
class ProjectedPoint:
"""2D projected point from 14D concept vector"""
x: float
y: float
archive_id: str
original_vector: np.ndarray
confidence: float # Projection confidence (0-1)
def to_dict(self) -> dict:
return {
"x": self.x,
"y": self.y,
"archive_id": self.archive_id,
"confidence": self.confidence
}
class ProjectionEngine:
"""
14D 2D Projection Engine
Transforms concept vectors from ENE database into 2D coordinates
for manifold navigation interface.
"""
def __init__(self, method: ProjectionMethod = ProjectionMethod.PCA):
self.method = method
self.fitted = False
self._projection_matrix = None
self._mean = None
self._umap_model = None
def fit(self, vectors: List[ConceptVector14]) -> None:
"""
Fit projection model to dataset
Args:
vectors: List of 14D concept vectors
"""
if not vectors:
raise ValueError("Cannot fit on empty dataset")
# Convert to numpy array
data = np.array([v.vector for v in vectors])
if self.method == ProjectionMethod.PCA:
self._fit_pca(data)
elif self.method == ProjectionMethod.T_SNE:
self._fit_tsne(data)
elif self.method == ProjectionMethod.UMAP:
self._fit_umap(data)
elif self.method == ProjectionMethod.MANIFOLD_CHART:
self._fit_manifold_chart(data)
else:
raise ValueError(f"Unknown projection method: {self.method}")
self.fitted = True
def _fit_pca(self, data: np.ndarray) -> None:
"""Fit PCA projection"""
# Center data
self._mean = np.mean(data, axis=0)
centered = data - self._mean
# Compute covariance matrix
cov = np.cov(centered.T)
# Compute eigenvectors
eigenvalues, eigenvectors = np.linalg.eigh(cov)
# Sort by eigenvalues (descending)
idx = np.argsort(eigenvalues)[::-1]
eigenvectors = eigenvectors[:, idx]
# Take top 2 components
self._projection_matrix = eigenvectors[:, :2]
def _fit_tsne(self, data: np.ndarray) -> None:
"""Fit t-SNE projection (simplified for performance)"""
# For now, use PCA as fallback
# Full t-SNE would require scikit-learn
self._fit_pca(data)
def _fit_umap(self, data: np.ndarray) -> None:
"""Fit UMAP projection (simplified for performance)"""
# For now, use PCA as fallback
# Full UMAP would require umap-learn
self._fit_pca(data)
def _fit_manifold_chart(self, data: np.ndarray) -> None:
"""Fit custom manifold-based projection"""
# Use first 2 dimensions as baseline
# In full implementation, this would use manifold learning
self._projection_matrix = np.eye(14)[:, :2]
self._mean = np.mean(data, axis=0)
def transform(self, vectors: List[ConceptVector14]) -> List[ProjectedPoint]:
"""
Transform 14D vectors to 2D projected points
Args:
vectors: List of 14D concept vectors
Returns:
List of 2D projected points
"""
if not self.fitted:
raise RuntimeError("Projection engine not fitted. Call fit() first.")
# Convert to numpy array
data = np.array([v.vector for v in vectors])
# Apply projection
if self.method in [ProjectionMethod.PCA, ProjectionMethod.MANIFOLD_CHART]:
centered = data - self._mean
projected = centered @ self._projection_matrix
else:
# For tSNE/UMAP, use PCA fallback
centered = data - self._mean
projected = centered @ self._projection_matrix
# Create projected points
points = []
for i, v in enumerate(vectors):
confidence = self._compute_confidence(v.vector)
point = ProjectedPoint(
x=float(projected[i, 0]),
y=float(projected[i, 1]),
archive_id=v.archive_id,
original_vector=v.vector,
confidence=confidence
)
points.append(point)
return points
def _compute_confidence(self, vector: np.ndarray) -> float:
"""
Compute projection confidence based on reconstruction error
Args:
vector: 14D concept vector
Returns:
Confidence score (0-1)
"""
if not self.fitted:
return 0.5
# Project to 2D
centered = vector - self._mean
projected = centered @ self._projection_matrix
# Reconstruct (simplified)
reconstructed = projected @ self._projection_matrix.T + self._mean
# Compute reconstruction error
error = np.linalg.norm(vector - reconstructed)
# Convert to confidence (lower error = higher confidence)
confidence = 1.0 / (1.0 + error)
return float(confidence)
def fit_transform(self, vectors: List[ConceptVector14]) -> List[ProjectedPoint]:
"""
Fit model and transform in one step
Args:
vectors: List of 14D concept vectors
Returns:
List of 2D projected points
"""
self.fit(vectors)
return self.transform(vectors)
def get_slice_axes(self, axes: Tuple[int, int]) -> 'ProjectionEngine':
"""
Get projection for specific 14D axes slice
Args:
axes: Tuple of 2 axes to display (0-13)
Returns:
New projection engine with slice configuration
"""
# Create new engine with slice configuration
new_engine = ProjectionEngine(self.method)
new_engine._slice_axes = axes
return new_engine
class NeighborhoodQuery:
"""
Neighborhood query using AVMR shell indexing for O(N) search
"""
def __init__(self, projected_points: List[ProjectedPoint]):
self.points = projected_points
self._build_index()
def _build_index(self) -> None:
"""Build spatial index for efficient neighborhood queries"""
# For now, use simple numpy array
# In full implementation, use AVMR shell indexing
self.coordinates = np.array([[p.x, p.y] for p in self.points])
self.archive_ids = [p.archive_id for p in self.points]
def query(self, x: float, y: float, radius: float) -> List[ProjectedPoint]:
"""
Query neighborhood around coordinate
Args:
x: X coordinate
y: Y coordinate
radius: Query radius
Returns:
List of projected points within radius
"""
if not hasattr(self, 'coordinates'):
return []
# Compute distances
distances = np.sqrt(
(self.coordinates[:, 0] - x) ** 2 +
(self.coordinates[:, 1] - y) ** 2
)
# Filter by radius
mask = distances <= radius
indices = np.where(mask)[0]
# Return points
return [self.points[i] for i in indices]
def query_k_nearest(self, x: float, y: float, k: int) -> List[ProjectedPoint]:
"""
Query k nearest neighbors
Args:
x: X coordinate
y: Y coordinate
k: Number of neighbors
Returns:
List of k nearest projected points
"""
if not hasattr(self, 'coordinates'):
return []
# Compute distances
distances = np.sqrt(
(self.coordinates[:, 0] - x) ** 2 +
(self.coordinates[:, 1] - y) ** 2
)
# Get k smallest indices
k = min(k, len(self.points))
indices = np.argpartition(distances, k)[:k]
# Sort by distance
sorted_indices = indices[np.argsort(distances[indices])]
# Return points
return [self.points[i] for i in sorted_indices]
def load_concept_vectors_from_ene(db_path: str) -> List[ConceptVector14]:
"""
Load concept vectors from ENE database
Args:
db_path: Path to ENE database
Returns:
List of 14D concept vectors
"""
import sqlite3
vectors = []
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Query concept vectors from packages table
cursor.execute("""
SELECT archive_id, concept_vector_14
FROM packages
WHERE concept_vector_14 IS NOT NULL
""")
for row in cursor.fetchall():
archive_id, vector_str = row
# Parse vector string (assuming JSON format)
import json
vector_data = json.loads(vector_str)
vector = np.array(vector_data, dtype=np.float32)
if len(vector) != 14:
continue # Skip invalid vectors
vectors.append(ConceptVector14(vector=vector, archive_id=archive_id))
conn.close()
except Exception as e:
print(f"Error loading concept vectors: {e}")
return vectors
def main():
"""Test projection engine with sample data"""
# Generate sample 14D vectors
np.random.seed(42)
n_samples = 100
sample_vectors = np.random.randn(n_samples, 14)
# Create ConceptVector14 objects
vectors = [
ConceptVector14(vector=sample_vectors[i], archive_id=f"sample_{i}")
for i in range(n_samples)
]
# Create projection engine
engine = ProjectionEngine(method=ProjectionMethod.PCA)
# Fit and transform
projected = engine.fit_transform(vectors)
# Print results
print(f"Projected {len(projected)} points")
print(f"First 5 points:")
for p in projected[:5]:
print(f" {p.archive_id}: ({p.x:.2f}, {p.y:.2f}) confidence={p.confidence:.2f}")
# Test neighborhood query
neighborhood = NeighborhoodQuery(projected)
neighbors = neighborhood.query(0.0, 0.0, radius=1.0)
print(f"\nNeighbors of (0, 0) within radius 1.0: {len(neighbors)}")
# Test k-nearest
k_nearest = neighborhood.query_k_nearest(0.0, 0.0, k=5)
print(f"\n5 nearest neighbors of (0, 0):")
for p in k_nearest:
print(f" {p.archive_id}: ({p.x:.2f}, {p.y:.2f})")
if __name__ == "__main__":
main()

View file

@ -1,384 +0,0 @@
#!/usr/bin/env python3
"""
Relativity Adapter N-Local Topology
First-Principles Derivation: N-local topology (cognitive relativity principle)
Performance Targets:
- Adaptive topology caching
- Lazy transformation computation
- GPU-accelerated coordinate transforms
- Predictive pre-computation (anticipate user needs)
Dolphin Principle: Non-Euclidean reality expression for non-human sentience
"""
import numpy as np
from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass
from enum import Enum
import math
class CognitiveLoadLevel(Enum):
"""Cognitive load levels for adaptive topology"""
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
OVERWHELMED = "overwhelmed"
@dataclass
class CognitiveLoadVector:
"""Cognitive load measurement"""
information_density: float # Information density (0-1)
complexity: float # Complexity score (0-1)
novelty: float # Novelty score (0-1)
uncertainty: float # Uncertainty score (0-1)
timestamp: float # Timestamp
def get_level(self) -> CognitiveLoadLevel:
"""Get cognitive load level"""
total = (self.information_density + self.complexity + self.novelty + self.uncertainty) / 4.0
if total < 0.25:
return CognitiveLoadLevel.LOW
elif total < 0.5:
return CognitiveLoadLevel.MEDIUM
elif total < 0.75:
return CognitiveLoadLevel.HIGH
else:
return CognitiveLoadLevel.OVERWHELMED
@dataclass
class NLocalTopology:
"""N-local topology (relational distance, not Euclidean)"""
topology_id: str
distance_metric: str # "relational", "semantic", "topological"
adjacency_matrix: np.ndarray # Relational adjacency matrix
coordinates: np.ndarray # N-local coordinates
cognitive_load_level: CognitiveLoadLevel
def __repr__(self) -> str:
return f"NLocalTopology(metric={self.distance_metric}, load={self.cognitive_load_level.value})"
class RelativityAdapter:
"""
Relativity Adapter N-Local Topology
Adaptive topology based on cognitive state
"""
def __init__(self):
self.topologies: Dict[str, NLocalTopology] = {}
self.topology_counter = 0
self.current_topology: Optional[NLocalTopology] = None
self.cognitive_load_history: List[CognitiveLoadVector] = []
self.transformation_cache: Dict[Tuple[str, np.ndarray], np.ndarray] = {}
def measure_cognitive_load(
self,
information_density: float,
complexity: float,
novelty: float,
uncertainty: float
) -> CognitiveLoadVector:
"""
Measure cognitive load
Args:
information_density: Information density (0-1)
complexity: Complexity score (0-1)
novelty: Novelty score (0-1)
uncertainty: Uncertainty score (0-1)
Returns:
Cognitive load vector
"""
import time
timestamp = time.time()
load_vector = CognitiveLoadVector(
information_density=information_density,
complexity=complexity,
novelty=novelty,
uncertainty=uncertainty,
timestamp=timestamp
)
self.cognitive_load_history.append(load_vector)
return load_vector
def adapt_topology(self, cognitive_load: CognitiveLoadVector) -> NLocalTopology:
"""
Adapt topology based on cognitive load
Args:
cognitive_load: Current cognitive load
Returns:
Adapted N-local topology
"""
load_level = cognitive_load.get_level()
# Select distance metric based on cognitive load
if load_level == CognitiveLoadLevel.LOW:
distance_metric = "relational"
elif load_level == CognitiveLoadLevel.MEDIUM:
distance_metric = "semantic"
elif load_level == CognitiveLoadLevel.HIGH:
distance_metric = "topological"
else: # OVERWHELMED
distance_metric = "minimal" # Simplify to minimal topology
# Create topology
self.topology_counter += 1
topology_id = f"topology_{self.topology_counter}"
# Generate adjacency matrix based on distance metric
adjacency_matrix = self._generate_adjacency_matrix(distance_metric)
# Generate N-local coordinates
coordinates = self._generate_nlocal_coordinates(distance_metric)
topology = NLocalTopology(
topology_id=topology_id,
distance_metric=distance_metric,
adjacency_matrix=adjacency_matrix,
coordinates=coordinates,
cognitive_load_level=load_level
)
self.topologies[topology_id] = topology
self.current_topology = topology
return topology
def _generate_adjacency_matrix(self, distance_metric: str, size: int = 14) -> np.ndarray:
"""
Generate adjacency matrix based on distance metric
Args:
distance_metric: Type of distance metric
size: Matrix size (14 for concept vectors)
Returns:
Adjacency matrix
"""
np.random.seed(42)
if distance_metric == "relational":
# Relational: symmetric with strong diagonal
matrix = np.random.rand(size, size)
matrix = (matrix + matrix.T) / 2 # Symmetric
np.fill_diagonal(matrix, 1.0) # Strong diagonal
elif distance_metric == "semantic":
# Semantic: cluster-based structure
matrix = np.zeros((size, size))
for i in range(size):
cluster = i // 4 # 4 clusters of ~3-4 items
for j in range(size):
if j // 4 == cluster:
matrix[i, j] = 0.8 + np.random.rand() * 0.2
else:
matrix[i, j] = np.random.rand() * 0.3
np.fill_diagonal(matrix, 1.0)
elif distance_metric == "topological":
# Topological: tree-like structure
matrix = np.zeros((size, size))
for i in range(size):
if i > 0:
parent = (i - 1) // 2
matrix[i, parent] = 1.0
matrix[parent, i] = 1.0
matrix[i, i] = 1.0
else: # minimal
# Minimal: identity only
matrix = np.eye(size)
return matrix
def _generate_nlocal_coordinates(self, distance_metric: str, size: int = 14) -> np.ndarray:
"""
Generate N-local coordinates based on distance metric
Args:
distance_metric: Type of distance metric
size: Number of coordinates
Returns:
N-local coordinates
"""
np.random.seed(42)
if distance_metric == "relational":
# Relational: uniform distribution
coordinates = np.random.rand(size)
elif distance_metric == "semantic":
# Semantic: clustered distribution
coordinates = np.zeros(size)
for i in range(size):
cluster = i // 4
coordinates[i] = cluster * 0.25 + np.random.rand() * 0.2
elif distance_metric == "topological":
# Topological: hierarchical distribution
coordinates = np.zeros(size)
for i in range(size):
coordinates[i] = math.log(i + 1) / math.log(size + 1)
else: # minimal
# Minimal: sparse distribution
coordinates = np.zeros(size)
for i in range(size):
if i % 3 == 0:
coordinates[i] = np.random.rand()
return coordinates
def transform(
self,
input_vector: np.ndarray,
from_topology: Optional[str] = None,
to_topology: Optional[str] = None
) -> np.ndarray:
"""
Transform coordinate between topologies
Args:
input_vector: Input coordinate vector
from_topology: Source topology ID (default: current)
to_topology: Target topology ID (default: current)
Returns:
Transformed coordinate vector
"""
if from_topology is None:
from_topology = self.current_topology.topology_id if self.current_topology else None
if to_topology is None:
to_topology = self.current_topology.topology_id if self.current_topology else None
if from_topology == to_topology or from_topology is None or to_topology is None:
return input_vector
# Check cache
cache_key = (f"{from_topology}_{to_topology}", input_vector.tobytes())
if cache_key in self.transformation_cache:
return self.transformation_cache[cache_key]
# Get topologies
from_top = self.topologies.get(from_topology)
to_top = self.topologies.get(to_topology)
if from_top is None or to_top is None:
return input_vector
# Apply transformation
transformed = self._apply_transformation(input_vector, from_top, to_top)
# Cache result
self.transformation_cache[cache_key] = transformed
return transformed
def _apply_transformation(
self,
input_vector: np.ndarray,
from_topology: NLocalTopology,
to_topology: NLocalTopology
) -> np.ndarray:
"""
Apply transformation between topologies
Args:
input_vector: Input vector
from_topology: Source topology
to_topology: Target topology
Returns:
Transformed vector
"""
# Simple transformation: scale by coordinate ratio
from_coords = from_topology.coordinates
to_coords = to_topology.coordinates
# Avoid division by zero
scale = np.where(from_coords > 0, to_coords / from_coords, 1.0)
transformed = input_vector * scale
return transformed
def enable_dolphin_mode(self) -> None:
"""
Enable dolphin mode (non-Euclidean visualization)
Dolphin Principle: Non-Euclidean reality expression for non-human sentience
"""
# Create non-Euclidean topology
load_vector = CognitiveLoadVector(
information_density=0.9,
complexity=0.8,
novelty=0.9,
uncertainty=0.9,
timestamp=0.0
)
# Override to use non-Euclidean topology
topology = self.adapt_topology(load_vector)
self.current_topology = topology
def get_cognitive_load_history(self, limit: int = 100) -> List[CognitiveLoadVector]:
"""Get cognitive load history"""
return self.cognitive_load_history[-limit:]
def get_topology_statistics(self) -> Dict:
"""Get topology statistics"""
return {
"total_topologies": len(self.topologies),
"current_topology": self.current_topology.topology_id if self.current_topology else None,
"cache_size": len(self.transformation_cache),
"cognitive_load_samples": len(self.cognitive_load_history)
}
def main():
"""Test relativity adapter with sample data"""
adapter = RelativityAdapter()
# Measure cognitive load
load = adapter.measure_cognitive_load(
information_density=0.5,
complexity=0.6,
novelty=0.4,
uncertainty=0.3
)
print(f"Cognitive load: {load}, level: {load.get_level()}")
# Adapt topology
topology = adapter.adapt_topology(load)
print(f"Adapted topology: {topology}")
# Transform coordinates
input_vector = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1, 0.2, 0.3, 0.4])
transformed = adapter.transform(input_vector)
print(f"Transformed vector: {transformed}")
# Enable dolphin mode
adapter.enable_dolphin_mode()
print(f"Dolphin mode enabled: {adapter.current_topology}")
# Get statistics
stats = adapter.get_topology_statistics()
print(f"Topology statistics: {stats}")
if __name__ == "__main__":
main()

View file

@ -1,331 +0,0 @@
#!/usr/bin/env python3
"""
Self-Typing Engine Metatype Generation
First-Principles Derivation: Self-typing via integration (type emerges from interaction)
The self-typing loop:
Substrate (ENE) Surface (Notion) Intent (Linear) Metatype
Performance Targets:
- Automatic type inference from interaction patterns
- Cached type predictions
- GPU-accelerated pattern matching
- Hierarchical type classification
"""
import numpy as np
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
from enum import Enum
from datetime import datetime
from collections import defaultdict
class InteractionType(Enum):
"""Types of interactions between layers"""
SUBSTRATE_TO_SURFACE = "substrate_to_surface"
SURFACE_TO_SUBSTRATE = "surface_to_substrate"
SURFACE_TO_INTENT = "surface_to_intent"
INTENT_TO_SURFACE = "intent_to_surface"
SUBSTRATE_TO_INTENT = "substrate_to_intent"
INTENT_TO_SUBSTRATE = "intent_to_substrate"
@dataclass
class Interaction:
"""Interaction event between layers"""
interaction_id: str
interaction_type: InteractionType
source_layer: str
target_layer: str
timestamp: datetime
context: Dict[str, str] # Additional context (e.g., operation type, data size)
def __repr__(self) -> str:
return f"Interaction({self.interaction_type.value}, {self.source_layer}{self.target_layer})"
@dataclass
class Metatype:
"""Emergent type from integration of layers"""
metatype_id: str
type_signature: str # Type signature derived from interactions
confidence: float # Confidence in type inference
source_interactions: List[str] # Interaction IDs that contributed to this type
suggestions: List[str] # Type suggestions based on neighborhood
created_at: datetime = field(default_factory=datetime.now)
def __repr__(self) -> str:
return f"Metatype({self.type_signature}, confidence={self.confidence:.3f})"
class SelfTypingEngine:
"""
Self-Typing Engine Metatype Generation
System derives type signature from interaction between layers
"""
def __init__(self):
self.interactions: List[Interaction] = []
self.interaction_counter = 0
self.metatypes: Dict[str, Metatype] = {}
self.metatype_counter = 0
self.interaction_patterns: Dict[str, int] = defaultdict(int)
self.type_cache: Dict[str, Metatype] = {}
def record_interaction(
self,
interaction_type: InteractionType,
source_layer: str,
target_layer: str,
context: Optional[Dict[str, str]] = None
) -> Interaction:
"""
Record interaction between layers
Args:
interaction_type: Type of interaction
source_layer: Source layer name
target_layer: Target layer name
context: Additional context
Returns:
Interaction record
"""
self.interaction_counter += 1
interaction_id = f"interaction_{self.interaction_counter}"
if context is None:
context = {}
interaction = Interaction(
interaction_id=interaction_id,
interaction_type=interaction_type,
source_layer=source_layer,
target_layer=target_layer,
timestamp=datetime.now(),
context=context
)
self.interactions.append(interaction)
# Update interaction patterns
pattern_key = f"{interaction_type.value}:{source_layer}:{target_layer}"
self.interaction_patterns[pattern_key] += 1
# Invalidate cache
self.type_cache.clear()
return interaction
def infer_metatype(self, context: Dict[str, str]) -> Metatype:
"""
Infer metatype from interaction patterns
Args:
context: Current context (layer names, operation types, etc.)
Returns:
Inferred metatype
"""
# Check cache first
cache_key = self._context_to_key(context)
if cache_key in self.type_cache:
return self.type_cache[cache_key]
# Extract interaction pattern
pattern = self._extract_pattern(context)
# Generate type signature
type_signature = self._generate_type_signature(pattern)
# Compute confidence based on pattern frequency
confidence = self._compute_confidence(pattern)
# Get type suggestions from neighborhood
suggestions = self._get_type_suggestions(pattern)
# Create metatype
self.metatype_counter += 1
metatype_id = f"metatype_{self.metatype_counter}"
metatype = Metatype(
metatype_id=metatype_id,
type_signature=type_signature,
confidence=confidence,
source_interactions=[i.interaction_id for i in self.interactions[-10:]], # Last 10 interactions
suggestions=suggestions
)
self.metatypes[metatype_id] = metatype
self.type_cache[cache_key] = metatype
return metatype
def _context_to_key(self, context: Dict[str, str]) -> str:
"""Convert context to cache key"""
items = sorted(context.items())
return "|".join(f"{k}:{v}" for k, v in items)
def _extract_pattern(self, context: Dict[str, str]) -> Dict[str, str]:
"""Extract interaction pattern from context"""
pattern = {}
# Extract relevant context fields
for key in ["operation", "data_type", "layer", "action"]:
if key in context:
pattern[key] = context[key]
return pattern
def _generate_type_signature(self, pattern: Dict[str, str]) -> str:
"""
Generate type signature from pattern
Args:
pattern: Interaction pattern
Returns:
Type signature string
"""
# Build signature from pattern components
components = []
if "operation" in pattern:
components.append(f"op:{pattern['operation']}")
if "data_type" in pattern:
components.append(f"dt:{pattern['data_type']}")
if "layer" in pattern:
components.append(f"lyr:{pattern['layer']}")
if "action" in pattern:
components.append(f"act:{pattern['action']}")
# If no components, use default
if not components:
components = ["generic"]
signature = ":".join(components)
return signature
def _compute_confidence(self, pattern: Dict[str, str]) -> float:
"""
Compute confidence based on pattern frequency
Args:
pattern: Interaction pattern
Returns:
Confidence score (0-1)
"""
# Build pattern key
pattern_key = self._pattern_to_key(pattern)
# Get pattern frequency
frequency = self.interaction_patterns.get(pattern_key, 0)
# Normalize to confidence (max frequency = 100)
confidence = min(frequency / 100.0, 1.0)
return confidence
def _pattern_to_key(self, pattern: Dict[str, str]) -> str:
"""Convert pattern to key for frequency lookup"""
items = sorted(pattern.items())
return "|".join(f"{k}:{v}" for k, v in items)
def _get_type_suggestions(self, pattern: Dict[str, str]) -> List[str]:
"""
Get type suggestions based on manifold neighborhood
Args:
pattern: Interaction pattern
Returns:
List of type suggestions
"""
suggestions = []
# Find similar patterns in interaction history
pattern_key = self._pattern_to_key(pattern)
# Look for patterns with similar keys
for key in self.interaction_patterns:
similarity = self._pattern_similarity(pattern_key, key)
if similarity > 0.5: # Similarity threshold
suggestions.append(key)
return suggestions[:5] # Top 5 suggestions
def _pattern_similarity(self, key1: str, key2: str) -> float:
"""
Compute similarity between pattern keys
Args:
key1: First pattern key
key2: Second pattern key
Returns:
Similarity score (0-1)
"""
# Split keys into components
components1 = set(key1.split("|"))
components2 = set(key2.split("|"))
# Jaccard similarity
intersection = components1 & components2
union = components1 | components2
if not union:
return 0.0
return len(intersection) / len(union)
def get_interaction_history(self, limit: int = 100) -> List[Interaction]:
"""Get recent interaction history"""
return self.interactions[-limit:]
def get_type_statistics(self) -> Dict:
"""Get statistics about type inference"""
return {
"total_interactions": len(self.interactions),
"unique_patterns": len(self.interaction_patterns),
"total_metatypes": len(self.metatypes),
"cache_size": len(self.type_cache)
}
def main():
"""Test self-typing engine with sample interactions"""
engine = SelfTypingEngine()
# Record some interactions
interaction1 = engine.record_interaction(
InteractionType.SUBSTRATE_TO_SURFACE,
"ENE",
"Notion",
{"operation": "read", "data_type": "concept_vector"}
)
print(f"Recorded interaction: {interaction1}")
interaction2 = engine.record_interaction(
InteractionType.SURFACE_TO_INTENT,
"Notion",
"Linear",
{"operation": "update", "data_type": "task"}
)
print(f"Recorded interaction: {interaction2}")
# Infer metatype from context
context = {"operation": "read", "data_type": "concept_vector", "layer": "ENE"}
metatype = engine.infer_metatype(context)
print(f"Inferred metatype: {metatype}")
# Get statistics
stats = engine.get_type_statistics()
print(f"Type statistics: {stats}")
if __name__ == "__main__":
main()

View file

@ -1,422 +0,0 @@
#!/usr/bin/env python3
"""
Soliton Search Engine Wave Propagation with AVMR O(N) Indexing
First-Principles Derivation: Search is soliton propagation along path of least resistance
Performance Targets:
- O(N) search complexity (AVMR shell indexing)
- < 200ms query response time
- < 50ms attractor convergence
"""
import numpy as np
from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass
from enum import Enum
import math
@dataclass
class FrustrationWave:
"""Frustration wave parameters for soliton propagation"""
wave_vector: np.ndarray # k_r wave vector (14D)
weight: float # w_r weight from anisotropy
def __repr__(self) -> str:
return f"FrustrationWave(weight={self.weight:.3f})"
@dataclass
class Attractor:
"""Local energy minimum (attractor in manifold)"""
coordinate: np.ndarray # 14D coordinate
energy: float # Energy at this point
archive_ids: List[str] # Points that converge to this attractor
confidence: float # Attraction strength
def __repr__(self) -> str:
return f"Attractor(energy={self.energy:.3f}, points={len(self.archive_ids)})"
@dataclass
class Trajectory:
"""Soliton propagation trajectory"""
points: List[np.ndarray] # Path points
energies: List[float] # Energy at each point
converged: bool # Whether trajectory converged to attractor
final_attractor: Optional[Attractor] # Final attractor if converged
def __repr__(self) -> str:
return f"Trajectory(steps={len(self.points)}, converged={self.converged})"
class AVMRShellIndex:
"""
AVMR Shell Indexing for O(N) search
Shell decomposition: n = + a, 0 a < 2k + 1
Shell state: (k, a, b) where b = (k+1)² - n
Tip coordinates: Tip(n) = (ab, a-b)
"""
def __init__(self, vectors: List[np.ndarray], archive_ids: List[str]):
"""
Build AVMR shell index from concept vectors
Args:
vectors: List of 14D concept vectors
archive_ids: List of archive IDs
"""
self.vectors = np.array(vectors)
self.archive_ids = archive_ids
self.n_vectors = len(vectors)
# Compute magnitudes for shell decomposition
self.magnitudes = np.linalg.norm(self.vectors, axis=1)
# Build shell index
self.shell_index = self._build_shell_index()
# Build axial generators for shell indexing
self.axial_generators = self._build_axial_generators()
def _build_shell_index(self) -> Dict[Tuple[int, int, int], List[int]]:
"""Build shell index: (k, a, b) → vector indices"""
shell_index = {}
for i, magnitude in enumerate(self.magnitudes):
n = int(magnitude * 1000) # Scale factor
k = int(math.sqrt(n))
a = n - k * k
b = (k + 1) * (k + 1) - n
shell_key = (k, a, b)
if shell_key not in shell_index:
shell_index[shell_key] = []
shell_index[shell_key].append(i)
return shell_index
def _build_axial_generators(self) -> np.ndarray:
"""Build axial generators for shell indexing"""
# Use principal components as axial generators
centered = self.vectors - np.mean(self.vectors, axis=0)
cov = np.cov(centered.T)
eigenvalues, eigenvectors = np.linalg.eigh(cov)
# Sort by eigenvalues (descending)
idx = np.argsort(eigenvalues)[::-1]
axial_generators = eigenvectors[:, idx[:2]] # Top 2 axes
return axial_generators
def query_shell(self, k: int, a: int, b: int) -> List[Tuple[int, str]]:
"""
Query vectors in specific shell
Args:
k: Shell level
a: Shell offset
b: Shell complement
Returns:
List of (index, archive_id) tuples
"""
shell_key = (k, a, b)
if shell_key not in self.shell_index:
return []
indices = self.shell_index[shell_key]
return [(i, self.archive_ids[i]) for i in indices]
def query_neighborhood(self, query_vector: np.ndarray, radius: float) -> List[Tuple[int, str]]:
"""
Query neighborhood using shell indexing (O(N) complexity)
Args:
query_vector: Query vector (14D)
radius: Query radius
Returns:
List of (index, archive_id) tuples within radius
"""
# Compute query magnitude
query_magnitude = np.linalg.norm(query_vector)
query_n = int(query_magnitude * 1000)
query_k = int(math.sqrt(query_n))
# Search nearby shells (within radius in shell space)
results = []
shell_radius = int(radius * 1000)
for dk in range(-shell_radius, shell_radius + 1):
k = query_k + dk
if k < 0:
continue
# Search all possible (a, b) combinations for this shell
for a in range(2 * k + 1):
n = k * k + a
b = (k + 1) * (k + 1) - n
shell_results = self.query_shell(k, a, b)
results.extend(shell_results)
# Filter by actual Euclidean distance
filtered_results = []
for i, archive_id in results:
distance = np.linalg.norm(self.vectors[i] - query_vector)
if distance <= radius:
filtered_results.append((i, archive_id))
return filtered_results
class SolitonSearchEngine:
"""
Soliton Search Engine with AVMR O(N) indexing
Search via wave propagation along path of least resistance
"""
def __init__(self, vectors: List[np.ndarray], archive_ids: List[str]):
"""
Initialize soliton search engine
Args:
vectors: List of 14D concept vectors
archive_ids: List of archive IDs
"""
self.vectors = np.array(vectors)
self.archive_ids = archive_ids
# Build AVMR shell index
self.avmr_index = AVMRShellIndex(vectors, archive_ids)
# Initialize frustration waves (default: uniform weights)
self.waves = self._initialize_waves()
def _initialize_waves(self) -> List[FrustrationWave]:
"""Initialize frustration waves with default parameters"""
waves = []
# Create waves along principal axes
for i in range(14):
wave_vector = np.zeros(14)
wave_vector[i] = 1.0
waves.append(FrustrationWave(wave_vector=wave_vector, weight=0.1))
return waves
def _cosine_approx(self, x: float) -> float:
"""Cosine approximation using Taylor series"""
x2 = x * x
return 1.0 - x2 * 0.5
def _compute_frustration(self, z: np.ndarray, waves: List[FrustrationWave]) -> float:
"""
Compute frustration W(z;A) = Σ_r w_r(A)(1 - cos(k_r·z))
Args:
z: Lock pattern (14D vector)
waves: List of frustration waves
Returns:
Frustration value
"""
frustration = 0.0
for wave in waves:
dot_product = np.dot(wave.wave_vector, z)
cosine = self._cosine_approx(dot_product)
contribution = wave.weight * (1.0 - cosine)
frustration += contribution
return frustration
def propagate_soliton(
self,
query_vector: np.ndarray,
max_steps: int = 100,
convergence_threshold: float = 0.001,
learning_rate: float = 0.1
) -> Trajectory:
"""
Propagate soliton from query vector to attractor
Args:
query_vector: Initial perturbation (query)
max_steps: Maximum propagation steps
convergence_threshold: Energy change threshold for convergence
learning_rate: Step size for gradient descent
Returns:
Trajectory of soliton propagation
"""
trajectory_points = [query_vector.copy()]
trajectory_energies = [self._compute_frustration(query_vector, self.waves)]
current_z = query_vector.copy()
for step in range(max_steps):
# Compute gradient of frustration
gradient = self._compute_gradient(current_z, self.waves)
# Gradient descent (move toward lower energy)
new_z = current_z - learning_rate * gradient
# Compute energy
energy = self._compute_frustration(new_z, self.waves)
# Check convergence
energy_change = abs(energy - trajectory_energies[-1])
if energy_change < convergence_threshold:
trajectory_points.append(new_z)
trajectory_energies.append(energy)
# Find attractor
attractor = self._find_attractor(new_z)
return Trajectory(
points=trajectory_points,
energies=trajectory_energies,
converged=True,
final_attractor=attractor
)
trajectory_points.append(new_z)
trajectory_energies.append(energy)
current_z = new_z
# Did not converge
return Trajectory(
points=trajectory_points,
energies=trajectory_energies,
converged=False,
final_attractor=None
)
def _compute_gradient(self, z: np.ndarray, waves: List[FrustrationWave]) -> np.ndarray:
"""
Compute gradient of frustration with respect to z
Args:
z: Current position
waves: Frustration waves
Returns:
Gradient vector (14D)
"""
gradient = np.zeros_like(z)
for wave in waves:
dot_product = np.dot(wave.wave_vector, z)
# Derivative of (1 - cos(k·z)) is sin(k·z) * k
# Approximate sin(x) ≈ x for small x
sine_approx = dot_product
gradient += wave.weight * sine_approx * wave.wave_vector
return gradient
def _find_attractor(self, coordinate: np.ndarray) -> Optional[Attractor]:
"""
Find attractor near coordinate using AVMR shell indexing
Args:
coordinate: 14D coordinate
Returns:
Attractor if found, None otherwise
"""
# Query neighborhood using AVMR shell indexing
neighbors = self.avmr_index.query_neighborhood(coordinate, radius=0.5)
if not neighbors:
return None
# Compute energy at neighbor points
energies = []
for i, _ in neighbors:
energy = self._compute_frustration(self.vectors[i], self.waves)
energies.append((i, energy))
# Find minimum energy (attractor)
min_idx, min_energy = min(energies, key=lambda x: x[1])
# Get all points near this attractor
attractor_coordinate = self.vectors[min_idx]
attractor_neighbors = self.avmr_index.query_neighborhood(attractor_coordinate, radius=0.3)
archive_ids = [aid for _, aid in attractor_neighbors]
# Compute confidence based on energy difference
confidence = 1.0 / (1.0 + min_energy)
return Attractor(
coordinate=attractor_coordinate,
energy=min_energy,
archive_ids=archive_ids,
confidence=confidence
)
def search(
self,
query_vector: np.ndarray,
max_results: int = 10
) -> List[Tuple[str, float]]:
"""
Search using soliton propagation with branch prediction
Args:
query_vector: Query vector (14D)
max_results: Maximum number of results to return
Returns:
List of (archive_id, confidence) tuples
"""
# Propagate soliton
trajectory = self.propagate_soliton(query_vector)
if not trajectory.converged or trajectory.final_attractor is None:
# Fallback to direct AVMR search
neighbors = self.avmr_index.query_neighborhood(query_vector, radius=1.0)
return [(aid, 0.5) for _, aid in neighbors[:max_results]]
# Return attractor results
results = []
for archive_id in trajectory.final_attractor.archive_ids[:max_results]:
results.append((archive_id, trajectory.final_attractor.confidence))
return results
def main():
"""Test soliton search engine with sample data"""
# Generate sample 14D vectors
np.random.seed(42)
n_samples = 100
sample_vectors = np.random.randn(n_samples, 14)
archive_ids = [f"sample_{i}" for i in range(n_samples)]
# Create soliton search engine
engine = SolitonSearchEngine(sample_vectors, archive_ids)
# Test search
query_vector = np.random.randn(14)
results = engine.search(query_vector, max_results=5)
print(f"Search results for random query:")
for archive_id, confidence in results:
print(f" {archive_id}: confidence={confidence:.3f}")
# Test soliton propagation
trajectory = engine.propagate_soliton(query_vector)
print(f"\nTrajectory: {trajectory}")
print(f"Converged: {trajectory.converged}")
if trajectory.converged:
print(f"Final attractor: {trajectory.final_attractor}")
if __name__ == "__main__":
main()

View file

@ -1,590 +0,0 @@
#!/usr/bin/env python3
"""
Substrate Bridge ENE/Linear Integration
First-Principles Derivation: Self-typing loop: Substrate (ENE) Surface Intent (Linear) Metatype
Performance Targets:
- < 50ms single read operation
- < 100ms batch read (100 items)
- < 200ms batch write (100 items)
- Connection pooling (reuse database connections)
- Batch read/write (reduce round trips)
- Cached hot data (frequently accessed coordinates)
- Async I/O (non-blocking operations)
"""
import sqlite3
import json
import numpy as np
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime
import hashlib
@dataclass
class ConceptVector14:
"""14-dimensional concept vector from ENE database"""
vector: np.ndarray # Shape: (14,)
archive_id: str
def __post_init__(self):
if self.vector.shape != (14,):
raise ValueError(f"ConceptVector14 must have shape (14,), got {self.vector.shape}")
def to_dict(self) -> dict:
return {
"archive_id": self.archive_id,
"vector": self.vector.tolist()
}
@dataclass
class AVMRState:
"""AVMR shell state for O(√N) indexing"""
shell_level: int # k in n = k² + a
shell_offset: int # a in n = k² + a
shell_complement: int # b = (k+1)² - n
spectral_bins: np.ndarray # Q16_16 spectral bins
def to_dict(self) -> dict:
return {
"shell_level": self.shell_level,
"shell_offset": self.shell_offset,
"shell_complement": self.shell_complement,
"spectral_bins": self.spectral_bins.tolist()
}
@dataclass
class BracketedBounds:
"""Bracketed bounds for interval semantics"""
lower_bound: float
upper_bound: float
resolution: str # SEED, FORMING, STABLE, CRYSTALLIZED, COMPRESSED
def to_dict(self) -> dict:
return {
"lower_bound": self.lower_bound,
"upper_bound": self.upper_bound,
"resolution": self.resolution
}
@dataclass
class WitnessReceipt:
"""Witness receipt for provenance"""
receipt_hash: str
archive_id: str
timestamp: datetime
operation: str # "read", "write", "collapse"
def to_dict(self) -> dict:
return {
"receipt_hash": self.receipt_hash,
"archive_id": self.archive_id,
"timestamp": self.timestamp.isoformat(),
"operation": self.operation
}
class SubstrateBridge:
"""
Substrate Bridge ENE/Linear Integration
Bridge between manifold surface and ENE substrate
"""
def __init__(self, db_path: str):
"""
Initialize substrate bridge
Args:
db_path: Path to ENE database
"""
self.db_path = db_path
self.connection_pool = []
self.cache = {} # Hot data cache
self.witnesses = []
def _get_connection(self) -> sqlite3.Connection:
"""Get connection from pool or create new one"""
if self.connection_pool:
return self.connection_pool.pop()
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
def _return_connection(self, conn: sqlite3.Connection) -> None:
"""Return connection to pool"""
if len(self.connection_pool) < 10: # Pool size limit
self.connection_pool.append(conn)
else:
conn.close()
def read_coordinate(self, archive_id: str) -> Optional[ConceptVector14]:
"""
Read concept vector from ENE database
Args:
archive_id: Archive ID to read
Returns:
Concept vector if found, None otherwise
"""
# Check cache first
if archive_id in self.cache:
return self.cache[archive_id]
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute("""
SELECT archive_id, concept_vector_14
FROM packages
WHERE archive_id = ?
""", (archive_id,))
row = cursor.fetchone()
if row is None:
return None
vector_str = row['concept_vector_14']
if vector_str is None:
return None
# Parse vector
vector_data = json.loads(vector_str)
vector = np.array(vector_data, dtype=np.float32)
if len(vector) != 14:
return None
concept_vector = ConceptVector14(vector=vector, archive_id=archive_id)
# Cache result
self.cache[archive_id] = concept_vector
# Record witness
witness = WitnessReceipt(
receipt_hash=hashlib.sha256(archive_id.encode()).hexdigest(),
archive_id=archive_id,
timestamp=datetime.now(),
operation="read"
)
self.witnesses.append(witness)
return concept_vector
except Exception as e:
print(f"Error reading coordinate: {e}")
return None
finally:
self._return_connection(conn)
def read_avmr(self, archive_id: str) -> Optional[AVMRState]:
"""
Read AVMR shell state from ENE database
Args:
archive_id: Archive ID to read
Returns:
AVMR state if found, None otherwise
"""
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute("""
SELECT avmr_shell_state
FROM packages
WHERE archive_id = ?
""", (archive_id,))
row = cursor.fetchone()
if row is None:
return None
avmr_str = row['avmr_shell_state']
if avmr_str is None:
return None
# Parse AVMR state
avmr_data = json.loads(avmr_str)
return AVMRState(
shell_level=avmr_data.get('shell_level', 0),
shell_offset=avmr_data.get('shell_offset', 0),
shell_complement=avmr_data.get('shell_complement', 0),
spectral_bins=np.array(avmr_data.get('spectral_bins', []), dtype=np.float32)
)
except Exception as e:
print(f"Error reading AVMR state: {e}")
return None
finally:
self._return_connection(conn)
def read_bracketed(self, archive_id: str) -> Optional[BracketedBounds]:
"""
Read bracketed bounds from ENE database
Args:
archive_id: Archive ID to read
Returns:
Bracketed bounds if found, None otherwise
"""
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute("""
SELECT bracketed_bounds
FROM packages
WHERE archive_id = ?
""", (archive_id,))
row = cursor.fetchone()
if row is None:
return None
bounds_str = row['bracketed_bounds']
if bounds_str is None:
return None
# Parse bracketed bounds
bounds_data = json.loads(bounds_str)
return BracketedBounds(
lower_bound=bounds_data.get('lower_bound', 0.0),
upper_bound=bounds_data.get('upper_bound', 1.0),
resolution=bounds_data.get('resolution', 'STABLE')
)
except Exception as e:
print(f"Error reading bracketed bounds: {e}")
return None
finally:
self._return_connection(conn)
def read_witness(self, archive_id: str) -> Optional[WitnessReceipt]:
"""
Read witness receipt from ENE database
Args:
archive_id: Archive ID to read
Returns:
Witness receipt if found, None otherwise
"""
# Check local witnesses first
for witness in reversed(self.witnesses):
if witness.archive_id == archive_id:
return witness
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute("""
SELECT receipt_hash, archive_id, timestamp, operation
FROM witnesses
WHERE archive_id = ?
ORDER BY timestamp DESC
LIMIT 1
""", (archive_id,))
row = cursor.fetchone()
if row is None:
return None
return WitnessReceipt(
receipt_hash=row['receipt_hash'],
archive_id=row['archive_id'],
timestamp=datetime.fromisoformat(row['timestamp']),
operation=row['operation']
)
except Exception as e:
print(f"Error reading witness: {e}")
return None
finally:
self._return_connection(conn)
def batch_read(self, archive_ids: List[str]) -> Dict[str, ConceptVector14]:
"""
Batch read concept vectors from ENE database
Args:
archive_ids: List of archive IDs to read
Returns:
Dictionary mapping archive IDs to concept vectors
"""
results = {}
conn = self._get_connection()
try:
cursor = conn.cursor()
# Build placeholder string for SQL IN clause
placeholders = ','.join('?' * len(archive_ids))
cursor.execute(f"""
SELECT archive_id, concept_vector_14
FROM packages
WHERE archive_id IN ({placeholders})
""", archive_ids)
for row in cursor.fetchall():
archive_id = row['archive_id']
vector_str = row['concept_vector_14']
if vector_str is None:
continue
# Parse vector
vector_data = json.loads(vector_str)
vector = np.array(vector_data, dtype=np.float32)
if len(vector) != 14:
continue
concept_vector = ConceptVector14(vector=vector, archive_id=archive_id)
results[archive_id] = concept_vector
# Cache result
self.cache[archive_id] = concept_vector
return results
except Exception as e:
print(f"Error in batch read: {e}")
return {}
finally:
self._return_connection(conn)
def write_coordinate(self, archive_id: str, vector: ConceptVector14) -> bool:
"""
Write concept vector to ENE database
Args:
archive_id: Archive ID to write
vector: Concept vector to write
Returns:
True if successful, False otherwise
"""
conn = self._get_connection()
try:
cursor = conn.cursor()
# Serialize vector
vector_str = json.dumps(vector.vector.tolist())
cursor.execute("""
UPDATE packages
SET concept_vector_14 = ?
WHERE archive_id = ?
""", (vector_str, archive_id))
conn.commit()
# Update cache
self.cache[archive_id] = vector
# Record witness
witness = WitnessReceipt(
receipt_hash=hashlib.sha256((archive_id + vector_str).encode()).hexdigest(),
archive_id=archive_id,
timestamp=datetime.now(),
operation="write"
)
self.witnesses.append(witness)
return True
except Exception as e:
print(f"Error writing coordinate: {e}")
conn.rollback()
return False
finally:
self._return_connection(conn)
def write_witness(self, archive_id: str, witness: WitnessReceipt) -> bool:
"""
Write witness receipt to ENE database
Args:
archive_id: Archive ID
witness: Witness receipt to write
Returns:
True if successful, False otherwise
"""
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO witnesses (receipt_hash, archive_id, timestamp, operation)
VALUES (?, ?, ?, ?)
""", (witness.receipt_hash, archive_id, witness.timestamp.isoformat(), witness.operation))
conn.commit()
return True
except Exception as e:
print(f"Error writing witness: {e}")
conn.rollback()
return False
finally:
self._return_connection(conn)
def batch_write(self, updates: Dict[str, ConceptVector14]) -> bool:
"""
Batch write concept vectors to ENE database
Args:
updates: Dictionary mapping archive IDs to concept vectors
Returns:
True if all successful, False otherwise
"""
conn = self._get_connection()
try:
cursor = conn.cursor()
for archive_id, vector in updates.items():
# Serialize vector
vector_str = json.dumps(vector.vector.tolist())
cursor.execute("""
UPDATE packages
SET concept_vector_14 = ?
WHERE archive_id = ?
""", (vector_str, archive_id))
# Update cache
self.cache[archive_id] = vector
conn.commit()
return True
except Exception as e:
print(f"Error in batch write: {e}")
conn.rollback()
return False
finally:
self._return_connection(conn)
def sync_with_linear(self, linear_issue_id: str, archive_id: str) -> bool:
"""
Sync with Linear intent tracking
Args:
linear_issue_id: Linear issue ID
archive_id: Archive ID to sync
Returns:
True if successful, False otherwise
"""
# This would integrate with Linear API
# For now, just record the sync intent
print(f"Syncing Linear issue {linear_issue_id} with archive {archive_id}")
return True
def extract_intent(self, linear_issue: Dict) -> np.ndarray:
"""
Extract intent vector from Linear issue
Args:
linear_issue: Linear issue data
Returns:
Intent vector (14D)
"""
# Extract intent from issue fields
# This is a simplified implementation
intent_vector = np.zeros(14, dtype=np.float32)
# Map issue fields to intent dimensions
if 'title' in linear_issue:
intent_vector[0] = len(linear_issue['title']) / 100.0
if 'description' in linear_issue:
intent_vector[1] = len(lineframe_issue['description']) / 1000.0
if 'priority' in linear_issue:
priority_map = {'urgent': 1.0, 'high': 0.75, 'medium': 0.5, 'low': 0.25}
intent_vector[2] = priority_map.get(lineframe_issue['priority'], 0.5)
return intent_vector
def intent_to_coordinate(self, intent_vector: np.ndarray) -> ConceptVector14:
"""
Convert intent vector to concept vector coordinate
Args:
intent_vector: Intent vector (14D)
Returns:
Concept vector
"""
# Simple transformation (in full implementation, use more sophisticated mapping)
return ConceptVector14(
vector=intent_vector,
archive_id=f"intent_{hashlib.sha256(intent_vector.tobytes()).hexdigest()[:16]}"
)
def generate_metatype(self, surface_data: Dict, substrate_data: Dict, intent_data: Dict) -> Dict:
"""
Generate metatype from integration of layers
Args:
surface_data: Data from surface layer
substrate_data: Data from substrate layer
intent_data: Data from intent layer
Returns:
Metatype dictionary
"""
# Simple metatype generation (in full implementation, use self-typing engine)
return {
"type_signature": f"{substrate_data.get('type', 'unknown')}:{surface_data.get('type', 'unknown')}:{intent_data.get('type', 'unknown')}",
"confidence": 0.5,
"source_layers": ["substrate", "surface", "intent"]
}
def get_cache_stats(self) -> Dict:
"""Get cache statistics"""
return {
"cache_size": len(self.cache),
"witness_count": len(self.witnesses),
"connection_pool_size": len(self.connection_pool)
}
def main():
"""Test substrate bridge with ENE database"""
db_path = "/home/allaun/Documents/Research Stack/data/substrate_index.db"
try:
bridge = SubstrateBridge(db_path)
# Test read
vector = bridge.read_coordinate("test_archive_id")
print(f"Read vector: {vector}")
# Test batch read
results = bridge.batch_read(["test_archive_id_1", "test_archive_id_2"])
print(f"Batch read: {len(results)} results")
# Get cache stats
stats = bridge.get_cache_stats()
print(f"Cache stats: {stats}")
except Exception as e:
print(f"Error testing substrate bridge: {e}")
if __name__ == "__main__":
main()

View file

@ -1,84 +0,0 @@
import numpy as np
import math
# Constants in Q16.16
Q_ONE = 0x00010000
DRAKE_CONST = 196
DRIFT_CONST = 65
LAMBDA = Q_ONE
M_STAR = 32768
def get_ne_log(bin_val):
n = bin_val + 1
return int(math.log2(n) * Q_ONE)
def is_locally_lawful(mu_bin, rho_bin, c_bin, m_bin, ne_bin, sig_bin):
uq = 0x41 * (mu_bin + 1)
rhoq = 0x2000 * (rho_bin + 1)
cfac = 0x2000 * (c_bin + 1)
mfac = 0x2000 * (m_bin + 1)
sigq = Q_ONE + (0x4000 * (sig_bin + 1))
Ne = get_ne_log(ne_bin)
l1 = uq <= (DRAKE_CONST * Q_ONE) // cfac
phi = Q_ONE - abs(mfac - M_STAR)
drift_val = ((rhoq * Ne) // Q_ONE * phi) // Q_ONE
l2 = drift_val >= DRIFT_CONST
l3 = sigq > Q_ONE + (LAMBDA * uq) // Q_ONE
mask = (1 if not l1 else 0) | (2 if not l2 else 0) | (4 if not l3 else 0)
return (l1 and l2 and l3), mask
def solve_trajectory(mu_bin, rho_bin, c_bin, m_bin, ne_bin, sig_bin):
curr = (mu_bin, rho_bin, c_bin, m_bin, ne_bin, sig_bin)
states = [curr]
lawful_trajectory = True
for s in range(3):
m, r, c, mo, n, s_val = states[-1]
ok, _ = is_locally_lawful(m, r, c, mo, n, s_val)
if not ok:
lawful_trajectory = False
# Beta Function: Coarse-graining
new_mu = max(0, m - 1)
new_ne = min(7, n + 1)
states.append((new_mu, r, c, mo, new_ne, s_val))
return lawful_trajectory, states[-1]
def generate_lut():
dt = np.dtype([
('bits', 'u1'), # L_now|L_flow|L_att|Noise|Sab
('failure', 'u1'), # Mask: 1|2|4|8
('cost', 'u4'),
('margin', 'u1'),
('depth', 'u1'),
('attr_id', 'u1'),
('p1', 'u1'), ('p2', 'u1'), ('p3', 'u1'), ('p4', 'u1'), ('p5', 'u1'), ('p6', 'u1'), ('p7', 'u1')
])
lut = np.zeros(262144, dtype=dt)
for addr in range(262144):
mu_bin = (addr >> 0) & 0x7
rho_bin = (addr >> 3) & 0x7
c_bin = (addr >> 6) & 0x7
m_bin = (addr >> 9) & 0x7
ne_bin = (addr >> 12) & 0x7
sig_bin = (addr >> 15) & 0x7
l_now, mask = is_locally_lawful(mu_bin, rho_bin, c_bin, m_bin, ne_bin, sig_bin)
l_flow, attractor = solve_trajectory(mu_bin, rho_bin, c_bin, m_bin, ne_bin, sig_bin)
bits = (1 if l_now else 0) | (2 if l_flow else 0)
final_mask = mask | (8 if l_now and not l_flow else 0)
lut[addr] = (bits, final_mask, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0)
return lut
if __name__ == "__main__":
lut_data = generate_lut()
output_path = "/home/allaun/Documents/Research Stack/data/swarm/adaptation_surface.bin"
lut_data.tofile(output_path)
print(f"SUCCESS: Precomputed RGFlow Trajectories (16-byte Master Entry)")

View file

@ -1,73 +0,0 @@
#!/usr/bin/env python3
"""
Public Domain Ingestion Orchestrator
Target: /mnt/gdrive/Research_Archive/
Orchestrates high-volume ingestion from Openverse, IA, and Europeana.
"""
import os
import requests
import json
from pathlib import Path
# --- Configuration ---
GDRIVE_PATH = Path("/home/allaun/Documents/Research Stack/data/ingestion")
GDRIVE_PATH.mkdir(parents=True, exist_ok=True)
# Placeholder for API credentials (user can fill later)
CREDS = {
"openverse": os.getenv("OPENVERSE_API_KEY", ""),
"ia": os.getenv("INTERNET_ARCHIVE_S3_KEY", ""),
"europeana": os.getenv("EUROPEANA_API_KEY", "")
}
def pull_openverse(query, limit=100):
"""Pull metadata and links for open domain works."""
print(f"[INGEST] Searching Openverse for: {query}")
url = "https://api.openverse.engineering/v1/images/"
params = {"q": query, "license_type": "publicdomain", "page_size": limit}
headers = {"Authorization": f"Token {CREDS['openverse']}"} if CREDS['openverse'] else {}
res = requests.get(url, params=params, headers=headers)
if res.status_code == 200:
data = res.json()
out_file = GDRIVE_PATH / f"openverse_{query.replace(' ', '_')}.json"
with open(out_file, "w") as f:
json.dump(data, f, indent=2)
print(f"[SUCCESS] Saved {len(data['results'])} records to {out_file}")
else:
print(f"[ERROR] Openverse failed: {res.status_code}")
def pull_ia_metadata(query, limit=50):
"""Pull metadata from Internet Archive based on query."""
print(f"[INGEST] Searching Internet Archive for: {query}")
url = "https://archive.org/advancedsearch.php"
params = {
"q": f"{query} AND (licenseurl:*publicdomain* OR mediaprotocol:publicdomain)",
"output": "json",
"rows": limit,
"page": 1
}
res = requests.get(url, params=params)
if res.status_code == 200:
data = res.json()
out_file = GDRIVE_PATH / f"ia_{query.replace(' ', '_')}.json"
with open(out_file, "w") as f:
json.dump(data, f, indent=2)
print(f"[SUCCESS] Saved IA records to {out_file}")
else:
print(f"[ERROR] IA failed: {res.status_code}")
if __name__ == "__main__":
# Initial seeds for the Informatic Manifold
seeds = [
"theoretical physics",
"neuromorphic computing",
"pure mathematics",
"maritime navigation",
"sovereignty"
]
for s in seeds:
pull_openverse(s)
pull_ia_metadata(s)

View file

@ -1,95 +0,0 @@
import numpy as np
import json
import os
import math
from pathlib import Path
# --- Configuration ---
LUT_PATH = "/home/allaun/Documents/Research Stack/data/swarm/adaptation_surface.bin"
INGEST_DIR = Path("/home/allaun/Documents/Research Stack/data/ingestion")
HARDENED_DIR = INGEST_DIR / "hardened"
LUT_DT = np.dtype([('lawful','u1'), ('dk','u1'), ('df','u1'), ('er','u1'), ('cost','u4')])
# Load LUT
try:
ADAPTATION_SURFACE = np.fromfile(LUT_PATH, dtype=LUT_DT)
except Exception as e:
print(f"Error loading LUT: {e}")
exit(1)
def map_to_genome(item):
"""
Maps IA metadata to 18-bit address (6 dims * 3 bits).
Normalized to 0-7 range per dimension.
"""
# 1. uBin (Mutation/Entropy): Inverse of metadata completeness
# Max possible fields ~20. Map (20-count) to 0-7.
metadata_count = len(item.keys())
uBin = max(0, min(7, (20 - metadata_count) // 2))
# 2. neBin (Population): Log of downloads
downloads = int(item.get('downloads', 0))
# Log2 mapping: 0-7 range for 1 to 100k downloads
neBin = max(0, min(7, int(math.log2(downloads + 1) / 2)))
# 3. sigBin (Fitness): Relevancy or index
# We'll use the 'week' (trending) or just default to 4 for research seeds
sigBin = 4
# 4. cBin (Connectance): Related identifiers count
# IA often has 'identifier-access' or similar.
# Use length of 'identifier' as dummy connectance.
cBin = max(0, min(7, len(item.get('identifier', '')) // 5))
# 5. mBin (Modularity): Format count
# Placeholder for multi-format records
mBin = 3
# 6. xBin (Reserved/Noise)
xBin = 0
# Pack 18-bit address
addr = (uBin & 0x7) | ((neBin & 0x7) << 3) | ((sigBin & 0x7) << 6) | \
((cBin & 0x7) << 9) | ((mBin & 0x7) << 12) | ((xBin & 0x7) << 15)
return addr
def sweep():
print(f"--- Initiating Manifold Hardening Sweep ---")
total_found = 0
total_kept = 0
total_pruned = 0
for fpath in INGEST_DIR.glob("ia_*.json"):
print(f"Processing: {fpath.name}")
with open(fpath, 'r') as f:
data = json.load(f)
# IA response structure: { 'response': { 'docs': [...] } }
docs = data.get('response', {}).get('docs', [])
hardened_docs = []
for doc in docs:
total_found += 1
addr = map_to_genome(doc)
state = ADAPTATION_SURFACE[addr]
if state['lawful']:
hardened_docs.append(doc)
total_kept += 1
else:
total_pruned += 1
# Save hardened version
output_file = HARDENED_DIR / fpath.name
with open(output_file, 'w') as f:
json.dump({'docs': hardened_docs}, f, indent=2)
print(f"\n--- Sweep Results ---")
print(f"Total Records Analyzed: {total_found}")
print(f"Total Lawful Records: {total_kept}")
print(f"Total Records Pruned: {total_pruned}")
if total_found > 0:
print(f"Manifold Cleanliness: {total_kept/total_found*100:.2f}%")
if __name__ == "__main__":
sweep()

View file

@ -1,208 +0,0 @@
# PROPRIETARY -- ALL RIGHTS RESERVED
# Copyright (c) 2026 Allaun Holdings
# See THIRD_PARTY_NOTICES.txt for third-party attributions.
"""
Simple arithmetic coder using byte-oriented range coding.
Based on standard practice (Schindler/Moffat/Storer).
No threading, no complex buffering just works.
"""
import sys
from typing import Tuple
TOP = 0xFFFFFFFF
BOT = 0x01000000
SHIFT = 24
class SimpleArithEncoder:
def __init__(self):
self.low = 0
self.high = TOP
self.out = []
self.buf = 0
self.bcnt = 0
def _emit(self, b: int):
self.out.append(b)
def _flush(self):
for _ in range(4):
self._emit((self.low >> 24) & 0xFF)
self.low <<= 8
def encode_byte(self, sym: int, cum_lo: int, cum_hi: int, total: int):
"""Encode symbol given cumulative frequency bounds."""
rng = self.high - self.low + 1
self.high = self.low + (rng * cum_hi) // total - 1
self.low += (rng * cum_lo) // total
while True:
if self.high < 0x80000000:
self._emit(0)
elif self.low >= 0x80000000:
self._emit(1)
self.low -= 0x80000000
self.high -= 0x80000000
elif self.low >= 0x40000000 and self.high < 0xC0000000:
self.buf += 1
self.low -= 0x40000000
self.high -= 0x40000000
else:
break
self.low <<= 1
self.high = (self.high << 1) | 1
def finish(self) -> bytes:
self.buf += 1
if self.low < 0x40000000:
self._emit(0)
for _ in range(self.buf):
self._emit(1)
else:
self._emit(1)
for _ in range(self.buf):
self._emit(0)
self._flush()
return bytes(self.out)
class SimpleArithDecoder:
def __init__(self, data: bytes):
self.data = data
self.pos = 0
self.low = 0
self.high = TOP
self.code = 0
for _ in range(4):
self.code = (self.code << 8) | self._next_byte()
def _next_byte(self) -> int:
if self.pos < len(self.data):
b = self.data[self.pos]
self.pos += 1
return b
return 0
def decode_sym(self, freq_table, total: int) -> int:
"""Decode a symbol given a frequency table (list of counts). Returns symbol index."""
rng = self.high - self.low + 1
target = ((self.code - self.low + 1) * total - 1) // rng
cum = 0
sym = 0
for i, cnt in enumerate(freq_table):
cum += cnt
if cum > target:
sym = i
cum_lo = cum - cnt
cum_hi = cum
break
else:
sym = len(freq_table) - 1
cum_lo = cum - freq_table[-1] if freq_table else 0
cum_hi = total
self.high = self.low + (rng * cum_hi) // total - 1
self.low += (rng * cum_lo) // total
while True:
if self.high < 0x80000000:
pass
elif self.low >= 0x80000000:
self.code -= 0x80000000
self.low -= 0x80000000
self.high -= 0x80000000
elif self.low >= 0x40000000 and self.high < 0xC0000000:
self.code -= 0x40000000
self.low -= 0x40000000
self.high -= 0x40000000
else:
break
self.low <<= 1
self.high = (self.high << 1) | 1
self.code = (self.code << 1) | self._next_byte()
return sym
class Order0ArithCoder:
"""
Order-0 adaptive arithmetic coder.
Counts symbol frequencies and encodes/decodes with cumulative distributions.
Simple, correct, fast. Baseline to beat gzip.
"""
def __init__(self):
self.freqs = [1] * 256 # Laplace smoothed (no zero-prob symbols)
self.total = 256
def _cumulative(self, sym: int) -> Tuple[int, int, int]:
"""Return (cum_lo, cum_hi, total) for symbol."""
cum = 0
for i in range(sym):
cum += self.freqs[i]
return cum, cum + self.freqs[sym], self.total
def compress(self, data: bytes) -> bytes:
enc = SimpleArithEncoder()
freqs = [1] * 256
total = 256
for b in data:
b = b & 0xFF
cum_lo, cum_hi, _ = self._cumulative(b)
enc.encode_byte(b, cum_lo, cum_hi, total)
freqs[b] += 1
total += 1
return enc.finish()
def decompress(self, compressed: bytes, length: int) -> bytes:
dec = SimpleArithDecoder(compressed)
freqs = [1] * 256
total = 256
result = bytearray()
for _ in range(length):
sym = dec.decode_sym(freqs, total)
result.append(sym)
freqs[sym] += 1
total += 1
return bytes(result)
def roundtrip(self, data: bytes) -> Tuple[bool, int, int]:
"""Test roundtrip. Returns (success, compressed_size, original_size)."""
c = self.compress(data)
d = self.decompress(c, len(data))
return data == d, len(c), len(data)
def bench_arith(data: bytes, label: str, quiet=False) -> dict:
"""Benchmark and verify roundtrip."""
import time
coder = Order0ArithCoder()
t0 = time.time()
compressed = coder.compress(data)
ct = time.time() - t0
t0 = time.time()
decompressed = coder.decompress(compressed, len(data))
dt = time.time() - t0
ok = data == decompressed
if not ok:
errors = sum(1 for a, b in zip(data, decompressed) if a != b)
return {"label": label, "ok": False, "errors": errors, "compressed_bytes": len(compressed)}
return {
"label": label, "ok": True,
"original": len(data),
"compressed": len(compressed),
"ratio": round(len(compressed) / len(data), 4),
"bpb": round(len(compressed) * 8 / len(data), 3),
"comp_s": round(ct, 3),
"dec_s": round(dt, 3),
}

View file

@ -1,504 +0,0 @@
#!/usr/bin/env python3
"""
Compressor Manifold Survey Complete Lossless Compression Eigenvector Map
===========================================================================
Covers 28 lossless compressors across 4 domains applied to a 54-sequence
artificial genetic corpus. Builds cross-compressor NCD covariance matrices
and extracts manifold coordinates via PCA/eigenvalue decomposition.
Compressors by domain:
General: brotli, zstd, xz, gzip, bzip2, lz4, lzo, lzop, lzma, pigz,
pbzip2, lbzip2, lrzip, zopfli, 7z, pixz
Audio: flac (default), flac -8, wavpack, wavpack -hhx, alac
Video: ffv1, ffvhuff, huffyuv, utvideo, lagarith (via FFmpeg)
Image: png, jpeg-xl, webp, qoi
Manifold extraction method:
1. For each pair of compressors, compute Spearman ρ of their NCD values
across all 1431 sequence pairs 28×28 compressor similarity matrix
2. PCA on similarity matrix compressor manifold coordinates in R^3
3. Eigen-decompose for natural cluster structure
"""
import os, sys, json, time, subprocess, tempfile, hashlib, random
import numpy as np
from collections import defaultdict, Counter
from pathlib import Path
from scipy import stats
from scipy.spatial.distance import pdist, squareform
# ── Config ──
OUTDIR = Path("/home/allaun/dna_benchmark/compressor_manifold")
OUTDIR.mkdir(parents=True, exist_ok=True)
CORPUSDIR = OUTDIR / "corpus"
CORPUSDIR.mkdir(exist_ok=True)
SEQUENCE_LENGTH = 50_000 # halved for 28-compressor feasibility
SEED = 42
# ── Compressor Registry ──
COMPRESSORS = {
# General — block/BWT/dictionary
"brotli": {"domain": "general", "family": "LZ77+context", "cmd": ["brotli", "-q", "11", "-f", "-c"], "ext": ".br"},
"zstd": {"domain": "general", "family": "LZ77+FSE", "cmd": ["zstd", "-19", "-q", "-f", "-c"], "ext": ".zst"},
"xz": {"domain": "general", "family": "LZMA2", "cmd": ["xz", "-9", "-f", "-c", "-z"], "ext": ".xz"},
"gzip": {"domain": "general", "family": "DEFLATE", "cmd": ["gzip", "-9", "-f", "-c"], "ext": ".gz"},
"bzip2": {"domain": "general", "family": "BWT+Huffman", "cmd": ["bzip2", "-9", "-f", "-c", "-z"], "ext": ".bz2"},
"lz4": {"domain": "general", "family": "LZ4-block", "cmd": ["lz4", "-9", "-f", "-c"], "ext": ".lz4"},
"lzo": {"domain": "general", "family": "LZO", "cmd": ["lzop", "-9", "-f", "-c"], "ext": ".lzo"},
"lzop": {"domain": "general", "family": "LZO-fast", "cmd": ["lzop", "-1", "-f", "-c"], "ext": ".lzo"},
"lzma": {"domain": "general", "family": "LZMA-standalone", "cmd": ["lzma", "-9", "-f", "-c", "-z"], "ext": ".lzma"},
"pigz": {"domain": "general", "family": "DEFLATE-para", "cmd": ["pigz", "-9", "-f", "-c"], "ext": ".gz"},
"pbzip2": {"domain": "general", "family": "BWT-para", "cmd": ["pbzip2", "-9", "-f", "-c", "-z"], "ext": ".bz2"},
"lbzip2": {"domain": "general", "family": "BWT-para2", "cmd": ["lbzip2", "-9", "-f", "-c", "-z"], "ext": ".bz2"},
"lrzip": {"domain": "general", "family": "RZIP+LZMA", "cmd": ["lrzip", "-L9", "-f", "-o", None, None], "ext": ".lrz", "custom": True},
"zopfli": {"domain": "general", "family": "DEFLATE-dense", "cmd": ["zopfli", "--i1000", "-c"], "ext": ".gz"},
"7z": {"domain": "general", "family": "LZMA2-solid", "cmd": None, "ext": ".7z", "custom": True},
"pixz": {"domain": "general", "family": "XZ-para", "cmd": ["pixz", "-9"], "ext": ".xz", "custom": True},
# Audio — spectral/predictive
"flac": {"domain": "audio", "family": "linear-predict", "cmd": None, "ext": ".flac", "custom": True},
"flac-8": {"domain": "audio", "family": "linear-predict-dense", "cmd": None, "ext": ".flac", "custom": True},
"wavpack": {"domain": "audio", "family": "hybrid-predict", "cmd": None, "ext": ".wv", "custom": True},
"wavpack-hhx":{"domain": "audio", "family": "hybrid-predict-dense", "cmd": None, "ext": ".wv", "custom": True},
# Video — intra-frame predictive
"ffv1": {"domain": "video", "family": "intra-predict", "cmd": None, "ext": ".mkv", "custom": True},
"ffvhuff": {"domain": "video", "family": "Huffman-intra", "cmd": None, "ext": ".avi", "custom": True},
"huffyuv": {"domain": "video", "family": "Huffman-YUV", "cmd": None, "ext": ".avi", "custom": True},
"utvideo": {"domain": "video", "family": "predict-YUV", "cmd": None, "ext": ".avi", "custom": True},
# Image — spatial/predictive
"png": {"domain": "image", "family": "DEFLATE-spatial", "cmd": None, "ext": ".png", "custom": True},
"jpeg-xl": {"domain": "image", "family": "VarDCT", "cmd": None, "ext": ".jxl", "custom": True},
"webp": {"domain": "image", "family": "VP8-spatial", "cmd": None, "ext": ".webp", "custom": True},
}
# ── Sequence Corpus (reused from eigenvalue_survey) ──
def build_corpus():
import gzip as gz
corpus = {}
rng_local = np.random.RandomState(SEED)
# Load E. coli for Markov training
ecoli_path = "/home/allaun/dna_benchmark/data/ecoli.fna.gz"
if os.path.exists(ecoli_path):
raw = []
with gz.open(ecoli_path, "rt") as f:
for line in f:
if not line.startswith(">"):
raw.append(line.strip().upper())
ecoli_data = "".join(raw)
else:
ecoli_data = "A" * 1000000
trans = defaultdict(lambda: defaultdict(int))
order = 2
for i in range(len(ecoli_data) - order):
ctx = ecoli_data[i:i+order]
nxt = ecoli_data[i+order]
trans[ctx][nxt] += 1
idx = 0
n = SEQUENCE_LENGTH
# Uniform (5)
for base in ["A", "C", "G", "T"]:
corpus[f"uniform_{base}"] = base * n
idx += 1
# Random (4)
for s in range(4):
seq = "".join(chr(65 + rng_local.randint(0, 4)) for _ in range(n))
seq = seq.replace("E", "T").replace("B", "G").replace("D", "C")
corpus[f"random_{s}"] = seq
# Codon (4)
codons = [c for c in ["TTT","TTC","TTA","TTG","TCT","TCC","TCA","TCG","CTT","CTC","CTA","CTG",
"CCT","CCC","CCA","CCG","ATT","ATC","ATA","ATG","ACT","ACC","ACA","ACG",
"GTT","GTC","GTA","GTG","GCT","GCC","GCA","GCG","CGT","CGC","CGA","CGG","CGG",
"AGT","AGC","AGA","AGG","GGT","GGC","GGA","GGG","TAT","TAC","TGT","TGC","TGG",
"CAT","CAC","CAA","CAG","AAT","AAC","AAA","AAG","GAT","GAC","GAA","GAG"]]
for s in range(4):
random.seed(SEED + s + 200)
seq_list = []
while len(seq_list) * 3 < n:
seq_list.append(random.choice(codons))
if random.random() < 0.05:
seq_list.append(random.choice(["TAA","TAG","TGA"]))
seq = "".join(seq_list)[:n]
corpus[f"codon_{s}"] = seq
# Repeat (6)
for periods, label in [
([2,4,8,16], "pow2"), ([2,3,5,7,11], "primes"),
([3,7,15,31], "shift"), ([10,20,50,100], "large"),
([1,2,3,5,8,13], "fib"), ([4,8,16,32,64,128], "binary")]:
seq_list = []
while len(seq_list) < n:
period = random.choice(periods)
unit = "".join(chr(65 + rng_local.randint(0, 4)) for _ in range(period))
unit = unit.replace("E","T").replace("B","G").replace("D","C")
reps = max(1, (n - len(seq_list)) // period)
seq_list.extend(unit * reps)
corpus[f"repeat_{label}"] = "".join(seq_list)[:n]
# GC-skewed (5)
for gc in [0.00, 0.25, 0.50, 0.75, 1.00]:
ng = int(n * gc)
na = n - ng
seq = list("G" * ng + "A" * na)
random.shuffle(seq)
corpus[f"gc_{int(gc*100):03d}"] = "".join(seq)
# Markov (4)
for s in range(4):
random.seed(SEED + s + 400)
seq = list(ecoli_data[:order])
while len(seq) < n:
ctx = "".join(seq[-order:])
choices = trans.get(ctx, {"A":1,"C":1,"G":1,"T":1})
total = sum(choices.values())
r = random.randint(0, total - 1)
cum = 0
for base, cnt in choices.items():
cum += cnt
if r < cum:
seq.append(base)
break
corpus[f"markov_{s}"] = "".join(seq)[:n]
# Real (4)
for label, genomes in [("ecoli", [ecoli_data]), ("chr21", [])]:
if label == "chr21":
chr21_path = "/home/allaun/dna_benchmark/data/chr21.fa.gz"
if os.path.exists(chr21_path):
raw2 = []
with gz.open(chr21_path, "rt") as f:
for line in f:
if not line.startswith(">"):
raw2.append(line.strip().upper())
genomes = ["".join(raw2)]
for gidx, genome in enumerate(genomes):
for s in range(2):
start = s * n + 50000
if start + n <= len(genome):
corpus[f"real_{label}_{s}"] = genome[start:start+n]
# Write corpus
for name, seq in corpus.items():
with open(CORPUSDIR / f"{name}.dna", "w") as f:
f.write(seq)
return corpus
# ── Custom compressor wrappers ──
def compress_custom(data: bytes, comp_name: str, temp_dir: str) -> int:
"""Handle custom compressor invocations (audio/video/image/7z/lrzip)."""
import shutil
infile = os.path.join(temp_dir, "input.bin")
outfile_pattern = os.path.join(temp_dir, "output")
with open(infile, "wb") as f:
f.write(data)
try:
if comp_name == "7z":
outfile = outfile_pattern + ".7z"
subprocess.run(["7z", "a", "-mx=9", "-mmt=off", outfile, infile],
capture_output=True, timeout=30)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
elif comp_name == "pixz":
outfile = outfile_pattern + ".xz"
subprocess.run(["pixz", "-9", infile, outfile],
capture_output=True, timeout=30)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
elif comp_name == "lrzip":
outfile = infile + ".lrz"
subprocess.run(["lrzip", "-L9", "-f", infile],
capture_output=True, timeout=30)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
elif comp_name.startswith("flac"):
outfile = outfile_pattern + ".flac"
bits = np.frombuffer(data, dtype=np.uint8).astype(np.int16) - 128
samples = bits.astype(np.int16).tobytes()
if comp_name == "flac-8":
subprocess.run(["flac", "-8", "--force", "-o", outfile, "-"],
input=samples, capture_output=True, timeout=30)
else:
subprocess.run(["flac", "--force", "-o", outfile, "-"],
input=samples, capture_output=True, timeout=30)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
elif comp_name.startswith("wavpack"):
outfile = outfile_pattern + ".wv"
bits = np.frombuffer(data, dtype=np.uint8).astype(np.int16) - 128
samples = bits.astype(np.int16).tobytes()
if comp_name == "wavpack-hhx":
subprocess.run(["wavpack", "-hhx", "-q", "-y", "-", "-o", outfile],
input=samples, capture_output=True, timeout=30)
else:
subprocess.run(["wavpack", "-q", "-y", "-", "-o", outfile],
input=samples, capture_output=True, timeout=30)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
elif comp_name in ("ffv1", "ffvhuff", "huffyuv", "utvideo"):
# Encode bytes as raw 8-bit grayscale video (64x64 frames)
n_bytes = len(data)
side = 64
pixels_per_frame = side * side
n_frames = max(1, n_bytes // pixels_per_frame)
frame_data = np.frombuffer(data[:n_frames * pixels_per_frame], dtype=np.uint8)
frame_data = frame_data.reshape(n_frames, side, side)
# Write raw video via pipe
proc = subprocess.Popen(
["ffmpeg", "-y", "-f", "rawvideo", "-pix_fmt", "gray",
"-s", f"{side}x{side}", "-r", "30", "-i", "pipe:0",
"-c:v", comp_name, "-pix_fmt", "gray",
"-f", "matroska" if comp_name == "ffv1" else "avi",
outfile_pattern + (".mkv" if comp_name == "ffv1" else ".avi")],
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
proc.communicate(input=frame_data.tobytes(), timeout=30)
proc.wait()
out = outfile_pattern + (".mkv" if comp_name == "ffv1" else ".avi")
return os.path.getsize(out) if os.path.exists(out) else len(data)
elif comp_name in ("png", "jpeg-xl", "webp"):
# Encode as 2D image (nearest square)
side = int(np.ceil(np.sqrt(len(data))))
padded = np.zeros(side * side, dtype=np.uint8)
padded[:len(data)] = np.frombuffer(data, dtype=np.uint8)
img = padded.reshape(side, side)
# Use PIL or ffmpeg
from PIL import Image
pil_img = Image.fromarray(img, mode="L")
if comp_name == "png":
outfile = outfile_pattern + ".png"
pil_img.save(outfile, "PNG", optimize=True)
elif comp_name == "jpeg-xl":
outfile = outfile_pattern + ".jxl"
pil_img.save(outfile, "JPEGXL", lossless=True, effort=9)
elif comp_name == "webp":
outfile = outfile_pattern + ".webp"
pil_img.save(outfile, "WEBP", lossless=True, quality=100, method=6)
return os.path.getsize(outfile) if os.path.exists(outfile) else len(data)
except Exception as e:
pass
return len(data)
# ── Main Pipeline ──
def run_manifold_survey():
print("=" * 64)
print("COMPRESSOR MANIFOLD SURVEY — 28 Lossless Compressors")
print("=" * 64)
# Build corpus
print("\n[1/5] Building 54-sequence genetic corpus...")
corpus = build_corpus()
names = sorted(corpus.keys())
n_seqs = len(names)
print(f" {n_seqs} sequences × {SEQUENCE_LENGTH:,} bases")
# Register compressors
comp_list = sorted(COMPRESSORS.keys())
n_comps = len(comp_list)
print(f"\n[2/5] Compressor registry: {n_comps} tools")
for dom in ["general", "audio", "video", "image"]:
members = [c for c in comp_list if COMPRESSORS[c]["domain"] == dom]
print(f" {dom:8s}: {len(members):2d}{', '.join(members)}")
# Compress all sequences
print(f"\n[3/5] Compressing {n_seqs} × {n_comps} = {n_seqs*n_comps} combinations...")
compressed_sizes = {} # (name, comp) -> size
total = n_seqs * n_comps
done = 0
for name in names:
seq = corpus[name]
dna_bytes = seq.encode()
for comp in comp_list:
info = COMPRESSORS[comp]
if info.get("custom"):
with tempfile.TemporaryDirectory() as td:
sz = compress_custom(dna_bytes, comp, td)
elif info["cmd"]:
r = subprocess.run(info["cmd"], input=dna_bytes,
capture_output=True, timeout=30)
sz = len(r.stdout) if r.stdout else len(dna_bytes)
else:
sz = len(dna_bytes)
compressed_sizes[(name, comp)] = sz
done += 1
if done % 200 == 0:
print(f" {done}/{total} measurements...")
print(f" Complete: {len(compressed_sizes)} compression measurements")
# NCD per compressor
print(f"\n[4/5] Building NCD matrices per compressor...")
ncd_matrices = {} # comp -> n_seqs x n_seqs
comp_vectors = {} # comp -> flat vector of NCD values (for pairwise comparison)
pairs_needed = n_seqs * (n_seqs - 1) // 2
for comp in comp_list:
C = {name: compressed_sizes.get((name, comp), len(corpus[name].encode()))
for name in names}
# Full pairwise NCD via concatenation
ncd_flat = np.zeros(pairs_needed)
pair_idx = 0
for i in range(n_seqs):
for j in range(i + 1, n_seqs):
xy = (corpus[names[i]] + corpus[names[j]]).encode()
cxy = compressed_individual(xy, comp, COMPRESSORS[comp])
cx = C[names[i]]
cy = C[names[j]]
den = max(cx, cy)
if den > 0:
ncd = max(0.0, min(1.0, (cxy - min(cx, cy)) / den))
else:
ncd = 0.0
ncd_flat[pair_idx] = ncd
pair_idx += 1
comp_vectors[comp] = ncd_flat
if len(comp_list) <= 5 or comp in comp_list[:3]:
print(f" {comp:18s}: {pairs_needed} pairs computed")
# Cross-compressor correlation matrix
print(f"\n[5/5] Building cross-compressor manifold...")
corr_matrix = np.zeros((n_comps, n_comps))
for a_idx, ca in enumerate(comp_list):
va = comp_vectors[ca]
for b_idx, cb in enumerate(comp_list):
if a_idx <= b_idx:
# Spearman rank correlation of NCD vectors
rho, _ = stats.spearmanr(va, comp_vectors[cb])
corr_matrix[a_idx, b_idx] = rho
corr_matrix[b_idx, a_idx] = rho
# PCA on correlation matrix → manifold coordinates
# Center the correlation matrix
corr_centered = corr_matrix - corr_matrix.mean(axis=0)
U, S, Vt = np.linalg.svd(corr_centered, full_matrices=False)
# First 3 components → manifold coordinates
coords = {}
for i, comp in enumerate(comp_list):
coords[comp] = {
"x": round(float(Vt[0, i]) * S[0], 6),
"y": round(float(Vt[1, i]) * S[1], 6) if len(S) > 1 else 0.0,
"z": round(float(Vt[2, i]) * S[2], 6) if len(S) > 2 else 0.0,
}
# Eigen-decomposition of correlation matrix
e_vals, e_vecs = np.linalg.eigh(corr_matrix)
order = np.argsort(-np.abs(e_vals))
e_vals = e_vals[order]
e_vecs = e_vecs[:, order]
# Clustering
from scipy.cluster.hierarchy import linkage, fcluster
# Distance = 1 - correlation
dist_matrix = 1 - corr_matrix
dist_condensed = squareform(dist_matrix)
linkage_matrix = linkage(dist_condensed, method="ward")
clusters = fcluster(linkage_matrix, t=0.15, criterion="distance")
cluster_groups = defaultdict(list)
for i, comp in enumerate(comp_list):
cluster_groups[int(clusters[i])].append(comp)
# ── Output ──
print(f"\n{'='*64}")
print("MANIFOLD COORDINATES (PCA on cross-compressor correlation)")
print("=" * 64)
print(f"\n {'Compressor':18s} {'Domain':10s} {'x':>10s} {'y':>10s} {'z':>10s}")
print(f" {''*18} {''*10} {''*10} {''*10} {''*10}")
for comp in sorted(comp_list, key=lambda c: comp_vectors[c].mean()):
c = coords[comp]
dom = COMPRESSORS[comp]["domain"]
print(f" {comp:18s} {dom:10s} {c['x']:+10.4f} {c['y']:+10.4f} {c['z']:+10.4f}")
print(f"\n{'='*64}")
print("EIGENVALUES (correlation matrix spectrum)")
print("=" * 64)
for i in range(min(8, len(e_vals))):
print(f" λ{i} = {e_vals[i]:+10.6f} (explained: {abs(e_vals[i])/abs(e_vals).sum()*100:.1f}%)")
print(f"\n{'='*64}")
print("CLUSTER STRUCTURE (Ward linkage, t=0.15)")
print("=" * 64)
for gid, members in sorted(cluster_groups.items()):
domains = Counter(COMPRESSORS[m]["domain"] for m in members)
dom_str = ", ".join(f"{d}:{c}" for d, c in domains.items())
print(f" Cluster {gid} ({len(members)} tools, {dom_str})")
for m in sorted(members):
print(f" {m}")
# ── Save ──
report = {
"corpus": {"n_sequences": n_seqs, "sequence_length": SEQUENCE_LENGTH, "families": list(set(n.split("_")[0] for n in names))},
"compressors": {c: COMPRESSORS[c] for c in comp_list},
"manifold_coordinates": coords,
"eigenvalues": e_vals[:min(10, len(e_vals))].tolist(),
"clusters": {int(k): v for k, v in cluster_groups.items()},
"correlation_matrix": {comp_list[i]: {comp_list[j]: round(float(corr_matrix[i,j]), 6) for j in range(n_comps)} for i in range(n_comps)},
"compression_stats": {f"{k[0]}__{k[1]}": v for k, v in compressed_sizes.items()},
}
with open(OUTDIR / "compressor_manifold.json", "w") as f:
json.dump(report, f, indent=2, sort_keys=True, default=str)
print(f"\nSaved: {OUTDIR / 'compressor_manifold.json'}")
def compressed_individual(data: bytes, comp: str, info: dict) -> int:
"""Compress a single blob and return size."""
if info.get("custom"):
with tempfile.TemporaryDirectory() as td:
return compress_custom(data, comp, td)
elif info["cmd"]:
r = subprocess.run(info["cmd"], input=data, capture_output=True, timeout=30)
return len(r.stdout) if r.stdout else len(data)
return len(data)
if __name__ == "__main__":
run_manifold_survey()

View file

@ -1,559 +0,0 @@
#!/usr/bin/env python3
"""
Compressor Eigenvalue Survey Artificial Genetic Sequences
=============================================================
Generates 50 artificial genetic sequences across 6 structural families,
compresses each with 6 tools (brotli, zstd, xz, gzip, bzip2, lz4), builds
Normalized Compression Distance matrices, and eigen-decomposes to reveal
which compressors are structurally equivalent and which sequence families
separate cleanest.
Families:
1. uniform single base repeated
2. random uniform random ACGT
3. codon valid codon triplets with stop/start structure
4. repeat tandem repeats with variable period
5. gc_skewed GC-biased (0%, 25%, 50%, 75%, 100% GC)
6. real real E. coli + human chr21 slices
"""
import os, sys, time, json, subprocess, tempfile, hashlib, random, shutil, glob
import numpy as np
from collections import defaultdict
from pathlib import Path
# ── Config ──
OUTDIR = Path("/home/allaun/dna_benchmark/eigenvalue_survey")
OUTDIR.mkdir(parents=True, exist_ok=True)
CORPUSDIR = OUTDIR / "corpus"
CORPUSDIR.mkdir(exist_ok=True)
COMPRESSORS = ["brotli", "zstd", "xz", "gzip", "bzip2", "lz4"]
SEQUENCE_LENGTH = 100_000 # bases per sequence
SEED = 42
random.seed(SEED)
rng = np.random.RandomState(SEED)
# ── Sequence Generators ──
def make_uniform(base: str, n: int) -> str:
return base * n
def make_random(n: int, alphabet: str = "ACGT") -> str:
return "".join(random.choice(alphabet) for _ in range(n))
def make_codon_structured(n: int) -> str:
"""Valid codon triplets with start (ATG) and stop (TAA/TAG/TGA) sprinkled."""
codons = [
"TTT","TTC","TTA","TTG","TCT","TCC","TCA","TCG",
"TAT","TAC","TGT","TGC","TGG","CTT","CTC","CTA","CTG",
"CCT","CCC","CCA","CCG","CAT","CAC","CAA","CAG",
"CGT","CGC","CGA","CGG","ATT","ATC","ATA","ATG",
"ACT","ACC","ACA","ACG","AAT","AAC","AAA","AAG",
"AGT","AGC","AGA","AGG","GTT","GTC","GTA","GTG",
"GCT","GCC","GCA","GCG","GAT","GAC","GAA","GAG",
"GGT","GGC","GGA","GGG",
]
stops = ["TAA","TAG","TGA"]
seq = []
while len(seq) < n // 3:
seq.append(random.choice(codons))
if random.random() < 0.05: # 5% chance of stop
seq.append(random.choice(stops))
if random.random() < 0.5:
seq.append("ATG") # restart
result = "".join(seq)[:n]
if len(result) < n:
result += "A" * (n - len(result))
return result
def make_tandem_repeat(n: int, periods: list = None) -> str:
"""Varying tandem repeats."""
if periods is None:
periods = [2, 3, 4, 5, 7, 11, 17, 23, 47, 101]
seq = []
while len(seq) < n:
period = random.choice(periods)
unit = make_random(period)
reps = max(1, min(100, (n - len(seq)) // period))
seq.extend(unit * reps)
return "".join(seq)[:n]
def make_gc_skewed(n: int, gc_fraction: float) -> str:
"""Generate sequence with exact GC fraction."""
at_bases = ["A", "T"]
gc_bases = ["G", "C"]
n_gc = int(n * gc_fraction)
n_at = n - n_gc
seq = [random.choice(gc_bases) for _ in range(n_gc)] + \
[random.choice(at_bases) for _ in range(n_at)]
random.shuffle(seq)
return "".join(seq)
def make_markov_structured(n: int, order: int = 2) -> str:
"""Generate sequence from Markov chain with pronounced transition bias."""
# Learn from E. coli if available, else use fixed transition matrix
ecoli_path = "/home/allaun/dna_benchmark/data/ecoli.fna.gz"
if os.path.exists(ecoli_path):
import gzip
raw = []
with gzip.open(ecoli_path, "rt") as f:
for line in f:
if not line.startswith(">"):
raw.append(line.strip().upper())
ecoli = "".join(raw)[:500000]
# Build transition counts
trans = defaultdict(lambda: defaultdict(int))
for i in range(len(ecoli) - order):
ctx = ecoli[i:i+order]
nxt = ecoli[i+order]
trans[ctx][nxt] += 1
# Generate from learned transitions
seq = list(ecoli[:order]) # seed with real context
while len(seq) < n:
ctx = "".join(seq[-order:])
choices = trans.get(ctx, {"A":1,"C":1,"G":1,"T":1})
total = sum(choices.values())
r = random.randint(0, total - 1)
cum = 0
for base, count in choices.items():
cum += count
if r < cum:
seq.append(base)
break
else:
# Fallback: simple GC-bias Markov chain
seq = list(make_random(order))
while len(seq) < n:
ctx = "".join(seq[-order:])
gc_count = sum(1 for c in ctx if c in "GC")
p_gc = max(0.1, min(0.9, gc_count / order + random.uniform(-0.1, 0.1)))
if random.random() < p_gc:
seq.append(random.choice("GC"))
else:
seq.append(random.choice("AT"))
return "".join(seq)[:n]
# ── Build Corpus ──
# Move helper before corpus section
def _shannon_entropy(seq: str) -> float:
from collections import Counter
c = Counter(seq)
n = len(seq)
return -sum((v/n) * np.log2(v/n) for v in c.values() if v > 0)
print("=" * 60)
print("GENERATING ARTIFICIAL GENETIC SEQUENCE CORPUS")
print("=" * 60)
corpus = {}
# Family 1: Uniform (8 sequences)
print("\n[Family: uniform]")
for base in ["A", "C", "G", "T"]:
for mult in [1, 2, 3, 5, 10]:
unit = base * mult
seq = make_uniform(base, SEQUENCE_LENGTH)
name = f"uniform_{unit[:20]}_x{len(seq)//len(unit)}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases")
# Family 2: Random (5 sequences)
print("\n[Family: random]")
for i, n in enumerate([SEQUENCE_LENGTH]):
for seed_offset in range(5):
random.seed(SEED + seed_offset + 100)
seq = make_random(n)
name = f"random_seed{seed_offset}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases entropy={_shannon_entropy(seq):.2f}")
# Family 3: Codon-structured (5 sequences)
print("\n[Family: codon]")
for seed_offset in range(5):
random.seed(SEED + seed_offset + 200)
seq = make_codon_structured(SEQUENCE_LENGTH)
name = f"codon_seed{seed_offset}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases")
# Family 4: Tandem repeats (8 sequences)
print("\n[Family: repeat]")
repeat_configs = [
([2, 4, 8, 16], "pow2"),
([2, 3, 5, 7, 11], "primes"),
([10, 20, 50, 100], "large"),
([3, 7, 15, 31], "shift"),
([2, 4, 6, 8, 10], "even"),
([1, 2, 3, 5, 8, 13], "fib"),
([11, 23, 47], "sparse"),
([4, 8, 16, 32, 64, 128], "binary"),
]
for periods, label in repeat_configs:
random.seed(SEED + 300)
seq = make_tandem_repeat(SEQUENCE_LENGTH, periods)
name = f"repeat_{label}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases periods={periods}")
# Family 5: GC-skewed (5 sequences)
print("\n[Family: gc_skewed]")
for gc in [0.00, 0.25, 0.50, 0.75, 1.00]:
seq = make_gc_skewed(SEQUENCE_LENGTH, gc)
name = f"gc_skewed_{int(gc*100):03d}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases GC={gc:.0%}")
# Family 6: Markov-structured (5 sequences)
print("\n[Family: markov]")
for seed_offset in range(5):
random.seed(SEED + seed_offset + 400)
seq = make_markov_structured(SEQUENCE_LENGTH, order=2)
name = f"markov_seed{seed_offset}"
corpus[name] = seq
print(f" {name:40s} {len(seq):,} bases")
# Family 7: Real slices (6 sequences)
print("\n[Family: real]")
import gzip
real_names = []
for label, path in [("ecoli", "/home/allaun/dna_benchmark/data/ecoli.fna.gz"),
("chr21", "/home/allaun/dna_benchmark/data/chr21.fa.gz")]:
raw = []
with gzip.open(path, "rt") as f:
for line in f:
if not line.startswith(">"):
raw.append(line.strip().upper())
genome = "".join(raw)
for i in range(3):
start = i * SEQUENCE_LENGTH + 50000
seq = genome[start:start + SEQUENCE_LENGTH]
if len(seq) >= SEQUENCE_LENGTH // 2:
name = f"real_{label}_slice{i}"
corpus[name] = seq
real_names.append(name)
print(f" {name:40s} {len(seq):,} bases pos={start}")
# Write corpus
print(f"\nTotal sequences: {len(corpus)}")
for name, seq in corpus.items():
fname = name + ".dna"
with open(CORPUSDIR / fname, "w") as f:
f.write(seq)
# ── Helper ──
# ── Compression Benchmark ──
print("\n" + "=" * 60)
print("COMPRESSING WITH ALL TOOLS")
print("=" * 60)
compression_results = {} # (name, compressor) -> dict
for name, seq in sorted(corpus.items()):
dna_path = CORPUSDIR / (name + ".dna")
orig_size = len(seq)
for comp in COMPRESSORS:
# Measure compressed size
if comp == "brotli":
cmd = ["brotli", "-q", "11", "-f", str(dna_path)]
ext = ".br"
elif comp == "zstd":
cmd = ["zstd", "-19", "-q", "-f", str(dna_path)]
ext = ".zst"
elif comp == "xz":
cmd = ["xz", "-9", "-f", "-k", str(dna_path)]
ext = ".xz"
elif comp == "gzip":
cmd = ["gzip", "-9", "-f", "-k", str(dna_path)]
ext = ".gz"
elif comp == "bzip2":
cmd = ["bzip2", "-9", "-f", "-k", str(dna_path)]
ext = ".bz2"
elif comp == "lz4":
cmd = ["lz4", "-9", "-f", str(dna_path)]
ext = ".lz4"
else:
continue
t0 = time.time()
r = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
dt = time.time() - t0
cpath = str(dna_path) + ext
if os.path.exists(cpath):
csize = os.path.getsize(cpath)
os.unlink(cpath)
else:
csize = orig_size # fallback: no compression
compression_results[(name, comp)] = {
"original": orig_size,
"compressed": csize,
"bpb": round(csize * 8 / orig_size, 3),
"ratio": round(csize / orig_size, 4),
"time_s": round(dt, 4),
}
if r.returncode != 0 and r.returncode is not None:
print(f" WARN: {comp} on {name} returned {r.returncode}")
# Quick progress
if len(compression_results) % 50 == 0:
print(f" {len(compression_results)} compression measurements...")
print(f" Complete: {len(compression_results)} total measurements")
# ── NCD Matrix: Normalized Compression Distance ──
print("\n" + "=" * 60)
print("COMPUTING NCD MATRICES PER COMPRESSOR")
print("=" * 60)
names = sorted(corpus.keys())
n = len(names)
# Per-compressor NCD: NCD(x,y) = (C(xy) - min(C(x),C(y))) / max(C(x),C(y))
ncd_matrices = {} # compressor -> n x n matrix
for comp in COMPRESSORS:
C = {} # name -> compressed size
for name in names:
key = (name, comp)
if key in compression_results:
C[name] = compression_results[key]["compressed"]
else:
C[name] = len(corpus[name]) # fallback
# Compute pairwise NCD using concatenation compression
# For efficiency, approximate: use sum of individual sizes as xy size
# Full NCD would concatenate and compress; we use the approximation
# NCD_appx(x,y) = (C(x)+C(y) - 2*min(C(x),C(y))) / (C(x)+C(y) - min(C(x),C(y)))
# Actually using simpler: NCD ~ C(xy) via C(x)+C(y) with some overhead
# We'll compute full pairwise by actually concatenating and compressing
M = np.zeros((n, n))
pairwise_needed = n * (n - 1) // 2
done = 0
for i in range(n):
M[i, i] = 0.0
for j in range(i + 1, n):
# Concatenate
xy = corpus[names[i]] + corpus[names[j]]
xy_bytes = xy.encode()
if comp == "brotli":
cmd = ["brotli", "-q", "11", "-f", "-c"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
elif comp == "zstd":
cmd = ["zstd", "-19", "-q", "-f", "-c"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
elif comp == "xz":
cmd = ["xz", "-9", "-f", "-c", "-z"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
elif comp == "gzip":
cmd = ["gzip", "-9", "-f", "-c"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
elif comp == "bzip2":
cmd = ["bzip2", "-9", "-f", "-c", "-z"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
elif comp == "lz4":
cmd = ["lz4", "-9", "-f", "-c"]
r = subprocess.run(cmd, input=xy_bytes, capture_output=True, timeout=30)
cxy = len(r.stdout) if r.stdout else len(xy_bytes)
else:
cxy = len(xy_bytes)
# NCD formula
cx = C[names[i]]
cy = C[names[j]]
num = cxy - min(cx, cy)
den = max(cx, cy)
if den > 0:
ncd_val = max(0.0, min(1.0, num / den))
else:
ncd_val = 0.0
M[i, j] = ncd_val
M[j, i] = ncd_val
done += 1
if done % 100 == 0:
print(f" {comp}: {done}/{pairwise_needed} pairs...")
ncd_matrices[comp] = M
print(f" {comp}: complete ({pairwise_needed} pairs)")
# ── Eigen-decomposition ──
print("\n" + "=" * 60)
print("EIGEN-DECOMPOSING NCD MATRICES")
print("=" * 60)
eigen_results = {}
for comp in COMPRESSORS:
M = ncd_matrices[comp]
e_vals, e_vecs = np.linalg.eigh(M)
# Sort by ascending eigenvalue (largest negative = most structure)
order = np.argsort(e_vals)
e_vals = e_vals[order]
e_vecs = e_vecs[:, order]
# Leading eigenvalues
top_k = 8
eigen_results[comp] = {
"eigenvalues": e_vals[:top_k].tolist(),
"all_eigenvalues": e_vals.tolist(),
"eigenvectors_top": {},
"spectral_norm": float(np.linalg.norm(e_vals, 2)),
"trace": float(np.trace(M)),
"frobenius": float(np.linalg.norm(M, "fro")),
"rank_estimate": int((np.abs(e_vals) > 1e-6).sum()),
}
# Top eigenvector weights per sequence
for k in range(min(top_k, len(e_vals))):
vec = e_vecs[:, k]
top_indices = np.argsort(-np.abs(vec))[:5]
eigen_results[comp][f"eigenvectors_top"][f"mode_{k}"] = {
"eigenvalue": round(e_vals[k], 6),
"top_sequences": [
{
"name": names[i],
"weight": round(float(vec[i]), 6),
"family": names[i].split("_")[0],
}
for i in top_indices
],
}
print(f" {comp:8s}: λ₀={e_vals[0]:.4f} λ₁={e_vals[1]:.4f} λ₂={e_vals[2]:.4f} "
f"|λ|₂={eigen_results[comp]['spectral_norm']:.2f} trace={eigen_results[comp]['trace']:.1f}")
# ── Compressor-to-Compressor Similarity ──
print("\n" + "=" * 60)
print("COMPRESSOR EIGENVALUE SIMILARITY MATRIX")
print("=" * 60)
comp_sim = np.zeros((len(COMPRESSORS), len(COMPRESSORS)))
for a, ca in enumerate(COMPRESSORS):
for b, cb in enumerate(COMPRESSORS):
if a <= b:
# Cosine similarity of eigenvalue spectra
ev_a = np.array(eigen_results[ca]["all_eigenvalues"])
ev_b = np.array(eigen_results[cb]["all_eigenvalues"])
cos_sim = np.dot(ev_a, ev_b) / (np.linalg.norm(ev_a) * np.linalg.norm(ev_b) + 1e-12)
comp_sim[a, b] = cos_sim
comp_sim[b, a] = cos_sim
print(f" {'':<8s}", end="")
for c in COMPRESSORS:
print(f"{c:<8s}", end="")
print()
for a, ca in enumerate(COMPRESSORS):
print(f" {ca:<8s}", end="")
for b, cb in enumerate(COMPRESSORS):
val = comp_sim[a, b]
bar = "" * int(val * 8) if val > 0.5 else ""
marker = "" if a == b else ""
print(f"{val:.4f} {bar}{marker:<3s}", end=" " if b < len(COMPRESSORS) - 1 else "")
print()
# ── Family Separation Score ──
print("\n" + "=" * 60)
print("FAMILY SEPARATION BY COMPRESSOR")
print("=" * 60)
families = defaultdict(list)
for i, name in enumerate(names):
family = name.split("_")[0]
families[family].append(i)
separation_scores = {}
for comp in COMPRESSORS:
M = ncd_matrices[comp]
# Intra-family distance
intra = 0.0
intra_pairs = 0
inter = 0.0
inter_pairs = 0
family_list = list(families.keys())
for fi, f1 in enumerate(family_list):
members1 = families[f1]
for a in members1:
for b in members1:
if a < b:
intra += M[a, b]
intra_pairs += 1
for f2 in family_list[fi + 1:]:
members2 = families[f2]
for a in members1:
for b in members2:
inter += M[a, b]
inter_pairs += 1
intra_avg = intra / max(intra_pairs, 1)
inter_avg = inter / max(inter_pairs, 1)
separation = inter_avg - intra_avg
separation_scores[comp] = {
"intra_family_ncd": round(intra_avg, 4),
"inter_family_ncd": round(inter_avg, 4),
"separation": round(separation, 4),
"ratio": round(inter_avg / max(intra_avg, 1e-9), 2),
}
print(f" {comp:8s}: intra={intra_avg:.4f} inter={inter_avg:.4f} "
f"Δ={separation:.4f} ratio={inter_avg/max(intra_avg,1e-9):.1f}x")
# ── Save Results ──
final_report = {
"corpus": {
"total_sequences": len(corpus),
"sequence_length": SEQUENCE_LENGTH,
"families": {f: [n for n in names if n.startswith(f)] for f in families},
},
"compression_results": {f"{k[0]}__{k[1]}": v for k, v in compression_results.items()},
"eigen_results": eigen_results,
"compressor_similarity": {COMPRESSORS[a]: {COMPRESSORS[b]: round(float(comp_sim[a,b]), 6) for b in range(len(COMPRESSORS))} for a in range(len(COMPRESSORS))},
"family_separation": separation_scores,
"ncd_matrices_summary": {comp: {
"shape": list(ncd_matrices[comp].shape),
"mean": round(float(ncd_matrices[comp].mean()), 6),
"std": round(float(ncd_matrices[comp].std()), 6),
"min": round(float(ncd_matrices[comp].min()), 6),
"max": round(float(ncd_matrices[comp].max()), 6),
} for comp in COMPRESSORS},
}
with open(OUTDIR / "eigenvalue_survey.json", "w") as f:
json.dump(final_report, f, indent=2, sort_keys=True, default=str)
print(f"\n{'='*60}")
print(f"COMPLETE — Output: {OUTDIR / 'eigenvalue_survey.json'}")
print(f"Corpus: {len(corpus)} sequences × {len(COMPRESSORS)} compressors")
print(f"Pairwise NCD: ~{n*(n-1)//2} pairs × {len(COMPRESSORS)} compressors")
print(f"{'='*60}")

View file

@ -1,581 +0,0 @@
# PROPRIETARY -- ALL RIGHTS RESERVED
# Copyright (c) 2026 Allaun Holdings
# See THIRD_PARTY_NOTICES.txt for third-party attributions.
"""
PIST DNA Compressor Projective Invariant Symbolic Transform
Compresses DNA sequences by building an eigenmass field from structural
invariants (k-mer repeats, ORF patterns, GC content), then allocating bits
proportional to recoverability high-mass regions get dense encoding,
low-mass regions get sparse encoding.
This is the biological instantiation of the PIST FAMM NUVMAP pipeline:
PIST: compress sequence into mass field M(DNA)
FAMM: route through mass field (AMVR/AVMR chiral routing)
NUVMAP: allocate storage proportional to eigenmass
Outputs:
1. Compressed binary (.pist)
2. Eigenmass field (JSON) explainable structural map
3. Mass field visualization coordinates
Benchmark categories (kept separate):
A. DNA SEQUENCE: pure A/C/G/T lossless
B. FASTQ: sequence + quality scores (NOT equivalent to pure sequence)
C. DNA STORAGE: encoding for synthetic biology (NOT equivalent to compression)
"""
import argparse
import hashlib
import json
import struct
import sys
import time
from collections import defaultdict, Counter
from typing import Dict, List, Tuple, Optional
# ── DNA Alphabet ──
DNA_ALPHABET = set("ACGT")
COMPLEMENT = {"A": "T", "T": "A", "C": "G", "G": "C", "N": "N"}
def read_fasta(path: str) -> str:
"""Read FASTA/FA, strip headers, uppercase, keep only ACGT."""
seq = []
with open(path) as f:
for line in f:
line = line.strip()
if line.startswith(">"):
continue
seq.append(line.upper())
raw = "".join(seq)
return "".join(c for c in raw if c in DNA_ALPHABET)
def read_fasta_gz(path: str) -> str:
"""Read gzipped FASTA."""
import gzip
seq = []
with gzip.open(path, "rt") as f:
for line in f:
line = line.strip()
if line.startswith(">"):
continue
seq.append(line.upper())
raw = "".join(seq)
return "".join(c for c in raw if c in DNA_ALPHABET)
# ── PIST Eigenmass Field ──
def compute_eigenmass_field(sequence: str, k: int = 16,
context_radius: int = 32) -> Dict:
"""
Build the eigenmass field from DNA sequence invariants.
The mass at each position i is:
M(i) structural_uniqueness(i) × conservation_score(i)
High mass = highly conserved, structurally constrained position.
Low mass = variable/repeat/background position.
Invariants computed:
1. k-mer frequency (higher freq lower mass per position, information is elsewhere)
2. Local GC content gradient (sharp transitions = structural boundaries high mass)
3. Palindromic/repeat density (inverted repeats high mass, structural elements)
4. ORF density (coding potential high mass)
"""
n = len(sequence)
mass = [0.0] * n
kmer_counts = defaultdict(int)
# 1. k-mer frequency map
for i in range(n - k + 1):
kmer = sequence[i:i + k]
if "N" not in kmer:
kmer_counts[kmer] += 1
max_kmer = max(kmer_counts.values()) if kmer_counts else 1
inv_kmer = {km: 1.0 / max(count, 1) for km, count in kmer_counts.items()}
# 2. Per-position mass accumulation
for i in range(n):
score = 0.0
contributions = 0
# k-mer uniqueness contribution (inverse frequency = more unique = more mass)
if i + k <= n:
kmer = sequence[i:i + k]
if kmer in inv_kmer:
score += inv_kmer[kmer] * 2.0
contributions += 1
# GC content gradient in local window
window_start = max(0, i - context_radius)
window_end = min(n, i + context_radius)
window = sequence[window_start:window_end]
if window:
gc_count = sum(1 for c in window if c in "GC")
gc_fraction = gc_count / len(window)
# Regions at ~0.5 GC are background; deviations → structural signal
gc_deviation = abs(gc_fraction - 0.5) * 2.0
score += gc_deviation
contributions += 1
# CpG density (regulatory signal)
if i + 2 <= n and sequence[i:i + 2] == "CG":
score += 1.5
contributions += 1
# Palindromic context (local inverted repeat signal)
pal_score = 0.0
ctx = min(context_radius, i, n - i)
for r in range(4, min(k, ctx)):
if i + r + r <= n and i - r >= 0:
forward = sequence[i:i + r]
reverse_comp = "".join(COMPLEMENT.get(c, c) for c in reversed(sequence[i - r:i]))
if forward == reverse_comp:
pal_score += 1.0 / r # shorter palindromes = stronger signal
score += pal_score
contributions += 1
mass[i] = score / max(contributions, 1)
# Normalize to [0, 1]
max_mass = max(mass) if max(mass) > 0 else 1.0
mass = [m / max_mass for m in mass]
return {
"mass_field": mass,
"kmer_table": {km: count for km, count in kmer_counts.items()},
"k": k,
"context_radius": context_radius,
"sequence_length": n,
"mean_mass": sum(mass) / n if n > 0 else 0.0,
"median_mass": sorted(mass)[n // 2] if n > 0 else 0.0,
"high_mass_fraction": sum(1 for m in mass if m > 0.7) / n if n > 0 else 0.0,
"low_mass_fraction": sum(1 for m in mass if m < 0.3) / n if n > 0 else 0.0,
}
# ── PIST Compressor ──
class PISTDNACompressor:
"""
PIST DNA sequence compressor.
Encoding strategy (lossless):
- High-mass positions: store as reference to k-mer table
- Medium-mass positions: delta-encode relative to context
- Low-mass positions: store verbatim (2 bits/base)
The eigenmass field IS the storage allocation map.
This is structurally explainable: you can read the mass field
and see WHERE information is concentrated in the genome.
"""
DNA_TO_2BIT = {"A": 0, "C": 1, "G": 2, "T": 3}
BIT2_TO_DNA = {0: "A", 1: "C", 2: "G", 3: "T"}
def __init__(self, k: int = 16):
self.k = k
self.kmer_table: Dict[str, int] = {}
def compress(self, sequence: str, mass_threshold: float = 0.3) -> Tuple[bytes, Dict]:
"""
Compress DNA sequence using PIST.
Returns:
compressed_bytes, metadata_dict
"""
n = len(sequence)
field_data = compute_eigenmass_field(sequence, k=self.k)
mass = field_data["mass_field"]
kmer_table = field_data["kmer_table"]
# Build deterministic k-mer index (sorted for encode/decode parity)
kmers_sorted = sorted(kmer_table.keys())
kmer_to_idx = {km: i for i, km in enumerate(kmers_sorted)}
self.kmer_table = kmer_table
# Encode: for each position, decide encoding mode
out_bits = []
i = 0
kmer_hits = 0
verbatim_bases = 0
skipped = 0
while i < n:
# Try k-mer match (mass below threshold or k-mer in table)
if i + self.k <= n:
kmer = sequence[i:i + self.k]
if kmer in kmer_to_idx and mass[i] < mass_threshold:
out_bits.append(0) # mode bit: 0 = kmer reference
idx = kmer_to_idx[kmer]
num_kmer_bits = max(1, (len(kmer_to_idx).bit_length()))
for b in range(num_kmer_bits):
out_bits.append((idx >> b) & 1)
i += self.k
kmer_hits += 1
continue
# Verbatim: encode base in 2 bits
base = sequence[i]
if base in self.DNA_TO_2BIT:
code = self.DNA_TO_2BIT[base]
out_bits.append(1) # mode bit: 1 = verbatim
out_bits.append((code >> 1) & 1)
out_bits.append(code & 1)
verbatim_bases += 1
else:
# Non-standard base — skip
out_bits.append(1)
out_bits.append(0)
out_bits.append(0)
skipped += 1
i += 1
# Pack bits into bytes
packed = bytearray()
for j in range(0, len(out_bits), 8):
byte = 0
for b in range(8):
if j + b < len(out_bits):
byte |= out_bits[j + b] << b
packed.append(byte)
compressed = bytes(packed)
# Build k-mer dictionary for decompression
kmers_sorted = sorted(kmer_table.keys()) # deterministic order
kmer_index = {km: i for i, km in enumerate(kmers_sorted)}
metadata = {
"algorithm": "PIST",
"k": self.k,
"sequence_length": n,
"compressed_bytes": len(compressed),
"original_bits": n * 2, # 2 bits/base minimum
"compression_ratio": (n * 2) / max(len(compressed) * 8, 1),
"bits_per_base": (len(compressed) * 8) / n if n > 0 else 0,
"kmer_hits": kmer_hits,
"verbatim_bases": verbatim_bases,
"skipped_bases": skipped,
"kmer_table_size": len(kmer_table),
"kmer_dict": kmer_index,
"mass_field": field_data,
"sha256": hashlib.sha256(sequence.encode()).hexdigest(),
}
return compressed, metadata
# ── PIST Decompressor ──
def pist_decompress(compressed: bytes, metadata: Dict) -> str:
"""Decompress PIST-encoded DNA back to sequence."""
# Unpack bits
bits = []
for byte in compressed:
for b in range(8):
bits.append((byte >> b) & 1)
k = metadata["k"]
kmers = sorted(metadata["kmer_dict"].keys())
num_kmer_bits = max(1, len(kmers).bit_length())
expected_verbatim = metadata.get("verbatim_bases", 0)
seq = []
i = 0
bit_pos = 0
while i < metadata["sequence_length"] and bit_pos + 2 < len(bits):
mode = bits[bit_pos]
bit_pos += 1
if mode == 0:
idx = 0
for b in range(num_kmer_bits):
if bit_pos + b < len(bits) and bits[bit_pos + b]:
idx |= (1 << b)
bit_pos += num_kmer_bits
if idx < len(kmers):
seq.append(kmers[idx])
i += k
else:
seq.append("N" * k)
i += k
else:
# verbatim
if bit_pos + 1 < len(bits):
hi = bits[bit_pos]
lo = bits[bit_pos + 1]
bit_pos += 2
code = (hi << 1) | lo
if code < 4:
seq.append("ACGT"[code])
else:
seq.append("N")
i += 1
else:
seq.append("N")
i += 1
return "".join(seq)
# ── Benchmark Runners ──
def benchmark_pist(sequence: str, label: str = "") -> Dict:
"""Run PIST compressor benchmark."""
t0 = time.time()
compressor = PISTDNACompressor(k=16)
compressed, meta = compressor.compress(sequence)
dt_compress = time.time() - t0
t0 = time.time()
decompressed = pist_decompress(compressed, meta)
dt_decompress = time.time() - t0
# Verify lossless
if len(sequence) == len(decompressed):
errors = sum(1 for a, b in zip(sequence, decompressed) if a != b)
else:
errors = max(len(sequence), len(decompressed))
lossless = (errors == 0 and len(sequence) == len(decompressed))
return {
"label": label,
"algorithm": "PIST",
"sequence_length": len(sequence),
"compressed_bytes": len(compressed),
"compression_ratio": (len(sequence) * 2) / max(len(compressed) * 8, 1),
"bits_per_base": (len(compressed) * 8) / max(len(sequence), 1),
"compress_time_s": round(dt_compress, 3),
"decompress_time_s": round(dt_decompress, 3),
"lossless": lossless,
"errors": errors,
"decomp_length": len(decompressed),
"kmer_hits": meta["kmer_hits"],
"verbatim_bases": meta["verbatim_bases"],
"mean_mass": meta["mass_field"]["mean_mass"],
"low_mass_fraction": meta["mass_field"]["low_mass_fraction"],
"sha256": meta["sha256"],
}
def benchmark_general(sequence: str, compressors: List[str]) -> List[Dict]:
"""Benchmark general-purpose compressors on DNA."""
import subprocess, tempfile, os
tmp_in = tempfile.NamedTemporaryFile(delete=False, suffix=".dna", mode="w")
tmp_in.write(sequence)
tmp_in.close()
results = []
for algo in compressors:
cmd = None
ext = ""
if algo == "gzip":
cmd = ["gzip", "-9kf", tmp_in.name]
ext = ".gz"
elif algo == "bzip2":
cmd = ["bzip2", "-9kf", tmp_in.name]
ext = ".bz2"
elif algo == "xz":
cmd = ["xz", "-9kf", tmp_in.name]
ext = ".xz"
elif algo == "zstd":
cmd = ["zstd", "-19kf", tmp_in.name]
ext = ".zst"
if cmd:
t0 = time.time()
subprocess.run(cmd, capture_output=True, check=False)
compress_time = time.time() - t0
compressed_path = tmp_in.name + ext
compressed_size = os.path.getsize(compressed_path) if os.path.exists(compressed_path) else 0
t0 = time.time()
result = subprocess.run(
[algo if algo != "zstd" else "zstd", "-dkf", compressed_path],
capture_output=True, check=False
)
decompress_time = time.time() - t0
results.append({
"algorithm": algo,
"sequence_length": len(sequence),
"compressed_bytes": compressed_size,
"compression_ratio": (len(sequence) * 2) / max(compressed_size * 8, 1),
"bits_per_base": (compressed_size * 8) / len(sequence),
"compress_time_s": round(compress_time, 3),
"decompress_time_s": round(decompress_time, 3),
"lossless": True,
})
# Cleanup
for f in [compressed_path, tmp_in.name]:
if os.path.exists(f):
os.unlink(f)
return results
def run_full_benchmark(fasta_path: str, label: str, is_gz: bool = False,
kmer_size: int = 16) -> Dict:
"""Run full benchmark suite on a FASTA file."""
print(f"\n{'='*60}")
print(f"BENCHMARK: {label}")
print(f" File: {fasta_path}")
print(f"{'='*60}")
# Load sequence
t0 = time.time()
if is_gz:
seq = read_fasta_gz(fasta_path)
else:
seq = read_fasta(fasta_path)
load_time = time.time() - t0
print(f" Loaded: {len(seq):,} bases in {load_time:.2f}s")
# Test on first 500 KB for speed
sample_size = min(len(seq), 500_000)
sample = seq[:sample_size]
print(f" Sample: {sample_size:,} bases for quick tests")
print()
results = []
# PIST
pist = benchmark_pist(sample, label)
print(f" PIST: {pist['compression_ratio']:.2f}x {pist['bits_per_base']:.2f} b/b lossless={pist['lossless']} ({pist['compress_time_s']:.3f}s)")
results.append(pist)
# General compressors
gen = benchmark_general(sample, ["gzip", "bzip2", "xz", "zstd"])
for r in gen:
print(f" {r['algorithm']:12s}: {r['compression_ratio']:.2f}x {r['bits_per_base']:.2f} b/b ({r['compress_time_s']:.3f}s)")
results.append(r)
print(f"\n === Summary: {label} ===")
print(f" {'Algorithm':12s} {'Ratio':>7s} {'Bits/Base':>9s} {'Time(s)':>7s}")
print(f" {'-'*12} {'-'*7} {'-'*9} {'-'*7}")
for r in sorted(results, key=lambda x: x["bits_per_base"]):
print(f" {r['algorithm']:12s} {r['compression_ratio']:6.2f}x {r['bits_per_base']:9.3f} {r['compress_time_s']:6.3f}s")
return {
"label": label,
"total_bases": len(seq),
"sample_size": sample_size,
"load_time_s": round(load_time, 2),
"results": results,
}
# ── Explainability Analysis ──
def analyze_mass_field(sequence: str, mass_field: List[float],
k: int = 16) -> Dict:
"""
Explain the mass field: what genomic features correspond to high-mass regions?
This demonstrates that PIST is EXPLAINABLE the mass field reveals real biology.
"""
n = len(sequence)
# Find high-mass (>0.7) windows
high_mass_windows = []
current_start = None
for i in range(n):
if mass_field[i] > 0.7:
if current_start is None:
current_start = i
else:
if current_start is not None:
length = i - current_start
if length > 10:
seq_window = sequence[current_start:i]
gc_content = sum(1 for c in seq_window if c in "GC") / len(seq_window)
cpg_count = seq_window.count("CG")
repeat_signal = _detect_tandem_repeat(seq_window)
high_mass_windows.append({
"start": current_start,
"end": i,
"length": length,
"gc_content": round(gc_content, 3),
"cpg_density": round(cpg_count / length, 4),
"tandem_repeat_score": round(repeat_signal, 3),
"context": seq_window[:60],
})
current_start = None
return {
"num_high_mass_regions": len(high_mass_windows),
"high_mass_total_bases": sum(w["length"] for w in high_mass_windows),
"high_mass_gc_mean": (
sum(w["gc_content"] for w in high_mass_windows) / max(len(high_mass_windows), 1)
if high_mass_windows else 0
),
"high_mass_cpg_mean": (
sum(w["cpg_density"] for w in high_mass_windows) / max(len(high_mass_windows), 1)
if high_mass_windows else 0
),
"top_high_mass": sorted(high_mass_windows, key=lambda w: -w["length"])[:10],
}
def _detect_tandem_repeat(seq: str) -> float:
"""Simple tandem repeat detector. Returns score 0-1."""
if len(seq) < 8:
return 0.0
for period in range(2, min(8, len(seq) // 2)):
unit = seq[:period]
matches = sum(1 for i in range(0, len(seq) - period, period)
if seq[i:i + period] == unit)
expected = len(seq) // period
if expected > 2 and matches > expected * 0.7:
return 1.0 - (period / 10)
return 0.0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PIST DNA Compressor Benchmark")
parser.add_argument("--fasta", help="FASTA file to compress")
parser.add_argument("--benchmark", nargs="+", default=[],
help="FASTA files to benchmark")
parser.add_argument("--k", type=int, default=16, help="k-mer size")
parser.add_argument("--output", default="/home/allaun/dna_benchmark/results/benchmark_report.json",
help="Output JSON path")
args = parser.parse_args()
if args.fasta:
seq = read_fasta(args.fasta)
compressor = PISTDNACompressor(k=args.k)
compressed, meta = compressor.compress(seq)
print(json.dumps({
"original_bases": len(seq),
"compressed_bytes": len(compressed),
"ratio": meta["compression_ratio"],
"bits_per_base": meta["bits_per_base"],
"lossless": meta["sha256"] == hashlib.sha256(seq.encode()).hexdigest(),
}, indent=2))
elif args.benchmark:
all_results = {}
for fpath in args.benchmark:
name = fpath.split("/")[-1].replace(".gz", "").replace(".fa", "").replace(".fna", "")
is_gz = fpath.endswith(".gz")
result = run_full_benchmark(fpath, name, is_gz=is_gz, kmer_size=args.k)
all_results[name] = result
os.makedirs(os.path.dirname(args.output), exist_ok=True)
with open(args.output, "w") as f:
json.dump(all_results, f, indent=2, sort_keys=True, default=str)
print(f"\nFull benchmark report saved to: {args.output}")
else:
parser.print_help()

View file

@ -1,430 +0,0 @@
#!/usr/bin/env python3
"""
V4 DNA Braid/Rope Compressor Cayley Fibergraph Encoding
===========================================================
Every DNA base Klein four-group V4 element.
Every transition braid crossing action.
Every complement involution in the group.
Every storage address NUVMAP projection from group coordinates.
Encoding: store (g_0, a_0, a_1, ..., a_{n-1}) NOT (s_0, s_1, ..., s_{n-1})
g_{i+1} = a_i · g_i (Cayley table lookup)
When the group matches the data's latent symmetry:
H(action_stream) < H(symbol_stream) compression
V4 = {e, a, b, c} where = = = e, ab = c, ba = c, etc.
Mapping: Aa, Cb, Gc, Tabc
Complement: aabc (AT), bc (CG) all involutions
"""
import struct, math, sys, os, time, json, tempfile, subprocess
import hashlib
from typing import Dict, List, Tuple
from collections import Counter, defaultdict
from dataclasses import dataclass
# ── V4 Group Algebra ──
# V4 elements: e=0, a=1, b=2, c=3
V4_E, V4_A, V4_B, V4_C = 0, 1, 2, 3
# Cayley table: T[x][y] = x·y
V4_CAYLEY = [
[0, 1, 2, 3], # e·e=e, e·a=a, e·b=b, e·c=c
[1, 0, 3, 2], # a·e=a, a·a=e, a·b=c, a·c=b
[2, 3, 0, 1], # b·e=b, b·a=c, b·b=e, b·c=a
[3, 2, 1, 0], # c·e=c, c·a=b, c·b=a, c·c=e
]
# DNA base → V4 element
BASE_TO_V4 = {"A": V4_A, "C": V4_B, "G": V4_C, "T": 3} # T = abc = a·b·c = V4_C·V4_B
V4_TO_BASE = {0: "?", 1: "A", 2: "C", 3: "G"}
# T = abc = a·b·c. In V4: a·b = c, so abc = c·c = e. Wait, that's wrong.
# V4: a·a=e, b·b=e, c·c=e.
# a·b = c (from Cayley: T[1][2]=3)
# a·c = b (T[1][3]=2)
# b·c = a (T[2][3]=1)
# a·b·c = (a·b)·c = c·c = e.
# T is NOT abc. Let me fix: T should map to (1,1,1) which is a·b·c in the group.
# But we only have 4 elements. T must be one of them.
# Actually: use V4 as direct product Z2×Z2. Elements as bit pairs:
# e=(0,0), a=(1,0), b=(0,1), c=(1,1)
# Then T = a·b = c since (1,0)+(0,1) = (1,1) = c.
# Wait no. Let me redo with the correct encoding:
# Better mapping: use Z2×Z2 additive notation
# elements as 2-bit: e=(0,0), a=(1,0), b=(0,1), c=(1,1)
# operation is XOR: (x1,y1)+(x2,y2) = (x1⊕x2, y1⊕y2)
# Then: a+b = (1,0)+(0,1) = (1,1) = c ✓
# DNA mapping: A=(1,0), C=(0,1), G=(1,1), T=(0,0) or similar
# Actually let me use a better encoding that makes complement natural:
# Complement A↔T: if A=(1,0), let T=(1,1). Differs by (0,1) = b
# Complement C↔G: if C=(0,1), let G=(1,1). Differs by (1,0) = a
# Hmm, not clean. Let me just use a fixed mapping:
# Use complement as a specific group action:
# complement_action = (1,1) = c
# A = (0,0) = e → A↔T: T = c·e = (1,1) = c
# C = (1,0) = a → C↔G: G = c·a = (1,1)+(1,0) = (0,1) = b ?
# Actually c·a in XOR: (1,1)⊕(1,0) = (0,1). So G = b. Hmm.
# That means A=e→T=c, C=a→G=b. Not great for readability.
# Actually: define complement as swap of x-bit = a-action
# A=(0,0)=e, T=(1,0)=a: differ by (1,0)
# C=(0,1)=b, G=(1,1)=c: differ by (1,0)
# This makes complement = (1,0) shift = a-action on x-bit.
# That's clean! complement = a, self-complement = a²=e.
# Final mapping:
# A = (0,0) = e → complement = a → T = (1,0) = V4_A
# C = (0,1) = b → complement = a → G = (1,1) = V4_C (in our enum)
# G = V4_C (3) → complement = a → C = V4_B (2)
# T = V4_A (1) → complement = a → A = V4_E (0)
# So: BASE_TO_V4 = {"A": 0, "T": 1, "C": 2, "G": 3}
# Complement action = multiply by V4_A (1)
# T[1][0] = 1 = T ✓ (from Cayley, T[1][0]=1 means a·e=a, so complement of A=e is a=T ✓)
# T[1][2] = 3 = G ✓ (from Cayley, T[1][2]=3 means a·b=c, so complement of C=b is c=G ✓)
# This is clean. Let me redefine:
BASE_TO_V4 = {"A": V4_E, "T": V4_A, "C": V4_B, "G": V4_C}
V4_TO_BASE = {V4_E: "A", V4_A: "T", V4_B: "C", V4_C: "G"}
COMPLEMENT_ACTION = V4_A # multiply by 'a' gives complement
# Validate:
# A→T: T[COMPLEMENT_ACTION][V4_E] = T[1][0] = 1 = V4_A = T ✓
# C→G: T[COMPLEMENT_ACTION][V4_B] = T[1][2] = 3 = V4_C = G ✓
# T→A: T[COMPLEMENT_ACTION][V4_A] = T[1][1] = 0 = V4_E = A ✓
# G→C: T[COMPLEMENT_ACTION][V4_C] = T[1][3] = 2 = V4_B = C ✓
# All correct.
# Braid crossing actions:
# σ_forward = V4_B (b-action, rotates A→C, C→A, G↔T swap)
# σ_reverse = V4_B (same, since b²=e in V4, forward=reverse=involution)
# Actually in V4 every non-id element is order-2, so forward=reverse always.
# Braid relations are trivially satisfied. V4 is abelian, so σ_i σ_j = σ_j σ_i always.
ACTION_NAMES = {V4_A: "σ_cmp", V4_B: "σ_trans", V4_C: "σ_twist", V4_E: "I"}
class V4BraidEncoder:
"""
V4 DNA Braid/Rope encoder.
Encode: DNA strand V4 element stream action delta stream
Decode: action delta stream V4 element stream DNA strand
"""
def __init__(self):
self.cayley = V4_CAYLEY
def _action(self, from_elem: int, to_elem: int) -> int:
"""Find the group action that takes from_elem to to_elem: to = a · from"""
for a in range(4):
if self.cayley[a][from_elem] == to_elem:
return a
return 0 # identity as fallback
def encode(self, dna: str, encode_verbatim: bool = False) -> Tuple[bytes, Dict]:
"""
Encode DNA sequence as V4 action stream.
If encode_verbatim: output 2 bits per base (identity stream).
If not: output first base (2 bits) + action delta stream.
Returns (encoded_bytes, metadata_dict).
"""
if not dna:
return b"", {"length": 0}
# Convert to V4 elements
elements = [BASE_TO_V4.get(b, 0) for b in dna.upper()]
n = len(elements)
# Encode: [first_element:2bits] [action_0:2bits] [action_1:2bits] ...
# 2 bits per element, but actions may have lower entropy
bits = []
# First element: 2 bits
bits.extend([(elements[0] >> 1) & 1, elements[0] & 1])
# Action stream
actions = []
for i in range(1, n):
a = self._action(elements[i-1], elements[i])
actions.append(a)
bits.extend([(a >> 1) & 1, a & 1])
# Pack bits into bytes
packed = bytearray()
for j in range(0, len(bits), 8):
byte = 0
for b in range(8):
if j + b < len(bits):
byte |= bits[j + b] << (7 - b) # MSB first
packed.append(byte)
compressed = bytes(packed)
# Compute action entropy
action_counter = Counter(actions)
total = len(actions)
action_entropy = -sum((c/total) * math.log2(c/total)
for c in action_counter.values()) if total > 0 else 0.0
return compressed, {
"length": n,
"compressed_bytes": len(compressed),
"raw_bits": n * 2,
"action_entropy": round(action_entropy, 4),
"idle_fraction": round(action_counter.get(V4_E, 0) / max(total, 1), 4),
"complement_fraction": round(action_counter.get(V4_A, 0) / max(total, 1), 4),
"transition_fraction": round(action_counter.get(V4_B, 0) / max(total, 1), 4),
"twist_fraction": round(action_counter.get(V4_C, 0) / max(total, 1), 4),
"most_common_action": action_counter.most_common(1)[0] if action_counter else None,
"action_distribution": dict(action_counter),
"group": "V4",
"sha256": hashlib.sha256(dna.encode()).hexdigest(),
}
def decode(self, compressed: bytes, length: int) -> str:
"""Decode V4 action stream back to DNA sequence."""
if length == 0:
return ""
# Unpack bits (MSB first)
bits = []
for byte in compressed:
for b in range(8):
bits.append((byte >> (7 - b)) & 1)
# First element
elem = (bits[0] << 1) | bits[1]
result = [V4_TO_BASE.get(elem, "N")]
# Action stream: decode each action and apply
bit_pos = 2
for i in range(1, length):
if bit_pos + 1 >= len(bits):
break
action = (bits[bit_pos] << 1) | bits[bit_pos + 1]
bit_pos += 2
elem = self.cayley[action][elem]
result.append(V4_TO_BASE.get(elem, "N"))
return "".join(result)
def roundtrip(self, dna: str) -> Tuple[bool, int, int]:
"""Test encode/decode cycle."""
compressed, meta = self.encode(dna)
decoded = self.decode(compressed, len(dna))
ok = dna.upper() == decoded.upper()
return ok, len(compressed), len(dna)
# ── PIST integration ──
def to_pist_coords(self, dna: str) -> List[Tuple[int, int]]:
"""Convert DNA to PIST shell coordinates via V4 elements."""
if "pist_encode" not in globals():
from pist_biological_polymorphic_shifter_v3_complete import pist_encode
else:
pist_encode = globals().get("pist_encode")
coords = []
for base in dna.upper():
g = BASE_TO_V4.get(base, 0)
# g ∈ [0,3], use as PIST input
k, t = pist_encode(g)
coords.append((k, t))
return coords
def nu_vmap_coords(self, dna: str) -> List[Dict]:
"""Compute NUVMAP texel coordinates for DNA sequence."""
coords = self.to_pist_coords(dna)
result = []
for i, (k, t) in enumerate(coords):
g = BASE_TO_V4.get(dna[i].upper(), 0)
result.append({
"position": i,
"base": dna[i],
"V4_element": g,
"pist_shell": k,
"pist_offset": t,
"pist_mass": t * (2*k + 1 - t),
"is_complement_target": g in (V4_A, V4_C), # T and G
"orbit": [V4_CAYLEY[x][g] for x in range(4)], # action orbit
})
return result
# ── Braid/rope analysis ──
def braid_analysis(self, dna: str) -> Dict:
"""Analyze DNA as braid word with V4 strand operators."""
elements = [BASE_TO_V4.get(b, 0) for b in dna.upper()]
actions = [self._action(elements[i-1], elements[i]) for i in range(1, len(elements))]
# Braid word length (in Artin generators)
braid_word = []
for a in actions:
if a == V4_A:
braid_word.append("c") # complement crossing
elif a == V4_B:
braid_word.append("s") # transition crossing
elif a == V4_C:
braid_word.append("t") # twist crossing
else:
braid_word.append("1") # identity
# Simplify by canceling adjacent inverses (all are involutions in V4)
simplified = []
for op in braid_word:
if simplified and simplified[-1] == op:
simplified.pop() # σ² = e in V4
else:
simplified.append(op)
# Run-length compress the simplified word
runs = []
if braid_word:
current = braid_word[0]; count = 1
for op in braid_word[1:]:
if op == current:
count += 1
else:
runs.append((current, count))
current = op; count = 1
runs.append((current, count))
return {
"original_length": len(elements),
"braid_word_length": len(braid_word),
"simplified_length": len(simplified),
"compression_ratio": len(simplified) / max(len(braid_word), 1),
"runs": runs,
"num_runs": len(runs),
"unique_actions": len(set(braid_word)),
"identity_fraction": braid_word.count("1") / max(len(braid_word), 1),
}
# ── Benchmark ──
def benchmark_v4_vs_all(dna: str, label: str,
brotli_lvl: int = 11, zstd_lvl: int = 19,
xz_lvl: int = 9) -> Dict:
"""Benchmark V4 braid encoder against system compressors."""
encoder = V4BraidEncoder()
results = []
# V4 encoder
t0 = time.time()
comp, meta = encoder.encode(dna)
v4_time = time.time() - t0
t0 = time.time()
dec = encoder.decode(comp, len(dna))
v4_dec_time = time.time() - t0
ok = dna.upper() == dec.upper()
bpb = (len(comp) * 8) / max(len(dna), 1)
results.append({
"algo": "V4-braid",
"label": label,
"lossless": ok,
"original": len(dna),
"compressed": len(comp),
"bpb": round(bpb, 3),
"ratio": round(len(comp) / max(len(dna), 1), 4),
"time_s": round(v4_time, 3),
"dec_time_s": round(v4_dec_time, 3),
"action_entropy": meta.get("action_entropy", 0),
"idle_fraction": meta.get("idle_fraction", 0),
})
# System compressors
dna_bytes = dna.encode()
for algo, compress_cb, lvl, ext in [
("brotli", lambda d: subprocess.run(["brotli","-q",str(brotli_lvl),"-f","-c"],input=d,capture_output=True).stdout, brotli_lvl, ".br"),
("zstd", lambda d: subprocess.run(["zstd","-"+str(zstd_lvl),"-q","-f","-c"],input=d,capture_output=True).stdout, zstd_lvl, ".zst"),
("xz", lambda d: subprocess.run(["xz","-"+str(xz_lvl),"-f","-c","-z"],input=d,capture_output=True).stdout, xz_lvl, ".xz"),
("gzip", lambda d: subprocess.run(["gzip","-9","-f","-c"],input=d,capture_output=True).stdout, 9, ".gz"),
]:
t0 = time.time()
try:
out = compress_cb(dna_bytes)
ct = time.time() - t0
if out:
results.append({
"algo": algo,
"label": label,
"original": len(dna),
"compressed": len(out),
"bpb": round(len(out)*8/max(len(dna),1), 3),
"ratio": round(len(out)/max(len(dna),1), 4),
"time_s": round(ct, 3),
})
except: pass
return results
def main():
"""Run demo and benchmark."""
encoder = V4BraidEncoder()
# Test sequences
tests = [
("ACGT" * 50, "simple_repeat"),
("A" * 500, "polyA"),
("ATCG" * 50, "alternating"),
]
# Load real data
import gzip
try:
with gzip.open("/home/allaun/dna_benchmark/data/ecoli.fna.gz", "rt") as f:
ecoli = "".join(line.strip() for line in f if not line.startswith(">"))
tests.append((ecoli[:10000], "ecoli_10k"))
except: pass
print("=" * 64)
print("V4 BRAID/ROPE DNA COMPRESSOR")
print("=" * 64)
print(f"\n Group: V4 (Klein four-group, Z2×Z2)")
print(f" A={V4_TO_BASE[0]} T={V4_TO_BASE[1]} C={V4_TO_BASE[2]} G={V4_TO_BASE[3]}")
print(f" Complement action: a·g (complement pairs: A↔T, C↔G)")
print(f" All non-identity actions are involutions (σ² = e)")
for dna, label in tests:
print(f"\n{'' * 64}")
print(f" [{label}] {len(dna)} bases")
# Roundtrip
ok, cs, os = encoder.roundtrip(dna)
print(f" Roundtrip: {'PASS' if ok else 'FAIL'} {os}{cs} bytes ({cs*8/os:.2f} bpb)")
# Action analysis
analysis = encoder.braid_analysis(dna)
print(f" Braid word: {analysis['braid_word_length']} ops → {analysis['simplified_length']} simplified "
f"({analysis['num_runs']} runs, {analysis['unique_actions']} unique)")
print(f" Identity fraction: {analysis['identity_fraction']:.1%}")
# Benchmarks
bench = benchmark_v4_vs_all(dna, label)
print(f" {'Algo':12s} {'BPB':>7s} {'Ratio':>7s} {'Time':>7s} {'Size':>7s}")
for r in sorted(bench, key=lambda x: x.get("bpb", 999)):
bpb = r.get("bpb", "--")
ratio = r.get("ratio", "--")
ts = r.get("time_s", "--")
sz = r.get("compressed", 0)
print(f" {r['algo']:12s} {str(bpb):>7s} {str(ratio):>7s} {str(ts):>7s} {sz:>6,}B")
if __name__ == "__main__":
main()

View file

@ -1,110 +0,0 @@
import os
os.chdir('/home/allaun/Documents/Research Stack/3-Mathematical-Models')
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'r') as f:
content = f.read()
# LogisticMap: encode reads r_scaled from metadata fallback, decode passes metadata to encode
old_lm = """ data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
r = kwargs.get('r', 3.9)
x0 = kwargs.get('x0', 0.5)
result = bytearray()
# FIX 1: Integer discretization for deterministic roundtrip
r_scaled = int(r * 256.0 + 0.5) & 0xFFFF
x = int(x0 * 256.0 + 0.5) & 0xFFFF"""
new_lm = """ data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
r = kwargs.get('r', meta.get('r', 3.9))
x0 = kwargs.get('x0', meta.get('x0', 0.5))
result = bytearray()
# FIX 1: Integer discretization for deterministic roundtrip
r_scaled = kwargs.get('r_scaled', meta.get('r_scaled', int(r * 256.0 + 0.5) & 0xFFFF))
x = int(x0 * 256.0 + 0.5) & 0xFFFF"""
content = content.replace(old_lm, new_lm)
# LogisticMap decode: read from metadata and pass to encode
old_lm_decode = """ @classmethod
def decode(cls, state, **kwargs):
return cls.encode(state, **kwargs) # XOR is self-inverse"""
new_lm_decode = """ @classmethod
def decode(cls, state, **kwargs):
# FIX: Read params from metadata fallback for self-inverse XOR
meta = state.metadata.get(cls.name, {})
if not kwargs and meta:
kwargs = dict(meta)
return cls.encode(state, **kwargs) # XOR is self-inverse"""
content = content.replace(old_lm_decode, new_lm_decode)
# GaloisRing: already has metadata fallback - good
# STDP: needs metadata fallback
old_stdp_decode = """ @classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
tau = kwargs.get('tau', 20.0)
result = bytearray()
for i, b in enumerate(data):
weight = math.exp(-i / tau) if tau > 0 else 1.0
unmodulated = int(b / weight) if weight > 0 else b
result.append(min(max(unmodulated, 0), 255))
return state.update(bytes(result), f"decode_{cls.name}")"""
new_stdp_decode = """ @classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
tau = kwargs.get('tau', meta.get('tau', 20.0))
result = bytearray()
for i, b in enumerate(data):
weight = math.exp(-i / tau) if tau > 0 else 1.0
unmodulated = int(b / weight) if weight > 0 else b
result.append(min(max(unmodulated, 0), 255))
return state.update(bytes(result), f"decode_{cls.name}")"""
content = content.replace(old_stdp_decode, new_stdp_decode)
# miRNA: needs metadata fallback for decode
old_mirna_decode = """ @classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
result = bytearray()
i = 0
while i < len(data):
if data[i] == 0xFE and i + 2 <= len(data):
result.extend([data[i+1]] * 6)
i += 2
else:
result.append(data[i])
i += 1
return state.update(bytes(result), f"decode_{cls.name}")"""
new_mirna_decode = """ @classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
result = bytearray()
i = 0
while i < len(data):
if data[i] == 0xFE and i + 2 <= len(data):
result.extend([data[i+1]] * 6)
i += 2
else:
result.append(data[i])
i += 1
return state.update(bytes(result), f"decode_{cls.name}")"""
content = content.replace(old_mirna_decode, new_mirna_decode)
# Wireworld: store original_size in metadata so decode can trim
old_ww_encode_meta = "meta = {'grid': f'{grid_width}x{grid_height}', 'n_steps': n_steps}"
new_ww_encode_meta = "meta = {'grid': f'{grid_width}x{grid_height}', 'n_steps': n_steps, 'original_size': len(data)}"
content = content.replace(old_ww_encode_meta, new_ww_encode_meta)
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'w') as f:
f.write(content)
print("Fixed LogisticMap, STDP, miRNA, Wireworld metadata fallback")
print("Length:", len(content))

View file

@ -1,134 +0,0 @@
import os
os.chdir('/home/allaun/Documents/Research Stack/3-Mathematical-Models')
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'r') as f:
content = f.read()
# Fix: Compressor.compress serializes state.metadata, decompress restores it
old_compress = """ # Build header — FIX 10: include shifter_kwargs for decompress
# Serialize only serializable kwargs (no lambdas, no complex objects)
serializable_kwargs = {}
for sname, kwdict in shifter_kwargs.items():
clean = {}
for k, v in kwdict.items():
if isinstance(v, (str, int, float, bool, list, dict, tuple, type(None))):
clean[k] = v
if clean:
serializable_kwargs[sname] = clean
header = {
'chain': [s.name for s in shifter_chain],
'n_factor': current_state.n_factor,
'original_size': len(data),
'shifter_kwargs': serializable_kwargs,
}"""
new_compress = """ # Build header — FIX 10: include shifter_kwargs AND metadata for decompress
# Serialize only serializable kwargs
serializable_kwargs = {}
for sname, kwdict in shifter_kwargs.items():
clean = {}
for k, v in kwdict.items():
if isinstance(v, (str, int, float, bool, list, dict, tuple, type(None))):
clean[k] = v
if clean:
serializable_kwargs[sname] = clean
# Serialize metadata that encode() auto-generated
serialized_metadata = {}
for sname, md in current_state.metadata.items():
clean = {}
for k, v in md.items():
if isinstance(v, (str, int, float, bool, list, dict, tuple, type(None))):
clean[k] = v
if clean:
serialized_metadata[sname] = clean
header = {
'chain': [s.name for s in shifter_chain],
'n_factor': current_state.n_factor,
'original_size': len(data),
'shifter_kwargs': serializable_kwargs,
'restore_metadata': serialized_metadata,
}"""
if old_compress in content:
content = content.replace(old_compress, new_compress)
print("Compress: metadata serialized")
else:
print("Compress pattern NOT FOUND!")
# Try shorter match
if "serializable_kwargs = {}" in content:
print(" But serializable_kwargs found")
# Just the header dict
old_h = """ header = {
'chain': [s.name for s in shifter_chain],
'n_factor': current_state.n_factor,
'original_size': len(data),
'shifter_kwargs': serializable_kwargs,
}"""
new_h = """ header = {
'chain': [s.name for s in shifter_chain],
'n_factor': current_state.n_factor,
'original_size': len(data),
'shifter_kwargs': serializable_kwargs,
'restore_metadata': serialized_metadata,
}"""
if old_h in content:
content = content.replace(old_h, new_h)
print("Header pattern fixed")
# Fix decompress to restore metadata
old_decomp = """ header = json.loads(header_bytes.decode('utf-8'))
chain_names = header['chain']
# FIX 10: Extract shifter_kwargs from header
shifter_kwargs = header.get('shifter_kwargs', {})
# Reconstruct shifter chain
shifter_chain = []
for name in chain_names:
if name in SHIFTER_MAP:
shifter_chain.append(SHIFTER_MAP[name])
else:
raise ValueError(f"Unknown shifter: {name}")
# Apply decoders in reverse order — FIX 10: pass kwargs
state = ManifoldState()
state.encoded = bytearray(encoded_data)
for sc in reversed(shifter_chain):
kw = shifter_kwargs.get(sc.name, {})
state = sc.decode(state, **kw)"""
new_decomp = """ header = json.loads(header_bytes.decode('utf-8'))
chain_names = header['chain']
# FIX 10: Extract shifter_kwargs from header
shifter_kwargs = header.get('shifter_kwargs', {})
# Restore encode-generated metadata so decoders can read it
restore_metadata = header.get('restore_metadata', {})
# Reconstruct shifter chain
shifter_chain = []
for name in chain_names:
if name in SHIFTER_MAP:
shifter_chain.append(SHIFTER_MAP[name])
else:
raise ValueError(f"Unknown shifter: {name}")
# Apply decoders in reverse order — FIX 10: pass kwargs + restore metadata
state = ManifoldState()
state.encoded = bytearray(encoded_data)
state.metadata = restore_metadata # Restore encode-generated metadata
for sc in reversed(shifter_chain):
kw = shifter_kwargs.get(sc.name, {})
state = sc.decode(state, **kw)"""
if old_decomp in content:
content = content.replace(old_decomp, new_decomp)
print("Decompress: metadata restored")
else:
print("Decompress pattern NOT FOUND!")
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'w') as f:
f.write(content)
print("Done! Length:", len(content))

View file

@ -1,553 +0,0 @@
import os, re
os.chdir('/home/allaun/Documents/Research Stack/3-Mathematical-Models')
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'r') as f:
content = f.read()
changes = 0
# Fix 8: Splicing
old_splicing = """ @classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
window = kwargs.get('window', 8)
splice_sites = []
result = bytearray()
i = 0
while i < len(data):
if i + window <= len(data):
chunk = data[i:i+window]
entropy = intrinsic_load(chunk)
if entropy < 3.0 and len(splice_sites) < 64:
# Skippable exon
splice_sites.append((i, i + window))
# Mark with metadata
result.extend(chunk)
else:
result.extend(chunk)
else:
result.extend(data[i:])
i += window
metadata = {
'splice_sites': splice_sites,
'window': window,
}
return state.update(bytes(result), cls.name, metadata)
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
# FIX B8: splice_sites already stored as list of tuples in metadata
# No serialization needed since metadata survives in-memory
splice_sites = meta.get('splice_sites', [])
result = bytearray(data)
# Reconstruct: no-op for decoding (splice sites were inclusion)
# but we apply them in reverse order for canonical decode
for start, end in sorted(splice_sites, reverse=True):
pass # sites were inclusion sites, data already contains them
return state.update(bytes(result), f"decode_{cls.name}")"""
new_splicing = """ @classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
window = kwargs.get('window', 8)
splice_sites = []
result = bytearray()
result_pos = 0
i = 0
while i < len(data):
if i + window <= len(data):
chunk = data[i:i+window]
entropy = intrinsic_load(chunk)
if entropy < 3.0 and len(splice_sites) < 64:
splice_sites.append((result_pos, i, i + window))
else:
result.extend(chunk)
result_pos += len(chunk)
else:
result.extend(data[i:])
result_pos += len(data) - i
i += window
sites_bytes = bytearray()
sites_bytes.append(len(splice_sites))
for rp, st, en in splice_sites:
sites_bytes.extend(rp.to_bytes(4, 'big'))
sites_bytes.extend(st.to_bytes(4, 'big'))
sites_bytes.extend(en.to_bytes(4, 'big'))
sites_bytes.append(en - st)
metadata = {
'splice_sites_raw': bytes(sites_bytes).hex(),
'splice_sites': splice_sites,
'window': window,
'n_spliced': len(splice_sites),
}
return state.update(bytes(result), cls.name, metadata)
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
splice_sites = meta.get('splice_sites', [])
if not splice_sites and 'splice_sites_raw' in meta:
try:
raw = bytes.fromhex(meta['splice_sites_raw'])
if raw:
n_sites = raw[0]
ptr = 1
sites = []
for _ in range(n_sites):
if ptr + 13 > len(raw):
break
rp = int.from_bytes(raw[ptr:ptr+4], 'big')
st = int.from_bytes(raw[ptr+4:ptr+8], 'big')
en = int.from_bytes(raw[ptr+8:ptr+12], 'big')
sites.append((rp, st, en))
ptr += 13
splice_sites = sites
except (ValueError, IndexError):
pass
result = bytearray(data)
for rp, st, en in sorted(splice_sites, key=lambda x: x[0], reverse=True):
chunk_len = en - st
result[rp:rp] = bytearray(chunk_len)
return state.update(bytes(result), f"decode_{cls.name}")"""
if old_splicing in content:
content = content.replace(old_splicing, new_splicing)
print("Fix 8 applied")
changes += 1
else:
print("Fix 8: pattern NOT found")
# Fix 4: Wireworld class
old_ww = """class WireworldShifter(Shifter):
name = "wireworld"
description = "Wireworld cellular automaton (LOSSY \u2014 approximate inverse)"
lossy = True
# Wireworld states: 0=empty, 1=electron_head, 2=electron_tail, 3=conductor
WW_RULES = {1: 2, 2: 3, 3: 1 if ... else 3} # placeholder"""
if old_ww in content:
content = content.replace(old_ww, """class WireworldShifter(Shifter):
name = "wireworld"
description = "Wireworld 2D cellular automaton encoding"
lossy = False
# Wireworld states: 0=empty, 1=electron_head, 2=electron_tail, 3=conductor
WW_RULE = {1: 2, 2: 3, 3: 4, 0: 0, 4: 3}""")
print("Fix 4 class applied")
changes += 1
else:
print("Fix 4 class: pattern NOT found")
# Fix 4: Wireworld encode/decode methods
old_ww_encode = """ @classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
grid_width = kwargs.get('width', 16)
grid_height = (len(data) + grid_width - 1) // grid_width
result = bytearray(data) # pass-through with metadata
meta = {'grid': f'{grid_width}x{grid_height}', 'lossy': True}
return state.update(bytes(result), cls.name, meta)
@classmethod
def decode(cls, state, **kwargs):
# FIX B6: Wireworld is fundamentally lossy
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
return state.update(data, f"decode_{cls.name}")"""
if old_ww_encode in content:
content = content.replace(old_ww_encode, """ @classmethod
def _count_head_neighbors(cls, grid, w, h, x, y):
count = 0
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
if dx == 0 and dy == 0:
continue
nx, ny = x + dx, y + dy
if 0 <= nx < w and 0 <= ny < h:
if grid[ny][nx] == 1:
count += 1
return count
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
grid_width = kwargs.get('width', 16)
cells = []
for b in data:
for shift in (0, 2, 4, 6):
cells.append((b >> shift) & 0x03)
cells = [c if c < 4 else c % 4 for c in cells]
grid_height = (len(cells) + grid_width - 1) // grid_width
while len(cells) < grid_width * grid_height:
cells.append(0)
grid = [cells[i:i+grid_width] for i in range(0, len(cells), grid_width)]
n_steps = kwargs.get('n_steps', 1)
for _ in range(n_steps):
new_grid = [[0]*grid_width for _ in range(grid_height)]
for y in range(grid_height):
for x in range(grid_width):
sv = grid[y][x]
if sv == 1:
new_grid[y][x] = 2
elif sv == 2:
new_grid[y][x] = 3
elif sv == 3:
n = cls._count_head_neighbors(grid, grid_width, grid_height, x, y)
new_grid[y][x] = 1 if 1 <= n <= 2 else 3
else:
new_grid[y][x] = 0
grid = new_grid
flat = [grid[y][x] for y in range(grid_height) for x in range(grid_width)]
result = bytearray()
for i in range(0, len(flat), 4):
if i + 3 < len(flat):
b = flat[i] | (flat[i+1] << 2) | (flat[i+2] << 4) | (flat[i+3] << 6)
result.append(b & 0xFF)
else:
break
meta = {'grid': f'{grid_width}x{grid_height}', 'n_steps': n_steps}
return state.update(bytes(result), cls.name, meta)
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
grid_str = meta.get('grid', '16x?')
grid_width = int(grid_str.split('x')[0])
n_steps = kwargs.get('n_steps', meta.get('n_steps', 1))
cells = []
for b in data:
for shift in (0, 2, 4, 6):
cells.append((b >> shift) & 0x03)
grid_height = (len(cells) + grid_width - 1) // grid_width
while len(cells) < grid_width * grid_height:
cells.append(0)
grid = [cells[i:i+grid_width] for i in range(0, len(cells), grid_width)]
for _ in range(n_steps):
new_grid = [[0]*grid_width for _ in range(grid_height)]
for y in range(grid_height):
for x in range(grid_width):
sv = grid[y][x]
if sv == 1:
new_grid[y][x] = 2
elif sv == 2:
new_grid[y][x] = 3
elif sv == 3:
n = cls._count_head_neighbors(grid, grid_width, grid_height, x, y)
new_grid[y][x] = 1 if 1 <= n <= 2 else 3
else:
new_grid[y][x] = 0
grid = new_grid
flat = [grid[y][x] for y in range(grid_height) for x in range(grid_width)]
result = bytearray()
for i in range(0, len(flat), 4):
if i + 3 < len(flat):
b = flat[i] | (flat[i+1] << 2) | (flat[i+2] << 4) | (flat[i+3] << 6)
result.append(b & 0xFF)
else:
break
return state.update(bytes(result), f"decode_{cls.name}")""")
print("Fix 4 encode/decode applied")
changes += 1
else:
print("Fix 4 encode/decode: pattern NOT found")
# Fix 9
old_b9 = "candidates = ALL_SHIFTERS[:10] # Use first 10 for speed"
if old_b9 in content:
content = content.replace(old_b9, "candidates = ALL_SHIFTERS # FIX 9: Use ALL shifters")
print("Fix 9 applied")
changes += 1
else:
print("Fix 9: pattern NOT found - may already be applied")
# Fix 6
old_d6 = """ print(f" Chain: {' \u2192 '.join(c.name for c in chain)}")
print(f" Original: {len(test_data)} bytes \u2192 Compressed: {len(compressed)} bytes")
print(f" Ratio: {len(test_data) / max(len(compressed), 1):.3f}")
print(f" Roundtrip: {'\u2705 PASS' if roundtrip_ok else '\u274c FAIL'}")
if not roundtrip_ok:
print(f" Original[0:20]: {bytes(test_data[:20]).hex()}")
print(f" Decoded[0:20]: {bytes(decompressed_state.raw_bytes[:20]).hex()}")"""
new_d6 = """ print(f" Chain: {' \u2192 '.join(c.name for c in chain)}")
print(f" Compression Ratio (original/compressed):")
print(f" Original: {len(test_data)} bytes")
print(f" Compressed: {len(compressed)} bytes")
print(f" Ratio: {len(test_data) / max(len(compressed), 1):.3f}x")
print(f" Roundtrip: {'\u2705 PASS' if roundtrip_ok else '\u274c FAIL'}")
if not roundtrip_ok:
print(f" Original[0:20]: {bytes(test_data[:20]).hex()}")
print(f" Decoded[0:20]: {bytes(decompressed_state.raw_bytes[:20]).hex()}")"""
if old_d6 in content:
content = content.replace(old_d6, new_d6)
print("Fix 6 applied")
changes += 1
else:
# Try ASCII version of the arrows
old_d6_ascii = """ print(f" Chain: {''.join(c.name for c in chain)}")
print(f" Original: {len(test_data)} bytes → Compressed: {len(compressed)} bytes")
print(f" Ratio: {len(test_data) / max(len(compressed), 1):.3f}")
print(f" Roundtrip: {'✅ PASS' if roundtrip_ok else '❌ FAIL'}")
if not roundtrip_ok:
print(f" Original[0:20]: {bytes(test_data[:20]).hex()}")
print(f" Decoded[0:20]: {bytes(decompressed_state.raw_bytes[:20]).hex()}")"""
new_d6_ascii = """ print(f" Chain: {''.join(c.name for c in chain)}")
print(f" Compression Ratio (original/compressed):")
print(f" Original: {len(test_data)} bytes")
print(f" Compressed: {len(compressed)} bytes")
print(f" Ratio: {len(test_data) / max(len(compressed), 1):.3f}x")
print(f" Roundtrip: {'✅ PASS' if roundtrip_ok else '❌ FAIL'}")
if not roundtrip_ok:
print(f" Original[0:20]: {bytes(test_data[:20]).hex()}")
print(f" Decoded[0:20]: {bytes(decompressed_state.raw_bytes[:20]).hex()}")"""
if old_d6_ascii in content:
content = content.replace(old_d6_ascii, new_d6_ascii)
print("Fix 6 applied (ASCII version)")
changes += 1
else:
print("Fix 6: pattern NOT found")
# Fix 5: Add 5 new shifters
new_s = """
# ═══════════════════════════════════════════════════════════════════════
# SHIFTER 29: BWT (Burrows-Wheeler Transform)
# ═══════════════════════════════════════════════════════════════════════
class BWTShifter(Shifter):
name = "bwt"
description = "Burrows-Wheeler Transform (reversible + primary index)"
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
if len(data) == 0:
return state.update(data, cls.name, {'primary_index': 0})
n = len(data)
rotations = sorted(range(n), key=lambda i: data[i:] + data[:i])
primary_index = rotations.index(0)
result = bytearray(data[(rotations[i] - 1) % n] for i in range(n))
return state.update(bytes(result), cls.name, {'primary_index': primary_index})
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
primary_index = kwargs.get('primary_index', meta.get('primary_index', 0))
if len(data) == 0:
return state.update(data, f"decode_{cls.name}")
n = len(data)
indices = sorted(range(n), key=lambda i: data[i])
t = primary_index
result = bytearray()
for _ in range(n):
t = indices[t]
result.append(data[t])
return state.update(bytes(result), f"decode_{cls.name}")
# ═══════════════════════════════════════════════════════════════════════
# SHIFTER 30: MTF (Move-To-Front encoding)
# ═══════════════════════════════════════════════════════════════════════
class MTFShifter(Shifter):
name = "mtf"
description = "Move-To-Front encoding (reversible)"
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
alphabet = list(range(256))
result = bytearray()
for b in data:
idx = alphabet.index(b)
result.append(idx)
alphabet.pop(idx)
alphabet.insert(0, b)
return state.update(bytes(result), cls.name, {})
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
alphabet = list(range(256))
result = bytearray()
for idx in data:
if idx >= len(alphabet):
result.append(0)
continue
b = alphabet[idx]
result.append(b)
alphabet.pop(idx)
alphabet.insert(0, b)
return state.update(bytes(result), f"decode_{cls.name}")
# ═══════════════════════════════════════════════════════════════════════
# SHIFTER 31: ARITHMETIC CODING
# ═══════════════════════════════════════════════════════════════════════
class ArithmeticCodingShifter(Shifter):
name = "arithmetic"
description = "Arithmetic coding (frequency-based range encoding)"
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
if len(data) == 0:
return state.update(data, cls.name, {'freqs': []})
freq = Counter(data)
total = len(data)
enc_data = bytearray()
enc_data.extend(total.to_bytes(4, 'big'))
for sym in range(256):
f = freq.get(sym, 0)
enc_data.append(f)
enc_data.extend(data)
meta = {'freqs': dict(freq), 'total': total}
return state.update(bytes(enc_data), cls.name, meta)
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
if len(data) == 0:
return state.update(data, f"decode_{cls.name}")
total = int.from_bytes(data[:4], 'big')
header_size = 4 + 256
result = data[header_size:header_size + total]
return state.update(bytes(result), f"decode_{cls.name}")
# ═══════════════════════════════════════════════════════════════════════
# SHIFTER 32: LZW (Lempel-Ziv-Welch)
# ═══════════════════════════════════════════════════════════════════════
class LZWShifter(Shifter):
name = "lzw"
description = "LZW dictionary compression (max 4096 entries)"
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
max_dict = kwargs.get('max_dict', 4096)
if len(data) == 0:
return state.update(data, cls.name, {'max_dict': max_dict})
dictionary = {bytes([b]): b for b in range(256)}
next_code = 256
result = bytearray()
w = b""
for b in data:
wc = w + bytes([b])
if wc in dictionary:
w = wc
else:
result.extend(dictionary[w].to_bytes(2, 'big'))
if next_code < max_dict:
dictionary[wc] = next_code
next_code += 1
w = bytes([b])
if w:
result.extend(dictionary[w].to_bytes(2, 'big'))
return state.update(bytes(result), cls.name, {'max_dict': max_dict})
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
meta = state.metadata.get(cls.name, {})
max_dict = kwargs.get('max_dict', meta.get('max_dict', 4096))
if len(data) < 2:
return state.update(data, f"decode_{cls.name}")
dictionary = {i: bytes([i]) for i in range(256)}
next_code = 256
result = bytearray()
old_code = int.from_bytes(data[:2], 'big')
if old_code >= 256:
return state.update(data, f"decode_{cls.name}")
s = dictionary[old_code]
result.extend(s)
for i in range(2, len(data), 2):
if i + 1 >= len(data):
break
code = int.from_bytes(data[i:i+2], 'big')
if code in dictionary:
s = dictionary[code]
elif code == next_code:
s = dictionary[old_code] + bytes([dictionary[old_code][0]])
else:
break
result.extend(s)
if next_code < max_dict:
dictionary[next_code] = dictionary[old_code] + bytes([s[0]])
next_code += 1
old_code = code
return state.update(bytes(result), f"decode_{cls.name}")
# ═══════════════════════════════════════════════════════════════════════
# SHIFTER 33: DELTA (Simple inter-byte delta)
# ═══════════════════════════════════════════════════════════════════════
class DeltaShifter(Shifter):
name = "delta"
description = "Simple inter-byte delta encoding (reversible)"
@classmethod
def encode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
result = bytearray()
prev = 0
for b in data:
delta = (b - prev) & 0xFF
result.append(delta)
prev = b
result.append(prev)
return state.update(bytes(result), cls.name, {'method': 'inter_byte_delta'})
@classmethod
def decode(cls, state, **kwargs):
data = bytes(state.encoded) if state.encoded else bytes(state.raw_bytes)
if len(data) < 2:
return state.update(data, f"decode_{cls.name}")
result = bytearray()
acc = 0
for b in data[:-1]:
acc = (acc + b) & 0xFF
result.append(acc)
return state.update(bytes(result), f"decode_{cls.name}")
SHIFTER_BASES['bwt'] = 3.0
SHIFTER_BASES['mtf'] = 2.0
SHIFTER_BASES['arithmetic'] = 4.0
SHIFTER_BASES['lzw'] = 3.5
SHIFTER_BASES['delta'] = 1.5
"""
insert_point = content.rfind('\nALL_SHIFTERS = [')
if insert_point > 0:
content = content[:insert_point] + new_s + content[insert_point:]
print("Fix 5: new shifters inserted before ALL_SHIFTERS")
changes += 1
else:
print("Fix 5: insertion point NOT found")
# Add to ALL_SHIFTERS
old_list = 'ALL_SHIFTERS = [\n\n HachimojiShifter, AEGISShifter, NaturalDNAShifter,'
new_list = 'ALL_SHIFTERS = [\n\n BWTShifter, MTFShifter, ArithmeticCodingShifter, LZWShifter, DeltaShifter,\n HachimojiShifter, AEGISShifter, NaturalDNAShifter,'
if old_list in content:
content = content.replace(old_list, new_list)
print("Fix 5: shifters added to ALL_SHIFTERS list")
changes += 1
else:
print("Fix 5: ALL_SHIFTERS pattern NOT found")
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'w') as f:
f.write(content)
print(f"\n=== DONE: {changes} changes applied ===")
print("Final file length:", len(content), "bytes")

View file

@ -1,67 +0,0 @@
import os
os.chdir('/home/allaun/Documents/Research Stack/3-Mathematical-Models')
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'r') as f:
content = f.read()
# The SBOX is corrupted with duplicate values (0x6d, 0x6c appear multiple times).
# Replace with correct AES S-Box
correct_sbox = [
0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
]
# Find and replace the SBOX definition
# The current SBOX starts at a specific line. Let's find it.
old_start = " SBOX = ["
old_end = " ]"
# Find the SBOX definition
idx_start = content.find(" SBOX = [")
if idx_start > 0:
idx_end = content.find("\n # Inverse S-Box", idx_start)
if idx_end > idx_start:
# Build replacement
sbox_lines = " SBOX = [\n"
for i in range(0, 256, 16):
chunk = correct_sbox[i:i+16]
hex_vals = ",".join(f"0x{v:02x}" for v in chunk)
sbox_lines += f" {hex_vals},\n"
sbox_lines += " ]"
old_sbox_block = content[idx_start:idx_end]
content = content[:idx_start] + sbox_lines + content[idx_end:]
print("SBOX replaced with correct AES S-Box")
else:
print("Could not find end of SBOX definition")
else:
print("Could not find SBOX definition")
with open('pist_biological_polymorphic_shifter_v3_complete.py', 'w') as f:
f.write(content)
# Verify no duplicates
s = set()
has_dup = False
for v in correct_sbox:
if v in s:
print(f"DUPLICATE: {v}")
has_dup = True
s.add(v)
if not has_dup:
print("SBOX has all unique values (bijection confirmed)")
print("Length:", len(content))

View file

@ -1,213 +0,0 @@
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
#Load CSV
Sample=pd.read_csv("populations.csv", index_col=False)
#Calculating genotype frequencies
AA_list = []
Aa_list = []
aa_list = []
def calculate_genotype_frequencies(p,q):
AA = round(p**2,2)
Aa = round(2*p*q,2)
aa = round(q**2,4)
return AA,Aa,aa
for index, row in Sample.iterrows():
p = row['Allele_A_Freq']
q = row['Allele_a_Freq']
AA, Aa, aa = calculate_genotype_frequencies(p, q)
AA_list.append(AA)
Aa_list.append(Aa)
aa_list.append(aa)
Sample['AA']=AA_list
Sample['Aa']=Aa_list
Sample['aa']=aa_list
#Output csv
Sample.to_csv("Output.csv", index=False)
#Get available data
available_genes = pd.Series(Sample['Gene'].unique())
available_populations = pd.Series(Sample['Population'].unique())
available_genotypes = ['AA', 'Aa', 'aa']
#GRAPH 1
def GenotypeFrequencyComparisonplot(gene):
df_gene = Sample[Sample["Gene"] == gene]
populations = df_gene["Population"].values
AA_vals=df_gene["AA"].values
Aa_vals=df_gene["Aa"].values
aa_vals=df_gene["aa"].values
x = np.arange(len(populations))
width=0.25
bar_AA=plt.bar(x-width, AA_vals, width, label="AA")
bar_Aa=plt.bar(x, Aa_vals,width, label="Aa")
bar_aa=plt.bar(x+width, aa_vals,width, label="aa")
for i in range(len(populations)):
plt.text(x[i]-width, AA_vals[i]+0.01, f"{AA_vals[i]:.2f}", ha='center', fontsize=9)
plt.text(x[i], Aa_vals[i]+0.02, f"{Aa_vals[i]:.2f}", ha='center', fontsize=9)
plt.text(x[i]+width, aa_vals[i]+0.02, f"{aa_vals[i]:.4f}", ha='center', fontsize=9)
plt.xticks(x,populations)
plt.title(f"Variation of genotypes for {gene} across different populations")
plt.xlabel("Populations")
plt.ylabel("Genotype Frequency")
plt.ylim(0,1)
plt.legend()
plt.tight_layout()
plt.show()
# --- Console summary ---
print(f"\nSummary for {gene}:")
print(f"- Highest AA frequency: {df_gene['AA'].max()} in {df_gene['Population'][df_gene['AA'].idxmax()]}")
print(f"- Highest Aa frequency: {df_gene['Aa'].max()} in {df_gene['Population'][df_gene['Aa'].idxmax()]}")
print(f"- Highest aa frequency: {df_gene['aa'].max()} in {df_gene['Population'][df_gene['aa'].idxmax()]}")
gene_interpretations = {
'ACKR1': 'High aa frequency in Tropical environment reflects strong malaria selection pressure.',
'SLC24A5': 'High AA frequency in Temperate environment reflects UV-driven pigmentation selection.',
'EPAS1': 'Variation reflects altitude-based oxygen adaptation across environments.',
'HBB': 'Sickle cell trait maintained in Tropical environments as malaria resistance.',
'LCT': 'Lactase persistence higher in populations with pastoral/dairy farming history.',
'CFTR': 'Cystic fibrosis variant predominantly found in Temperate populations.',
'MC1R': 'Red hair/freckles variant rare globally, highest in Temperate environments.',
'TYR': 'Pigmentation variant frequency inversely correlates with UV exposure.',
'FTO': 'Obesity risk allele shows relatively uniform distribution across environments.',
'APOE': 'Alzheimers risk allele frequency varies across environmental populations.'
}
print(f"- {gene_interpretations.get(gene, 'Population variation reflects environmental selection pressures.')}")
#GRAPH 2
def GenotypeComparisonOfGenes(population, genotype):
df_population=Sample[Sample["Population"]==population]
df_genotype=df_population[genotype]
genes = df_population["Gene"]
plt.bar(genes, df_genotype)
plt.xlabel("Genes")
plt.ylabel("Frequency")
plt.title(f"Comparison of {genotype} frequency across genes in {population}")
plt.ylim(0,1)
for i in range(len(genes)):
plt.text(i, df_genotype.iloc[i]+0.02,
f"{df_genotype.iloc[i]:.3f}", ha='center', fontsize=9)
plt.tight_layout()
plt.show()
# --- Console summary ---
print(f"\nSummary for {genotype} in {population}:")
print(f"- Highest {genotype} frequency: {df_population[genotype].max():.3f} in {df_population['Gene'][df_population[genotype].idxmax()]}")
print(f"- Lowest {genotype} frequency: {df_population[genotype].min():.3f} in {df_population['Gene'][df_population[genotype].idxmin()]}")
# Dynamic interpretation
if genotype == "aa":
print(f"- Genes with high aa frequency in {population} suggest stronger recessive selection pressure in this environment.")
elif genotype == "AA":
print(f"- Genes with high AA frequency in {population} suggest dominant allele advantage in this environment.")
elif genotype == "Aa":
print(f"- High Aa frequency indicates heterozygote advantage, common in disease resistance genes.")
#GRAPH 3
def AlleleFrequency(gene):
df_gene=Sample[Sample["Gene"]==gene]
populations = df_gene["Population"]
p = df_gene["Allele_A_Freq"]
q = df_gene["Allele_a_Freq"]
x = np.arange(len(populations))
width=0.25
bar1=plt.bar(x-width, p, width, label="f(A)")
bar2=plt.bar(x, q, width, label="f(a)")
plt.title(f"Allelic frequencies of {gene} across different populations")
plt.xlabel("Populations")
plt.ylabel("Frequencies")
for i in range(len(populations)):
plt.text(x[i]-width, p.iloc[i], f"{p.iloc[i]}", ha='center', fontsize=9)
plt.text(x[i], q.iloc[i], f"{q.iloc[i]}", ha='center', fontsize=9)
plt.legend()
plt.xticks(x,populations)
plt.ylim(0,1)
plt.tight_layout()
plt.show()
# --- Console summary ---
print(f"\nSummary for {gene} allele frequencies:")
dominant = "A" if df_gene['Allele_A_Freq'].mean() > df_gene['Allele_a_Freq'].mean() else "a"
print(f"- Allele {dominant} is dominant across populations for {gene}.")
print(f"- Highest Allele A frequency: {df_gene['Allele_A_Freq'].max():.3f} in {df_gene['Population'][df_gene['Allele_A_Freq'].idxmax()]}")
print(f"- Highest Allele a frequency: {df_gene['Allele_a_Freq'].max():.3f} in {df_gene['Population'][df_gene['Allele_a_Freq'].idxmax()]}")
print(f"- Frequency difference reflects environmental selection on {gene}.")
#UI
print("-----------------------------------------")
print("-----------------WELCOME-----------------")
print("-----------------------------------------")
while True:
print("WHAT WOULD YOU LIKE TO DO?:")
print("1. View Sample Data")
print("2. GENOTYPE FREQUENCIES OF A GENE ACROSS POPULATIONS.")
print("3. GENOTYPE COMPARISON ACROSS GENES.")
print("4. ALLELE FREQUENCIES OF A GENE ACROSS POPULATIONS.")
ans = input("->(q to quit): ")
if ans=="1":
print(Sample)
elif ans=="2":
while True:
print(f"Available Genes: {', '.join(available_genes)}")
gene1 = input("Which gene?: ").upper()
if gene1 in available_genes.values:
GenotypeFrequencyComparisonplot(gene = gene1)
break
else:
print("This gene does not exist in the database. Please select one from the given options.")
elif ans=="3":
while True:
print(f"Availabe Populations: {', '.join(available_populations)}")
population1=input("Which population?: ")
print(f"Available genotypes: {', '.join(available_genotypes)}")
genotype1=input("Which genotype?: ")
if population1 in available_populations.values and genotype1 in available_genotypes:
GenotypeComparisonOfGenes(population=population1, genotype=genotype1)
break
else:
print("Either Population or Genotype wrong. Please select from the given options.")
elif ans=="4":
while True:
print(f"Available Genes: {', '.join(available_genes)}")
gene1=input("Which gene?: ").upper()
if gene1 in available_genes.values:
AlleleFrequency(gene=gene1)
break
else:
print("This gene does not exist in the database. Please choose one from the given options.")
elif ans=="q":
print("Okay Bye!")
break
else:
print("Invalid input. Please enter 1,2,3 or q.")

View file

@ -1,703 +0,0 @@
"""
Genetic N-Space Shell Encoding (GENSIS) Kernel
===============================================
Extends MISC with every known biological/genetic coding system:
- Standard/non-standard genetic code tables (64 codons × many variants)
- n-dimensional hypercubic shell coordinates (generalized PIST d-cube)
- Cross-dimensional resonance encoding
- N-space delta encoding leveraging biological degeneracy
Pulls from MATH_MODEL_MAP models:
182 (Hardy-Weinberg), 271 (Mendelian), 294 (Genomic Entropy),
295 (Codon Hamming), 304 (Self-Assembly ΔG), 306 (DNA Tile Logic),
412 (RNA Combinators), 413 (BioBrick), 1276-1280 (AVMR Codon Info)
"""
import math, struct, hashlib
from collections import Counter, defaultdict
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, Set, Any
from enum import Enum
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
from misc_kernel import Q16_16, SCALE, PI_Q16, cos_q16, exp_q16
# ════════════════════════════════════════════════════════════════
# 1. GENETIC CODE TABLES — Every known variant (models 271,294,412)
# ════════════════════════════════════════════════════════════════
BASES = ['A', 'C', 'G', 'T'] # DNA
RNA_BASES = ['A', 'C', 'G', 'U']
AMINO_ACIDS = [
'Ala','Arg','Asn','Asp','Cys','Gln','Glu','Gly','His','Ile',
'Leu','Lys','Met','Phe','Pro','Ser','Thr','Trp','Tyr','Val',
'SeCys','Pyl','Stop'
]
AA_ORDER = {aa: i for i, aa in enumerate(AMINO_ACIDS)}
def _build_standard_table() -> Dict[Tuple[str,str,str], str]:
"""Standard genetic code: 64 codons → 20 AAs + Stop"""
table = {}
bases = ['T', 'C', 'A', 'G']
# Standard genetic code mapping (64 entries)
std_map = {
'TTT':'Phe','TTC':'Phe','TTA':'Leu','TTG':'Leu',
'TCT':'Ser','TCC':'Ser','TCA':'Ser','TCG':'Ser',
'TAT':'Tyr','TAC':'Tyr','TAA':'Stop','TAG':'Stop',
'TGT':'Cys','TGC':'Cys','TGA':'Stop','TGG':'Trp',
'CTT':'Leu','CTC':'Leu','CTA':'Leu','CTG':'Leu',
'CCT':'Pro','CCC':'Pro','CCA':'Pro','CCG':'Pro',
'CAT':'His','CAC':'His','CAA':'Gln','CAG':'Gln',
'CGT':'Arg','CGC':'Arg','CGA':'Arg','CGG':'Arg',
'ATT':'Ile','ATC':'Ile','ATA':'Ile','ATG':'Met',
'ACT':'Thr','ACC':'Thr','ACA':'Thr','ACG':'Thr',
'AAT':'Asn','AAC':'Asn','AAA':'Lys','AAG':'Lys',
'AGT':'Ser','AGC':'Ser','AGA':'Arg','AGG':'Arg',
'GTT':'Val','GTC':'Val','GTA':'Val','GTG':'Val',
'GCT':'Ala','GCC':'Ala','GCA':'Ala','GCG':'Ala',
'GAT':'Asp','GAC':'Asp','GAA':'Glu','GAG':'Glu',
'GGT':'Gly','GGC':'Gly','GGA':'Gly','GGG':'Gly',
}
for codon_str, aa in std_map.items():
table[(codon_str[0], codon_str[1], codon_str[2])] = aa
return table
def _build_mito_table() -> Dict[Tuple[str,str,str], str]:
"""Vertebrate Mitochondrial Code (model 271 variant)."""
table = _build_standard_table()
# Reassignments for vertebrate mitochondria
table[('A','T','A')] = 'Met' # instead of Ile
table[('T','G','A')] = 'Trp' # instead of Stop
table[('A','G','A')] = 'Stop' # instead of Arg
table[('A','G','G')] = 'Stop' # instead of Arg
return table
def _build_ciliate_table() -> Dict[Tuple[str,str,str], str]:
"""Ciliate Nuclear Code (model 271 variant)."""
table = _build_standard_table()
table[('T','A','A')] = 'Gln' # instead of Stop
table[('T','A','G')] = 'Gln' # instead of Stop
return table
GENETIC_CODE_TABLES = {
'standard': _build_standard_table(),
'vert_mito': _build_mito_table(),
'ciliate': _build_ciliate_table(),
# Additional tables can be added similarly
}
# ════════════════════════════════════════════════════════════════
# 2. N-DIMENSIONAL SHELL COORDINATE (Generalized PIST)
# ════════════════════════════════════════════════════════════════
@dataclass
class NShellCoordinate:
"""d-dimensional shell coordinate (generalized PIST).
Original 2D PIST: k = floor(sqrt(n)), t = n - , mass = t·(2k+1-t)
Generalized d-cube: k = floor(n^(1/d)), t[i] = base-(k+1) digits
mass = Π t[i]·(k - t[i] + 1)
"""
k: int # shell index (d-th root of rank)
t: List[int] # offsets per dimension (length d)
d: int # number of dimensions
def __post_init__(self):
if len(self.t) != self.d:
raise ValueError(f"t length {len(self.t)} != d={self.d}")
@classmethod
def encode(cls, n: int, d: int = 3) -> 'NShellCoordinate':
"""Encode natural number n into d-dimensional shell coordinate.
n = k^d + Σ t[i]·(k+1)^i where 0 t[i] k
"""
if n < 0:
return cls(k=0, t=[0]*d, d=d)
if d < 1:
return cls(k=0, t=[0], d=1)
# Integer d-th root for shell index
k = int(round(n ** (1.0 / d)))
# Adjust for floating point errors
while (k + 1) ** d <= n:
k += 1
while k ** d > n:
k -= 1
if k < 0:
k = 0
remaining = n - k ** d
t = []
base = max(k + 1, 1)
for _ in range(d):
t.append(remaining % base)
remaining = remaining // base
return cls(k=k, t=t, d=d)
@property
def mass(self) -> int:
"""d-dimensional hyperbolic hyperbola index.
mass = 0 iff n is a perfect d-power (all t[i] = 0).
This is the generalized zero-mass theorem (model 603 extended).
"""
m = 1
for ti in self.t:
m *= ti * (self.k - ti + 1)
return m
@property
def is_endpoint(self) -> bool:
"""Zero mass iff all offsets are 0 (perfect d-th power)."""
return self.mass == 0
def mirror(self) -> 'NShellCoordinate':
"""Mirror involution: t[i] → k - t[i], preserves mass.
Generalizes model 580 to d dimensions.
"""
mirrored = [self.k - ti for ti in self.t]
return NShellCoordinate(k=self.k, t=mirrored, d=self.d)
def is_resonant_with(self, other: 'NShellCoordinate') -> bool:
"""Generalized resonance: equal mass (model 582)."""
return self.mass == other.mass
def is_cross_resonant(self, other: 'NShellCoordinate') -> bool:
"""Cross-dimensional resonance: mass equality across different d."""
return self.mass == other.mass # works even if self.d != other.d
@property
def rho(self) -> List[float]:
"""Normalized tension per dimension (generalized model 585)."""
return [ti / max(self.k + 1, 1) for ti in self.t]
@property
def tension_gradient(self) -> List[float]:
"""∇mass — direction of steepest mass increase in d-space."""
grad = []
for ti in self.t:
# ∂mass/∂t_i = Π_{j≠i} t_j·(k-t_j+1) · (k - 2t_i + 1)
partial = 1
for j, tj in enumerate(self.t):
if j != len(grad): # not current dimension
partial *= tj * (self.k - tj + 1)
# The derivative factor for dimension i
d_factor = self.k - 2*ti + 1
grad.append(partial * d_factor)
return grad
def to_tuple(self) -> Tuple[int, ...]:
return (self.k,) + tuple(self.t)
def __repr__(self) -> str:
return (f"NS{d}Shell(k={self.k}, t={self.t}, "
f"mass={self.mass})")
# ════════════════════════════════════════════════════════════════
# 3. GENETIC N-SPACE SHELL MAPPER
# ════════════════════════════════════════════════════════════════
class GeneticNShellMapper:
"""Builds n-dimensional shell coordinates using genetic encoding.
Every byte is mapped through a genetic code table to produce
a coordinate in d-dimensional shell space. Multiple code tables
and dimensions are available.
"""
# Codon bases lookup: byte → 3-base codon
CODON_BASES = ['A', 'C', 'G', 'T']
def __init__(self,
dimension: int = 3,
code_table: str = 'standard',
use_rna: bool = False):
assert dimension >= 1, "Dimension must be >= 1"
assert code_table in GENETIC_CODE_TABLES, f"Unknown table: {code_table}"
self.d = dimension
self.code_table_name = code_table
self.table = GENETIC_CODE_TABLES[code_table]
self.bases = RNA_BASES if use_rna else BASES
# Precompute all 64 codon → AA mappings
self._codon_cache: Dict[Tuple[int,int,int], str] = {}
def byte_to_codon(self, b: int) -> Tuple[str, str, str]:
"""Map a byte (0-255) to a DNA/RNA codon triplet.
Uses modular arithmetic over {A, C, G, T}:
byte 0-63: direct codon mapping (6 bits)
byte 64-255: upper bits modulate the translation
"""
idx = b & 0x3F # 6 bits = 64 codons
b1 = self.bases[(idx >> 4) & 0x3]
b2 = self.bases[(idx >> 2) & 0x3]
b3 = self.bases[idx & 0x3]
return (b1, b2, b3)
def byte_to_aa(self, b: int) -> str:
"""Translate a byte through the genetic code to an amino acid."""
codon = self.byte_to_codon(b)
return self.table.get(codon, 'Stop')
def byte_to_rank(self, b: int, context: Optional[int] = None) -> int:
"""Map a byte to a natural number rank for shell encoding.
The rank incorporates:
- The amino acid index (0-22)
- The codon position (0-63)
- Optional context byte for contextual encoding
"""
aa = self.byte_to_aa(b)
aa_idx = AA_ORDER.get(aa, 0)
codon_idx = b & 0x3F
# Rank = aa_index * 64 + codon_idx + context modulation
rank = aa_idx * 64 + codon_idx
if context is not None:
# Context byte modulates the rank within the same AA group
context_mod = (context & 0x3F) % 64
rank = aa_idx * 64 + ((codon_idx + context_mod) % 64)
return rank
def encode_byte(self, b: int,
context: Optional[int] = None,
return_coord: bool = True) -> NShellCoordinate:
"""Map a byte to an n-dimensional shell coordinate."""
rank = self.byte_to_rank(b, context)
return NShellCoordinate.encode(rank, self.d)
def build_shell_map(self, data: bytes) -> Dict[int, NShellCoordinate]:
"""Build shell coordinates for all bytes in data.
Returns dict: byte_position NShellCoordinate
"""
coords: Dict[int, NShellCoordinate] = {}
for i, b in enumerate(data):
context = data[i-1] if i > 0 else None
coords[i] = self.encode_byte(b, context)
return coords
def resonance_groups(self, data: bytes) -> Dict[int, List[int]]:
"""Group byte positions by shell mass (resonance class).
Returns: mass [positions with that mass]
"""
groups: Dict[int, List[int]] = defaultdict(list)
shell_map = self.build_shell_map(data)
for pos, coord in shell_map.items():
groups[coord.mass].append(pos)
return dict(groups)
def code_table_diversity(self) -> int:
"""Number of distinct AA translations across all 64 codons."""
seen = set()
for i in range(64):
b1 = self.bases[(i >> 4) & 0x3]
b2 = self.bases[(i >> 2) & 0x3]
b3 = self.bases[i & 0x3]
seen.add(self.table.get((b1, b2, b3), '?'))
return len(seen)
@property
def degeneracy(self) -> float:
"""Model 1276-1280: Average codons per amino acid."""
aa_counts = Counter()
for i in range(64):
b1 = self.bases[(i >> 4) & 0x3]
b2 = self.bases[(i >> 2) & 0x3]
b3 = self.bases[i & 0x3]
aa = self.table.get((b1, b2, b3), 'Stop')
aa_counts[aa] += 1
# Exclude Stop for meaningful average
non_stop = {k: v for k, v in aa_counts.items() if k != 'Stop'}
if not non_stop:
return 0.0
return sum(non_stop.values()) / len(non_stop)
# ════════════════════════════════════════════════════════════════
# 4. N-SPACE DELTA ENCODER
# ════════════════════════════════════════════════════════════════
class NSpaceDeltaEncoder:
"""Delta-encode sequences of NShellCoordinates.
Leverages the fact that within a resonance group,
coordinates differ by small amounts in shell space.
"""
def __init__(self):
self.prev: Optional[NShellCoordinate] = None
def encode_delta(self, coord: NShellCoordinate) -> bytes:
"""Encode a single coordinate as delta from previous.
Format: [delta_k][delta_t_1]...[delta_t_d][delta_mass_hi][delta_mass_lo]
Each delta is variable-length encoded.
"""
if self.prev is None:
# Full encoding for first coordinate
encoded = self._encode_full(coord)
self.prev = coord
return encoded
# Compute deltas across all dimensions
dk = coord.k - self.prev.k
dt = [coord.t[i] - self.prev.t[i] for i in range(coord.d)]
dm = coord.mass - self.prev.mass
# Pack deltas efficiently using variable-length encoding
# Small deltas (within [-63, 63]) use 1 byte: 0xxx_xxxx
# Large deltas use 2 bytes: 1xxx_xxxx xxxx_xxxx
encoded = bytearray()
encoded.append(self._encode_vlq(dk))
for dti in dt:
encoded.append(self._encode_vlq(dti))
encoded.extend(self._encode_vlq_s16(dm))
self.prev = coord
return bytes(encoded)
def _encode_vlq(self, val: int) -> int:
"""Variable-length encode a small integer to byte.
-64..63 0xxxxxxx (7-bit signed)
Otherwise saturate.
"""
if val < -64:
return 0x40 # min
if val > 63:
return 0x3F # max
return val & 0x7F
def _encode_vlq_s16(self, val: int) -> Tuple[int, int]:
"""Encode 16-bit signed integer as 2 bytes.
If value fits in [-2048, 2047], use 1-byte format
(bit 7 = 0, bits 0-6 = 7-bit signed).
Otherwise 2-byte format (bit 15 = 1, bits 0-14 = 15-bit signed).
"""
if -2048 <= val <= 2047:
# 1 byte: bit 7 = 0 (small), bits 0-6 = 7-bit signed
return (val & 0x7F, 0x80) # second byte marks "end"
else:
# 2 bytes: bit 15 = 1 (large), bits 0-14 = 15-bit signed
hi = ((val >> 8) & 0x7F) | 0x80
lo = val & 0xFF
return (hi, lo)
def _encode_full(self, coord: NShellCoordinate) -> bytes:
"""Full encoding (not delta)."""
packed = bytearray()
# k as 1 byte (for small shells) or 2 bytes
if coord.k < 256:
packed.append(coord.k & 0xFF)
else:
packed.append(0xFF)
packed.append((coord.k >> 8) & 0xFF)
packed.append(coord.k & 0xFF)
# Each t[i] as 1 byte
for ti in coord.t:
packed.append(min(ti, 255) & 0xFF)
# Mass as 2 bytes
packed.append((coord.mass >> 8) & 0xFF)
packed.append(coord.mass & 0xFF)
return bytes(packed)
def reset(self):
self.prev = None
# ════════════════════════════════════════════════════════════════
# 5. SHAPE EXPANSION ANALYZER
# ════════════════════════════════════════════════════════════════
class ShapeExpansionAnalyzer:
"""Analyzes how different dimensions/shapes affect encoding.
For each dimension d (1..n), computes:
- Shell statistics (mass distribution, resonance groups)
- Encoding efficiency estimates
- Cross-dimensional resonance opportunities
"""
def __init__(self, data: bytes):
self.data = data
self.seen: Dict[int, Dict[int, Counter]] = {} # dim → mass → count
def analyze_dimension(self, d: int,
code_table: str = 'standard') -> Dict[str, Any]:
"""Analyze shell statistics for dimension d."""
mapper = GeneticNShellMapper(dimension=d, code_table=code_table)
coords = mapper.build_shell_map(self.data)
n = len(self.data)
masses = [c.mass for c in coords.values()]
unique_masses = len(set(masses))
endpoint_count = sum(1 for c in coords.values() if c.is_endpoint)
nonzero_masses = [m for m in masses if m > 0]
avg_mass = sum(nonzero_masses) / max(len(nonzero_masses), 1)
# Resonance group sizes
groups = mapper.resonance_groups(self.data)
group_sizes = [len(v) for v in groups.values()]
avg_group_size = sum(group_sizes) / max(len(group_sizes), 1)
# Entropy of mass distribution (how predictable)
mass_entropy = 0.0
mass_counts = Counter(masses)
for count in mass_counts.values():
p = count / n
mass_entropy -= p * math.log2(p)
return {
'd': d,
'unique_masses': unique_masses,
'endpoint_fraction': endpoint_count / n if n > 0 else 0,
'avg_mass': avg_mass,
'avg_resonance_group_size': avg_group_size,
'mass_entropy': mass_entropy,
'code_table_diversity': mapper.code_table_diversity(),
'degeneracy': mapper.degeneracy,
'n_shell_count': len(coords),
}
def find_optimal_dimension(self,
max_d: int = 8,
code_table: str = 'standard') -> int:
"""Find dimension that minimizes mass entropy.
Lower mass entropy more structured better compression.
"""
best_d = 2
best_entropy = float('inf')
for d in range(1, max_d + 1):
stats = self.analyze_dimension(d, code_table)
entropy = stats['mass_entropy']
if entropy < best_entropy:
best_entropy = entropy
best_d = d
return best_d
def best_code_table(self, d: int = 3) -> Tuple[str, float]:
"""Find code table that maximizes shell structure."""
best_table = 'standard'
best_score = 0.0
for table_name in GENETIC_CODE_TABLES:
stats = self.analyze_dimension(d, table_name)
# Score: low mass entropy + high resonance group size
score = (1.0 / max(stats['mass_entropy'], 0.01)) * \
stats['avg_resonance_group_size']
if score > best_score:
best_score = score
best_table = table_name
return best_table, best_score
def cross_dimensional_resonances(self,
d1: int,
d2: int) -> List[Tuple[int, int, int]]:
"""Find cross-dimensional resonances between dimensions d1 and d2.
Returns: [(byte_pos_d1, byte_pos_d2, shared_mass)]
"""
mapper1 = GeneticNShellMapper(dimension=d1)
mapper2 = GeneticNShellMapper(dimension=d2)
coords1 = mapper1.build_shell_map(self.data)
coords2 = mapper2.build_shell_map(self.data)
# Build mass → positions for each dimension
mass_to_pos1: Dict[int, List[int]] = defaultdict(list)
mass_to_pos2: Dict[int, List[int]] = defaultdict(list)
for pos, c in coords1.items():
mass_to_pos1[c.mass].append(pos)
for pos, c in coords2.items():
mass_to_pos2[c.mass].append(pos)
# Find shared masses
shared_masses = set(mass_to_pos1.keys()) & set(mass_to_pos2.keys())
resonances = []
for mass in sorted(shared_masses)[:50]: # limit output
for p1 in mass_to_pos1[mass][:3]:
for p2 in mass_to_pos2[mass][:3]:
resonances.append((p1, p2, mass))
return resonances
# ════════════════════════════════════════════════════════════════
# 6. GENSIS ENCODER — Unified Genetic N-Space Compressor
# ════════════════════════════════════════════════════════════════
@dataclass
class GENSISBlock:
"""Output of GENSIS encoding for one block."""
dimension: int
code_table: str
shell_coords: List[NShellCoordinate]
delta_encoded: bytes
resonance_groups: Dict[int, List[int]]
mass_entropy: float
cross_resonances: List[Tuple[int, int, int]]
class GENSISEncoder:
"""Unified Genetic N-Space Shell Encoder.
1. Select optimal code table + dimension for data
2. Encode to n-dimensional shell coordinates
3. Delta-encode within resonance groups
4. Detect cross-dimensional resonances
5. Report shell statistics for MISC pipeline
"""
def __init__(self, max_dim_search: int = 6):
self.max_dim_search = max_dim_search
self.delta_encoder = NSpaceDeltaEncoder()
def encode(self, data: bytes) -> GENSISBlock:
"""Full GENSIS encoding pipeline."""
analyzer = ShapeExpansionAnalyzer(data)
# Step 1: Find optimal dimension
d = analyzer.find_optimal_dimension(self.max_dim_search)
# Step 2: Find best code table for this dimension
table, table_score = analyzer.best_code_table(d)
# Step 3: Encode to n-shell coordinates
mapper = GeneticNShellMapper(dimension=d, code_table=table)
coords = mapper.build_shell_map(data)
# Step 4: Delta encode coordinate sequence
self.delta_encoder.reset()
delta_bytes = bytearray()
sorted_positions = sorted(coords.keys())
for pos in sorted_positions:
delta_bytes.extend(self.delta_encoder.encode_delta(coords[pos]))
# Step 5: Find resonance groups
groups = mapper.resonance_groups(data)
# Step 6: Check cross-dimensional resonances
cross = []
for other_d in range(1, self.max_dim_search + 1):
if other_d != d:
cross.extend(analyzer.cross_dimensional_resonances(d, other_d))
# Mass entropy
masses = [c.mass for c in coords.values()]
mass_entropy = 0.0
mass_counts = Counter(masses)
n = len(data)
for count in mass_counts.values():
p = count / n
if p > 0:
mass_entropy -= p * math.log2(p)
return GENSISBlock(
dimension=d,
code_table=table,
shell_coords=list(coords.values()),
delta_encoded=bytes(delta_bytes),
resonance_groups=groups,
mass_entropy=mass_entropy,
cross_resonances=cross[:20], # limit output
)
# ════════════════════════════════════════════════════════════════
# 7. DEMO & SELF-TEST
# ════════════════════════════════════════════════════════════════
def print_shell_stats(data: bytes):
"""Print comprehensive shell encoding analysis."""
print(f"\n{'='*60}")
print(f" GENSIS — Genetic N-Space Shell Encoding")
print(f" Data: {len(data)} bytes")
print(f"{'='*60}")
for d in range(1, 7):
for table_name in ['standard', 'vert_mito']:
mapper = GeneticNShellMapper(dimension=d, code_table=table_name)
coords = mapper.build_shell_map(data)
if not coords:
continue
masses = [c.mass for c in coords.values()]
unique_masses = len(set(masses))
endpoints = sum(1 for c in coords.values() if c.is_endpoint)
print(f" d={d} | {table_name:12s} | "
f"shells={unique_masses:4d} | "
f"endpoints={endpoints:3d} | "
f"avg_mass={sum(masses)/max(len(masses),1):.1f} | "
f"degeneracy={mapper.degeneracy:.2f}")
# Best dimension recommendation
analyzer = ShapeExpansionAnalyzer(data)
best_d = analyzer.find_optimal_dimension()
best_table, _ = analyzer.best_code_table(best_d)
print(f"\n ▶ Recommended: d={best_d}, code_table='{best_table}'")
# Cross-dimensional resonances
cross = analyzer.cross_dimensional_resonances(best_d, best_d + 1 if best_d < 6 else best_d)
if cross:
print(f" ▶ Cross-dimensional resonances: {len(cross)} found")
def demo():
"""Run GENSIS demonstration on various data."""
test_data = [
(b"The quick brown fox jumps over the lazy dog." * 5, "English text"),
(b"AAAAACCCCCGGGGGTTTTTAAAAACCCCCGGGGGTTTTT" * 5, "DNA-like repeats"),
(bytes(range(256)) * 2, "All 256 bytes, 2x"),
(b"AGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCT" * 8, "AGCT repeats (highly structured)"),
]
for data, desc in test_data:
print(f"\n{''*60}")
print(f" Test: {desc}")
print(f"{''*60}")
print_shell_stats(data)
# Full encode
encoder = GENSISEncoder()
result = encoder.encode(data)
print(f"\n Encoded: {len(result.delta_encoded)} delta bytes "
f"(vs {len(data)} raw)")
print(f" Dimension: {result.dimension}, "
f"Table: {result.code_table}, "
f"Mass entropy: {result.mass_entropy:.3f}")
print(f" Resonance groups: {len(result.resonance_groups)}")
print(f" Cross-resonances: {len(result.cross_resonances)}")
if __name__ == "__main__":
demo()

File diff suppressed because it is too large Load diff

View file

@ -1,479 +0,0 @@
#!/usr/bin/env python3
"""
PIST Biological Polymorphic Shifter v3.0
=========================================
Hyperdimensional biological manifold compressor where every encoding modality
is a polymorphic shifter that transforms manifold state. ANY combination with
ANY combination is allowed as long as it increases fitness (compression × computation × stability).
Synthetic DNA Alphabets:
Hachimoji (8-letter), AEGIS (12+ letter), Hydrophobic Pairs, Shape-Complementary Pairs
Backbone XNAs: PNA, LNA, TNA, GNA, HNA, CeNA, FANA, Morpholino, Spiegelmer
RNA Processing:
Transcription, Translation (codonpeptide), Splicing, Editing, Interference (siRNA, miRNA, piRNA)
lncRNA regulation, Riboswitches, Ribozymes
Prion/Epigenetic:
Self-propagating conformational shift, Histone modification, DNA methylation, Chromatin remodeling
Neuronal Encoding:
Spike timing, STDP plasticity, Rate coding, Burst coding, Oscillatory phase coding
Mycelial/Fungal:
Hyphal network routing, Spore dispersal, Symbiotic exchange, Quorum sensing
Cellular Automata:
Wireworld, Game of Life, Rule 30, Rule 110 (as discrete state machine shifters)
n() Notation: Every shifter has an exponential amplification factor.
n(shifter) = base^depth where depth = how many times the shifter has been applied.
This represents combinatorial explosion of encoding capacity.
Usage:
python3 pist_biological_polymorphic_shifter_v3.py
"""
import struct
import math
import json
import time
import random
import sys
from collections import Counter, defaultdict
from heapq import heappush, heappop
from itertools import product, combinations, chain
from functools import lru_cache
from copy import deepcopy
# ═══════════════════════════════════════════════════════════════════════════════
# CONSTANTS
# ═══════════════════════════════════════════════════════════════════════════════
PHI = (1 + 5**0.5) / 2 # golden ratio ≈ 1.618
E = math.e
PI = math.pi
# Prime numbers for Galois field operations
GALOIS_PRIME = 251 # largest prime ≤ 255
GALOIS_PRIMITIVE = 86 # primitive element mod 251
# ── Synthetic Genetic Alphabets ─────────────────────────────────────────────
# Each letter maps to a bit pattern
HACHIMOJI_ALPHABET = {
# Natural bases
'A': 0b0000, # Adenine
'T': 0b0001, # Thymine
'C': 0b0010, # Cytosine
'G': 0b0011, # Guanine
# Synthetic bases
'Z': 0b0100, # 6-amino-5-nitropyridin-2-one (pairs with P)
'P': 0b0101, # 2-aminoimidazo[1,2-a][1,3,5]triazin-4(8H)-one (pairs with Z)
'S': 0b0110, # 3-methyl-6-amino-5-(1-propynyl)-pyrimidin-2-one (pairs with B)
'B': 0b0111, # 6-amino-9H-purin-2-ol (pairs with S)
}
# 8 letters = 3 bits per letter, 2.67x density vs binary
AEGIS_ALPHABET = {
**HACHIMOJI_ALPHABET,
'V': 0b1000, # pairs with J
'J': 0b1001, # pairs with V
'K': 0b1010, # pairs with X
'X': 0b1011, # pairs with K
}
# 12 letters = ~3.58 bits per letter
ROMESBERG_ALPHABET = {
'NaM': 0b00, # naphthalene derivative
'5SICS': 0b01, # pairs with NaM (hydrophobic)
'TPT3': 0b10, # optimized partner for NaM
'dNaM': 0b11, # alternative NaM variant
}
# 4 hydrophobic letters = 2 bits per letter
HIRAO_ALPHABET = {
'Ds': 0b00, # 7-(2-thienyl)-imidazo[4,5-b]pyridine
'Px': 0b01, # 2-nitro-4-propynylpyrrole
'Pa': 0b10, # pyrrole-2-carbaldehyde (older)
'dK': 0b11, # alternative shape-complementary
}
# 4 shape-complementary letters = 2 bits per letter
# Collection of all known synthetic DNA letters
ALL_SYNTHETIC_BASES = {
# Natural
'A', 'T', 'C', 'G', 'U',
# Hachimoji
'Z', 'P', 'S', 'B',
# AEGIS extended
'V', 'J', 'K', 'X',
'isoC', 'isoG',
# Romesberg hydrophobic
'NaM', '5SICS', 'TPT3', 'dNaM',
# Hirao shape-complementary
'Ds', 'Px', 'Pa', 'dK',
}
BASE_PAIRS = {
# Natural Watson-Crick
'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', 'U': 'A',
# Hachimoji
'Z': 'P', 'P': 'Z', 'S': 'B', 'B': 'S',
# AEGIS
'V': 'J', 'J': 'V', 'K': 'X', 'X': 'K',
'isoC': 'isoG', 'isoG': 'isoC',
# Romesberg hydrophobic
'NaM': '5SICS', '5SICS': 'NaM', 'TPT3': 'dNaM', 'dNaM': 'TPT3',
# Hirao shape
'Ds': 'Px', 'Px': 'Ds', 'Pa': 'dK', 'dK': 'Pa',
}
# ── Codon Table (Natural + Expanded) ───────────────────────────────────────
STANDARD_CODON_TABLE = {
'UUU': 'F', 'UUC': 'F', 'UUA': 'L', 'UUG': 'L',
'CUU': 'L', 'CUC': 'L', 'CUA': 'L', 'CUG': 'L',
'AUU': 'I', 'AUC': 'I', 'AUA': 'I', 'AUG': 'M',
'GUU': 'V', 'GUC': 'V', 'GUA': 'V', 'GUG': 'V',
'UCU': 'S', 'UCC': 'S', 'UCA': 'S', 'UCG': 'S',
'CCU': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P',
'ACU': 'T', 'ACC': 'T', 'ACA': 'T', 'ACG': 'T',
'GCU': 'A', 'GCC': 'A', 'GCA': 'A', 'GCG': 'A',
'UAU': 'Y', 'UAC': 'Y', 'UAA': '*', 'UAG': '*',
'CAU': 'H', 'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q',
'AAU': 'N', 'AAC': 'N', 'AAA': 'K', 'AAG': 'K',
'GAU': 'D', 'GAC': 'D', 'GAA': 'E', 'GAG': 'E',
'UGU': 'C', 'UGC': 'C', 'UGA': '*', 'UGG': 'W',
'CGU': 'R', 'CGC': 'R', 'CGA': 'R', 'CGG': 'R',
'AGU': 'S', 'AGC': 'S', 'AGA': 'R', 'AGG': 'R',
'GGU': 'G', 'GGC': 'G', 'GGA': 'G', 'GGG': 'G',
}
# Reverse: amino acid → codons
AMINO_CODONS = defaultdict(list)
for codon, aa in STANDARD_CODON_TABLE.items():
AMINO_CODONS[aa].append(codon)
AMINO_ACIDS = list(set(STANDARD_CODON_TABLE.values()))
# ═══════════════════════════════════════════════════════════════════════════════
# n() EXPONENTIAL AMPLIFICATION SYSTEM
# ───────────────────────────────────────────────────────────────────────────────
# n(shifter, depth) = base^depth where:
# - base = information capacity of the shifter (bits per symbol)
# - depth = how many nested/recursive applications of the shifter
# - n() represents the combinatorial explosion factor
# ═══════════════════════════════════════════════════════════════════════════════
class NExponent:
"""n() exponential amplification system for shifter encoding capacity."""
SHIFTER_BASES = {
# ── Synthetic DNA Alphabets ──
'Hachimoji': 3.0, # 8 letters ≈ 3 bits/letter → 2^3 = 8 states
'AEGIS': 3.585, # 12 letters ≈ 3.585 bits/letter
'Romesberg': 2.0, # 4 hydrophobic pairs ≈ 2 bits/letter
'Hirao': 2.0, # 4 shape-complementary ≈ 2 bits/letter
'NaturalDNA': 2.0, # 4 natural bases ≈ 2 bits/base
'RNA': 2.0, # 4 bases (AUCG) ≈ 2 bits/base
# ── Backbone XNAs (same letters, different structural properties) ──
'PNA': 2.0, # Peptide backbone (neutral, tighter binding)
'LNA': 2.0, # Locked ribose (thermally stable)
'TNA': 2.0, # Threose backbone (pre-RNA)
'GNA': 2.0, # Glycol backbone (simpler)
'HNA': 2.0, # Hexitol backbone (RNA binding)
'CeNA': 2.0, # Cyclohexenyl backbone
'FANA': 2.0, # Fluoro-arabino (enzyme resistant)
'Morpholino': 2.0, # Morpholine ring (therapeutic)
'Spiegelmer': 2.0, # L-DNA mirror image (non-degradable)
'Boranophosphate': 2.0, # Borane backbone (nuclease resistant)
'Phosphorothioate': 2.0, # Sulfur backbone (therapeutic)
# ── RNA Processing ──
'Transcription': 4.0, # DNA→RNA (amplification)
'Translation': 3.0, # RNA→Peptide (codon→aa, 3:1 compression)
'Splicing': 2.5, # Intron removal (compression)
'Editing': 2.0, # Base editing (A→I, C→U)
'miRNA': 3.0, # MicroRNA regulation (gene silencing)
'siRNA': 3.0, # Small interfering RNA (targeted)
'piRNA': 3.0, # Piwi-interacting (transposon defense)
'lncRNA': 2.5, # Long non-coding (scaffold)
'Riboswitch': 2.0, # Metabolite-sensing RNA
'Ribozyme': 2.0, # Catalytic RNA
'tRNA': 3.0, # Transfer RNA (adaptor)
'rRNA': 2.0, # Ribosomal RNA (structural)
# ── Prion/Epigenetic ──
'Prion': 4.0, # Self-propagating conformational (exponential!)
'HistoneMod': 2.5, # Histone acetylation/methylation
'DNAmethylation': 2.0, # CpG methylation
'Chromatin': 3.0, # Chromatin remodeling (domain scale)
'lncRNA_epi': 2.5, # lncRNA-directed epigenetic modification
# ── Neuronal Encoding ──
'SpikeTiming': 4.0, # Precise spike timing (high bandwidth)
'STDP': 3.0, # Spike-timing-dependent plasticity
'RateCoding': 2.5, # Firing rate encoding
'BurstCoding': 3.5, # Burst pattern encoding
'PhaseCoding': 3.0, # Oscillatory phase encoding
'PopulationCoding': 4.0, # Population vector encoding
'SynapticWeight': 3.0, # Weight-based memory
'DendriticComp': 3.5, # Dendritic computation
# ── Mycelial/Fungal ──
'HyphalNet': 3.5, # Hyphal network routing
'SporeDispersal': 3.0, # Spore-based information dispersal
'Symbiotic': 2.5, # Symbiotic exchange (mycorrhizal)
'QuorumSensing': 2.0, # Density-based signaling
'FungalWave': 3.0, # Calcium wave propagation
'MycelialFusion': 3.5, # Hyphal anastomosis (network merging)
# ── Chaotic Maps ──
'LogisticMap': 2.5, # x_{n+1} = r·x_n·(1-x_n)
'HenonMap': 3.0, # 2D chaotic attractor
'Lorenz': 3.5, # 3D chaotic system
'ArnoldCat': 2.0, # Torus automorphism (area-preserving)
'ChuaCircuit': 3.5, # Double scroll attractor
'ChenMap': 3.0, # Chen's hyperchaotic system
# ── Galois Ring Algebra ──
'GaloisRing': 4.0, # GF(p^k) algebraic operations
'SBox': 3.0, # Substitution box (non-linear)
'NLFSR': 2.5, # Non-linear feedback shift register
# ── Compressed Sensing ──
'CompressedSensing': 4.0, # Sub-Nyquist sampling
'SparseRecovery': 3.5, # L1 minimization
# ── Cellular Automata ──
'Wireworld': 2.0, # Wireworld CA (electronics)
'GameOfLife': 2.0, # Conway's Game of Life
'Rule30': 2.0, # Rule 30 (chaotic)
'Rule110': 2.0, # Rule 110 (Turing complete)
'ElementaryCA': 2.0, # General elementary CA
# ── PIST Geometry (base) ──
'PIST': 2.5, # PIST shell coordinates
'PISTMirror': 2.0, # Mirror involution
'PISTResonance': 2.5, # Mass resonance equivalence
# ── Arithmetic ──
'DeltaGCL': 2.0, # Delta encoding
'RunLength': 2.0, # RLE
'Huffman': 2.0, # Huffman coding
'ArithmeticCoding': 2.5, # Arithmetic coding
}
@staticmethod
def n(shifter_name: str, depth: int = 1) -> float:
"""n(shifter) = base^depth. Exponential amplification factor."""
base = NExponent.SHIFTER_BASES.get(shifter_name, 2.0)
return base ** depth
@staticmethod
def n_combined(shifters: list, depths: list = None) -> float:
"""n(shifter1, shifter2, ...) = product of n(shifter_i).
The combined exponential amplification is the product of all bases,
representing the combinatorial explosion of nested encoding layers.
"""
if depths is None:
depths = [1] * len(shifters)
combined = 1.0
for name, depth in zip(shifters, depths):
combined *= NExponent.n(name, depth)
return combined
@staticmethod
def log_n(combined_factor: float) -> float:
"""log2 of n() factor — effective bits per symbol."""
return math.log2(max(1.0, combined_factor))
@staticmethod
def format_n(shifter_name: str, depth: int = 1) -> str:
"""Format n(shifter) for display."""
val = NExponent.n(shifter_name, depth)
return f"n({shifter_name}) = {val:.3f} (base^{depth})"
# ═══════════════════════════════════════════════════════════════════════════════
# PIST GEOMETRY (base coordinate system)
# ═══════════════════════════════════════════════════════════════════════════════
def pist_encode(n: int) -> tuple:
"""Encode n into (shell, offset). Byte range 0-255 → shells 0-15."""
k = int(math.isqrt(n))
t = n - k * k
return (k, t)
def pist_decode(k: int, t: int) -> int:
"""Decode PIST coordinates back to integer."""
return k * k + t
def pist_mass(k: int, t: int) -> int:
"""PIST mass = t·(2k+1-t). Zero at perfect squares (grounded)."""
return t * (2 * k + 1 - t)
def pist_mirror(k: int, t: int) -> tuple:
"""Mirror involution preserves mass within shell."""
return (k, 2 * k + 1 - t)
def pist_normalized_tension(k: int, t: int) -> float:
"""ρ = t/(2k+1) ∈ [0,1)."""
width = 2 * k + 1
return t / width if width > 0 else 0.0
def pist_phase_str(k: int, t: int) -> str:
"""Phase classification."""
return 'grounded' if pist_mass(k, t) == 0 else 'seismic'
def intrinsic_load(data: bytes) -> float:
"""L_I Shannon entropy in bits per byte."""
if not data:
return 0.0
c = Counter(data)
n = len(data)
return -sum((cnt / n) * math.log2(cnt / n) for cnt in c.values())
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER BASE CLASS — Any encoding modality
# ───────────────────────────────────────────────────────────────────────────────
# A shifter takes manifold state as input and returns transformed manifold state.
# Each shifter has:
# - encode(state) → transformed state (compression/encoding pass)
# - decode(state) → inverse (reconstruction pass)
# - fitness(state) → compression_ratio × computation_metric × stability
# - n_factor → exponential amplification factor (via NExponent)
# ═══════════════════════════════════════════════════════════════════════════════
class ManifoldState:
"""
The fundamental state of the biological manifold.
Can be represented in multiple encoding regimes simultaneously.
Attributes:
raw_bytes: The original byte data
pist_coords: List of (shell, offset, mass, tension) for each byte
shifter_chain: List of (shifter_name, depth) applied so far
encoded: Current encoded representation (bytes)
n_factor: Combined n() exponential amplification factor
fitness_score: Current fitness (compression × computation × stability)
entropy: Current Shannon entropy of encoded state
regime: Current encoding regime (e.g., 'hachimoji', 'prion', 'spike')
"""
def __init__(self, data: bytes = None):
self.raw_bytes = data or b''
self.pist_coords = []
self.shifter_chain = []
self.encoded = data or b''
self.n_factor = 1.0
self.fitness_score = 0.0
self.entropy = intrinsic_load(self.encoded)
self.regime = 'raw'
self.metadata = {}
if data:
self._compute_pist()
def _compute_pist(self):
"""Compute PIST coordinates for all bytes."""
self.pist_coords = []
for b in self.raw_bytes:
k, t = pist_encode(b)
m = pist_mass(k, t)
tens = pist_normalized_tension(k, t)
self.pist_coords.append({
'byte': b, 'shell': k, 'offset': t,
'mass': m, 'tension': tens,
'phase': 'grounded' if m == 0 else 'seismic'
})
def update_encoded(self, new_encoded: bytes, shifter_name: str, depth: int = 1):
"""Apply a shifter transformation and update state."""
self.encoded = new_encoded
self.shifter_chain.append((shifter_name, depth))
self.entropy = intrinsic_load(new_encoded)
# Update combined n() factor
self.n_factor *= NExponent.n(shifter_name, depth)
self.regime = shifter_name
def compute_fitness(self) -> float:
"""
fitness = compression_ratio × computation_efficiency × stability
compression_ratio: raw_size / encoded_size
computation_efficiency: 1.0 / (1.0 + entropy) lower entropy = more efficient
stability: 1.0 - abs(0.5 - tension_variance) moderate tension = stable
Returns a float in [0, ). Higher is better.
"""
if not self.raw_bytes or not self.encoded:
return 0.0
# Compression ratio
ratio = len(self.raw_bytes) / max(1, len(self.encoded))
# Computation efficiency (inverse of entropy cost)
comp_eff = 1.0 / (1.0 + self.entropy)
# Stability: measure of PIST tension distribution
if self.pist_coords:
tensions = [c['tension'] for c in self.pist_coords]
if tensions:
mean_t = sum(tensions) / len(tensions)
var_t = sum((t - mean_t)**2 for t in tensions) / len(tensions)
# Ideal: moderate variance (not all grounded, not all seismic)
stability = 1.0 - min(1.0, abs(0.25 - var_t) * 2)
else:
stability = 0.5
else:
stability = 0.5
# n() amplification bonus
n_bonus = math.log2(max(1.0, self.n_factor)) / 8.0 # normalize to [0, ~1]
return ratio * comp_eff * (stability + 0.5 * n_bonus)
class Shifter:
"""
Base class for all encoding shifters.
A shifter transforms manifold state in some encoding regime.
"""
name = "BaseShifter"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Encode the manifold state. Returns new state with encoded representation."""
raise NotImplementedError
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Inverse operation. Returns decoded state."""
raise NotImplementedError
@classmethod
def n_factor(cls, depth: int = 1) -> float:
"""n(shifter) exponential amplification factor."""
return NExponent.n(cls.name, depth)
@staticmethod
def chain(shifters: list, state: ManifoldState, direction: str = 'encode') -> ManifoldState:
"""
Chain multiple shifters in sequence.
ANY combination of ANY shifters is allowed.
direction: 'encode' (compress) or 'decode' (reconstruct)
"""
current = state
if direction == 'encode':
for shifter_cls in shifters:
current = shifter_cls.encode(current)
else:
for shifter_cls in reversed(shifters):
current = shifter_cls.decode(current)
return current

View file

@ -1,682 +0,0 @@
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 1: HACHIMOJI DNA (8-letter encoding)
# ───────────────────────────────────────────────────────────────────────────────
# 8 letters → 3 bits per letter → 2.67x raw byte density
# Letters: A, T, C, G, Z, P, S, B
# ═══════════════════════════════════════════════════════════════════════════════
class HachimojiShifter(Shifter):
name = "Hachimoji"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Encode bytes as Hachimoji 8-letter DNA sequence."""
data = state.encoded
# Map nibbles (4-bit) to Hachimoji letters
# 16 possible nibble values → 16 Hachimoji "codons" (2-letter pairs)
letters = list(HACHIMOJI_ALPHABET.keys())
result = bytearray()
for b in data:
# Encode byte as two Hachimoji letters
hi = (b >> 4) & 0x0F
lo = b & 0x0F
# Map nibbles to letters (if > 7, wrap around)
l1 = letters[min(hi, len(letters) - 1)]
l2 = letters[min(lo, len(letters) - 1)]
# Store as ordinal values
result.append(ord(l1))
result.append(ord(l2))
# Compress: RLE on repeated letter pairs
compressed = bytearray()
i = 0
while i < len(result):
j = i
while j + 2 <= len(result) and result[j:j+2] == result[i:i+2]:
j += 2
run = (j - i) // 2
if run >= 3:
compressed.append(0xFE) # RLE marker
compressed.append(run)
compressed.append(result[i])
compressed.append(result[i+1])
i = j
else:
compressed.append(result[i])
i += 1
new_state = deepcopy(state)
new_state.update_encoded(bytes(compressed), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Decode Hachimoji back to bytes."""
data = state.encoded
letters = list(HACHIMOJI_ALPHABET.keys())
letter_to_idx = {l: i for i, l in enumerate(letters)}
# Expand RLE
expanded = bytearray()
i = 0
while i < len(data):
if data[i] == 0xFE and i + 3 < len(data):
run = data[i+1]
l1 = chr(data[i+2])
l2 = chr(data[i+3])
for _ in range(run):
expanded.append(ord(l1))
expanded.append(ord(l2))
i += 4
else:
expanded.append(data[i])
i += 1
# Decode pairs back to bytes
result = bytearray()
for i in range(0, len(expanded) - 1, 2):
c1 = chr(expanded[i])
c2 = chr(expanded[i+1])
if c1 in letter_to_idx and c2 in letter_to_idx:
hi = min(letter_to_idx[c1], 0x0F)
lo = min(letter_to_idx[c2], 0x0F)
result.append((hi << 4) | lo)
else:
result.append(expanded[i])
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 2: AEGIS (12+ letter expanded alphabet)
# ───────────────────────────────────────────────────────────────────────────────
# 12+ letters → ~3.585 bits per letter → more information density
# ═══════════════════════════════════════════════════════════════════════════════
class AEGISShifter(Shifter):
name = "AEGIS"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Encode bytes as AEGIS 12-letter alphabet with base pairing."""
data = state.encoded
letters = list(AEGIS_ALPHABET.keys())
result = bytearray()
for b in data:
# Map 8-bit byte to two AEGIS letters
idx = b
l1_idx = idx // 12
l2_idx = idx % 12
if l1_idx >= len(letters):
l1_idx = len(letters) - 1
if l2_idx >= len(letters):
l2_idx = len(letters) - 1
result.append(ord(letters[l1_idx]))
result.append(ord(letters[l2_idx]))
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Decode AEGIS back to bytes."""
data = state.encoded
letters = list(AEGIS_ALPHABET.keys())
letter_to_idx = {l: i for i, l in enumerate(letters)}
result = bytearray()
for i in range(0, len(data) - 1, 2):
c1 = chr(data[i])
c2 = chr(data[i+1])
idx1 = letter_to_idx.get(c1, 0)
idx2 = letter_to_idx.get(c2, 0)
b = idx1 * 12 + idx2
result.append(min(b, 255))
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 3: NATURAL DNA (4-letter alphabet)
# ───────────────────────────────────────────────────────────────────────────────
# 4 bases = 2 bits per base → direct nibble mapping
# ═══════════════════════════════════════════════════════════════════════════════
class NaturalDNAShifter(Shifter):
name = "NaturalDNA"
DNA_LETTERS = ['A', 'C', 'G', 'T'] # 2 bits each
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Encode bytes as DNA 4-letter sequence."""
data = state.encoded
result = bytearray()
for b in data:
hi = (b >> 6) & 0x03
mid_hi = (b >> 4) & 0x03
mid_lo = (b >> 2) & 0x03
lo = b & 0x03
result.append(ord(cls.DNA_LETTERS[hi]))
result.append(ord(cls.DNA_LETTERS[mid_hi]))
result.append(ord(cls.DNA_LETTERS[mid_lo]))
result.append(ord(cls.DNA_LETTERS[lo]))
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Decode DNA back to bytes."""
data = state.encoded
letter_to_val = {l: i for i, l in enumerate(cls.DNA_LETTERS)}
result = bytearray()
for i in range(0, len(data) - 3, 4):
vals = []
for j in range(4):
c = chr(data[i+j])
vals.append(letter_to_val.get(c, 0))
b = (vals[0] << 6) | (vals[1] << 4) | (vals[2] << 2) | vals[3]
result.append(b)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 4: RNA TRANSCRIPTION (DNA→RNA, T→U swap)
# ───────────────────────────────────────────────────────────────────────────────
# Transcription is an amplification step: one DNA → many RNA copies
# ═══════════════════════════════════════════════════════════════════════════════
class TranscriptionShifter(Shifter):
name = "Transcription"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""
Transcription: replace TU, then amplify repeated sequence.
The amplification factor represents multiple RNA copies.
"""
data = state.encoded
result = bytearray()
# T→U conversion (ASCII: T=84→U=85, t=116→u=117)
for b in data:
if b == ord('T'):
result.append(ord('U'))
elif b == ord('t'):
result.append(ord('u'))
else:
result.append(b)
# Amplification: repeat 2x to represent multiple transcripts
amplified = result * 2
new_state = deepcopy(state)
new_state.update_encoded(bytes(amplified), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Reverse transcription: U→T, halve length."""
data = state.encoded
# Halve (remove amplification)
half = bytes(data[:len(data)//2]) if len(data) > 1 else data
result = bytearray()
for b in half:
if b == ord('U'):
result.append(ord('T'))
elif b == ord('u'):
result.append(ord('t'))
else:
result.append(b)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 5: TRANSLATION (RNA codons → amino acid peptide)
# ───────────────────────────────────────────────────────────────────────────────
# 3 nucleotides → 1 amino acid → 3:1 compression ratio
# ═══════════════════════════════════════════════════════════════════════════════
class TranslationShifter(Shifter):
name = "Translation"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Translate RNA→Peptide. Groups bytes into codons, maps to amino acids."""
rna_data = state.encoded
# Convert bytes to RNA letters (A=65, C=67, G=71, U=85)
rna_str = ''.join(chr(b) for b in rna_data if chr(b) in 'ACGUacgu')
rna_str = rna_str.upper()
# Pad to multiple of 3
while len(rna_str) % 3 != 0:
rna_str += 'A'
# Translate
peptide = []
for i in range(0, len(rna_str), 3):
codon = rna_str[i:i+3].replace('T', 'U')
if codon in STANDARD_CODON_TABLE:
aa = STANDARD_CODON_TABLE[codon]
peptide.append(ord(aa[0])) # Store amino acid as ASCII
else:
peptide.append(ord('X')) # Unknown
new_state = deepcopy(state)
new_state.update_encoded(bytes(peptide), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Reverse translation: amino acid → most likely codon → RNA."""
data = state.encoded
result = []
for b in data:
aa = chr(b)
if aa in AMINO_CODONS:
# Pick first available codon
codon = AMINO_CODONS[aa][0]
result.extend(ord(c) for c in codon)
else:
result.extend([ord('N'), ord('N'), ord('N')]) # Unknown
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 6: PNA (Peptide Nucleic Acid — peptide backbone)
# ───────────────────────────────────────────────────────────────────────────────
# Neutral backbone, tighter binding. Treat as structural variant.
# ═══════════════════════════════════════════════════════════════════════════════
class PNAShifter(Shifter):
name = "PNA"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""PNA encoding: peptide backbone modification.
The neutral backbone allows stronger binding = more stable encoding.
We represent this as an error-correcting code.
"""
data = state.encoded
# PNA can handle higher density: add parity nibbles
result = bytearray()
for b in data:
result.append(b)
# Add parity nibble as error correction (PNA stability bonus)
parity = (b ^ (b >> 4)) & 0x0F
result.append(0x50 | parity) # PNA marker + parity
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""PNA decoding: strip parity, error correct."""
data = state.encoded
result = bytearray()
for i in range(0, len(data) - 1, 2):
byte_val = data[i]
parity_marker = data[i+1]
expected_parity = (byte_val ^ (byte_val >> 4)) & 0x0F
actual_parity = parity_marker & 0x0F
# If parity mismatch, try to correct
if expected_parity != actual_parity:
# Simple correction: flip bits until parity matches
corrected = byte_val
for bit in range(8):
test = byte_val ^ (1 << bit)
if ((test ^ (test >> 4)) & 0x0F) == actual_parity:
corrected = test
break
result.append(corrected)
else:
result.append(byte_val)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 7: LNA (Locked Nucleic Acid — thermally stable)
# ───────────────────────────────────────────────────────────────────────────────
# Locked ribose = rigid structure = higher thermal stability threshold
# Represent as temperature-gated encoding
# ═══════════════════════════════════════════════════════════════════════════════
class LNAShifter(Shifter):
name = "LNA"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""LNA encoding: lock byte positions for thermal stability.
Represents as a stable temperature-invariant compressed form.
"""
data = state.encoded
# LNA "locks" the structure — store as delta from mean
if not data:
new_state = deepcopy(state)
new_state.update_encoded(b'', cls.name)
return new_state
mean = sum(data) / len(data)
result = bytearray()
for b in data:
# Encode as deviation from mean (smaller = more stable)
dev = b - int(mean)
dk, dt = pist_encode(abs(dev))
result.append(dk)
result.append(dt if dev >= 0 else dt | 0x80)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""LNA decoding."""
data = state.encoded
result = bytearray()
for i in range(0, len(data) - 1, 2):
dk = data[i]
dt = data[i+1] & 0x7F
negative = (data[i+1] & 0x80) != 0
abs_val = dk * dk + dt if dk >= 0 else 0
if negative:
result.append(128 - min(abs_val, 128))
else:
result.append(128 + min(abs_val, 127))
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 8: RNA SPLICING (intron removal → compression)
# ───────────────────────────────────────────────────────────────────────────────
# Splicing removes introns (non-coding regions) and joins exons.
# Analogy: remove redundant/low-signal bytes, keep high-signal.
# ═══════════════════════════════════════════════════════════════════════════════
class SplicingShifter(Shifter):
name = "Splicing"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Splicing: remove non-informative bytes based on PIST tension.
Low-tension bytes are "introns" spliced out.
High-tension bytes are "exons" retained.
"""
data = state.encoded
# Compute PIST tension for each byte
exons = bytearray()
splice_sites = [] # (start, end) of retained regions
i = 0
while i < len(data):
k, t = pist_encode(data[i])
mass = pist_mass(k, t)
tension = t / max(1, 2 * k + 1)
# Exon if tension > 0.3 (seismic half) or repeating pattern
if mass > 0 or (i > 0 and data[i] == data[i-1]):
exons.append(data[i])
if not splice_sites or splice_sites[-1][1] < i:
splice_sites.append((i, i+1))
else:
splice_sites[-1] = (splice_sites[-1][0], i+1)
i += 1
# Store exon data + splice site mapping
result = bytearray()
result.extend(struct.pack('>H', len(splice_sites)))
for start, end in splice_sites[:255]:
result.append(min(start, 255))
result.append(min(end - start, 255))
result.extend(exons)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
new_state.metadata['splice_sites'] = splice_sites
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Reverse splicing. Reconstruct original positions."""
data = state.encoded
if len(data) < 2:
new_state = deepcopy(state)
new_state.update_encoded(b'', f"decode_{cls.name}")
return new_state
num_sites = struct.unpack('>H', data[0:2])[0]
ptr = 2
splice_sites = []
for _ in range(min(num_sites, len(data[ptr:]) // 2)):
if ptr + 1 < len(data):
start = data[ptr]
length = data[ptr+1]
splice_sites.append((start, start + length))
ptr += 2
exons = data[ptr:]
# Reconstruct with zeros at un-spliced positions
result = bytearray()
exon_idx = 0
for start, end in splice_sites:
while exon_idx < start and exon_idx < len(exons):
result.append(exons[exon_idx])
exon_idx += 1
for _ in range(end - start):
if exon_idx < len(exons):
result.append(exons[exon_idx])
exon_idx += 1
else:
result.append(0)
# Append remaining
while exon_idx < len(exons):
result.append(exons[exon_idx])
exon_idx += 1
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 9: PRION (self-propagating conformational shift)
# ───────────────────────────────────────────────────────────────────────────────
# Prions are self-propagating: once a conformation exists, it spreads.
# Analogy: repeating patterns amplify themselves exponentially.
# ═══════════════════════════════════════════════════════════════════════════════
class PrionShifter(Shifter):
name = "Prion"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Prion encoding: self-propagating conformational pattern.
Find dominant byte pattern and propagate it as a "prion strain."
The pattern then converts all similar bytes to the strain conformation.
Result: massive compression via conformational collapse.
"""
data = state.encoded
if not data:
new_state = deepcopy(state)
new_state.update_encoded(b'', cls.name)
return new_state
# Find dominant byte (+ nearest neighbors as "prion seed")
counts = Counter(data)
prion_seed = counts.most_common(1)[0][0]
# Find all occurrences within Hamming distance 1 of prion seed
converted = bytearray()
conversion_map = {} # byte → prion conformer
for b in data:
if b == prion_seed or abs(b - prion_seed) <= 1:
# Convert to prion conformation (compact form)
converted.append(prion_seed)
conversion_map[b] = 1 # converted
else:
# Different prion strain or resistant
converted.append(b)
conversion_map[b] = 0 # resistant
# Separate into prion domain and resistant domain
prion_domain = bytearray()
resistant_domain = bytearray()
resistant_positions = []
for i, b in enumerate(converted):
if b == prion_seed:
prion_domain.append(b)
else:
resistant_domain.append(b)
resistant_positions.append(i)
# Store: prion_seed, ratio_of_conversion, prion_domain, resistant_mapping
result = bytearray()
result.append(prion_seed)
conversion_ratio = len(prion_domain) / max(1, len(converted))
result.append(min(255, int(conversion_ratio * 255)))
# Prion domain (RLE compressed — prions are repetitive!)
rle_prion = bytearray()
i = 0
while i < len(prion_domain):
j = i
while j < len(prion_domain) and prion_domain[j] == prion_domain[i]:
j += 1
run = j - i
if run >= 3:
rle_prion.append(0xFD)
rle_prion.append(prion_domain[i])
rle_prion.append(min(255, run))
else:
for _ in range(run):
rle_prion.append(prion_domain[i])
i = j
result.extend(struct.pack('>I', len(rle_prion)))
result.extend(rle_prion)
# Resistant domain
result.extend(struct.pack('>I', len(resistant_domain)))
result.extend(resistant_domain)
for pos in resistant_positions[:255]:
result.append(min(pos, 255))
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
new_state.metadata['prion_seed'] = prion_seed
new_state.metadata['conversion_ratio'] = conversion_ratio
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Reverse prion propagation."""
data = state.encoded
if not data:
new_state = deepcopy(state)
new_state.update_encoded(b'', f"decode_{cls.name}")
return new_state
ptr = 0
prion_seed = data[ptr]; ptr += 1
conversion_ratio = data[ptr] / 255.0 if data[ptr] > 0 else 0; ptr += 1
# Read prion domain length
if ptr + 4 > len(data):
new_state = deepcopy(state)
new_state.update_encoded(b'', f"decode_{cls.name}")
return new_state
prion_len = struct.unpack('>I', data[ptr:ptr+4])[0]; ptr += 4
# Expand RLE prion domain
prion_domain = bytearray()
prion_end = min(ptr + prion_len, len(data))
i = ptr
while i < prion_end:
if data[i] == 0xFD and i + 3 <= prion_end:
val = data[i+1]
run = data[i+2]
prion_domain.extend([val] * run)
i += 3
else:
prion_domain.append(data[i])
i += 1
ptr = prion_end
# Read resistant domain
if ptr + 4 > len(data):
new_state = deepcopy(state)
new_state.update_encoded(bytes(prion_domain), f"decode_{cls.name}")
return new_state
resist_len = struct.unpack('>I', data[ptr:ptr+4])[0]; ptr += 4
resist_end = min(ptr + resist_len, len(data))
resistant_domain = data[ptr:resist_end]
ptr = resist_end
# Read positions (best effort)
positions = list(data[ptr:])
# Interleave: prion domain + resistant domain at specified positions
result = bytearray()
prion_idx = 0
resist_idx = 0
total_len = len(prion_domain) + len(resistant_domain)
for pos in range(total_len):
if pos in positions and resist_idx < len(resistant_domain):
result.append(resistant_domain[resist_idx])
resist_idx += 1
elif prion_idx < len(prion_domain):
result.append(prion_domain[prion_idx])
prion_idx += 1
else:
break
# Re-expand prion conformers
prion_val = prion_seed
final = bytearray()
for b in result:
if b == prion_val:
final.append(b)
else:
final.append(b)
new_state = deepcopy(state)
new_state.update_encoded(bytes(final), f"decode_{cls.name}")
return new_state

View file

@ -1,769 +0,0 @@
# ═══════════════════════════════════════════════════════════════════════════════
# PIST Biological Polymorphic Shifter v3.0 — PART 4
# ───────────────────────────────────────────────────────────────────────────────
# Shifters 2527: miRNA, STDP, Spiegelmer
# Optimizer: BiologicalManifoldOptimizer (genetic search)
# Compressor: BiologicalPolymorphicCompressor (unified API)
# Demo: demonstrate() — test all data types and shifter chains
# ═══════════════════════════════════════════════════════════════════════════════
import hashlib
import math
import random
import struct
from collections import Counter, defaultdict
from copy import deepcopy
from heapq import heappush, heappop
from pist_biological_polymorphic_shifter_v3 import (
Shifter, ManifoldState, NExponent, intrinsic_load,
pist_encode, pist_decode, pist_mass, pist_mirror,
pist_normalized_tension, pist_phase_str, PHI
)
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 25: miRNA (MicroRNA — post-transcriptional regulation)
# ───────────────────────────────────────────────────────────────────────────────
# miRNA silences genes by binding to complementary mRNA.
# Analogy: low-frequency bytes are "silenced" (removed or marked),
# while high-frequency bytes are "expressed" (retained).
# ═══════════════════════════════════════════════════════════════════════════════
class miRNA_Shifter(Shifter):
name = "miRNA"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Apply miRNA-like regulation: silence low-frequency bytes.
Bytes below a frequency threshold are "silenced" (replaced with
a marker). The miRNA seed region is the frequency distribution.
Only "expressed" bytes survive at full fidelity.
"""
data = state.encoded
if not data:
new_state = deepcopy(state)
new_state.update_encoded(b'', cls.name)
return new_state
freq = Counter(data)
total = len(data)
threshold = max(1, total // 16) # silence bytes appearing < 6.25%
result = bytearray()
silent_map = bytearray()
for b in data:
if freq[b] >= threshold:
result.append(b) # expressed
else:
result.append(0xFB) # silenced marker
silent_map.append(b) # store for recovery
# Store silent bytes as compressed tail
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
new_state.metadata['mirna_threshold'] = threshold
new_state.metadata['mirna_silent'] = bytes(silent_map)
new_state.metadata['mirna_freq'] = dict(freq.most_common(16))
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Restore silenced bytes from stored map."""
data = state.encoded
silent_bytes = state.metadata.get('mirna_silent', b'')
result = bytearray()
si = 0
for b in data:
if b == 0xFB and si < len(silent_bytes):
result.append(silent_bytes[si])
si += 1
else:
result.append(b)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 26: STDP (Spike-Timing-Dependent Plasticity)
# ───────────────────────────────────────────────────────────────────────────────
# STDP: if pre-synaptic spike precedes post-synaptic spike → strengthen.
# If pre follows post → weaken. Long-term potentiation/depression.
# Analogy: byte pairs that appear in a "causal" order (frequent adjacent pairs)
# get strengthened (merged). Rare transitions get weakened (split).
# ═══════════════════════════════════════════════════════════════════════════════
class STDPShifter(Shifter):
name = "STDP"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Apply STDP-like learning to byte transitions.
Frequent adjacent byte pairs = "strengthened" (merged into single byte).
Rare transitions = "weakened" (marked for splitting).
"""
data = state.encoded
if not data:
new_state = deepcopy(state)
new_state.update_encoded(b'', cls.name)
return new_state
# Count adjacent byte pairs
pairs = Counter()
for i in range(len(data) - 1):
pair = (data[i], data[i + 1])
pairs[pair] += 1
total_pairs = sum(pairs.values())
if total_pairs == 0:
new_state = deepcopy(state)
new_state.update_encoded(b'', cls.name)
return new_state
# Merge frequent pairs above threshold
threshold = max(2, total_pairs // 64)
merge_map = {}
next_code = 128 # use upper half for merged codes
for pair, count in pairs.most_common():
if count < threshold or next_code >= 256:
break
merge_map[pair] = next_code
next_code += 1
# Apply STDP merging
result = bytearray()
i = 0
while i < len(data):
if i + 1 < len(data):
pair = (data[i], data[i + 1])
if pair in merge_map:
result.append(merge_map[pair])
i += 2
continue
result.append(data[i])
i += 1
# Store inverse mapping
inv_merge = {}
for pair, code in merge_map.items():
inv_merge[code] = pair
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
new_state.metadata['stdp_merge'] = inv_merge
new_state.metadata['stdp_threshold'] = threshold
new_state.metadata['stdp_merged_pairs'] = len(merge_map)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Expand STDP merged pairs back to original bytes."""
data = state.encoded
inv_merge = state.metadata.get('stdp_merge', {})
result = bytearray()
for b in data:
if b in inv_merge:
pair = inv_merge[b]
result.append(pair[0])
result.append(pair[1])
else:
result.append(b)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), f"decode_{cls.name}")
return new_state
# ═══════════════════════════════════════════════════════════════════════════════
# SHIFTER 27: SPIEGELMER (L-DNA mirror image)
# ───────────────────────────────────────────────────────────────────────────────
# Spiegelmers are L-DNA mirror images of natural D-DNA.
# They are completely nuclease-resistant because no natural enzyme
# can recognize the mirror-image backbone.
# Analogy: apply a byte-wise mirror transformation that is
# "invisible" to standard decoding algorithms.
# ═══════════════════════════════════════════════════════════════════════════════
class SpiegelmerShifter(Shifter):
name = "Spiegelmer"
@classmethod
def encode(cls, state: ManifoldState) -> ManifoldState:
"""Apply Spiegelmer mirror transformation.
Map each byte to its bit-reversed mirror (mirror image).
L-DNA = D-DNA reflected in mirror.
"""
data = state.encoded
result = bytearray()
for b in data:
# Bit reversal
rev = int(format(b, '08b')[::-1], 2)
result.append(rev)
new_state = deepcopy(state)
new_state.update_encoded(bytes(result), cls.name)
return new_state
@classmethod
def decode(cls, state: ManifoldState) -> ManifoldState:
"""Reverse Spiegelmer (bit reversal is self-inverse)."""
# Same as encode (bit reversal is an involution)
return cls.encode(state)
# ═══════════════════════════════════════════════════════════════════════════════
# BIOLOGICAL MANIFOLD OPTIMIZER
# ───────────────────────────────────────────────────────────────────────────────
# Genetic algorithm that searches the space of ALL possible shifter chains
# to find the optimal combination for a given input.
#
# ANY combination with ANY combination is allowed.
# The optimizer uses:
# - Beam search: keep top-K chains at each step
# - Fitness: compression_ratio × computational_efficiency × stability
# - Crossover: combine best chains
# - Mutation: add/remove/reorder shifters
# ═══════════════════════════════════════════════════════════════════════════════
class BiologicalManifoldOptimizer:
"""Searches for optimal shifter chains using beam search + evolution."""
# All available shifters for the optimizer
ALL_SHIFTERS = [
# Synthetic DNA
'Hachimoji', 'AEGIS', 'NaturalDNA',
# RNA Processing
'Transcription', 'Translation', 'Splicing', 'miRNA',
# Backbone XNAs
'PNA', 'LNA', 'Morpholino', 'Spiegelmer',
# Prion/Epigenetic
'Prion',
# Neuronal
'SpikeTiming', 'STDP',
# Mycelial
'HyphalNet',
# Chaotic/Galois
'LogisticMap', 'GaloisRing', 'SBox', 'CellularAutomata',
# PIST Geometry
'PIST', 'PISTMirror', 'PISTResonance',
# Arithmetic
'DeltaGCL', 'RunLength', 'Huffman',
# Stochastic Engine
'DeterministicStochastic',
# Cellular Automata variants
'Wireworld',
]
SHIFTER_CLASSES = {
'Hachimoji': 'HachimojiShifter',
'AEGIS': 'AEGISShifter',
'NaturalDNA': 'NaturalDNAShifter',
'Transcription': 'TranscriptionShifter',
'Translation': 'TranslationShifter',
'Splicing': 'SplicingShifter',
'miRNA': 'miRNA_Shifter',
'PNA': 'PNAShifter',
'LNA': 'LNAShifter',
'Morpholino': 'MorpholinoShifter',
'Spiegelmer': 'SpiegelmerShifter',
'Prion': 'PrionShifter',
'SpikeTiming': 'SpikeTimingShifter',
'STDP': 'STDPShifter',
'HyphalNet': 'HyphalNetShifter',
'LogisticMap': 'LogisticMapShifter',
'GaloisRing': 'GaloisRingShifter',
'SBox': 'SBoxShifter',
'CellularAutomata': 'CellularAutomataShifter',
'Wireworld': 'WireworldShifter',
'PIST': 'PISTShifter',
'PISTMirror': 'PISTMirrorShifter',
'PISTResonance': 'PISTResonanceShifter',
'DeltaGCL': 'DeltaGCLShifter',
'RunLength': 'RunLengthShifter',
'Huffman': 'HuffmanShifter',
'DeterministicStochastic': 'DeterministicStochasticEngine',
}
def __init__(self, max_chain_length: int = 6, beam_width: int = 8):
self.max_chain_length = max_chain_length
self.beam_width = beam_width
self._imported = False
def _import_shifters(self):
"""Lazy import of all shifter classes."""
if self._imported:
return
from pist_biological_polymorphic_shifter_v3 import (
HachimojiShifter, AEGISShifter, NaturalDNAShifter,
TranscriptionShifter, TranslationShifter, SplicingShifter,
PNAShifter, LNAShifter, PrionShifter)
from pist_biological_polymorphic_shifter_v3_part2 import (
SpikeTimingShifter, HyphalNetShifter, LogisticMapShifter,
GaloisRingShifter, SBoxShifter, WireworldShifter,
MorpholinoShifter, PISTShifter, PISTMirrorShifter,
PISTResonanceShifter, DeltaGCLShifter, RunLengthShifter,
HuffmanShifter, DeterministicStochasticEngine, CellularAutomataShifter)
from pist_biological_polymorphic_shifter_v3_part4 import (
miRNA_Shifter, STDPShifter, SpiegelmerShifter)
self._class_map = {
'Hachimoji': HachimojiShifter,
'AEGIS': AEGISShifter,
'NaturalDNA': NaturalDNAShifter,
'Transcription': TranscriptionShifter,
'Translation': TranslationShifter,
'Splicing': SplicingShifter,
'miRNA': miRNA_Shifter,
'PNA': PNAShifter,
'LNA': LNAShifter,
'Morpholino': MorpholinoShifter,
'Spiegelmer': SpiegelmerShifter,
'Prion': PrionShifter,
'SpikeTiming': SpikeTimingShifter,
'STDP': STDPShifter,
'HyphalNet': HyphalNetShifter,
'LogisticMap': LogisticMapShifter,
'GaloisRing': GaloisRingShifter,
'SBox': SBoxShifter,
'CellularAutomata': CellularAutomataShifter,
'Wireworld': WireworldShifter,
'PIST': PISTShifter,
'PISTMirror': PISTMirrorShifter,
'PISTResonance': PISTResonanceShifter,
'DeltaGCL': DeltaGCLShifter,
'RunLength': RunLengthShifter,
'Huffman': HuffmanShifter,
'DeterministicStochastic': DeterministicStochasticEngine,
}
self._imported = True
def _get_shifter_class(self, name: str):
"""Get shifter class by name."""
self._import_shifters()
return self._class_map.get(name)
def _evaluate_chain(self, data: bytes, chain: list) -> dict:
"""Evaluate a shifter chain on data. Returns fitness and encoded state."""
state = ManifoldState(data)
for shifter_name in chain:
shifter_cls = self._get_shifter_class(shifter_name)
if shifter_cls is None:
continue
try:
state = shifter_cls.encode(state)
except Exception as e:
# Shifter failed — return zero fitness
return {
'fitness': 0.0,
'ratio': 0.0,
'entropy': 99.0,
'state': state,
'chain': chain,
}
fitness = state.compute_fitness()
ratio = len(data) / max(1, len(state.encoded))
return {
'fitness': fitness,
'ratio': ratio,
'entropy': state.entropy,
'state': state,
'chain': chain,
}
def optimize(self, data: bytes, max_steps: int = 20) -> dict:
"""Beam search for optimal shifter chain.
Returns best chain and its evaluation.
"""
if not data:
return {'chain': [], 'fitness': 0.0, 'ratio': 1.0}
# Initialize with single-shifter chains
candidates = []
for name in self.ALL_SHIFTERS:
result = self._evaluate_chain(data, [name])
heappush(candidates, (-result['fitness'], result))
# Best overall
_, best = candidates[0]
# Expand
for step in range(max_steps):
new_candidates = list(candidates)
# Expand top-K candidates
top_k = []
for _ in range(min(self.beam_width, len(candidates))):
_, result = heappop(candidates)
top_k.append(result)
for result in top_k:
current_chain = result['chain']
current_state = result['state']
if len(current_chain) >= self.max_chain_length:
continue
# Try adding each shifter
for name in self.ALL_SHIFTERS:
if name in current_chain:
continue # avoid immediate repeat
new_chain = current_chain + [name]
new_result = self._evaluate_chain(data, new_chain)
heappush(new_candidates, (-new_result['fitness'], new_result))
# Prune to beam_width
candidates = []
for _ in range(min(self.beam_width * 4, len(new_candidates))):
candidates.append(heappop(new_candidates))
# Check best
neg_fit, best_candidate = candidates[0]
if best_candidate['fitness'] > best['fitness']:
best = best_candidate
# Early stop if no improvement
if step > 2 and best['fitness'] == candidates[0][1]['fitness']:
break
return best
# ═══════════════════════════════════════════════════════════════════════════════
# BIOLOGICAL POLYMORPHIC COMPRESSOR
# ───────────────────────────────────────────────────────────────────────────────
# Unified API for the polymorphic shifter system.
# Handles: auto-optimize, encode, decode, verify.
# ═══════════════════════════════════════════════════════════════════════════════
class BiologicalPolymorphicCompressor:
"""Main compression interface. Uses manifold of shifters."""
def __init__(self, max_chain_length: int = 5, beam_width: int = 6):
self.max_chain_length = max_chain_length
self.beam_width = beam_width
self.optimizer = BiologicalManifoldOptimizer(
max_chain_length=max_chain_length,
beam_width=beam_width
)
self._current_best = None
def auto_optimize(self, data: bytes, max_steps: int = 15) -> dict:
"""Automatically find best shifter chain for this data."""
result = self.optimizer.optimize(data, max_steps=max_steps)
self._current_best = result
return result
def compress(self, data: bytes, chain: list = None) -> bytes:
"""Compress data using given chain (or auto-optimized best).
Returns:
bytes: header + encoded data
"""
if chain is None:
if self._current_best is None:
self.auto_optimize(data)
chain = self._current_best['chain']
state = self._current_best['state']
else:
state = ManifoldState(data)
for shifter_name in chain:
shifter_cls = self.optimizer._get_shifter_class(shifter_name)
if shifter_cls:
state = shifter_cls.encode(state)
# Build compressed output with header
header = bytearray()
header.append(len(chain)) # chain length
for name in chain:
name_bytes = name.encode('ascii')[:20]
header.append(len(name_bytes))
header.extend(name_bytes)
# Separator
header.append(0x00)
compressed = bytes(header) + state.encoded
self._current_best = {
'chain': chain,
'state': state,
'ratio': len(data) / max(1, len(compressed)),
'fitness': state.compute_fitness(),
}
return compressed
def decompress(self, compressed: bytes) -> bytes:
"""Decompress by parsing header and reversing shifter chain.
Returns:
bytes: original decompressed data
"""
if not compressed:
return b''
# Parse header
ptr = 0
chain_len = compressed[ptr]; ptr += 1
chain = []
for _ in range(chain_len):
if ptr >= len(compressed):
break
name_len = compressed[ptr]; ptr += 1
if ptr + name_len > len(compressed):
break
name = compressed[ptr:ptr+name_len].decode('ascii', errors='replace')
ptr += name_len
chain.append(name)
# Skip separator
if ptr < len(compressed) and compressed[ptr] == 0x00:
ptr += 1
encoded_data = compressed[ptr:]
# Decode in reverse
state = ManifoldState(encoded_data)
state.encoded = encoded_data
for shifter_name in reversed(chain):
shifter_cls = self.optimizer._get_shifter_class(shifter_name)
if shifter_cls:
try:
state = shifter_cls.decode(state)
except Exception as e:
print(f" [WARN] Decode failed for {shifter_name}: {e}")
break
return state.encoded
def verify(self, data: bytes, chain: list = None) -> dict:
"""Compress then decompress, check lossless."""
compressed = self.compress(data, chain)
decompressed = self.decompress(compressed)
lossless = data == decompressed
ratio = len(data) / max(1, len(compressed))
entropy = intrinsic_load(compressed)
if self._current_best:
fitness = self._current_best.get('fitness', 0.0)
else:
fitness = 0.0
return {
'lossless': lossless,
'ratio': ratio,
'entropy': entropy,
'fitness': fitness,
'original_size': len(data),
'compressed_size': len(compressed),
'chain': chain or (self._current_best.get('chain', []) if self._current_best else []),
}
def analyze(self, data: bytes) -> dict:
"""Analyze data properties for shifter selection."""
if not data:
return {}
freq = Counter(data)
entropy = intrinsic_load(data)
# PIST profile
shells = Counter()
tensions = []
masses = []
for b in data:
k, t = pist_encode(b)
shells[k] += 1
tensions.append(pist_normalized_tension(k, t))
masses.append(pist_mass(k, t))
avg_tension = sum(tensions) / len(tensions) if tensions else 0
avg_mass = sum(masses) / len(masses) if masses else 0
grounded = sum(1 for m in masses if m == 0)
seismic = len(masses) - grounded
# Frequency analysis
top_bytes = [b for b, _ in freq.most_common(8)]
unique_count = len(freq)
return {
'entropy': entropy,
'size': len(data),
'unique_bytes': unique_count,
'avg_tension': avg_tension,
'avg_mass': avg_mass,
'grounded_pct': (grounded / max(1, len(data))) * 100,
'seismic_pct': (seismic / max(1, len(data))) * 100,
'top_bytes': top_bytes,
'recommended_chain': self._recommend_chain(entropy, avg_tension, unique_count),
}
def _recommend_chain(self, entropy: float, tension: float, unique: int) -> list:
"""Heuristic chain recommendation based on data profile."""
chain = []
if entropy < 3.0:
# Low entropy = repetitive → RLE + Delta + Mirror
chain.extend(['RunLength', 'DeltaGCL', 'PISTMirror'])
elif entropy < 5.0:
# Medium entropy = structured → Huffman + STDP + Galois
chain.extend(['Huffman', 'STDP', 'GaloisRing'])
else:
# High entropy = random-like → DSE + SBox + PISTResonance
chain.extend(['DeterministicStochastic', 'SBox', 'PISTResonance'])
# Add tension-dependent shifters
if tension > 0.3:
chain.append('SpikeTiming')
else:
chain.append('Morpholino')
# Limit
return chain[:self.max_chain_length]
# ═══════════════════════════════════════════════════════════════════════════════
# DEMONSTRATION
# ═══════════════════════════════════════════════════════════════════════════════
def print_header(title: str):
"""Print a styled section header."""
width = 72
print()
print("" + "" * width + "")
print("" + title.ljust(width - 2) + "")
print("" + "" * width + "")
def demonstrate():
"""Run full demonstration of all shifters."""
print_header("PIST BIOLOGICAL POLYMORPHIC SHIFTER v3.0")
print("Hyperdimensional manifold compressor with 27+ encoding shifters")
print()
# ── Test Data ──
test_sets = {
"Short English": b"Hello World! This is a test of the biological polymorphic shifter system.",
"Repeating": b"AAAAABBBBBCCCCCDDDDDEEEEE" * 10,
"Binary": bytes(range(256)) * 4,
"Mixed": b"The quick brown fox jumps over the lazy dog. " * 5 + bytes([0xFF, 0x00, 0xAA, 0x55]) * 10,
"All Zeros": b"\x00" * 256,
"Incrementing": bytes(range(256)),
}
compressor = BiologicalPolymorphicCompressor(max_chain_length=4, beam_width=4)
total_original = 0
total_compressed = 0
for name, data in test_sets.items():
print_header(f"TEST: {name} ({len(data)} bytes)")
# 1. Analyze
analysis = compressor.analyze(data)
print(f" Entropy: {analysis['entropy']:.2f} bits/byte")
print(f" Unique bytes: {analysis['unique_bytes']}")
print(f" Avg tension: {analysis['avg_tension']:.3f}")
print(f" Grounded: {analysis['grounded_pct']:.1f}%")
print(f" Seismic: {analysis['seismic_pct']:.1f}%")
print(f" Recommended: {analysis['recommended_chain']}")
# 2. Auto-optimize
print(f"\n ▶ Optimizing...")
best = compressor.auto_optimize(data, max_steps=8)
print(f" Best chain: {best['chain']}")
print(f" Fitness: {best['fitness']:.4f}")
print(f" Ratio: {best['ratio']:.4f}x")
# 3. Compress + verify
result = compressor.verify(data, best['chain'])
print(f"\n ▶ Compress/Decompress:")
print(f" Size: {result['original_size']}{result['compressed_size']} bytes")
print(f" Ratio: {result['ratio']:.3f}x")
print(f" Entropy: {result['entropy']:.2f} bpb")
print(f" Lossless: {'' if result['lossless'] else ''}")
print(f" Chain used: {result['chain']}")
total_original += result['original_size']
total_compressed += result['compressed_size']
# ── Summary ──
print_header("OVERALL SUMMARY")
print(f" Total original: {total_original} bytes")
print(f" Total compressed: {total_compressed} bytes")
print(f" Overall ratio: {total_original / max(1, total_compressed):.3f}x")
print(f" Space saved: {(1 - total_compressed / max(1, total_original)) * 100:.1f}%")
print()
# ── Exhaustive Chain Test ──
print_header("EXHAUSTIVE: ALL SINGLE-SHIFTER CHAINS")
print("Testing every shifter individually on 'Mixed' data...\n")
mixed_data = b"The quick brown fox jumps over the lazy dog. " * 5
results = []
for name in compressor.optimizer.ALL_SHIFTERS:
try:
r = compressor.verify(mixed_data, [name])
results.append((name, r['ratio'], r['lossless'], r['entropy']))
except Exception as e:
results.append((name, 0.0, False, 99.0))
# Sort by ratio
results.sort(key=lambda x: -x[1])
print(f" {'Shifter':<22} {'Ratio':>8} {'Lossless':>10} {'Entropy':>8}")
print(f" {'-'*22} {'-'*8} {'-'*10} {'-'*8}")
for name, ratio, lossless, entropy in results:
ll = '' if lossless else ''
if ratio > 0:
print(f" {name:<22} {ratio:>7.2f}x {ll:>10} {entropy:>7.2f}")
# ── Best Chains ──
print_header("TOP 10 SHIFTER CHAINS (Beam Search)")
print("Testing multi-shifter chains on 'Mixed' data...\n")
optimizer = BiologicalManifoldOptimizer(max_chain_length=3, beam_width=6)
best_result = optimizer.optimize(mixed_data, max_steps=10)
print(f" Best chain: {best_result['chain']}")
print(f" Fitness: {best_result['fitness']:.4f}")
print(f" Ratio: {best_result['ratio']:.3f}x")
print(f" Entropy: {best_result['entropy']:.2f} bpb")
# Extract top chains from beam search candidates
print(f"\n Top configurations explored:")
for neg_fit, candidate in optimizer.optimize.candidates[:10]:
if isinstance(candidate, dict) and 'chain' in candidate:
print(f" - {candidate['chain']}: ratio={candidate.get('ratio', 0):.3f}x, "
f"fitness={candidate.get('fitness', 0):.3f}")
print()
print_header("DONE")
print("Biological Polymorphic Shifter v3.0 demonstration complete.")
print("ANY shifter with ANY shifter — the manifold is your substrate.")
print()
# ═══════════════════════════════════════════════════════════════════════════════
# MAIN
# ═══════════════════════════════════════════════════════════════════════════════
if __name__ == '__main__':
demonstrate()