Files
sim-numerics/diag_analogy_sequence.py
T

59 lines
2.3 KiB
Python

"""The state-by-state picture behind diag_analogy_late.py, for a few flat problems.
Every state the gate sees (trace hook, flatanalog, projection), in order:
n evaluations spent (the trace does not carry it; states are numbered instead)
flag r = f/f_prev >= 1/4 (the promise failed, sign kept)
xi, phi the configuration
ratio d_past / d_line (< 1 means analogous to the nearest past configuration)
lag how many states back that nearest past configuration is
m the pair's similarity exponent
Nothing in the method changes.
python -u diag_analogy_sequence.py fun6.3 fun4.1 fun3.4
"""
from __future__ import annotations
import math
import sys
from simroot.pure import _config3, pure_root
from sweep_full import MAXFEV, XTOL, load_problems
def show(pid, f, a, b):
st = {"trace": []}
pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project",
prestep="flatanalog", stats=st)
print(f"\n== {pid} (state, flag, xi, phi, d_past/d_line, lag, m)")
past = []
for k, s in enumerate(st["trace"][:30]):
cur = _config3(s["x"], s["fx"], s["xp"], s["fp"], s["xp2"], s["fp2"])
flag = s["fp"] != 0.0 and s["fx"] / s["fp"] >= 0.25
if cur is None:
print(f" {k:3d} {'F' if flag else '.'} (no configuration)")
continue
line = abs(cur[1] - cur[0]) / math.sqrt(2.0)
if past:
j = min(range(len(past)), key=lambda i: math.hypot(cur[0] - past[i][0],
cur[1] - past[i][1]))
q = past[j]
ratio = math.hypot(cur[0] - q[0], cur[1] - q[1]) / line if line > 0 else math.inf
lx, lf = cur[2] / q[2], cur[3] / q[3]
m = (math.log(lf) / math.log(lx)) if lx not in (0.0, 1.0) and lf > 0 else math.nan
lag = len(past) - j
print(f" {k:3d} {'F' if flag else '.'} xi {cur[0]:+9.4f} phi {cur[1]:+10.4f} "
f"ratio {ratio:7.3f} lag {lag:2d} m {m:6.2f}")
else:
print(f" {k:3d} {'F' if flag else '.'} xi {cur[0]:+9.4f} phi {cur[1]:+10.4f} (no memory)")
past.append(cur)
def main(ids):
for s, pid, f, a, b, _root in load_problems():
if s == "CHA" and pid in ids:
show(pid, f, a, b)
if __name__ == "__main__":
main(sys.argv[1:] or ["fun6.3", "fun4.1"])