"""Evaluation protocol (Sec 5.2 / App C-D): pairwise win-rate vs expert reference with 3 judges x 2 order-swaps (6 votes/dim), MOScore (Eq 14), and computable novelty / diversity metrics (all-MiniLM-L6-v2).""" from __future__ import annotations import numpy as np from . import llm from .tournament import judge_stage, PF_DIMS, PS_DIMS from .flow import idea_to_text, motivation_text from . import supernet as _sn # MOScore weights (NOT STATED in paper -> uniform, documented) W_PF = np.array([1 / 3, 1 / 3, 1 / 3]) W_PS = np.array([1 / 3, 1 / 3, 1 / 3]) def moscore(s, w): s = np.clip(np.asarray(s, dtype=float), 1e-6, 1.0) w = np.asarray(w, dtype=float) w = w / w.sum() arith = float((w * s).sum()) geo = float(np.prod(s ** w)) return 0.5 * (arith + geo) def _method_win(winner, method_is): """winner is 'A'/'B'/None; method_is is which slot the method occupies.""" if winner is None: return 0.5 return 1.0 if winner == method_is else 0.0 def winrate_vs_reference(method_idea, ref_idea, context, judges, seed=0): """Return dict of 6 dims -> win-rate in [0,1] via 3 judges x 2 order swaps. The (judge, order, stage) calls are independent -> run concurrently.""" votes = {"PF_" + d: [] for d in PF_DIMS} votes.update({"PS_" + d: [] for d in PS_DIMS}) tasks = [] # (ji, swap, stage) for ji, jm in enumerate(judges): for swap in (False, True): for stage in ("motivation", "idea"): tasks.append((ji, jm, swap, stage)) def _do(t): ji, jm, swap, stage = t A, B = (ref_idea, method_idea) if swap else (method_idea, ref_idea) off = 7 if stage == "idea" else 0 res = judge_stage(A, B, context, stage, model=jm, seed=seed + ji * 13 + off + swap) return (swap, stage, res) outs = llm.parallel_map(_do, tasks, workers=min(12, len(tasks))) for swap, stage, res in outs: method_is = "B" if swap else "A" dims = PF_DIMS if stage == "motivation" else PS_DIMS pref = "PF_" if stage == "motivation" else "PS_" for d in dims: votes[pref + d].append(_method_win(res.get(d), method_is)) return {k: float(np.mean(v)) for k, v in votes.items()} def aggregate_winrates(per_query_winrates): """per_query_winrates: list of dicts -> mean per dim + MOScores + overall.""" dims = list(per_query_winrates[0].keys()) mean = {d: float(np.mean([q[d] for q in per_query_winrates])) for d in dims} s_pf = [mean["PF_Novelty"], mean["PF_Significance"], mean["PF_Timeliness"]] s_ps = [mean["PS_Novelty"], mean["PS_Effectiveness"], mean["PS_Feasibility"]] mo_pf = moscore(s_pf, W_PF) mo_ps = moscore(s_ps, W_PS) overall = 0.5 * (mo_pf + mo_ps) out = dict(mean) out["MOScore_PF"] = mo_pf out["MOScore_PS"] = mo_ps out["Overall"] = overall return out # ---- computable metrics ------------------------------------------------------- def _emb(texts): return _sn.encode(texts, normalize=True) def novelty(idea, related_works): """1 - mean cosine of (motivation, method) vs related works (Eq 17-18).""" if not related_works: return float("nan") rw = _emb(list(related_works)) mot = _emb([motivation_text(idea)])[0] met = _emb([idea.get("method", "")])[0] n_m = 1.0 - float(np.mean(rw @ mot)) n_s = 1.0 - float(np.mean(rw @ met)) return 0.5 * (n_m + n_s) def diversity(ideas): """1 - mean pairwise cosine among ideas (motivation & method averaged).""" if len(ideas) < 2: return float("nan") mot = _emb([motivation_text(i) for i in ideas]) met = _emb([i.get("method", "") for i in ideas]) def _div(E): S = E @ E.T n = len(E) off = (S.sum() - np.trace(S)) / (n * (n - 1)) return 1.0 - float(off) return 0.5 * (_div(mot) + _div(met))