Skip to main content

Exhaustive ternary strategy search — the spreadsheet moment for AI decisions

Project description

si-superinstance

PyPI license: MIT

Exhaustive ternary strategy search — in 1ms, not 1 epoch.

The Problem

You have N decision dimensions, each with 3 choices: avoid (-1), neutral (0), choose (+1). How do you find the best strategy?

If N=4, there are exactly 3⁴ = 81 strategies. You could enumerate every single one and rank them. No gradient descent. No training loop. No hyperparameters. Just evaluate all 81 in a millisecond and sort.

That's what this library does.

The Insight

Ternary genomes {-1, 0, +1} create tiny, fully enumerable strategy spaces. The search cost is O(3ᴺ) — exponential in theory, but in practice:

  • N=4 → 81 strategies → instant
  • N=6 → 729 strategies → milliseconds
  • N=8 → 6,561 strategies → still under a second
  • N=10 → 59,049 strategies → ~1 second

You don't need machine learning when you can just try everything.

Quick Start

from superinstance import exhaustive, Strategy, Environment

# Define your decision problem as a payoff matrix
env = Environment.payoff_matrix([
    [3, 0],   # Action 0: high reward / no reward
    [5, 1],   # Action 1: best if chosen
    [0, 6],   # Action 2: best in bad state
    [2, 4],   # Action 3: moderate
])

# Exhaustively evaluate ALL 81 strategies
results = exhaustive(env, top_k=5)

for r in results:
    print(f"#{r.rank} {r.strategy} → fitness {r.fitness:.3f}")

Output:

#1 Strategy(w=[1, 1, -1, 0]) → fitness 3.040
#2 Strategy(w=[1, 1, -1, 1]) → fitness 2.980
#3 Strategy(w=[1, 1, 0, 0]) → fitness 2.540
...

API

Core

Function What it does
exhaustive(env, n_weights=4, top_k=None) Enumerate ALL 3^N strategies, rank by fitness
evolve(population, env, generations=10) Run natural selection on a population
pareto(strategies, envs) Find Pareto-optimal strategies across environments
species(strategies, threshold=1) Cluster strategies by Hamming distance
correlation_matrix(strategies, envs) Pearson correlation between environments
all_strategies(n=4) Generate all 3^N strategies

Strategy

s = Strategy([1, -1, 0, 1])
s.best_action(4)     # → 0 (action with highest weight sum)
s.choose([10, 5, 3]) # → 13.0 (weighted dot product)
s.mutate(p=0.3)      # → new Strategy with random mutations
s.hamming(other)     # → Hamming distance
Strategy.random(4)   # → random strategy

Built-in Environments

from superinstance import PrisonersDilemma, StagHunt, MatchingPennies, RandomEnvironment

env = PrisonersDilemma(rounds=10)
env = StagHunt(coordination_bonus=2.0)
env = MatchingPennies()
env = RandomEnvironment(n=4, seed=42)

# Or define your own:
env = Environment.payoff_matrix([[3, 0], [5, 1], [0, 6], [2, 4]])

Custom Environments

from superinstance import Environment, Strategy

class MyEnv(Environment):
    def score(self, strategy: Strategy) -> float:
        # Your scoring logic here
        return sum(w * 0.5 for w in strategy.w if w > 0)

results = exhaustive(MyEnv(name="custom"), n_weights=4)

Design Decisions

Why ternary, not binary? Binary {-1, +1} forces every dimension to take a side. Ternary {-1, 0, +1} allows "I don't know" — which is how real decision-makers think. The zero state is the negative space: it's not abstention, it's information.

Why exhaustive, not evolutionary? For N ≤ 8 (6,561 strategies), exhaustive search is faster and more honest than any evolutionary algorithm. You get the global optimum, not a local one. You get the full ranking, not just the top. Evolution is for N > 8 where enumeration becomes expensive.

Why stochastic scoring? Real environments are noisy. Averaging over 50 rounds smooths out randomness while keeping the API simple. Set rounds=1 for deterministic environments.

From the Browser Spreadsheet

This is the Python API for SuperInstance Spreadsheet. The spreadsheet's =EXHAUSTIVE() function became this library's exhaustive() function. The =EVOLVE() became evolve(). The =PARETO() became pareto().

Same math. Better packaging.

License

MIT

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

si_superinstance-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

si_superinstance-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: si_superinstance-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for si_superinstance-0.1.0.tar.gz
Algorithm Hash digest
SHA256 66a45b3bad128c771fad668facf9930a10b5a3a6eead934a880d969144dfc42d
MD5 94f140a65f0185eab8c3838fe31ca0a9
BLAKE2b-256 f5c7ed518899ed7b1716695ec29d5ae4b49d244cd7318d169ad196926e8a3a2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for si_superinstance-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e766bf949324a1d34ee6eef7196762f7498778c75b7b37067da709d63dee634e
MD5 ef12f6d5b1e7d24837a5da47181827e0
BLAKE2b-256 00af265cc41f28dccec48b1c6d33a6d9227502803de22995c4b88d5ed82b9ef9

See more details on using hashes here.

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