diff --git a/experiments/optimize_fullfitness.py b/experiments/optimize_fullfitness.py new file mode 100644 index 0000000..e2c9473 --- /dev/null +++ b/experiments/optimize_fullfitness.py @@ -0,0 +1,89 @@ +"""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() diff --git a/experiments/sweep_failtypes.py b/experiments/sweep_failtypes.py new file mode 100644 index 0000000..102c8ed --- /dev/null +++ b/experiments/sweep_failtypes.py @@ -0,0 +1,79 @@ +"""Equal-offset (perpendicular) warm-start sweep with failure-type histogram. + +Isolates which fitness constraints break when we move equal-offset cuts to fix +programme room sizes. Equal offset (a == b) keeps walls perpendicular on +near-rectangular plots, so any regression here is NOT a perpendicularity +artifact -- it tells us what sizing genuinely trades against. +""" + +import collections +import shutil +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) + +from homemaker import dom, oracle, programme, solver # noqa: E402 + +URB = Path("/home/bruno/src/urb") +EX = URB / "examples/programme-house" + + +def classify(line: str) -> str: + s = line.strip().lower() + if not s or s == "---" or s.startswith(("- ", "type:", "issue:", "actual:", "min:")): + # structured YAML fails: pull the keyword if present + if "staircase" in s: + return "staircase" + if "access" in s or "inaccessible" in s: + return "access" + if not s or s == "---": + return "" + for key in ("perpendicular", "crinkliness", "width", "proportion", "size", + "adjacent", "access", "inaccessible", "level", "staircase"): + if key in s: + return "adjacency" if key == "adjacent" else ( + "access" if key == "inaccessible" else key) + return "other" + + +def tally(fails: str, counter: collections.Counter) -> None: + for line in fails.splitlines(): + t = classify(line) + if t: + counter[t] += 1 + + +def main() -> None: + scratch = Path(__file__).resolve().parents[1] / "scratch" + scratch.mkdir(exist_ok=True) + shutil.copy(EX / "patterns.config", scratch / "patterns.config") + targets = programme.load_programme(str(EX / "patterns.config")) + + before = collections.Counter() + after = collections.Counter() + doms = sorted(p for p in EX.glob("*.dom") if p.stem != "init") + + for src in doms: + shutil.copy(src, scratch / "orig.dom") + s0 = oracle.score(scratch / "orig.dom", URB) + root = dom.load(str(src)) + solver.solve_ratios( # equal offset, shape-aware (strong width/proportion) + root, targets, strip=False, perpendicular=True, + weight_width=3.0, weight_proportion=2.0, min_width_generic=1.5, + ) + dom.dump(root, str(scratch / "ref.dom")) + s1 = oracle.score(scratch / "ref.dom", URB) + tally(s0.fails, before) + tally(s1.fails, after) + + types = sorted(set(before) | set(after), key=lambda t: -(after[t] - before[t])) + print(f"{'failure type':16s} {'before':>8s} {'after':>8s} {'delta':>8s}") + for t in types: + print(f"{t:16s} {before[t]:8d} {after[t]:8d} {after[t] - before[t]:+8d}") + print(f"{'TOTAL':16s} {sum(before.values()):8d} {sum(after.values()):8d} " + f"{sum(after.values()) - sum(before.values()):+8d}") + + +if __name__ == "__main__": + main()