feat(dommer): the power solutions compared on equal terms, and the axes of the surfaces (clarifications 134, 135 and 137)

M7 gives, for each of the five solutions, the share of the draws in which it
is best alone and in which it shares the place, with no verdict on A; part II
and the thresholds for M7 are gone. T-F and T-K share the place where they
are the same path. The surfaces carry the classes along their axes, and
kontroll prints a line per file. The made-up reference files are
regenerated: everything outside the comparison is unchanged to the last
digit. Nothing is computed on the frozen parameters.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-11-13 23:03:05 +01:00
commit 5a54fba628
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
15 changed files with 8804 additions and 758 deletions

View file

@ -10,19 +10,20 @@ from __future__ import annotations
import json
import math
import shutil
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from tokenfabrikker import datafiler as df
from tokenfabrikker import grunnlag as gl
from tokenfabrikker import kjoring as kj
from tokenfabrikker import terskler
from tokenfabrikker.dommer import Dom, Forsyning
from tokenfabrikker.kjoring import Bane, Kjoring, Utfall
from tokenfabrikker.utbygging import Kraft, Losning
if TYPE_CHECKING:
from pathlib import Path
from tokenfabrikker.kjoring import Scenario
NORM, VARER = kj.SCENARIOER[1], kj.SCENARIOER[0]
@ -134,7 +135,6 @@ def test_dommer_json_har_tallene_sett_dommer_gir() -> None:
"grunnlag_for_trinnet",
"teller",
"del_I",
"del_II",
"per_scenario",
]
assert (lest["trinn"], lest["grunnlag_for_trinnet"]) == (5, "25 %-trinnet")
@ -157,7 +157,32 @@ def test_dommer_json_har_tallene_sett_dommer_gir() -> None:
# B cannot be built in 25 of 40 draws: the median in M1 has no value
assert sd.per_trinn[5][Losning.B].median_M1 == -math.inf
assert norm["trinn"][0]["losninger"]["B"]["median_M1"] is None
assert norm["trinn"][0]["M7"]["dom"] == sd.M7[5].dom.value
# M7 is the comparison: A is best in all 40 draws, B cannot be built in 25 (135 f)
assert norm["trinn"][0]["M7"] == {
"A": {
"best_alene": 1.0,
"deler_plassen": 0.0,
"ikke_byggbar": 0.0,
"best_alene_blant_byggbare": 1.0,
},
"B": {
"best_alene": 0.0,
"deler_plassen": 0.0,
"ikke_byggbar": 0.625,
"best_alene_blant_byggbare": 0.0,
},
"delt": {"andel": 0.0, "par": [{"losninger": ["A", "B"], "andel": 0.0}]},
"ingen": {"andel": 0.0},
}
assert norm["trinn"][0]["M7"] == df.sammenligning(sd.M7[5])
assert list(norm) == [
"navn",
"teller",
"trinn",
"del_I",
"del_I_avgjort",
"betingelser",
]
ved = kj.ved_siden_av_m6(kjoring.utfall(NORM, Losning.A, 5))["snitt"]
assert a["ved_siden_av_M6"]["snitt"] == {
"antall": ved.antall,
@ -221,6 +246,94 @@ def test_tidslinje_json_har_tallene_over_trekningene() -> None:
assert isinstance(rader[0]["aar"], int)
def test_sammenligningen_i_M7_staar_i_fast_rekkefolge_med_delt_og_ingen_sist() -> None:
"""Clarification 137 d: the four shares per solution, then «delt» with the pairs, «ingen»."""
m7 = kj.Sammenligning(
losninger={
Losning.A: kj.Bestandel(0.25, 0.25, 0.5, 0.5),
Losning.C: kj.Bestandel(0.25, 0.25, 0.0, 0.25),
Losning.T_K: kj.Bestandel(0.0, 0.0, 1.0, None),
},
delt=0.25,
par={
(Losning.A, Losning.C): 0.25,
(Losning.A, Losning.T_K): 0.0,
(Losning.C, Losning.T_K): 0.0,
},
ingen=0.25,
)
lest = _lest(df.sammenligning(m7))
assert isinstance(lest, dict)
assert list(lest) == ["A", "C", "T-K", "delt", "ingen"]
assert lest["T-K"] == {
"best_alene": 0.0,
"deler_plassen": 0.0,
"ikke_byggbar": 1.0,
"best_alene_blant_byggbare": None,
}
assert lest["delt"] == {
"andel": 0.25,
"par": [
{"losninger": ["A", "C"], "andel": 0.25},
{"losninger": ["A", "T-K"], "andel": 0.0},
{"losninger": ["C", "T-K"], "andel": 0.0},
],
}
assert lest["ingen"] == {"andel": 0.25}
alene = [lest[a]["best_alene"] for a in ("A", "C", "T-K")]
assert math.fsum([*alene, lest["delt"]["andel"], lest["ingen"]["andel"]]) == 1.0
def test_aksene_i_flatene_har_antall_og_median_per_klasse() -> None:
"""Clarification 134: five classes as the surface makes them; a class without draws is empty.
Ten values: the class is floor(5 r / 10), two draws per class, the median the midpoint.
Three distinct values in six draws: the classes 0, 1 and 3 (floor(5 r / 6) for r = 0, 2, 4).
"""
x = [10.0, 1.0, 9.0, 2.0, 8.0, 3.0, 7.0, 4.0, 6.0, 5.0]
assert df.akse(x) == [
{"antall": 2, "median": 1.5},
{"antall": 2, "median": 3.5},
{"antall": 2, "median": 5.5},
{"antall": 2, "median": 7.5},
{"antall": 2, "median": 9.5},
]
assert df.akse([1.0, 1.0, 2.0, 2.0, 3.0, 3.0]) == [
{"antall": 2, "median": 1.0},
{"antall": 2, "median": 2.0},
{"antall": 0, "median": None},
{"antall": 2, "median": 3.0},
{"antall": 0, "median": None},
]
assert df.akse([]) == [{"antall": 0, "median": None}] * 5
# the counts are those of the surface's cells summed along the other axis
y = [float(j % 3) for j in range(10)]
flate = terskler.flate(x, y, [1.0] * 10)
assert [sum(r.antall for r in rad) for rad in flate] == [k["antall"] for k in df.akse(x)] # type: ignore[index]
assert [sum(rad[j].antall for rad in flate) for j in range(5)] == [
k["antall"] # type: ignore[index]
for k in df.akse(y)
]
def test_hovedkjoringen_sier_fra_om_hver_fil_etter_hvert(tmp_path: Path) -> None:
"""Clarification 134: a line per file as it is made; the files are those it writes."""
sti = Path(__file__).resolve().parent / "oppdiktet" / "grunnlag.json"
data = json.loads(sti.read_text(encoding="utf-8"))
data["trekninger"] = data["trekninger"][:1]
liten = tmp_path / "grunnlag.json"
liten.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
laget: list[str] = []
skrevet = df.skriv_hovedkjoring(
gl.fra_fil(liten),
tmp_path / "ut",
stige=(1,),
filer=(df.DOMMER, df.TIDSLINJE),
laget=laget.append,
)
assert laget == list(skrevet) == [df.DOMMER, df.TIDSLINJE]
def test_d_stjerne_med_den_faste_eps_d_og_med_0_5_og_2() -> None:
assert df.d_stjerne(0.6, 1.0) == [
{"eps_d": 1.0, "d_stjerne": 0.4},