Files
WoW-Hybrid-gateway/ops/spell_modifier_overlap.py

64 lines
2.9 KiB
Python

"""Qualify source any-intersection totals from per-bit aggregate observations.
This is an offline linear identifiability check, not a native mask allocator.
The caller must supply the complete effective source modifier-mask cohort for
one family, operation and flat/percent kind. Default spell masks are not enough.
No equal-value deduplication or inferred active-aura identity is performed.
"""
from fractions import Fraction
def source_union_weights(modifier_masks, target_mask):
return source_application_weights([(m,m) for m in modifier_masks],target_mask)
def source_application_weights(modifier_masks, target_mask):
"""Allow explicit (emitted mask, application mask) source script contracts.
This proves numeric identifiability only. The caller must separately prove
that updates/removals keep the emitted aggregate state coherent.
"""
masks = sorted(set(modifier_masks))
if not masks or any(len(m)!=2 or any(type(v) is not int or not 0<v<1<<64 for v in m) for m in masks):
raise ValueError("complete nonzero uint64 wire/application mask cohort required")
if type(target_mask) is not int or not 0 <= target_mask < 1 << 64:
raise ValueError("uint64 target mask required")
bits = [i for i in range(64) if any(wire & (1 << i) for wire, application in masks)]
rows = [[Fraction(bool(wire & (1 << b))) for b in bits] +
[Fraction(bool(application & target_mask))] for wire, application in masks]
pivot_rows = []
at = 0
for col in range(len(bits)):
pivot = next((i for i in range(at, len(rows)) if rows[i][col]), None)
if pivot is None:
continue
rows[at], rows[pivot] = rows[pivot], rows[at]
scale = rows[at][col]
rows[at] = [v / scale for v in rows[at]]
for i in range(len(rows)):
if i != at and rows[i][col]:
scale = rows[i][col]
rows[i] = [a - scale * b for a, b in zip(rows[i], rows[at])]
pivot_rows.append((col, at))
at += 1
if at == len(rows):
break
if any(not any(row[:-1]) and row[-1] for row in rows):
return None
# A deterministic exact solution with all free variables zero. Other
# solutions produce the same result for every vector in the proved cohort.
weights = {bits[col]: rows[index][-1] for col, index in pivot_rows
if rows[index][-1]}
for wire, application in masks:
if sum(v for bit, v in weights.items() if wire & (1 << bit)) != bool(application & target_mask):
raise ValueError("source union witness failed")
return weights
def apply_weights(weights, observed):
if weights is None:
raise ValueError("source aggregate does not identify target total")
if any(bit not in observed for bit in weights):
raise ValueError("required source aggregate not observed")
return sum((weight * observed[bit] for bit, weight in weights.items()), Fraction(0))