SPB Git forge

spb/ai-atlas

Public
41commits 1branches 0releases
4.6 MBsize
maindefault branch
13 days agolast push
HTML 77.2% TypeScript 10.5% Python 9.6% JavaScript 2.5%
2.0 KB · 45 lines python
Raw Blame History
1"""Pareto frontier (maximise y, minimise x) over labelled points. Pure Python, deterministic.23A point is *Pareto-efficient* when no other point has both a lower-or-equal x and a higher-or-equal y with at least one strict inequality.4Exact ties (same x and same y) are kept together on the frontier — we never break a tie by choosing one model over another."""5from __future__ import annotations67from typing import Any8910def pareto_frontier(points: list[dict[str, Any]], *, x: str = "x", y: str = "y", key: str = "id", maximize_y: bool = True) -> list[Any]:11    """Return the `key`s of the Pareto-efficient points, ordered by increasing x. Points with a missing x or y are ignored."""12    valid = [p for p in points if p.get(x) is not None and p.get(y) is not None]13    if not valid:14        return []15    sign = 1.0 if maximize_y else -1.016    # sort by x ascending, then by y descending (best first) so a sweep with the running best y finds the frontier17    ordered = sorted(valid, key=lambda p: (float(p[x]), -sign * float(p[y])))18    frontier: list[Any] = []19    best_y: float | None = None20    best_x: float | None = None21    for p in ordered:22        px, py = float(p[x]), sign * float(p[y])23        if best_y is None or py > best_y:24            frontier.append(p[key])25            best_y, best_x = py, px26        elif py == best_y and best_x is not None and px == best_x:27            frontier.append(p[key])  # exact tie: keep both28    return frontier293031def is_dominated(p: dict[str, Any], others: list[dict[str, Any]], *, x: str = "x", y: str = "y", maximize_y: bool = True) -> bool:32    """True when some other point is at least as good on both axes and strictly better on one."""33    sign = 1.0 if maximize_y else -1.034    px, py = float(p[x]), sign * float(p[y])35    for o in others:36        if o is p or o.get(x) is None or o.get(y) is None:37            continue38        ox, oy = float(o[x]), sign * float(o[y])39        if ox <= px and oy >= py and (ox < px or oy > py):40            return True41    return False424344__all__ = ["is_dominated", "pareto_frontier"]45