SPB Git

Repository Public

ranking.ts 3.1 KB 85 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : packages/engine/src/ranking.ts9 * Module           : engine10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Ranking Engine.45 *46 * Ordre officiel :47 *  1. nombre de bonnes réponses, décroissant ;48 *  2. erreur relative moyenne, croissante ;49 *  3. erreur relative médiane, croissante ;50 *  4. rang partagé si l'égalité persiste.51 * Le temps de réponse n'intervient jamais.52 */53export interface RankInput {54  id: string;55  correct: number;56  /** Erreur relative moyenne (%). */57  mape: number;58  /** Erreur relative médiane (%). */59  medianRelError: number;60}6162export type Ranked<T> = T & { rank: number; tied: boolean };6364const EPSILON = 1e-9;6566export function compareEntries(a: RankInput, b: RankInput): number {67  if (a.correct !== b.correct) return b.correct - a.correct;68  if (Math.abs(a.mape - b.mape) > EPSILON) return a.mape - b.mape;69  if (Math.abs(a.medianRelError - b.medianRelError) > EPSILON) return a.medianRelError - b.medianRelError;70  return 0;71}7273/** Classement « 1, 2, 2, 4 » : les ex æquo partagent le rang, le suivant saute. */74export function rankEntries<T extends RankInput>(entries: readonly T[]): Ranked<T>[] {75  const sorted = [...entries].sort((a, b) => compareEntries(a, b) || a.id.localeCompare(b.id));76  const out: Ranked<T>[] = [];77  sorted.forEach((entry, index) => {78    const previous = out[index - 1];79    const same = previous !== undefined && compareEntries(previous, entry) === 0;80    out.push({ ...entry, rank: same ? previous.rank : index + 1, tied: same });81    if (same) previous.tied = true;82  });83  return out;84}85
Files main
Menu

Theme