Skip to content

About

Random-Key Optimization with cooperative metaheuristic portfolios (Python, Rust and C++ engines)

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

324 Commits

Folders and files

RKO

Write a decoder. Not a metaheuristic.

CI Development documentation · Alpha — see current limitations.

This README follows main, which contains changes newer than the published 0.3.0a4 prerelease. A matching version string does not mean matching code. Choose the correct installation and documentation version.

RKO is a domain-neutral random-key optimization library with cooperating metaheuristics, inspired by RKO C++ v2. Every solution is encoded as a vector of keys in [0, 1); your decoder turns keys into your domain's solution, your objective scores it, and the library's search algorithms explore the key space without knowing anything about your problem. You do not reimplement BRKGA, simulated annealing, VNS or a population loop for each new problem — you implement two functions.

What you implement

Your code Responsibility
decoder(keys) Convert a key vector into your domain solution.
objective(solution) Return a finite scalar to minimize. Passed as cost.
read_data(source) (optional) Load your instance once, outside the search budget.
validate(solution) (optional) Post-solve domain check, returns a ValidationReport.

RKO calls only decoder and objective during search. Bind loaded data with functools.partial or a small adapter class — neither is a required interface. See the complete data-loading tutorial.

What it provides

  • Eleven algorithms in one portfolio: BRKGA, BRKGA-CS, GA, GRASP, ILS, IPR, LNS, MultiStart, PSO, SA, VNS — sequential, thread or process execution, shared elites, owned RNG streams, immutable results.
  • An optional compiled Rust engine (engine="rust") measured against a pinned C++ reference on Knapsack, TSP and Rastrigin: won 8 of 11 head-to-head rounds, tied 3, lost 0 (full protocol and results). Every claim there is scoped to those problems, budgets and machine — read it before extrapolating.
  • Python callbacks for any problem; NumPy is the only required runtime dependency. A prebuilt wheel needs no compiler; building from source needs Rust. No C++ toolchain or agent framework required.

Install

Requires Python 3.14+ and uv. Source installs also need a Rust toolchain (the native extension builds from source). CI tests Python 3.14 on Ubuntu, Windows and macOS (x86_64/ARM).

git clone https://github.com/MarcosPTProenca/rko-solver.git
cd rko-solver
uv sync --locked --no-dev

Run scripts with uv run --locked --no-dev python your_script.py from this checkout. For reproducible use, record the Git revision alongside the lock file.

First solve

import numpy as np
from rko_solver import RKO


def decode(keys: np.ndarray) -> np.ndarray:
    """Map unit-interval keys to a numerical solution in [-5, 5]."""
    return 10.0 * keys - 5.0


def objective(solution: np.ndarray) -> float:
    """Return the squared norm to minimize."""
    return float(np.dot(solution, solution))


if __name__ == "__main__":
    solver = RKO(
        decode, objective, n_variables=10, algorithms=["SA"],
        adaptation="fixed", execution="sequential", random_state=42,
        hyperparameters={"SA": {"sa_max": 100, "t0": 1000.0}},
    )
    result = solver.solve(time_limit=2.0)
    print("Solution:", decode(np.asarray(result.keys)))
    print("Objective:", result.cost)

Run with uv run --locked --no-dev python first_solve.py. algorithms=["SA"] selects one algorithm through the portfolio API; swap in any other algorithm or add more for a cooperating portfolio, without touching decode/objective. RKO's default execution is a process portfolio, which needs importable callbacks and a guarded entry point; sequential execution above is the simplest first run. See execution modes before switching, and the file-backed tutorial for loading real instance data and optional post-solve validation.

Deadlines are cooperative — an already-running callback cannot be interrupted — and time_limit gives each selected algorithm its own allowance under sequential execution, not one allowance shared across the portfolio.

Choose a workflow

Need Guide
Choose source versus a published package, or install optional features Installation
Configure engines, execution, budgets, stopping and results Usage and API
Load your own data and bind callbacks File-to-solution tutorial
Choose one algorithm or tune fixed/adaptive settings Individual solvers, parameters
Use compiled or block objectives and automatic engine selection Rust and C++ engines
Check final solutions or generate offline reports Validation, reports
Inspect declared decoder structure or record original decisions Diagrams, observed decisions
Convert a bounded mathematical model Model conversion
Host trusted callbacks privately or explore decoder evolution Service, experimental evolution
Extend the library or inspect evidence and limitations Architecture, contributing, status

Repository examples live in examples/ (not installed with the wheel). Run uv run --locked --no-dev python -m examples.instance_pipeline for the complete read/decode/objective/validate tutorial, or see the terminal guide for the supplied Knapsack instances.

Status and attribution

This is an alpha library: not a stable API, not a universal performance guarantee. The current limitations distinguish functional checks, historical measurements and open qualification work. Read the identified benchmarks instead of extrapolating from one language, one instance or one machine.

The LICENSE and NOTICE retain project, fixture and vendored-code attribution. Every publication still requires separate owner approval. Report reproducible bugs through GitHub issues.

About

Random-Key Optimization with cooperative metaheuristic portfolios (Python, Rust and C++ engines)

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages