Moteur : chances de gagner le match (table de gnubg), export SGF d'un match entier

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 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-12 23:55:51 +01:00
co-authored by Claude Opus 5.5
parent ee661661fd
commit f9b8663afb
21 changed files with 596 additions and 8 deletions
+56
View File
@@ -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]
}
]
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
+12
View File
@@ -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)
+51 -2
View File
@@ -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("<f", struct.pack("<f", x))[0]
def met_at_score(ci):
"""getMEAtScore de gnubg (matchequity.c) pour le joueur au trait : une
lecture de la table que gnubg donne (gnubg.met), sans autre calcul.
Avant ou pendant la partie Crawford, la table d'avant Crawford ; après,
la table d'après Crawford du joueur qui mène."""
length = ci["matchto"]
table = gnubg.met(length)
away0 = length - ci["score"][0] - 1
away1 = length - ci["score"][1] - 1
if not ci["crawford"] and (away0 == 0 or away1 == 0):
me0 = single(1.0 - table[2][away1]) if away0 == 0 else table[1][away0]
else:
me0 = table[0][away0][away1]
return me0 if ci["move"] == 0 else single(1.0 - me0)
def match_chances(case):
gnubg.command("set gnubgid %s:%s" % (START_POSITION, case["match_id"]))
if gnubg.matchid() != case["match_id"]:
raise SystemExit("%s : gnubg relit %s" % (case["id"], gnubg.matchid()))
ci = cube_info()
zero = gnubg.eq2mwc(0.0, ci)
return {
"at_score": f32(met_at_score(ci)),
"equities": [
{"equity": equity, "mwc": f32(gnubg.eq2mwc(equity, ci)),
"loss": f32(single(gnubg.eq2mwc(equity, ci) - zero))}
for equity in case["equities"]
],
}
def write_analysis(out_dir, result):
with open(os.path.join(out_dir, result["id"] + ".json"), "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=2)
@@ -408,10 +455,12 @@ def generate_analyses(commit):
gnubg.command("set display off")
gnubg.command("set automatic game off")
gnubg.command("set automatic roll off")
for kind in ("moves", "cubes", "levels"):
for kind in ("moves", "cubes", "levels", "chances"):
for case in cases[kind]:
result = dict(case, type=kind[:-1], gnubg_commit=commit, met=MET)
if kind == "moves":
if kind == "chances":
result.update(match_chances(case))
elif kind == "moves":
result["plies"] = {}
for ply in PLIES:
result["plies"][str(ply)], result["luck"] = analyse_move(case, ply)
+52 -1
View File
@@ -67,10 +67,61 @@ def check(path):
return errors
def check_game_records(records, expected, where):
"""Avance dans la partie courante de gnubg enregistrement par
enregistrement : après chaque coup, sa position doit être celle du
journal."""
gnubg.command("first move")
after = []
for record in records:
gnubg.command("next")
if record["action"] == "move":
after.append(gnubg.positionid())
errors = []
if len(after) != len(expected["after"]):
errors.append("%s%d coups lus, %d attendus" % (where, len(after), len(expected["after"])))
for i, (got, want) in enumerate(zip(after, expected["after"])):
if got != want:
errors.append("%saprès le coup %d : gnubg %s, journal %s" % (where, i + 1, got, want))
break
return errors
def check_match(path):
"""Un match entier (étape 1l) : gnubg charge le fichier (load match),
y trouve la longueur du match, et pour chaque partie, dans l'ordre, le
score avant elle, les positions après chaque coup et le résultat."""
with open(path[:-4] + ".json") as f:
expected = json.load(f)
gnubg.command("load match " + path)
match = gnubg.match(analysis=0, boards=0, statistics=0, verbose=0)
games = match["games"]
errors = []
if match["match-info"].get("match-length") != expected["length"]:
errors.append("longueur : gnubg %s" % match["match-info"])
if len(games) != len(expected["games"]):
return errors + ["%d parties lues, %d attendues" % (len(games), len(expected["games"]))]
gnubg.command("first game")
for index, (game, want) in enumerate(zip(games, expected["games"])):
where = "partie %d : " % (index + 1)
if index > 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