UNPKG

gepa-spo

Version:

Genetic-Pareto prompt optimizer to evolve system prompts from a few rollouts with modular support and intelligent crossover

34 lines (33 loc) 1.09 kB
/** UCB1 bandit to select strategy IDs by historical uplift */ export class UCB1 { t = 0; stats; constructor(ids) { this.stats = ids.map(id => ({ id, n: 0, mean: 0 })); } pick() { this.t++; const c = Math.sqrt(2); let bestId = this.stats[0]?.id ?? ''; let bestU = -Infinity; for (const s of this.stats) { const bonus = s.n ? c * Math.sqrt(Math.log(this.t) / s.n) : Number.POSITIVE_INFINITY; const u = (s.n ? s.mean : 0) + bonus; if (u > bestU) { bestU = u; bestId = s.id; } } return bestId; } update(id, reward) { const s = this.stats.find(x => x.id === id); if (!s) return; const r = Math.max(0, Math.min(1, reward)); s.n += 1; s.mean += (r - s.mean) / s.n; } serialize() { return { t: this.t, stats: this.stats.map(s => ({ ...s })) }; } static from(obj) { const b = new UCB1([]); b.t = obj.t; b.stats = obj.stats.map(s => ({ ...s })); return b; } }