elorasmus/public/js/pairing.js
agent 226e5f46f8 Add Elo, pair selection and comparison metrics with tests
Ratings are replayed from an event log (undo = skip the target event). K decays
48 -> 16 over a school's first 15 games. Smart pairing favours rarely compared
schools, then close ratings, with 10% exploration and random sides. metrics.js
derives grant, out-of-pocket cost, language and subject fit and the duel rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
2026-11-12 20:55:35 +00:00

66 lines
2.8 KiB
JavaScript

// Choosing the next pair to compare. Pure functions; `rng` is injectable for tests.
//
// "smart" mode spends picks where they teach the ranking the most:
// 1. School A is drawn with weight 1/(1+games)^2, so rarely-compared schools come first.
// 2. School B is drawn with weight
// closeness(A,B) * 1/(1+games_B) * repeatPenalty * recentPenalty
// where closeness is a Gaussian on the rating gap. Its width shrinks as both schools
// gain games: wide while ratings are still rough, narrow once they're established.
// 3. 10% of the time B is uniform-random (exploration), so an early lucky streak can't hide
// a school from strong opponents forever.
// "random" mode: uniform pairs, avoiding immediate repeats.
import { pairKey } from './elo.js';
function weightedPick(items, weightOf, rng) {
const weights = items.map((it) => {
const w = weightOf(it);
return Number.isFinite(w) && w > 0 ? w : 0;
});
const total = weights.reduce((s, w) => s + w, 0);
if (total <= 0) return items[Math.floor(rng() * items.length)];
let r = rng() * total;
for (let i = 0; i < items.length; i++) {
r -= weights[i];
if (r < 0) return items[i];
}
return items[items.length - 1];
}
/**
* @param {Map<string, {code, rating, games, dropped}>} stats from replay()
* @param {object} opts
* mode 'smart' | 'random'
* rng () => [0,1)
* recent pair keys shown recently (picked or skipped), newest last
* pairCounts Map pairKey -> times compared
* @returns {[string, string] | null} codes [left, right], or null if fewer than 2 active schools
*/
export function nextPair(stats, { mode = 'smart', rng = Math.random, recent = [], pairCounts = new Map() } = {}) {
const pool = [...stats.values()].filter((s) => !s.dropped);
if (pool.length < 2) return null;
const recentSet = new Set(recent.slice(-25));
const recentPenalty = (a, b) => (recentSet.has(pairKey(a.code, b.code)) && pool.length > 3 ? 0.01 : 1);
let a;
let b;
if (mode === 'random') {
a = pool[Math.floor(rng() * pool.length)];
b = weightedPick(pool.filter((s) => s !== a), (s) => recentPenalty(a, s), rng);
} else {
a = weightedPick(pool, (s) => 1 / (1 + s.games) ** 2, rng);
const others = pool.filter((s) => s !== a);
if (rng() < 0.1) {
b = weightedPick(others, (s) => recentPenalty(a, s), rng);
} else {
b = weightedPick(others, (s) => {
const width = 120 + 280 / (1 + Math.min(a.games, s.games) / 4);
const closeness = Math.exp(-(((a.rating - s.rating) / width) ** 2));
const repeats = pairCounts.get(pairKey(a.code, s.code)) ?? 0;
return closeness * (1 / (1 + s.games)) * (1 / (1 + 2 * repeats)) * recentPenalty(a, s);
}, rng);
}
}
// Random sides: no school should benefit from always being on the left.
return rng() < 0.5 ? [a.code, b.code] : [b.code, a.code];
}