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.1.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.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: si_superinstance-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 19fb83f9a0bfd8a90d22791e21836e9e574a718feb09da0f6e1b8a7916995d58
MD5 4f0d966d9d257d0f9166d3611156d4b0
BLAKE2b-256 f9ce526e0ab4e8b070cd92b1c12a43a3715b6448178a605d404f68a0acd7cc9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for si_superinstance-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6991b6c4aa6818a985984c3fbf748b755efde94c4f786d75e395488cd8d15c21
MD5 824a1a747b1c71bcbc93592baccf4da5
BLAKE2b-256 c1ab03708e343841ab58c44e1e1cc503d4cdb6652dffa59fd4ac26979eef395d

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