Open the context
Choose the project folder and name the files or behavior involved.
Compute exact Shapley shares for up to eight players from an integer coalition-value table bounded by 1000000 in magnitude. Test additive games and zero players, rejecting a nonzero empty coalition or a table whose length is not a power of two.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from fractions import Fraction
from math import comb
def shapley_values(values):
if type(values) not in (list, tuple) or not 1 <= len(values) <= 256 or len(values) & (len(values)-1):
raise ValueError("shapley.table")
if any(type(v) is not int or abs(v) > 10**6 for v in values) or values[0] != 0:
raise ValueError("shapley.value")
players = len(values).bit_length()-1
result = []
for player in range(players):
share = Fraction(0)
for subset in range(len(values)):
if not subset & (1 << player):
size = subset.bit_count()
weight = Fraction(1, players*comb(players-1, size))
share += weight*(values[subset | (1 << player)]-values[subset])
result.append(share)
return resultThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.