Files
pokerfreak/score/messung.py
T

99 lines
4.3 KiB
Python

"""Pure parts of the score: map an answer of a version onto the reference menu, and the loss against the frozen reference.
Reference file of a spot (written by `sharingan-referenz`): `actions` (fold / check / call / raise with `to`, `frac` = raise size as a
fraction of the pot after calling, `all_in`), `hands` [{c, w, q: [chips per action], sigma}], `pot`, `to_call`, `cur_bet`, `max_raise_to`.
Loss of a version at a spot (chips): sum_h w(h) * ( max_a Q(h,a) - sum_a pi(a|h) Q(h,a) ), where pi is the version's mixture over the menu.
"""
JAM_SHARE = 0.97 # a raise of at least this share of the largest legal raise is the all-in
CHIPS_PER_BB = 100
def harmonic(fracs, f):
"""Pseudo-harmonic mapping (Ganzfried/Sandholm) of the pot fraction `f` onto the sorted fractions `fracs`: {index: probability}."""
if f <= fracs[0]:
return {0: 1.0}
if f >= fracs[-1]:
return {len(fracs) - 1: 1.0}
j = max(i for i, x in enumerate(fracs) if x <= f)
a, b = fracs[j], fracs[j + 1]
if f == a:
return {j: 1.0}
pa = (b - f) * (1 + a) / ((b - a) * (1 + f))
return {j: pa, j + 1: 1.0 - pa}
def round_half_up(x):
import math
return int(math.floor(x + 0.5))
def map_to(ref, to):
"""Raise to `to` chips (total on the street) -> ({action index: probability}, distance in pot fractions)."""
raises = [i for i, a in enumerate(ref["actions"]) if a["kind"] == "raise"]
jam = [i for i in raises if ref["actions"][i].get("all_in")]
if to >= JAM_SHARE * ref["max_raise_to"]:
return {(jam or raises)[-1]: 1.0}, 0.0
f = (to - ref["cur_bet"]) / (ref["pot"] + ref["to_call"])
fracs = [ref["actions"][i]["frac"] for i in raises]
m = harmonic(fracs, f)
return {raises[j]: p for j, p in m.items()}, min(abs(f - x) for x in fracs)
def map_answer(ref, answer, big_blind):
"""-> ({action index: probability}, distance in pot fractions or None). `answer` = {action, size} of the motor, size in money."""
acts = ref["actions"]
idx = {k: [i for i, a in enumerate(acts) if a["kind"] == k] for k in ("fold", "check", "call", "raise")}
passive = (idx["check"] or idx["call"])[0]
kind = answer.get("action")
if kind == "FOLD":
return ({idx["fold"][0]: 1.0} if idx["fold"] else {passive: 1.0}), None
if kind in ("CHECK", "CALL"):
return {passive: 1.0}, None
if kind not in ("RAISE", "BET", "ALL_IN") or not idx["raise"]:
return {passive: 1.0}, None
if kind == "ALL_IN":
return map_to(ref, ref["max_raise_to"])
return map_to(ref, round_half_up(answer["size"] / big_blind * CHIPS_PER_BB))
def map_menu_entry(ref, art, size_bb):
"""Entry of the motor's `knoten_mischung.menue` (art 0 fold, 1 check/call, 2 bet/raise/all-in to `size_bb` big blinds) -> mapping."""
acts = ref["actions"]
passive = next(i for i, a in enumerate(acts) if a["kind"] in ("check", "call"))
if art == 0:
fold = [i for i, a in enumerate(acts) if a["kind"] == "fold"]
return ({fold[0]: 1.0} if fold else {passive: 1.0}), None
if art == 1:
return {passive: 1.0}, None
return map_to(ref, round_half_up(size_bb * CHIPS_PER_BB))
def mixture(ref, answers, big_blind):
"""Empirical mixture over the menu from the answers of several seeds: ({index: p}, mean distance)."""
pi, dist, nd = {}, 0.0, 0
for a in answers:
m, d = map_answer(ref, a, big_blind)
for i, p in m.items():
pi[i] = pi.get(i, 0.0) + p / len(answers)
if d is not None:
dist += d
nd += 1
return pi, (dist / nd if nd else None)
def loss_chips(ref, pi_of_hand):
"""Loss of a version at one spot, chips: `pi_of_hand[c]` = {action index: p} for hand string `c`; hands without answer count as full loss-free?
No: a missing hand is an error (raises KeyError) so that a gap never counts as zero."""
total = 0.0
for h in ref["hands"]:
pi = pi_of_hand[h["c"]]
best = max(h["q"])
total += h["w"] * (best - sum(p * h["q"][i] for i, p in pi.items()))
return total
def reference_loss_chips(ref):
"""Loss of the reference's own root strategy: the noise floor of the score at this spot (0 for an exact equilibrium)."""
pi = {h["c"]: {i: p for i, p in enumerate(h["sigma"])} for h in ref["hands"]}
return loss_chips(ref, pi)