91 lines
4.0 KiB
Python
91 lines
4.0 KiB
Python
"""Grafting similarity onto existing methods: where it works and where not.
|
|
|
|
The project's one clear success was a graft - the credit safeguard turned
|
|
regula falsi, the secant rule and inverse quadratic interpolation into methods
|
|
that solve 193 of 199 with a near-bisection worst case. This file pins the
|
|
graft that FAILED, and the boundary the two of them mark out.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from simroot.baselines import brent, chandrupatla
|
|
from stage16_blend import brent_blend, chandrupatla_blend, load, measure
|
|
|
|
PROBS = load()
|
|
|
|
|
|
def both(m):
|
|
t, ok, worst = measure(m, PROBS)
|
|
return t["APS"] + t["CHA"], ok["APS"] + ok["CHA"], worst
|
|
|
|
|
|
def test_the_controls_reproduce_the_published_methods_exactly():
|
|
"""Without this the experiment is void: a blend must be compared against
|
|
the same code with the blend switched off, not against a different
|
|
implementation."""
|
|
pub_b = both(lambda f, a, b, xt, mx: brent(f, a, b, xt, mx))
|
|
off_b = both(lambda f, a, b, xt, mx: brent_blend(f, a, b, xt, mx,
|
|
mode="off"))
|
|
assert pub_b == off_b, (pub_b, off_b)
|
|
pub_c = both(lambda f, a, b, xt, mx: chandrupatla(f, a, b, xt, mx))
|
|
off_c = both(lambda f, a, b, xt, mx: chandrupatla_blend(f, a, b, xt, mx,
|
|
mode="off"))
|
|
assert pub_c == off_c, (pub_c, off_c)
|
|
|
|
|
|
def test_softening_brents_acceptance_test_makes_it_worse():
|
|
base, _s, _w = both(lambda f, a, b, xt, mx: brent(f, a, b, xt, mx))
|
|
blend, _s2, _w2 = both(lambda f, a, b, xt, mx: brent_blend(f, a, b, xt, mx,
|
|
mode="blend"))
|
|
assert blend > 1.2 * base, (base, blend)
|
|
|
|
|
|
def test_softening_chandrupatlas_acceptance_test_makes_it_worse():
|
|
base, s_base, _w = both(lambda f, a, b, xt, mx: chandrupatla(f, a, b, xt,
|
|
mx))
|
|
blend, s_blend, _w2 = both(
|
|
lambda f, a, b, xt, mx: chandrupatla_blend(f, a, b, xt, mx,
|
|
mode="blend"))
|
|
assert blend > 1.2 * base, (base, blend)
|
|
assert s_blend < s_base, (s_base, s_blend) # and it breaks problems
|
|
|
|
|
|
def test_projecting_onto_brents_bound_also_fails():
|
|
"""The move that worked inside our own method - project, do not replace -
|
|
does not transfer to somebody else's acceptance condition. Clamping to
|
|
the boundary produces the most aggressive still-permitted step, which is
|
|
exactly where the interpolation has stopped carrying information."""
|
|
base, s_base, _w = both(lambda f, a, b, xt, mx: brent(f, a, b, xt, mx))
|
|
proj, s_proj, _w2 = both(lambda f, a, b, xt, mx: brent_blend(
|
|
f, a, b, xt, mx, mode="project"))
|
|
assert proj > base, (base, proj)
|
|
assert s_proj < s_base, (s_base, s_proj)
|
|
|
|
|
|
def test_the_boundary_this_marks_out():
|
|
"""Three failures against one success, and they differ in kind.
|
|
|
|
The safeguard graft succeeded where the quantity being controlled is a
|
|
GRADED QUALITY - how much margin to leave, how bold a step to take. The
|
|
blend graft failed where the quantity is a VALIDITY CONDITION - whether
|
|
the interpolation model applies at all. Near such a boundary a point is
|
|
not partly good; it is where the model has just stopped holding, which is
|
|
why Brent and Chandrupatla discard it rather than shrink it."""
|
|
from stage14_is_anything_reusable import RULES, load as load14, measure as m14
|
|
p14 = load14()
|
|
for name, rule in RULES:
|
|
t_raw, s_raw, _w = m14(rule, False, p14)
|
|
t_g, s_g, _w2 = m14(rule, True, p14)
|
|
assert s_g > s_raw and t_g < t_raw, name # the graft that works
|
|
base, _s, _w = both(lambda f, a, b, xt, mx: brent(f, a, b, xt, mx))
|
|
blend, _s2, _w2 = both(lambda f, a, b, xt, mx: brent_blend(
|
|
f, a, b, xt, mx, mode="blend"))
|
|
assert blend > base # the graft that does not
|