From f9b8663afbeaff509d9c084ac5b1a68c73838707 Mon Sep 17 00:00:00 2001 From: Pierre MARTIN Date: Thu, 12 Nov 2026 23:55:51 +0100 Subject: [PATCH] Moteur : chances de gagner le match (table de gnubg), export SGF d'un match entier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commande mwc d'engine-cli et engine_match_chances_of : chances au score (getMEAtScore) et conversion d'une équité (eq2mwc), comparées à gnubg de bureau. Export d'un match entier en SGF, relu par gnubg (load match). Co-Authored-By: Claude Opus 5.5 --- app/src/domain/game/engine.ts | 23 ++++++++ app/src/domain/game/sgf.ts | 26 ++++++++- app/src/engine/wasm-engine.ts | 13 +++++ app/tests/game.test.ts | 5 ++ app/tests/tools/sgf-samples.ts | 43 +++++++++++++- engine-cli/main.c | 34 ++++++++++- engine/engine.c | 26 +++++++++ engine/include/engine.h | 26 +++++++++ tests/reference/analyses.json | 56 +++++++++++++++++++ tests/reference/analysis/chances-5-0-0.json | 26 +++++++++ tests/reference/analysis/chances-5-0-2.json | 26 +++++++++ .../analysis/chances-5-2-4-cube-2.json | 26 +++++++++ .../analysis/chances-5-3-4-post-crawford.json | 26 +++++++++ .../analysis/chances-5-4-3-crawford.json | 26 +++++++++ .../analysis/chances-5-4-3-post-crawford.json | 26 +++++++++ ...chances-5-post-crawford-other-on-roll.json | 26 +++++++++ .../analysis/chances-7-1-3-cube-4.json | 26 +++++++++ .../analysis/chances-7-other-on-roll.json | 26 +++++++++ tests/reference/compare.py | 12 ++++ tests/reference/generate.py | 53 +++++++++++++++++- tests/sgf/check.py | 53 +++++++++++++++++- 21 files changed, 596 insertions(+), 8 deletions(-) create mode 100644 tests/reference/analysis/chances-5-0-0.json create mode 100644 tests/reference/analysis/chances-5-0-2.json create mode 100644 tests/reference/analysis/chances-5-2-4-cube-2.json create mode 100644 tests/reference/analysis/chances-5-3-4-post-crawford.json create mode 100644 tests/reference/analysis/chances-5-4-3-crawford.json create mode 100644 tests/reference/analysis/chances-5-4-3-post-crawford.json create mode 100644 tests/reference/analysis/chances-5-post-crawford-other-on-roll.json create mode 100644 tests/reference/analysis/chances-7-1-3-cube-4.json create mode 100644 tests/reference/analysis/chances-7-other-on-roll.json diff --git a/app/src/domain/game/engine.ts b/app/src/domain/game/engine.ts index b667d17..716f84f 100644 --- a/app/src/domain/game/engine.ts +++ b/app/src/domain/game/engine.ts @@ -69,6 +69,12 @@ export interface Engine { points: number, ): Promise; + /** Les chances de gagner le match du joueur au trait (cube.match + * obligatoire), d'après la table d'équité de match du moteur : au + * score, avant la partie, et pour l'équité normalisée equity (une + * évaluation, ou une perte d'équité), avec ce videau et ce score. */ + matchChances(cube: EngineCube, equity: number): Promise; + /** L'analyse du coup joué (played : position après le coup, vue du joueur * qui l'a joué, comme Move.position), au niveau d'analyse ply ; null si * aucun coup n'était possible. */ @@ -129,6 +135,23 @@ export interface MatchDescription { readonly jacoby: boolean; } +/** + * Les chances de gagner le match d'un joueur, selon le moteur (table + * d'équité de match de gnubg, Kazaross-XG2), de 0 à 1. + * + * Glossaire : docs/glossaire/chances-de-gagner-le-match.md + */ +export interface MatchChances { + /** Au score du match, avant la partie (la table elle-même). */ + readonly atScore: number; + /** Pour l'équité demandée (eq2mwc de gnubg). */ + readonly ofEquity: number; + /** L'équité demandée vue comme une perte (ou un gain) : ses chances + * moins celles d'une équité nulle, comme le coût d'une erreur dans + * les statistiques de match de gnubg. */ + readonly ofLoss: number; +} + /** * Niveau de jeu de l'ordinateur : un réglage prédéfini de GNU Backgammon * (aecSettings dans eval.c ; voir engine/include/engine.h). Les quatre diff --git a/app/src/domain/game/sgf.ts b/app/src/domain/game/sgf.ts index 0b2cf71..368ea94 100644 --- a/app/src/domain/game/sgf.ts +++ b/app/src/domain/game/sgf.ts @@ -35,6 +35,9 @@ export interface SgfOptions { readonly computerName: string; /** Date de la partie, AAAA-MM-JJ (DT) ; absente si non fournie. */ readonly date?: string; + /** Dans un match exporté entier, l'indice de la partie (à partir de 0, + * comme MI[game:…] dans gnubg) ; 0 par défaut. */ + readonly gameIndex?: number; } /** Un nœud SGF et les variantes qui le suivent. */ @@ -82,13 +85,14 @@ export async function exportSgf( // sa longueur et le score avant la partie (W, l'ordinateur, est le // joueur 0 de gnubg), et les règles comme gnubg les écrit (sgf.c) : // Crawford, et CrawfordGame pour la partie Crawford. Le numéro de la - // partie dans le match n'est pas gardé : 0. + // partie dans le match : celui que donne l'appelant (exportMatchSgf), + // 0 pour une partie exportée seule. const { result, match } = replay(currentLine(journal)); const root = [ "FF[4]GM[6]CA[UTF-8]AP[Ustagammon]", match === null ? "MI[length:0][game:0][ws:0][bs:0]" - : `MI[length:${match.length}][game:0][ws:${match.score.computer}][bs:${match.score.player}]` + + : `MI[length:${match.length}][game:${options.gameIndex ?? 0}][ws:${match.score.computer}][bs:${match.score.player}]` + `RU[Crawford${match.crawford ? ":CrawfordGame" : ""}]`, `PW[${escapeText(options.computerName)}]PB[${escapeText(options.playerName)}]`, options.date === undefined ? "" : `DT[${escapeText(options.date)}]`, @@ -103,6 +107,24 @@ export async function exportSgf( return `(;${root}${sequence(tree)})\n`; } +/** + * Un match entier au format SGF de GNU Backgammon : ses parties dans + * l'ordre, chacune son arbre (MI porte son numéro et le score avant elle), + * à la suite dans le même fichier, comme gnubg enregistre un match (« save + * match ») et le relit (« load match »). + */ +export async function exportMatchSgf( + engine: Engine, + journals: readonly GameJournal[], + options: Omit, +): Promise { + const games: string[] = []; + for (const [gameIndex, journal] of journals.entries()) { + games.push(await exportSgf(engine, journal, { ...options, gameIndex })); + } + return games.join(""); +} + /** Écrit des nœuds : une suite tant qu'il n'y a qu'une variante, puis * chaque variante entre parenthèses. */ function sequence(nodes: readonly SgfNode[]): string { diff --git a/app/src/engine/wasm-engine.ts b/app/src/engine/wasm-engine.ts index 7712aec..4e15d3d 100644 --- a/app/src/engine/wasm-engine.ts +++ b/app/src/engine/wasm-engine.ts @@ -24,6 +24,7 @@ import type { Level, Luck, LuckRating, + MatchChances, MatchDescription, MatchId, Move, @@ -167,6 +168,18 @@ export function createWasmEngine(run: RunCommand): Engine { }; }, + async matchChances(cube: EngineCube, equity: number): Promise { + if (cube.match === undefined) { + throw new Error("pas de match : pas de chances de gagner le match"); + } + const answer = (await run(["mwc", await matchId(cube), String(equity)])) as { + at_score: number; + mwc: number; + loss: number; + }; + return { atScore: answer.at_score, ofEquity: answer.mwc, ofLoss: answer.loss }; + }, + async analyseMove( position: PositionId, cube: EngineCube, diff --git a/app/tests/game.test.ts b/app/tests/game.test.ts index 901657f..83b31e7 100644 --- a/app/tests/game.test.ts +++ b/app/tests/game.test.ts @@ -72,6 +72,11 @@ function fakeEngine(overrides: Partial = {}): Engine { crawford: score[winner] === match.length - 1, }; }, + matchChances: async (_cube, equity) => ({ + atScore: 0.5, + ofEquity: 0.5 + equity / 4, + ofLoss: equity / 4, + }), analyseMove: async () => null, analyseCube: async () => ({ analysed: false, diff --git a/app/tests/tools/sgf-samples.ts b/app/tests/tools/sgf-samples.ts index 065949e..c3ae8b8 100644 --- a/app/tests/tools/sgf-samples.ts +++ b/app/tests/tools/sgf-samples.ts @@ -15,8 +15,9 @@ import { mkdirSync, writeFileSync } from "node:fs"; import { join } from "node:path"; -import { Game } from "../../src/domain/game/game.js"; -import { exportSgf } from "../../src/domain/game/sgf.js"; +import { Game, type NewGame } from "../../src/domain/game/game.js"; +import { firstGameOf } from "../../src/domain/game/match.js"; +import { exportMatchSgf, exportSgf } from "../../src/domain/game/sgf.js"; import { loadNodeEngine } from "../support/node-engine.js"; import { playMoves, playToTheEnd, seededDice, step } from "../support/play.js"; @@ -133,6 +134,44 @@ async function main(): Promise { } game.goBackTo(original.head as string); } + // Un match entier en 3 points (étape 1l), exporté d'un seul fichier : + // chaque partie à la suite, avec son numéro et le score avant elle. La + // première graine dont le match compte au moins trois parties. + for (let seed = 21; ; seed++) { + const dice = seededDice(seed); + const played: Game[] = []; + let next: Game | null = Game.start(engine, { + dice, + level: "0ply", + match: firstGameOf(3, "exemple"), + }); + while (next !== null) { + await playToTheEnd(next, engine); + played.push(next); + const following: NewGame | null = next.nextGame; + next = following === null ? null : Game.start(engine, { ...following, dice }); + } + if (played.length < 3) { + continue; + } + const sgf = await exportMatchSgf( + engine, + played.map((g) => g.journal), + { playerName: "Vous", computerName: "Ordinateur", date: "2026-11-12" }, + ); + const expected = { + length: 3, + games: played.map((g) => ({ + after: g.events.flatMap((e) => (e.type === "MovePlayed" ? [e.position] : [])), + result: g.state.result, + score: g.state.match?.score, + })), + }; + writeFileSync(join(out, `match-${seed}.sgf`), sgf); + writeFileSync(join(out, `match-${seed}.json`), `${JSON.stringify(expected)}\n`); + files++; + break; + } console.log(`${files} parties exportées dans ${out}`); } diff --git a/engine-cli/main.c b/engine-cli/main.c index 421133e..4ff01dc 100644 --- a/engine-cli/main.c +++ b/engine-cli/main.c @@ -50,6 +50,11 @@ * no_double ou double (joueur au trait), * take ou pass (son adversaire) * luck POSITION_ID MATCH_ID DÉS chance du jet pour le joueur au trait + * mwc MATCH_ID [ÉQUITÉ] chances de gagner le match du joueur + * au trait : au score, avant la + * partie, et pour une équité + * normalisée (0 par défaut), en + * chances et en perte * * --ply : niveau de gnubg, de 0 (défaut) à 4 (meilleur coup comme « hint », * analyses comme l'analyse de partie de gnubg). @@ -83,7 +88,7 @@ /* Liste des commandes, pour les messages d'erreur. */ #define COMMANDS "bestmove, moves, legal, cube, eval, position, halfmoves, matchid, reference, " \ - "levels, analysemove, analysecube, luck, double, take ou aftergame" + "levels, analysemove, analysecube, luck, double, take, aftergame ou mwc" /* Coups gardés dans le classement de la commande reference (comme hint). */ #define REFERENCE_MAX_MOVES 10 @@ -597,6 +602,31 @@ run_after_game_command(const options *opt) return EXIT_SUCCESS; } +/* mwc : chances de gagner le match (voir engine_match_chances_of). */ +static int +run_mwc_command(const options *opt) +{ + engine_match_chances chances; + engine_status status; + float equity = 0.0f; + char *end; + + if (opt->arg_count != 1 && opt->arg_count != 2) + return usage_error("arguments attendus : MATCH_ID [ÉQUITÉ]"); + if (opt->arg_count == 2) { + equity = strtof(opt->args[1], &end); + if (end == opt->args[1] || *end != '\0') + return usage_error("ÉQUITÉ attend un nombre"); + } + status = engine_match_chances_of(opt->args[0], equity, &chances); + if (status != ENGINE_OK) + return print_engine_error(status); + printf("{\"match_id\": \"%s\", \"equity\": " NUMBER ", \"at_score\": " NUMBER + ", \"mwc\": " NUMBER ", \"loss\": " NUMBER "}\n", opt->args[0], equity, + chances.at_score, chances.mwc, chances.loss); + return EXIT_SUCCESS; +} + /* * reference :un niveau d'un fichier de résultat de tests/reference/, de * même structure que plies[""] (voir tests/reference/README.md). @@ -886,6 +916,8 @@ main(int argc, char **argv) result = run_analyse_cube_command(&opt); else if (strcmp(opt.command, "luck") == 0) result = run_luck_command(&opt); + else if (strcmp(opt.command, "mwc") == 0) + result = run_mwc_command(&opt); else result = usage_error("commande inconnue : " COMMANDS); diff --git a/engine/engine.c b/engine/engine.c index b40be99..ec0d743 100644 --- a/engine/engine.c +++ b/engine/engine.c @@ -838,6 +838,32 @@ engine_match_after_game(const char *match_id, int winner, int points, return ENGINE_OK; } +/* --------------------------------------- Chances de gagner le match */ + +engine_status +engine_match_chances_of(const char *match_id, float equity, engine_match_chances *chances) +{ + engine_match m; + cubeinfo cube; + engine_status status; + + if (engine_state != READY) + return ENGINE_ERROR_NOT_INITIALISED; + status = engine_decode_match(match_id, &m); + if (status != ENGINE_OK) + return status; + if (m.match_length == 0 + || SetCubeInfo(&cube, m.cube_value, m.cube_owner, m.player_on_roll, m.match_length, + m.score, m.crawford, m.jacoby, DEFAULT_BEAVERS, VARIATION_STANDARD) != 0) + return ENGINE_ERROR_MATCH_ID; + chances->at_score = getMEAtScore(m.score[0], m.score[1], m.match_length, m.player_on_roll, + m.crawford, aafMET, aafMETPostCrawford); + chances->mwc = eq2mwc(equity, &cube); + /* analysis.c, updateStatcontext : « eq2mwc(rSkill, &ci) - eq2mwc(0.0f, &ci) ». */ + chances->loss = eq2mwc(equity, &cube) - eq2mwc(0.0f, &cube); + return ENGINE_OK; +} + /* -------------------------------------------- Analyse d'une décision de videau */ /* Le plus petit de deux nombres. */ diff --git a/engine/include/engine.h b/engine/include/engine.h index 66197ad..edef11d 100644 --- a/engine/include/engine.h +++ b/engine/include/engine.h @@ -452,6 +452,32 @@ typedef struct { engine_status engine_match_after_game(const char *match_id, int winner, int points, engine_match_after *after); +/* Chances de gagner le match, d'après la table d'équité de match de gnubg + * (Kazaross-XG2, celle de gnubg de bureau par défaut). */ +typedef struct { + float at_score; /* chances du joueur au trait de gagner le match + * au score du Match ID, avant la partie + * (getMEAtScore de gnubg, matchequity.c, + * partie Crawford et suivantes comprises) */ + float mwc; /* l'équité normalisée donnée, pour ce videau et + * ce score, convertie en chances de gagner le + * match du joueur au trait (eq2mwc) */ + float loss; /* la même équité vue comme une perte : eq2mwc + * de l'équité moins eq2mwc de 0, comme le coût + * d'une erreur dans les statistiques de match de + * gnubg (updateStatcontext, analysis.c) */ +} engine_match_chances; + +/* + * Chances de gagner le match : au score du Match ID (avant la partie), et + * pour une équité normalisée (videau, score, joueur au trait et partie + * Crawford du Match ID ; les dés et le propriétaire du videau sont + * ignorés). ENGINE_ERROR_MATCH_ID en partie libre (longueur 0) : il n'y a + * pas de match. + */ +engine_status engine_match_chances_of(const char *match_id, float equity, + engine_match_chances *chances); + /* ------------------------------------------------------------- Analyse */ /* Appréciation de gnubg pour une perte d'équité (0 ou négative). */ diff --git a/tests/reference/analyses.json b/tests/reference/analyses.json index 1eeb7fb..d131b4e 100644 --- a/tests/reference/analyses.json +++ b/tests/reference/analyses.json @@ -279,5 +279,61 @@ "match_id": "cAkAAAAAAAAA", "dice": null } + ], + "chances": [ + { + "id": "chances-5-0-0", + "description": "Match en 5 points à 0-0 : table d'avant Crawford.", + "match_id": "MAGgAAAAAAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-0-2", + "description": "Match en 5 points, joueur au trait mené 0-2.", + "match_id": "MAGgAAAAEAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-2-4-cube-2", + "description": "Match en 5 points, 2-4, videau à 2 chez l'adversaire.", + "match_id": "EQGgACAAIAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-4-3-crawford", + "description": "Match en 5 points, 4-3, partie Crawford.", + "match_id": "sAGgAEAAGAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-4-3-post-crawford", + "description": "Match en 5 points, 4-3, après la partie Crawford.", + "match_id": "MAGgAEAAGAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-3-4-post-crawford", + "description": "Match en 5 points, 3-4, après la partie Crawford, joueur au trait mené.", + "match_id": "MAGgADAAIAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-7-1-3-cube-4", + "description": "Match en 7 points, 1-3, videau à 4 chez le joueur au trait.", + "match_id": "AgHgABAAGAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-7-other-on-roll", + "description": "Match en 7 points, l'autre joueur au trait (Match ID de match-7-cube).", + "match_id": "cAngAFAAGAAE", + "equities": [-0.25, -0.03, 0.4] + }, + { + "id": "chances-5-post-crawford-other-on-roll", + "description": "Match en 5 points après Crawford, l'autre joueur au trait (Match ID de match-post-crawford-cube).", + "match_id": "cAmgAEAAEAAE", + "equities": [-0.25, -0.03, 0.4] + } ] } diff --git a/tests/reference/analysis/chances-5-0-0.json b/tests/reference/analysis/chances-5-0-0.json new file mode 100644 index 0000000..7ebabd4 --- /dev/null +++ b/tests/reference/analysis/chances-5-0-0.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-0-0", + "description": "Match en 5 points à 0-0 : table d'avant Crawford.", + "match_id": "MAGgAAAAAAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.48067, + "loss": -0.019329995 + }, + { + "equity": -0.03, + "mwc": 0.4976804, + "loss": -0.0023196042 + }, + { + "equity": 0.4, + "mwc": 0.530928, + "loss": 0.030928016 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.5 +} diff --git a/tests/reference/analysis/chances-5-0-2.json b/tests/reference/analysis/chances-5-0-2.json new file mode 100644 index 0000000..1fbd0fc --- /dev/null +++ b/tests/reference/analysis/chances-5-0-2.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-0-2", + "description": "Match en 5 points, joueur au trait mené 0-2.", + "match_id": "MAGgAAAAEAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.32094374, + "loss": -0.021511257 + }, + { + "equity": -0.03, + "mwc": 0.33987364, + "loss": -0.002581358 + }, + { + "equity": 0.4, + "mwc": 0.376873, + "loss": 0.034417987 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.35205 +} diff --git a/tests/reference/analysis/chances-5-2-4-cube-2.json b/tests/reference/analysis/chances-5-2-4-cube-2.json new file mode 100644 index 0000000..2f28d7f --- /dev/null +++ b/tests/reference/analysis/chances-5-2-4-cube-2.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-2-4-cube-2", + "description": "Match en 5 points, 2-4, videau à 2 chez l'adversaire.", + "match_id": "EQGgACAAIAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.1875, + "loss": -0.0625 + }, + { + "equity": -0.03, + "mwc": 0.2425, + "loss": -0.007499993 + }, + { + "equity": 0.4, + "mwc": 0.35, + "loss": 0.099999994 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.32264 +} diff --git a/tests/reference/analysis/chances-5-3-4-post-crawford.json b/tests/reference/analysis/chances-5-3-4-post-crawford.json new file mode 100644 index 0000000..1db6bdf --- /dev/null +++ b/tests/reference/analysis/chances-5-3-4-post-crawford.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-3-4-post-crawford", + "description": "Match en 5 points, 3-4, après la partie Crawford, joueur au trait mené.", + "match_id": "MAGgADAAIAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.1875, + "loss": -0.0625 + }, + { + "equity": -0.03, + "mwc": 0.2425, + "loss": -0.007499993 + }, + { + "equity": 0.4, + "mwc": 0.35, + "loss": 0.099999994 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.48803 +} diff --git a/tests/reference/analysis/chances-5-4-3-crawford.json b/tests/reference/analysis/chances-5-4-3-crawford.json new file mode 100644 index 0000000..4544390 --- /dev/null +++ b/tests/reference/analysis/chances-5-4-3-crawford.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-4-3-crawford", + "description": "Match en 5 points, 4-3, partie Crawford.", + "match_id": "sAGgAEAAGAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.6875, + "loss": -0.0625 + }, + { + "equity": -0.03, + "mwc": 0.7425, + "loss": -0.007499993 + }, + { + "equity": 0.4, + "mwc": 0.85, + "loss": 0.100000024 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.67736 +} diff --git a/tests/reference/analysis/chances-5-4-3-post-crawford.json b/tests/reference/analysis/chances-5-4-3-post-crawford.json new file mode 100644 index 0000000..ed076a6 --- /dev/null +++ b/tests/reference/analysis/chances-5-4-3-post-crawford.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-4-3-post-crawford", + "description": "Match en 5 points, 4-3, après la partie Crawford.", + "match_id": "MAGgAEAAGAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.6875, + "loss": -0.0625 + }, + { + "equity": -0.03, + "mwc": 0.7425, + "loss": -0.007499993 + }, + { + "equity": 0.4, + "mwc": 0.85, + "loss": 0.100000024 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.51197004 +} diff --git a/tests/reference/analysis/chances-5-post-crawford-other-on-roll.json b/tests/reference/analysis/chances-5-post-crawford-other-on-roll.json new file mode 100644 index 0000000..0c7f926 --- /dev/null +++ b/tests/reference/analysis/chances-5-post-crawford-other-on-roll.json @@ -0,0 +1,26 @@ +{ + "id": "chances-5-post-crawford-other-on-roll", + "description": "Match en 5 points après Crawford, l'autre joueur au trait (Match ID de match-post-crawford-cube).", + "match_id": "cAmgAEAAEAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.18301125, + "loss": -0.061003745 + }, + { + "equity": -0.03, + "mwc": 0.23669454, + "loss": -0.0073204488 + }, + { + "equity": 0.4, + "mwc": 0.34162098, + "loss": 0.09760599 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.32264 +} diff --git a/tests/reference/analysis/chances-7-1-3-cube-4.json b/tests/reference/analysis/chances-7-1-3-cube-4.json new file mode 100644 index 0000000..a4216ef --- /dev/null +++ b/tests/reference/analysis/chances-7-1-3-cube-4.json @@ -0,0 +1,26 @@ +{ + "id": "chances-7-1-3-cube-4", + "description": "Match en 7 points, 1-3, videau à 4 chez le joueur au trait.", + "match_id": "AgHgABAAGAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.2507625, + "loss": -0.0835875 + }, + { + "equity": -0.03, + "mwc": 0.32431948, + "loss": -0.010030508 + }, + { + "equity": 0.4, + "mwc": 0.46809, + "loss": 0.13374001 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.35715 +} diff --git a/tests/reference/analysis/chances-7-other-on-roll.json b/tests/reference/analysis/chances-7-other-on-roll.json new file mode 100644 index 0000000..cbbaf1d --- /dev/null +++ b/tests/reference/analysis/chances-7-other-on-roll.json @@ -0,0 +1,26 @@ +{ + "id": "chances-7-other-on-roll", + "description": "Match en 7 points, l'autre joueur au trait (Match ID de match-7-cube).", + "match_id": "cAngAFAAGAAE", + "equities": [ + { + "equity": -0.25, + "mwc": 0.26622373, + "loss": -0.02686125 + }, + { + "equity": -0.03, + "mwc": 0.28986162, + "loss": -0.0032233596 + }, + { + "equity": 0.4, + "mwc": 0.33606297, + "loss": 0.04297799 + } + ], + "type": "chance", + "gnubg_commit": "14b67984737ded0f6de43d63dcb789f729d9b06b", + "met": "Kazaross-XG2", + "at_score": 0.33130002 +} diff --git a/tests/reference/compare.py b/tests/reference/compare.py index 8bdd272..318372e 100644 --- a/tests/reference/compare.py +++ b/tests/reference/compare.py @@ -255,6 +255,16 @@ def compare_cube_analysis(report, cli, case): expected["equity_loss"], SKILL_THRESHOLDS) +def compare_chances(report, cli, case): + """Chances de gagner le match : table au score, conversion d'équités.""" + for item in case["equities"]: + actual = run_cli(cli, ["mwc", case["match_id"], repr(item["equity"])]) + where = "%s, équité %s" % (case["id"], item["equity"]) + report.number(where, "at_score", case["at_score"], actual["at_score"]) + report.number(where, "mwc", item["mwc"], actual["mwc"]) + report.number(where, "loss", item["loss"], actual["loss"]) + + def compare_levels(report, cli, case, settings): for name, expected in sorted(case["levels"].items()): where = "%s niveau %s" % (case["id"], name) @@ -280,6 +290,8 @@ def compare_analyses(report, cli, folder): compare_move_analysis(report, cli, case) elif case["type"] == "cube": compare_cube_analysis(report, cli, case) + elif case["type"] == "chance": + compare_chances(report, cli, case) else: compare_levels(report, cli, case, settings) return len(paths) diff --git a/tests/reference/generate.py b/tests/reference/generate.py index 2a6b9b6..14ba0a8 100644 --- a/tests/reference/generate.py +++ b/tests/reference/generate.py @@ -391,6 +391,53 @@ def level_cube(case, name): } +# -------------------------------------------------------------------------- +# Chances de gagner le match (analyses.json, « chances ») : la table d'équité +# de match de gnubg au score du Match ID, et la conversion d'une équité +# normalisée en chances de gagner le match (eq2mwc), avec le coût d'une +# erreur des statistiques de match (updateStatcontext, analysis.c : +# « eq2mwc(rSkill, &ci) - eq2mwc(0.0f, &ci) »). + +START_POSITION = "4HPwATDgc/ABMA" + + +def single(x): + """x arrondi en simple précision, comme le calcul de gnubg en float.""" + return struct.unpack(" 0: + gnubg.command("next game") + info = game["info"] + score = (info.get("score-X"), info.get("score-O")) + if score != (want["score"]["computer"], want["score"]["player"]): + errors.append("%sscore avant la partie : gnubg %s, journal %s" % (where, score, want["score"])) + errors += check_game_records(list(game["game"]), want, where) + result = want["result"] + winner = "O" if result["winner"] == "player" else "X" + if (info.get("winner"), info.get("points-won")) != (winner, result["points"]): + errors.append("%srésultat : gnubg %s, journal %s" % (where, info, result)) + return errors + + failures = 0 paths = sorted(glob.glob(os.path.join(FOLDER, "*.sgf"))) for path in paths: - errors = check(path) + errors = check_match(path) if os.path.basename(path).startswith("match-") else check(path) name = os.path.basename(path) if errors: failures += 1