"""Pareto frontier utilities for multi-objective optimization."""
from __future__ import annotations
from typing import Any
[docs]
def pareto_frontier(points: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Compute accuracy-vs-parameters Pareto frontier.
Each point should have ``"accuracy"`` and ``"n_parameters"`` keys.
Returns the subset of non-dominated points sorted by parameter count.
"""
if not points:
return []
sorted_pts = sorted(points, key=lambda p: int(p.get("n_parameters", 0)))
frontier: list[dict[str, Any]] = []
best_acc = -1.0
for pt in sorted_pts:
acc = float(pt.get("accuracy", 0.0))
if acc > best_acc:
frontier.append(pt)
best_acc = acc
return frontier
[docs]
def select_best(
points: list[dict[str, Any]],
budget: float,
) -> dict[str, Any]:
"""Pick the smallest model within an accuracy budget.
``budget`` is the maximum allowed accuracy drop from the best candidate.
"""
if not points:
raise ValueError("No points provided")
max_acc = max(float(p.get("accuracy", 0.0)) for p in points)
min_acc = max_acc - budget
valid = [p for p in points if float(p.get("accuracy", 0.0)) >= min_acc]
if not valid:
return points[0]
return min(valid, key=lambda p: (int(p.get("n_parameters", 10**9)), -float(p["accuracy"])))