CCoalitionAster
Workflows

Coding workflows · Exact Shapley coalition values

03
Built around your work

A concrete starting point

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.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Exact Shapley coalition values

A concrete starting point
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 result

The 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.

AI coding workspace

Your next change starts here.

Get the Windows app ↗