Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
PRACTICALITY SYSTEM 3.
|
| 4 |
|
| 5 |
===================================================================
|
| 6 |
ARCHITECTURE & TENSOR TRANSITION MANIFESTO (V4.0 PREP)
|
|
@@ -23,16 +23,16 @@ without regressions, we must adhere to the following mapping:
|
|
| 23 |
Invalid regions generate boolean bitmasks `valid_mask = np.ones(N_boxes)`.
|
| 24 |
===================================================================
|
| 25 |
|
| 26 |
-
Changes from 3.
|
| 27 |
-
FIX 1 β
|
| 28 |
-
but
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
"""
|
| 37 |
|
| 38 |
import asyncio, time, random, math, threading, warnings
|
|
@@ -260,7 +260,7 @@ def expand_psl(prog:PSLProgram) -> ExpandedProblem:
|
|
| 260 |
|
| 261 |
def add_con(kind,expr,direction,scope="root",weight=1.0,branches=None):
|
| 262 |
idx=len(constraints)
|
| 263 |
-
constraints.append(Constraint(kind,expr,direction,weight,scope,branches or[]))
|
| 264 |
scope_groups[scope].append(idx)
|
| 265 |
try:
|
| 266 |
syms={v:sp.Symbol(v) for v in variables}
|
|
@@ -330,7 +330,7 @@ def expand_psl(prog:PSLProgram) -> ExpandedProblem:
|
|
| 330 |
return ExpandedProblem(variables,bounds,constraints, dict(scope_groups),dict(scope_vars),scope_order)
|
| 331 |
|
| 332 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 333 |
-
# SECTION 4: PROBLEM DEFINITION
|
| 334 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 335 |
|
| 336 |
PROJECTION_CACHE: Dict[str, Dict[str, List[Dict]]] = {}
|
|
@@ -349,7 +349,7 @@ class MathConstraint:
|
|
| 349 |
parsed:Optional[sp.Expr]=field(default=None,repr=False)
|
| 350 |
degree:int=1; scope:str="root"
|
| 351 |
branches:List['MathConstraint']=field(default_factory=list)
|
| 352 |
-
projections:Dict[str,List[Dict]]=field(default_factory=dict)
|
| 353 |
|
| 354 |
def compile_mc(kind,expr_str,direction,variables,weight=1.0,scope="root",branches=None):
|
| 355 |
mc=MathConstraint(kind=kind,expr_str=expr_str,direction=direction,weight=weight,scope=scope)
|
|
@@ -617,12 +617,10 @@ def _global_hc4_tighten(problem:Problem) -> Dict[str,Tuple[float,float]]:
|
|
| 617 |
if orig_vol > 0 and new_vol < orig_vol * HC4_VOLUME_FLOOR: return dict(problem.bounds)
|
| 618 |
return {v:(max(problem.bounds[v][0], contracted[v].lo), min(problem.bounds[v][1], contracted[v].hi)) for v in problem.bounds if v in contracted}
|
| 619 |
|
| 620 |
-
def _single_snap_pass(problem: Problem,
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
l9 = RESIDUAL_ORACLE.evaluate(proj_binding, problem,[])
|
| 625 |
-
if not l9.dominant_exprs: return best_binding, best_ce, False
|
| 626 |
|
| 627 |
for worst_expr in l9.dominant_exprs[:3]:
|
| 628 |
target_mc = next((mc for mc in problem.compiled_constraints if mc.expr_str == worst_expr), None)
|
|
@@ -639,21 +637,20 @@ def _single_snap_pass(problem: Problem, proj_binding: Dict[str, float], ordered_
|
|
| 639 |
for v in ordered_vars:
|
| 640 |
if v not in target_mc.projections: continue
|
| 641 |
for proj in target_mc.projections[v]:
|
| 642 |
-
|
| 643 |
-
# FIX 2: Topological Shrink (Domain Error Recovery)
|
| 644 |
-
# We attempt the projection. If it throws ValueError (e.g. sqrt(-x) due to being
|
| 645 |
-
# outside a sphere), we pull the inputs 10% towards bounds-center and retry.
|
| 646 |
val = None
|
| 647 |
try:
|
| 648 |
-
args = [
|
| 649 |
val = float(proj["func"](*args))
|
| 650 |
except (ValueError, TypeError):
|
| 651 |
-
|
| 652 |
for s in proj["syms"]:
|
| 653 |
lo, hi = problem.bounds.get(s, (-1e18, 1e18))
|
| 654 |
mid = (lo + hi) / 2.0 if lo != -1e18 else 0.0
|
| 655 |
-
|
| 656 |
-
try:
|
|
|
|
|
|
|
| 657 |
except: continue
|
| 658 |
|
| 659 |
if val is None or not math.isfinite(val) or abs(val) > 1e6:
|
|
@@ -662,34 +659,23 @@ def _single_snap_pass(problem: Problem, proj_binding: Dict[str, float], ordered_
|
|
| 662 |
lo, hi = problem.bounds.get(v, (-1e18, 1e18))
|
| 663 |
if not (lo <= val <= hi): continue
|
| 664 |
|
| 665 |
-
test_binding = dict(proj_binding)
|
| 666 |
test_binding[v] = val
|
| 667 |
new_ce = problem.constraint_energy(test_binding)
|
| 668 |
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
break
|
| 676 |
-
if improved_in_step: break
|
| 677 |
-
if improved_in_step: break
|
| 678 |
-
|
| 679 |
-
return best_binding, best_ce, improved_in_step
|
| 680 |
|
| 681 |
def _algebraic_snap(problem: Problem, binding: Dict[str, float]) -> Tuple[Dict[str, float], float]:
|
| 682 |
-
"""
|
| 683 |
-
FIX 1: Dual-Sniper logic. Runs both Greedy (minimum-distance) and Causal (topological)
|
| 684 |
-
paths, returning the best. Cures the quantum5 jitter while preserving chain10 solves.
|
| 685 |
-
"""
|
| 686 |
best_ce_overall = problem.constraint_energy(binding)
|
| 687 |
best_binding_overall = dict(binding)
|
| 688 |
|
| 689 |
active_scopes = [s for s in problem.scope_order if s != "root" and problem.scope_vars.get(s)]
|
| 690 |
l7 = TOPOLOGY_ORACLE.evaluate(problem, active_scopes)
|
| 691 |
|
| 692 |
-
# 1. Evaluate Causal (Topological) Order
|
| 693 |
ordered_vars =[]
|
| 694 |
if l7.solve_order:
|
| 695 |
for sn in l7.solve_order:
|
|
@@ -698,20 +684,25 @@ def _algebraic_snap(problem: Problem, binding: Dict[str, float]) -> Tuple[Dict[s
|
|
| 698 |
for v in problem.variables:
|
| 699 |
if v not in ordered_vars: ordered_vars.append(v)
|
| 700 |
|
| 701 |
-
|
|
|
|
|
|
|
| 702 |
for _ in range(20):
|
| 703 |
causal_b, causal_ce, improved = _single_snap_pass(problem, causal_b, ordered_vars, causal_ce, 0.15)
|
|
|
|
|
|
|
|
|
|
| 704 |
if not improved or causal_ce < SOLVE_THRESHOLD: break
|
| 705 |
|
| 706 |
-
# 2.
|
| 707 |
-
|
| 708 |
-
|
| 709 |
for _ in range(20):
|
| 710 |
-
greedy_b, greedy_ce, improved = _single_snap_pass(problem, greedy_b,
|
|
|
|
|
|
|
|
|
|
| 711 |
if not improved or greedy_ce < SOLVE_THRESHOLD: break
|
| 712 |
-
|
| 713 |
-
if causal_ce < best_ce_overall: best_ce_overall = causal_ce; best_binding_overall = causal_b
|
| 714 |
-
if greedy_ce < best_ce_overall: best_ce_overall = greedy_ce; best_binding_overall = greedy_b
|
| 715 |
|
| 716 |
return best_binding_overall, best_ce_overall
|
| 717 |
|
|
@@ -1297,7 +1288,7 @@ async def lifespan(app: FastAPI):
|
|
| 1297 |
yield
|
| 1298 |
POOL.shutdown(wait=False)
|
| 1299 |
|
| 1300 |
-
app = FastAPI(title="Practicality System 3.
|
| 1301 |
|
| 1302 |
@app.post("/inject/{pid}")
|
| 1303 |
async def inject_seed(pid:str, request:Request):
|
|
@@ -1372,7 +1363,7 @@ async def dashboard():
|
|
| 1372 |
f"font-size:0.85em;margin:2px'>{label}: {val}</span>")
|
| 1373 |
|
| 1374 |
html=f"""<!DOCTYPE html><html>
|
| 1375 |
-
<head><title>Practicality System 3.
|
| 1376 |
<meta http-equiv="refresh" content="3">
|
| 1377 |
<style>
|
| 1378 |
body{{background:#0a0a0a;color:#e0e0e0;font-family:monospace;padding:20px}}
|
|
@@ -1383,7 +1374,7 @@ async def dashboard():
|
|
| 1383 |
th{{color:#444}}
|
| 1384 |
.witness-box{{background:#111; border:1px solid #333; padding:15px; color:#aaa; white-space:pre-wrap; font-size:0.9em;}}
|
| 1385 |
</style></head><body>
|
| 1386 |
-
<h1>β Practicality System 3.
|
| 1387 |
<div style='margin-bottom:16px;line-height:2.2em;'>
|
| 1388 |
{badge("Trials",runs,"#aaa")}
|
| 1389 |
{badge("Avg rounds",avg_rounds,"#aaa")}
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
PRACTICALITY SYSTEM 3.11
|
| 4 |
|
| 5 |
===================================================================
|
| 6 |
ARCHITECTURE & TENSOR TRANSITION MANIFESTO (V4.0 PREP)
|
|
|
|
| 23 |
Invalid regions generate boolean bitmasks `valid_mask = np.ones(N_boxes)`.
|
| 24 |
===================================================================
|
| 25 |
|
| 26 |
+
Changes from 3.10:
|
| 27 |
+
FIX 1 β Coordinate Walker State Bug: V3.10 successfully evaluated CE spikes
|
| 28 |
+
but failed to persist them into the walking binding, effectively
|
| 29 |
+
paralyzing the walker and crashing chain10's solve rate. The state
|
| 30 |
+
tracking is now correctly decoupled: the walker takes the spike, while
|
| 31 |
+
the absolute `best_overall` state is safely preserved.
|
| 32 |
+
FIX 2 β Topological Shrink State Bug: V3.10 shrank variables to avoid math
|
| 33 |
+
domain errors (negative roots) but didn't save the shrunk variables
|
| 34 |
+
to the binding. It now formally updates the binding dependencies,
|
| 35 |
+
which creates the geometric tractor-beam fully10 requires.
|
| 36 |
"""
|
| 37 |
|
| 38 |
import asyncio, time, random, math, threading, warnings
|
|
|
|
| 260 |
|
| 261 |
def add_con(kind,expr,direction,scope="root",weight=1.0,branches=None):
|
| 262 |
idx=len(constraints)
|
| 263 |
+
constraints.append(Constraint(kind,expr,direction,weight,scope,branches or []))
|
| 264 |
scope_groups[scope].append(idx)
|
| 265 |
try:
|
| 266 |
syms={v:sp.Symbol(v) for v in variables}
|
|
|
|
| 330 |
return ExpandedProblem(variables,bounds,constraints, dict(scope_groups),dict(scope_vars),scope_order)
|
| 331 |
|
| 332 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 333 |
+
# SECTION 4: PROBLEM DEFINITION (WITH ALGEBRAIC CACHING)
|
| 334 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 335 |
|
| 336 |
PROJECTION_CACHE: Dict[str, Dict[str, List[Dict]]] = {}
|
|
|
|
| 349 |
parsed:Optional[sp.Expr]=field(default=None,repr=False)
|
| 350 |
degree:int=1; scope:str="root"
|
| 351 |
branches:List['MathConstraint']=field(default_factory=list)
|
| 352 |
+
projections:Dict[str,List[Dict]]=field(default_factory=dict)
|
| 353 |
|
| 354 |
def compile_mc(kind,expr_str,direction,variables,weight=1.0,scope="root",branches=None):
|
| 355 |
mc=MathConstraint(kind=kind,expr_str=expr_str,direction=direction,weight=weight,scope=scope)
|
|
|
|
| 617 |
if orig_vol > 0 and new_vol < orig_vol * HC4_VOLUME_FLOOR: return dict(problem.bounds)
|
| 618 |
return {v:(max(problem.bounds[v][0], contracted[v].lo), min(problem.bounds[v][1], contracted[v].hi)) for v in problem.bounds if v in contracted}
|
| 619 |
|
| 620 |
+
def _single_snap_pass(problem: Problem, current_binding: Dict[str, float], ordered_vars: List[str], current_ce: float, spike_tolerance: float) -> Tuple[Dict[str, float], float, bool]:
|
| 621 |
+
l9 = RESIDUAL_ORACLE.evaluate(current_binding, problem,[])
|
| 622 |
+
if not l9.dominant_exprs:
|
| 623 |
+
return current_binding, current_ce, False
|
|
|
|
|
|
|
| 624 |
|
| 625 |
for worst_expr in l9.dominant_exprs[:3]:
|
| 626 |
target_mc = next((mc for mc in problem.compiled_constraints if mc.expr_str == worst_expr), None)
|
|
|
|
| 637 |
for v in ordered_vars:
|
| 638 |
if v not in target_mc.projections: continue
|
| 639 |
for proj in target_mc.projections[v]:
|
| 640 |
+
test_binding = dict(current_binding)
|
|
|
|
|
|
|
|
|
|
| 641 |
val = None
|
| 642 |
try:
|
| 643 |
+
args = [test_binding.get(s, 0.0) for s in proj["syms"]]
|
| 644 |
val = float(proj["func"](*args))
|
| 645 |
except (ValueError, TypeError):
|
| 646 |
+
# FIX 2: Topological Shrink. Safe state persistence.
|
| 647 |
for s in proj["syms"]:
|
| 648 |
lo, hi = problem.bounds.get(s, (-1e18, 1e18))
|
| 649 |
mid = (lo + hi) / 2.0 if lo != -1e18 else 0.0
|
| 650 |
+
test_binding[s] = test_binding.get(s, mid) * 0.9 + mid * 0.1
|
| 651 |
+
try:
|
| 652 |
+
args = [test_binding.get(s, 0.0) for s in proj["syms"]]
|
| 653 |
+
val = float(proj["func"](*args))
|
| 654 |
except: continue
|
| 655 |
|
| 656 |
if val is None or not math.isfinite(val) or abs(val) > 1e6:
|
|
|
|
| 659 |
lo, hi = problem.bounds.get(v, (-1e18, 1e18))
|
| 660 |
if not (lo <= val <= hi): continue
|
| 661 |
|
|
|
|
| 662 |
test_binding[v] = val
|
| 663 |
new_ce = problem.constraint_energy(test_binding)
|
| 664 |
|
| 665 |
+
# FIX 1: Proper state tracking for spikes.
|
| 666 |
+
# Returns immediately so caller can preserve the spike into the next round.
|
| 667 |
+
if new_ce < current_ce * (1 + spike_tolerance):
|
| 668 |
+
return test_binding, new_ce, True
|
| 669 |
+
|
| 670 |
+
return current_binding, current_ce, False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
|
| 672 |
def _algebraic_snap(problem: Problem, binding: Dict[str, float]) -> Tuple[Dict[str, float], float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
best_ce_overall = problem.constraint_energy(binding)
|
| 674 |
best_binding_overall = dict(binding)
|
| 675 |
|
| 676 |
active_scopes = [s for s in problem.scope_order if s != "root" and problem.scope_vars.get(s)]
|
| 677 |
l7 = TOPOLOGY_ORACLE.evaluate(problem, active_scopes)
|
| 678 |
|
|
|
|
| 679 |
ordered_vars =[]
|
| 680 |
if l7.solve_order:
|
| 681 |
for sn in l7.solve_order:
|
|
|
|
| 684 |
for v in problem.variables:
|
| 685 |
if v not in ordered_vars: ordered_vars.append(v)
|
| 686 |
|
| 687 |
+
# 1. Causal order pass
|
| 688 |
+
causal_b = dict(binding)
|
| 689 |
+
causal_ce = best_ce_overall
|
| 690 |
for _ in range(20):
|
| 691 |
causal_b, causal_ce, improved = _single_snap_pass(problem, causal_b, ordered_vars, causal_ce, 0.15)
|
| 692 |
+
if causal_ce < best_ce_overall:
|
| 693 |
+
best_ce_overall = causal_ce
|
| 694 |
+
best_binding_overall = dict(causal_b)
|
| 695 |
if not improved or causal_ce < SOLVE_THRESHOLD: break
|
| 696 |
|
| 697 |
+
# 2. Greedy order pass
|
| 698 |
+
greedy_b = dict(binding)
|
| 699 |
+
greedy_ce = problem.constraint_energy(binding)
|
| 700 |
for _ in range(20):
|
| 701 |
+
greedy_b, greedy_ce, improved = _single_snap_pass(problem, greedy_b, problem.variables, greedy_ce, 0.15)
|
| 702 |
+
if greedy_ce < best_ce_overall:
|
| 703 |
+
best_ce_overall = greedy_ce
|
| 704 |
+
best_binding_overall = dict(greedy_b)
|
| 705 |
if not improved or greedy_ce < SOLVE_THRESHOLD: break
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
return best_binding_overall, best_ce_overall
|
| 708 |
|
|
|
|
| 1288 |
yield
|
| 1289 |
POOL.shutdown(wait=False)
|
| 1290 |
|
| 1291 |
+
app = FastAPI(title="Practicality System 3.11", lifespan=lifespan)
|
| 1292 |
|
| 1293 |
@app.post("/inject/{pid}")
|
| 1294 |
async def inject_seed(pid:str, request:Request):
|
|
|
|
| 1363 |
f"font-size:0.85em;margin:2px'>{label}: {val}</span>")
|
| 1364 |
|
| 1365 |
html=f"""<!DOCTYPE html><html>
|
| 1366 |
+
<head><title>Practicality System 3.11</title>
|
| 1367 |
<meta http-equiv="refresh" content="3">
|
| 1368 |
<style>
|
| 1369 |
body{{background:#0a0a0a;color:#e0e0e0;font-family:monospace;padding:20px}}
|
|
|
|
| 1374 |
th{{color:#444}}
|
| 1375 |
.witness-box{{background:#111; border:1px solid #333; padding:15px; color:#aaa; white-space:pre-wrap; font-size:0.9em;}}
|
| 1376 |
</style></head><body>
|
| 1377 |
+
<h1>β Practicality System 3.11</h1>
|
| 1378 |
<div style='margin-bottom:16px;line-height:2.2em;'>
|
| 1379 |
{badge("Trials",runs,"#aaa")}
|
| 1380 |
{badge("Avg rounds",avg_rounds,"#aaa")}
|