Skip to main content

Cooperative game theory (TU) algorithms + animation spec generator

Project description

tucoopy-py

Python package for cooperative game theory (TU) algorithms and for generating animation specs (JSON) consumed by the JS renderer.

Optional speedups:

  • pip install "tucoopy[fast]" (uses NumPy for small linear solves in geometry helpers)
  • pip install "tucoopy[lp]" (enables LP-based methods like least-core / nucleolus via SciPy)

Optional visualization:

  • pip install "tucoopy[viz]" for Matplotlib visualization (2 or 3 players only)

Optional extras (feature -> extra)

Feature Extra Notes
LP-backed methods lp SciPy backend (recommended)
LP-backed methods (fallback) lp_alt PuLP backend
Speedups fast NumPy helper routines
Static visualization viz Matplotlib (2 or 3 players only)
Dev tools dev pytest + mypy + ruff
Docs build docs mkdocs + mkdocstrings

Package layout

  • tucoopy.base: game/coalition primitives (bitmask-based)
  • tucoopy.properties: game properties / recognizers
  • tucoopy.games: classic games (glove, weighted voting, airport, bankruptcy, savings, unanimity, apex, ...)
  • tucoopy.geometry: geometry for visualization (core vertices, ...)
  • tucoopy.solutions: solution concepts (Shapley, Banzhaf, ...)
  • tucoopy.power: voting/simple-game power indices
  • tucoopy.transforms: transforms/representations (Harsanyi dividends, ...)
  • tucoopy.viz: optional visualization with Matplotlib (games with 2 or 3 players only)
  • tucoopy.io: JSON + animation spec helpers
  • tucoopy.backends: adapters for optional dependencies (LP, NumPy, ...)

Docs (in this repo): see packages/tucoopy-py/docs/en/index.md (EN) and packages/tucoopy-py/docs/pt/index.md (PT). Examples (runnable scripts): see packages/tucoopy-py/examples/README.md.

Install

pip install tucoopy

Optional extras:

  • pip install "tucoopy[lp]" for LP-based methods (least-core / nucleolus / modiclus / balancedness) using SciPy (recommended)
  • pip install "tucoopy[lp_alt]" for LP-based methods (least-core / nucleolus / modiclus / balancedness) using PuLP (fallback)
  • pip install "tucoopy[fast]" for NumPy speedups (kernel / prekernel and helpers)
  • pip install "tucoopy[viz]" for Matplotlib visualization (2 or 3 players only)

Quick example (generate an animation spec)

from tucoopy import Game
from tucoopy.solutions import shapley_value
from tucoopy.io.animation_spec import build_animation_spec

game = Game.from_coalitions(
    n_players=3,
    values={
        (): 0.0,
        (0,): 1.0,
        (1,): 1.2,
        (2,): 0.8,
        (0, 1): 2.8,
        (0, 2): 2.2,
        (1, 2): 2.0,
        (0, 1, 2): 4.0,
    },
    player_labels=["P1", "P2", "P3"],
)

phi = shapley_value(game)
spec = build_animation_spec(
    game,
    series_id="shapley",
    allocations=[phi] * 60,
    dt=1 / 30,
    series_description="Shapley value (static).",
    include_analysis=True,
)
print(spec.to_json())

Nucleolus / least-core (LP)

LP-based methods are behind the optional lp extra:

from tucoopy import Game
from tucoopy.solutions import least_core, nucleolus

g = Game.from_coalitions(
    n_players=3,
    values={
        (): 0.0,
        (0,): 1.0,
        (1,): 1.2,
        (2,): 0.8,
        (0, 1): 2.8,
        (0, 2): 2.2,
        (1, 2): 2.0,
        (0, 1, 2): 4.0,
    },
)

lc = least_core(g)
nu = nucleolus(g)
print(lc.epsilon, lc.x)
print(nu.levels, nu.x)

Core non-emptiness certificate (LP)

Bondareva–Shapley balancedness check (behind lp):

from tucoopy import Game
from tucoopy.properties.balancedness import balancedness_check

g = Game.from_coalitions(
    n_players=3,
    values={
        (): 0.0,
        (0,): 0.0,
        (1,): 0.0,
        (2,): 0.0,
        (0, 1): 1.0,
        (0, 2): 1.0,
        (1, 2): 1.0,
        (0, 1, 2): 1.0,
    },
)

res = balancedness_check(g)
print(res.core_nonempty, res.objective, res.weights)

Schema (Python <-> JS contract)

  • Canonical schema: schema/tucoopy-animation.schema.json
  • Bundled schema (package data): packages/tucoopy-py/src/tucoopy/io/schemas/tucoopy-animation.schema.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tucoopy-0.1.0.tar.gz (172.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tucoopy-0.1.0-py3-none-any.whl (227.9 kB view details)

Uploaded Python 3

File details

Details for the file tucoopy-0.1.0.tar.gz.

File metadata

  • Download URL: tucoopy-0.1.0.tar.gz
  • Upload date:
  • Size: 172.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tucoopy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa4b5f5dc82bbeea0bdbe9030445bb07e7c06882e9671bd69507041e6fc4e087
MD5 917ae7bb64925cdddb1ee0f76ab3ce1b
BLAKE2b-256 f598e2e13dfe7db1ef47759bff608fa52132c61e5fa374a4156bfbe370db9d22

See more details on using hashes here.

Provenance

The following attestation bundles were made for tucoopy-0.1.0.tar.gz:

Publisher: publish.yml on brennobarbosa/tucoopy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tucoopy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tucoopy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 227.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tucoopy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11cda703278b45ba67e1b7dac5d02d430ae722278104b1899591d5f49e72f958
MD5 ee027046009b42799f44b1f28350cded
BLAKE2b-256 b77087e027efd5f4a2e77922aa652e746c604a6eafd287391b5bfed0940fa4e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tucoopy-0.1.0-py3-none-any.whl:

Publisher: publish.yml on brennobarbosa/tucoopy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page