Files
sim-numerics/diag_fun7_root.py
T

58 lines
2.1 KiB
Python

"""Is fun7 a method failure or a SCORING failure?
Every method stops on fun7 within 8-22 evaluations and every one is scored unsolved. This
checks the only thing that matters: is f actually zero at the point each method returned?
If it is, the methods found a root and the benchmark's |x - declared_root| <= 1e-6 check is
what fails — 5 of the 45 problems then contribute 200 dead evaluations each to EVERY
method's total, which is a fact about the scoring, not about the methods.
python -u diag_fun7_root.py
"""
from __future__ import annotations
import inspect
import math
from scipy.optimize._tstutils import _CHANDRUPATLA_TESTS_DICTS
from simroot.baselines import best_of, chandrupatla, chandrupatla_scipy
from simroot.pure import pure_root
from sweep_full import MAXFEV, XTOL
CHA = best_of(chandrupatla, chandrupatla_scipy)
def main():
for d in _CHANDRUPATLA_TESTS_DICTS:
if not str(d["ID"]).startswith("fun7"):
continue
f, root = d["f"], float(d["root"])
a, b = (float(v) for v in d["bracket"])
x_o, n_o, _ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio",
scale="derived3", guard="safe", prestep="slope",
direction="dissimilar")
x_c, n_c, _okc = CHA(f, a, b, XTOL, MAXFEV)
print(f"{d['ID']:<8} declared root {root:+.3g}")
for nm, x, n in (("ours", x_o, n_o), ("chandrupatla", float(x_c), n_c)):
fx = f(x)
print(f" {nm:<13} returned x={x:+.10g} after {n:3d} evals "
f"f(x)={fx:+.3e} |x-root|={abs(x - root):.3g} "
f"{'f IS ZERO THERE' if fx == 0.0 else 'f is not zero'}")
# how wide is the flat zero region around the declared root?
w = 0.0
for k in range(0, 40):
t = 10.0 ** (-12 + k * 0.5)
if f(root + t) == 0.0:
w = t
else:
break
print(f" f is identically zero at least up to root + {w:.3g}")
src = inspect.getsource(_CHANDRUPATLA_TESTS_DICTS[0]["f"]) if False else None
if src:
print(src)
if __name__ == "__main__":
main()