homemaker-layout/tests/test_fitness_crinkliness_tail.py
Claude cd392e77c5
Rescale the underflowing crinkliness tail so the failing region has an ordering
quality_uncrinkliness evaluates a gaussian at x = 1/crink, so its exponent
grows like 1/crink^2 and underflows a double to exactly zero below crink ~
1/15. Measured over the twelve 500k cold-start runs (39.12): 430 leaves carry
a minimum-exposure requirement, 112 fail it, and those 112 span quality
1e-300..1e-1 while contributing 0.034% of total value on 23% of the floor
area. Every value in that range is numerically zero beside a passing leaf's
~1, so the search cannot rank two layouts that differ only in how exposed
their under-lit rooms are.

This is wider than the bead's diagnosis (a flat 0.0 for zero-exposure leaves)
and it explains why 38.1's `floor` mode measured as a no-op: max(q, 0.01) maps
110 of the 112 onto one constant, replacing a flat zero with a flat 0.01.

crinkliness_tail="ramp" (default OFF, "gaussian" is stock) replaces the tail --
only the tail, only below FAIL_THRESHOLD, only on the compact side -- with a
straight line in crinkliness meeting the gaussian exactly at the crossing.
_crink_at_fail_threshold inverts the gaussian there using the same truncated
_E the factor is evaluated with.

Deliberately conservative: nothing at or above FAIL_THRESHOLD moves, so no
calibration changes and no leaf crosses the threshold. The fail set is
byte-identical on all 21 committed corpus artefacts, the four init.dom seeds
included -- asserted in tests/test_fitness_crinkliness_tail.py, not assumed.
That invariance is also what makes it legal to score both arms of the A/B
under stock (the 38.9 trap's one exemption). A fully buried leaf still scores
exactly 0; this restores an ordering within the failing region, it does not
forgive it. Composing with 38.1's superseded modes is refused, since both
rewrite the same tail.

Score effect on the baseline artefacts: +0.3%..+2.8% on harbor and maple,
exactly +0.000% on health-centre, programme-house, and every init.dom -- a
programme with no partially-exposed failing rooms has nothing to grade, and
neither does any starting layout. The ramp is a mid-search signal by
construction, so experiments/ab_9gj_ramp.py defaults to seeding each run from
a 500k plateau artefact rather than cold.

The module-level math import replaces a now-redundant local one.

DESIGN.md 39.13 and the A/B verdict follow in a separate commit.

Refs homemaker-py-9gj.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MJ84Feep79Hhm3E4zZJmnB
2026-09-05 06:29:02 +00:00

113 lines
4.8 KiB
Python

"""The `crinkliness_tail="ramp"` rescale (homemaker-py-9gj, DESIGN.md §39.13).
The whole design rests on one invariant: the ramp rewrites the failing compact
tail and NOTHING else, so no leaf crosses FAIL_THRESHOLD and the fail set is
byte-identical to stock. That is what makes it legal to score both arms of the
A/B under the stock objective (the §38.9 trap's one exemption). It is asserted
here on every committed corpus artefact rather than assumed.
"""
from __future__ import annotations
import copy
from pathlib import Path
import pytest
from homemaker_layout import dom as dom_mod
from homemaker_layout.fitness import (
FAIL_THRESHOLD, Fitness, _crink_at_fail_threshold, gaussian, load_config,
)
EXAMPLES = Path(__file__).resolve().parent.parent / "examples"
PROGRAMMES = ["harbor-house", "maple-court", "health-centre", "programme-house"]
def _artefacts():
for name in PROGRAMMES:
d = EXAMPLES / name
if not d.is_dir():
continue
for p in sorted(d.glob("coldstart-500000-s*.dom")) + \
sorted(d.glob("evolved-3M*.dom")) + [d / "init.dom"]:
if p.exists():
yield d, p
def test_crossing_is_continuous_at_the_fail_threshold():
"""The ramp meets the gaussian exactly at FAIL_THRESHOLD, so the factor is
continuous there and the ordering across the boundary is preserved."""
for distance, sigma in ((5.0 / 6, 1.1 / 3), (1.2, 0.25), (0.5, 0.5)):
c0 = _crink_at_fail_threshold(distance, sigma)
assert gaussian(1 / c0, 1.0, distance, sigma) == pytest.approx(
FAIL_THRESHOLD, rel=1e-12)
# and it is the COMPACT-side root: less exposure than c0, not more
assert 1 / c0 > distance
def test_ramp_is_strictly_monotone_where_the_gaussian_has_underflowed():
"""The point of the change. Stock assigns the same double -- 0.0 -- to
every leaf below crink ~= 1/15; the ramp separates them."""
distance, sigma = 5.0 / 6, 1.1 / 3
c0 = _crink_at_fail_threshold(distance, sigma)
crinks = [0.0, 0.001, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, c0 * 0.999]
stock = [gaussian(1 / c, 1.0, distance, sigma) if c else 0.0 for c in crinks]
ramp = [FAIL_THRESHOLD * c / c0 for c in crinks]
assert len(set(stock)) < len(set(ramp)), "stock should collapse values the ramp keeps"
assert stock.count(0.0) > 1, "the flat-zero region is what this fixes"
assert all(b > a for a, b in zip(ramp, ramp[1:])), "ramp must be strictly increasing"
assert ramp[0] == 0.0, "a fully buried leaf is still worth nothing"
assert all(q < FAIL_THRESHOLD for q in ramp), "the ramp must never lift a leaf out of failing"
@pytest.mark.skipif(not (EXAMPLES / "harbor-house").is_dir(),
reason="examples absent")
def test_fail_set_is_byte_identical_across_the_corpus():
seen = 0
for d, p in _artefacts():
root = dom_mod.load(str(p))
c_stock, cost = load_config(d)
c_ramp, _ = load_config(d, overrides={"crinkliness_tail": "ramp"})
_, f_stock = Fitness(c_stock, cost).score_with_fails(copy.deepcopy(root))
_, f_ramp = Fitness(c_ramp, cost).score_with_fails(copy.deepcopy(root))
assert f_stock == f_ramp, f"{p} changed its fail set under the ramp"
seen += 1
assert seen >= 4, "expected to have checked several corpus artefacts"
@pytest.mark.skipif(not (EXAMPLES / "harbor-house").is_dir(),
reason="examples absent")
def test_ramp_never_lowers_the_score():
"""Every affected factor rises (0 or ~0 -> a representable fraction of
FAIL_THRESHOLD), and quality is a product with value accumulating
positively, so the scalar can only go up or stay put."""
for d, p in _artefacts():
root = dom_mod.load(str(p))
c_stock, cost = load_config(d)
c_ramp, _ = load_config(d, overrides={"crinkliness_tail": "ramp"})
s_stock, _ = Fitness(c_stock, cost).score_with_fails(copy.deepcopy(root))
s_ramp, _ = Fitness(c_ramp, cost).score_with_fails(copy.deepcopy(root))
assert s_ramp >= s_stock, f"{p} scored lower under the ramp"
def test_ramp_refuses_to_compose_with_the_superseded_modes():
"""§38.1's modes rewrite the same tail; stacking them would give a shape
neither was measured under."""
d = EXAMPLES / "harbor-house"
if not d.is_dir():
pytest.skip("examples absent")
conf, cost = load_config(d, overrides={"crinkliness_tail": "ramp",
"crinkliness_mode": "floor"})
with pytest.raises(ValueError, match="incompatible"):
Fitness(conf, cost)
def test_unknown_tail_is_rejected():
d = EXAMPLES / "harbor-house"
if not d.is_dir():
pytest.skip("examples absent")
conf, cost = load_config(d, overrides={"crinkliness_tail": "linear"})
with pytest.raises(ValueError, match="unknown crinkliness_tail"):
Fitness(conf, cost)