RKO-Go is a Go library for combinatorial optimization with Random Key Optimization (RKO). You describe your problem once, by implementing the problem.Environment interface, and the library runs several metaheuristics on it in parallel, sharing their best solutions through a common pool.
Based on A Random-Key optimizer for combinatorial optimization.
- Seven metaheuristics: MultiStart, Simulated Annealing (SA), Iterated Local Search (ILS), Variable Neighbourhood Search (VNS), Genetic Algorithm (GA), Biased Random-Key Genetic Algorithm (BRKGA) and Large Neighbourhood Search (LNS).
- Problem-agnostic: every metaheuristic works on any
Environment. - Parallel: each selected metaheuristic runs in its own goroutine; they exchange solutions through a thread-safe pool.
- Online parameter tuning: give a parameter two or more values and the metaheuristic picks among them while it runs, with Q-learning. See Parameter tuning.
- Stopping rules: a time limit, an iteration limit and your own
context.Context. - Configurable from Go or from a YAML file, with a terminal dashboard or plain-text logging.
go get github.com/RKO-solver/rko-goThe root package is named rko. Requires Go 1.27 or later.
A solution is a vector of random keys in [0, 1). You write how many keys your problem needs, what a vector costs (lower is better) and how to turn it into an answer. This program solves a five-city travelling salesman problem:
package main
import (
"context"
"fmt"
"time"
rko "github.com/RKO-solver/rko-go"
"github.com/RKO-solver/rko-go/logger/printer"
"github.com/RKO-solver/rko-go/metaheuristic"
"github.com/RKO-solver/rko-go/problem"
)
// TSP is a travelling salesman problem: visit every city once and return to the start.
type TSP struct {
Dist [][]int
}
// NumKeys returns one key per city.
func (t TSP) NumKeys() int { return len(t.Dist) }
// Cost returns the length of the tour encoded by r.
func (t TSP) Cost(r problem.RandomKey) int {
tour := r.SortedIndex()
cost := 0
for i := range tour {
cost += t.Dist[tour[i]][tour[(i+1)%len(tour)]]
}
return cost
}
// Decode returns the tour: cities in ascending order of their keys.
func (t TSP) Decode(r problem.RandomKey) any { return r.SortedIndex() }
// SwapSearch lets the swap local search exchange any two keys.
func (t TSP) SwapSearch() [][2]int { return [][2]int{{0, t.NumKeys()}} }
// ProblemName returns the name that labels the logger and its output files.
func (t TSP) ProblemName() string { return "tsp" }
func main() {
env := TSP{Dist: [][]int{
{0, 2, 9, 10, 7},
{2, 0, 6, 4, 3},
{9, 6, 0, 8, 5},
{10, 4, 8, 0, 6},
{7, 3, 5, 6, 0},
}}
log := printer.Default("tsp")
defer log.Close()
solver, err := rko.New(env, log,
rko.WithMetaheuristics(metaheuristic.ILS, metaheuristic.BRKGA),
rko.WithTimeLimit(5*time.Second))
if err != nil {
fmt.Println(err)
return
}
tour := solver.Solve(context.Background()).([]int)
cost := solver.Pool().BestCost()
fmt.Println("tour:", tour, "cost:", cost)
}After five seconds of progress lines, it prints an optimal tour (its rotation or direction may vary):
tour: [4 0 1 3 2] cost: 26
Solve returns the best solution found, already passed through your Decode. The problem package explains each Environment method and shows a non-permutation example.
The metaheuristics call your Environment concurrently. To give each one its own copy, also implement problem.Cloner (see Environment).
The config package builds the same solver from a file, including each metaheuristic's local search and tuning parameters.
config.yaml:
LogLevel: INFO
LogType: PRINT
TimeLimitSeconds: 60
MaxIterations: 10000
Metaheuristics:
- "ILS=Swap,Mirror"
- "BRKGA"
Parameters:
BRKGA:
PopulationSize: 100cfgs, err := config.ReadFile("config.yaml")
if err != nil {
log.Fatal(err)
}
solver, rkoLog, err := cfgs[0].NewSolver(env)
if err != nil {
log.Fatal(err)
}
defer rkoLog.Close()
tour := solver.Solve(context.Background()).([]int)A parameter can also be a list of values, such as PopulationSize: [100, 200], for the metaheuristic to tune while it runs (see Parameters section).
See the config package for every key and its default.
Solve(ctx) stops at whichever comes first:
- the time limit expires,
- every metaheuristic has run
MaxIterationsiterations, ctxis cancelled or reaches its deadline.
| Constructor | Time limit | Iteration limit |
|---|---|---|
rko.New(env, log, opts...) |
rko.DefaultTimeLimit (150 s) |
none |
rko.NewFromMetaheuristics(env, log, pool, metaheuristics, opts...) |
none | none |
cfg.NewSolver(env) (config) |
TimeLimitSeconds in YAML (cfg.TimeLimit) |
MaxIterations in YAML (cfg.MaxIterations) |
- Both
rkoconstructors take the optionsrko.WithTimeLimit(d)andrko.WithMaxIterations(n), which replace those defaults, and both return an error (see Choosing metaheuristics). - A zero or negative value means no limit.
MaxIterationsis one number for the whole run, counted by each metaheuristic separately: each one stops after that many iterations of its own outer loop (a generation for GA and BRKGA, a temperature step for SA).Solvereturns once all of them have stopped.- With neither limit, the solver runs until
ctxis cancelled.rko.Newandrko.NewFromMetaheuristics, and socfg.NewSolver, print a warning in that case when they succeed. - With
Restart: false, SA also ends on its own once its temperature reachesTemperatureGoal. WithRestart: true(the default), it restarts there and runs until one of the rules above stops it.
To stop the search yourself, for example on a shutdown signal, pass your own context. The configured limits still apply:
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
result := solver.Solve(ctx)rko.New takes the metaheuristics to run through the rko.WithMetaheuristics option, as metaheuristic.Type constants, and uses each one's default parameters and local search. Repeated calls append:
| Constant | Metaheuristic |
|---|---|
metaheuristic.MultiStart |
MultiStart |
metaheuristic.SA |
Simulated Annealing |
metaheuristic.GA |
Genetic Algorithm |
metaheuristic.BRKGA |
Biased Random-Key Genetic Algorithm |
metaheuristic.ILS |
Iterated Local Search |
metaheuristic.VNS |
Variable Neighbourhood Search |
metaheuristic.LNS |
Large Neighbourhood Search |
Listing a constant twice runs two independent instances. metaheuristic.GRASP, VLNS, ALNS and IPR are declared but not implemented. rko.New checks the list before it builds anything and returns an error you can test with errors.Is:
| Error | When |
|---|---|
rko.ErrNoMetaheuristics |
No metaheuristic was given. |
rko.ErrNotImplemented |
A Type that is not implemented, such as metaheuristic.GRASP. |
rko.ErrUnknownMetaheuristic |
A value that is not a Type. |
metaheuristic.ParseType(name) turns a name such as "ils" into a Type (case-insensitive; -1 if unknown), and t.Implemented() reports whether rko.New can build t. With the default parameters, metaheuristic.LNS makes rko.New panic on problems with fewer than 20 keys; leave it out or set its parameters in a YAML file.
To change parameters or local searches, use a YAML file, or build each metaheuristic yourself and pass them to rko.NewFromMetaheuristics (see the metaheuristic package).
Every constructor takes a logger.Logger. Two ship with the library:
| Logger | Constructor | Output |
|---|---|---|
logger/printer |
printer.Default(name), printer.New(w, name, opts) |
Plain lines: a metaheuristic's progress, skipping Options.PrintInterval iterations between lines. Suits batch jobs and CI. |
logger/dashboard |
dashboard.New(name, opts) |
A live dashboard in the terminal's alternate screen; the final frame stays in the scrollback. Suits interactive runs. |
Levels are logger.LevelSilent, logger.LevelInfo (default) and logger.LevelVerbose. Solve resets the logger at the start of every run, so Report and Pool describe the last run. Both loggers can save the run to CSV (SaveCSV), and spill each metaheuristic's history to temporary files on long runs. Solve never calls the logger's Close, which deletes those files: defer it after creating the logger. After a run with tuned parameters, PrintPolicies and SavePoliciesCSV print or save the policy each tuner learned (see Policies). See the logger package.
| Package | Use it to |
|---|---|
rko (root) |
Create a Solver (New with WithMetaheuristics, or NewFromMetaheuristics; options WithTimeLimit and WithMaxIterations; DefaultTimeLimit; errors ErrNoMetaheuristics, ErrNotImplemented, ErrUnknownMetaheuristic) and run it (Solve, Pool, Print). |
problem |
Implement Environment for your problem. |
config |
Build a solver from a YAML file or a Go struct. |
logger |
Choose or implement a logger, and export results to CSV. |
metaheuristic |
Name a metaheuristic with Type (ParseType, String, Short, Implemented); build metaheuristics, local searches and the solution pool by hand; implement Algorithm (with Result, LocalSearch and the context helpers) for your own. |
metaheuristic/tuning |
Tune parameters while a metaheuristic runs, with the Q-learning tuner (NewSpace, Add, NewQLearning). |
random |
Create random generators, seeded (New) or randomly seeded (NewRandom). |
Lucas Mendes - GitHub
Licensed under the Academic Free License v3.0. See LICENSE.