Sliding window permutation search

From wenmin-wu/ds-skills · View interactive version

Sliding window permutation search is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Local search that slides a window of size p across a word sequence, brute-forcing all permutations within each window to minimize an objective like LLM perplexity

How to use this skill

Skill file

Sliding Window Permutation Search

Overview

For sequences too long for full factorial search, slide a small window (3-5 elements) across the sequence and exhaustively try all permutations within each window position. The prefix and suffix remain fixed while the window contents are reordered. This is O(n * p!) per pass — tractable for p <= 7 — and converges quickly as each window improves its local ordering.

Quick Start

import itertools

def sliding_window_optimize(sequence, score_fn, window_size=4, skip=1):
    best = sequence[:]
    best_score = score_fn(best)
    improved = True
    while improved:
        improved = False
        for start in range(0, len(best) - window_size + 1):
            end = start + window_size
            prefix = best[:start]
            suffix = best[end:]
            window = best[start:end]
            for perm in itertools.permutations(window):
                candidate = prefix + list(perm) + suffix
                s = score_fn(candidate)
                if s < best_score:
                    best = candidate
                    best_score = s
                    improved = True
    return best, best_score

words = text.split()
result, score = sliding_window_optimize(
    words, lambda w: perplexity(' '.join(w)), window_size=4)

Workflow

  1. Start with an initial ordering (e.g., sorted or heuristic-initialized)
  2. For each window position [start:start+p], enumerate all p! permutations
  3. Score each candidate (prefix + permutation + suffix)
  4. Keep the best scoring permutation for that window
  5. Repeat passes until no window improves

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.