homemaker-layout/experiments/optimize_fullfitness.py
Bruno Postle d08d15e4d7 Full-fitness frozen-topology optimisation validates geometry inner loop
Driving equal-offset cut ratios with Nelder-Mead against the REAL oracle
fitness (full objective, no proxy) improves all three test candidates with
zero new failures:

  2f45907 (best evolved)  0.012617 -> 0.015684  x1.24  (2->2 fails)
  candidate-002          0.007375 -> 0.012319  x1.67  (2->2 fails)
  c964435 (baseline)     0.003667 -> 0.005836  x1.59  (3->3 fails)

Headroom widens on weaker designs. The EA under-optimises geometry by
24-67% even on its best result. This validates a full-fitness geometry
inner loop (NOT the earlier area-proxy solver) and motivates a memetic
architecture: topology search outside, full-objective geometry optimise
inside, gated on a native Python fitness (oracle at ~3s/call is too slow).
2026-06-10 22:27:30 +01:00

89 lines
2.8 KiB
Python

"""Full-fitness frozen-topology optimisation.
Drive the equal-offset division ratios with a derivative-free optimiser against
the REAL oracle fitness (the whole objective, not an area proxy) on a fixed
topology. This removes both confounds of the earlier sweeps (partial objective,
proxy target) and answers the only question that matters next:
Is there geometry headroom above the EA's designs, or are they already
geometry-optima (=> the bottleneck is topology + the 0.5^n cliff)?
For each candidate: report fitness/fails before and after.
"""
import sys
from pathlib import Path
import numpy as np
from scipy.optimize import minimize
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from homemaker import dom, oracle, solver # noqa: E402
URB = Path("/home/bruno/src/urb")
EX = URB / "examples/programme-house"
SCRATCH = Path(__file__).resolve().parents[1] / "scratch"
_EPS = 0.02
CANDIDATES = [
"candidate-002.dom", # ~0.0074 (MCP-refined)
"c964435454c459f86c3ed9a5a7621132.dom", # ~0.0037 (MCP baseline)
]
def optimise(src: Path, maxfev: int = 200):
import shutil
shutil.copy(src, SCRATCH / "orig.dom")
s0 = oracle.score(SCRATCH / "orig.dom", URB)
root = dom.load(str(src))
free = solver.free_branches(root)
x0 = np.array([b.division[0] for b in free], dtype=float)
opt_path = SCRATCH / "opt.dom"
best = {"f": s0.fitness, "fails": s0.n_fails, "x": x0.copy()}
def neg_fitness(x: np.ndarray) -> float:
xc = np.clip(x, _EPS, 1 - _EPS)
for j, b in enumerate(free):
b.division = [float(xc[j]), float(xc[j])]
dom.dump(root, str(opt_path))
try:
s = oracle.score(opt_path, URB)
except Exception: # noqa: BLE001
return 1e9
if s.fitness > best["f"]:
best.update(f=s.fitness, fails=s.n_fails, x=xc.copy())
return -s.fitness
minimize(
neg_fitness, x0, method="Nelder-Mead",
options={"maxfev": maxfev, "xatol": 1e-3, "fatol": 1e-12},
)
return s0, best, len(free)
def main() -> None:
SCRATCH.mkdir(exist_ok=True)
import shutil
shutil.copy(EX / "patterns.config", SCRATCH / "patterns.config")
maxfev = int(sys.argv[1]) if len(sys.argv) > 1 else 200
for name in CANDIDATES:
s0, best, ndof = optimise(EX / name, maxfev)
ratio = best["f"] / s0.fitness if s0.fitness else float("inf")
verdict = "IMPROVED" if best["f"] > s0.fitness * 1.001 else "no gain"
print(
f"{name:42s} dof={ndof:2d} "
f"orig={s0.fitness:.5g}(fails {s0.n_fails}) "
f"opt={best['f']:.5g}(fails {best['fails']}) "
f"x{ratio:.2f} {verdict}",
flush=True,
)
if __name__ == "__main__":
main()