Skip to main content

Overview

Top-K is a straightforward search algorithm that always selects the best program as the parent and uses the next best programs as context. It’s the simplest effective baseline in SkyDiscover.

Greedy Selection

Always picks the highest-scoring program to evolve

Elite Context

Provides top programs as examples in the prompt

No Stagnation

Continues refining even if improvement is slow

Minimal Overhead

Fast and simple with no complex bookkeeping

How It Works

Sampling Strategy

On each iteration, Top-K:
  1. Parent Selection: Choose the program with the highest combined_score
  2. Context Selection: Select the next K programs (ranks 2 to K+1) as context
  3. Prompt Generation: Include context programs as examples of good solutions
  4. Evolution: Generate a mutation of the best program
If only one program exists, it’s used as both parent and context.

Pure Exploitation

Top-K is a pure exploitation strategy:
  • No exploration of diverse solutions
  • No randomness in selection
  • Focuses entirely on refining the current best
This can be very effective for problems where iterative refinement is the key to success.

Configuration

Basic Usage

Python API

Configuration File

Configuration Options

int
default:"4"
Number of top programs (after the best) to include as context
Top-K uses the standard database configuration options:
string
Directory to save programs and checkpoints
bool
default:"false"
Whether to save prompts and responses

When to Use Top-K

  • Quick experiments and baselines
  • Problems where greedy refinement works well
  • Short discovery runs (< 50 iterations)
  • When you want simple, predictable behavior
  • As a starting point before trying more complex algorithms
  • Problems with many local optima requiring exploration
  • Need for diverse solution approaches
  • Risk of getting stuck in local optima

Comparison with Other Algorithms

Example

Simple Optimization

Output Structure

Tips for Best Results

Top-K is an excellent starting point. Run it first to establish a baseline before trying more complex algorithms.
If the score stops improving after several iterations, consider switching to an algorithm with exploration (like AdaEvolve or Beam Search).
Increase num_context_programs (e.g., to 8) to give the LLM more examples of successful solutions.
Top-K’s simplicity makes it fast. Use it for rapid iteration during development.

Variants

You can easily modify Top-K behavior:

Random Top-K

Sample randomly from top-K instead of always using #1:

Top-K with Temperature

Weight selection by score:
  • Best-of-N - Similar greedy approach with N attempts per parent
  • Beam Search - Maintains multiple candidates instead of just one
  • AdaEvolve - Adaptive version with exploration