b282709476
The v1.3.1 comparator used a sum-of-weights priorityScore. With weights
15..1 across 15 specs, three lower-priority specs (BNK+BRM+CRF, sum 39)
could outrank a single top-priority spec (HCR alone, sum 15). In
priority-order mode this surfaced lower-priority plans above the user's
top spec — the opposite of intent.
Fix: replace sum-of-weights with a lexicographic rank weight. Each spec
encodes as a bit, top-ranked spec = highest bit. So [HCR] = 16384 beats
[BNK,BRM,CRF,EMT,ENT,FIN,FIM,GLB,LCM,MGT,MKT,MTO,SBI,STR] = 16383. A plan
containing a higher-ranked spec ALWAYS outranks any plan that doesn't,
regardless of how many lower-ranked specs the latter contains. Lower
specs only act as tiebreakers among plans that all contain the same
higher-ranked spec.
Both modes use lex weight as the priority key; modes still differ in
ordering:
priority-order: (rankWeight desc, count desc, key asc)
maximize-count: (count desc, rankWeight desc, key asc)
Score display changes from the legacy sum (e.g. "score 29") to the lex
weight in compact form (e.g. "score 24.6k"). Hover for full integer.
The display now actually corresponds to ranking order.
Other:
- Cache cap (500k leaves) now retains existing entries instead of
clearing on overflow. New entries past the cap are dropped; the
cached subset stays available as a warm starting point.
- Two new lex-weight tests in searchDecisionTree.test.ts:
- single top-ranked spec outweighs all 14 others combined
- tiebreaker is the next-ranked spec
- All 84 tests pass; cached leaves stay valid across the comparator
change since achievedSpecs (the input to lex compare) is unchanged.
Files: solver/priority.ts (new functions), solver/decisionTree.ts
(comparators take ranking), components/{TopPlans,CourseSelection}.tsx
(score display + Recommended badge), state/appState.ts (cache-cap
behavior), vite.config.ts, CHANGELOG.md.
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { SPECIALIZATIONS } from '../data/specializations';
|
|
|
|
const FALLBACK_RANK = SPECIALIZATIONS.length - 1;
|
|
const MAX_RANK_WEIGHT = SPECIALIZATIONS.length;
|
|
|
|
export function priorityScore(specs: string[], ranking: string[]): number {
|
|
const rankIndex = new Map(ranking.map((id, i) => [id, i]));
|
|
return specs.reduce(
|
|
(sum, id) => sum + (MAX_RANK_WEIGHT - (rankIndex.get(id) ?? FALLBACK_RANK)),
|
|
0,
|
|
);
|
|
}
|
|
|
|
export function makePriorityScorer(ranking: string[]): (specs: string[]) => number {
|
|
const rankIndex = new Map(ranking.map((id, i) => [id, i]));
|
|
return (specs) =>
|
|
specs.reduce(
|
|
(sum, id) => sum + (MAX_RANK_WEIGHT - (rankIndex.get(id) ?? FALLBACK_RANK)),
|
|
0,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Lexicographic comparison weight: each spec encoded as a bit, top-ranked
|
|
* spec = highest bit. Higher rankWeight means a strictly preferred plan
|
|
* under priority order (a single top-ranked spec outweighs any combination
|
|
* of lower-ranked specs). Used by comparators; not for display.
|
|
*/
|
|
export function priorityRankWeight(specs: string[], ranking: string[]): number {
|
|
const rankIndex = new Map(ranking.map((id, i) => [id, i]));
|
|
const N = ranking.length;
|
|
let w = 0;
|
|
for (const id of specs) {
|
|
const r = rankIndex.get(id);
|
|
if (r === undefined) continue;
|
|
w += 1 << (N - 1 - r);
|
|
}
|
|
return w;
|
|
}
|
|
|
|
export function makePriorityRankWeight(ranking: string[]): (specs: string[]) => number {
|
|
const rankIndex = new Map(ranking.map((id, i) => [id, i]));
|
|
const N = ranking.length;
|
|
return (specs) => {
|
|
let w = 0;
|
|
for (const id of specs) {
|
|
const r = rankIndex.get(id);
|
|
if (r === undefined) continue;
|
|
w += 1 << (N - 1 - r);
|
|
}
|
|
return w;
|
|
};
|
|
}
|