alns

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 4 Imported by: 0

README

ALNS in Go

This is a partial port/adaptation of the Python library N-Wouda/ALNS to the Go programming language.

The original implementation can be found here: N-Wouda/ALNS.

Overview

  • Implements core components of the ALNS metaheuristic: destroy operators, repair operators, acceptance criteria, and the operator selection mechanism.
  • Can be used to solve complex combinatorial optimization problems such as TSP, VRP, and others, similar to the Python version.

Import the module in your Go project:

import "github.com/bibenga/alns"

Exmaple

initSol := NewMyProblemState(...)

destroyOperators := []alns.Operator{randomRemoval, pathRemoval, worstRemoval}
repairOperators := []alns.Operator{greedyRepair}

selector, err := alns.NewRouletteWheel(
    [4]float64{3, 2, 1, 0.5},
    0.8,
    len(destroyOperators),
    len(repairOperators),
    nil,
)
if err != nil {
    ...
}
acceptor := alns.HillClimbing{}
stop := alns.MaxRuntime{MaxRuntime: 1 * time.Second}

a := alns.ALNS{
    Rnd:               rnd,
    DestroyOperators:  destroyOperators,
    RepairOperators:   repairOperators,
    Selector:          &selector,
    Acceptor:          &acceptor,
    Stop:              &stop,
    InitialSolution:   initSol,
}

if result, err := a.Iterate(); err != nil {
    ...
} else {
    // do something with result
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RuntimeRand *rand.Rand = rand.New(&randomSource{})

Functions

This section is empty.

Types

type ALNS

type ALNS struct {
	Logger            *slog.Logger
	Rnd               *rand.Rand
	CollectObjectives bool
	Listener          Listener
	DestroyOperators  []Operator
	RepairOperators   []Operator
}

func NewAlns added in v0.3.0

func NewAlns(rnd *rand.Rand, dOps, rOps []Operator) *ALNS

func NewDefaultAlns added in v0.3.0

func NewDefaultAlns(dOps, rOps []Operator) *ALNS

func (*ALNS) Iterate

func (a *ALNS) Iterate(
	initSol State,
	selectOp OperatorSelectionScheme,
	accept AcceptanceCriterion,
	stop StoppingCriterion,
) (*Result, error)

def iterate(initial_solution, select, accept, stop)

type AcceptanceCriterion

type AcceptanceCriterion interface {
	Accept(rnd *rand.Rand, best, current, candidate State) (bool, error)
}

type Listener

type Listener func(outcome Outcome, cand State) error

type ObjectiveRecord added in v0.3.0

type ObjectiveRecord struct {
	ElapsedTime time.Duration
	Objective   float64
}

type Operator

type Operator func(state State, rnd *rand.Rand) (State, error)

type OperatorCounts added in v0.3.0

type OperatorCounts [4]uint // see Outcome

func (OperatorCounts) String added in v0.3.0

func (o OperatorCounts) String() string

type OperatorSelectionScheme

type OperatorSelectionScheme interface {
	Select(rnd *rand.Rand, best, current State) (deleteOpIndx, repairOpIndx int, err error)
	Update(candidate State, deleteOpIndx, repairOpIndx int, outcome Outcome) error
}

type Outcome

type Outcome int
const (
	Best   Outcome = iota // Candidate solution is a new global best
	Better                // Candidate solution is better than the current incumbent
	Accept                // Candidate solution is accepted
	Reject                // Candidate solution is rejected
)

func (Outcome) String

func (o Outcome) String() string

type Result

type Result struct {
	BestState  State
	Statistics Statistics
}

type State

type State interface {
	Objective() float64
}

type Statistics

type Statistics struct {
	IterationCount        uint              // the number of iterations
	TotalRuntime          time.Duration     // the total runtime
	Objectives            []ObjectiveRecord // previous objective values, tracking progress
	DestroyOperatorCounts []OperatorCounts  // the destroy operator counts
	RepairOperatorCounts  []OperatorCounts  // the repair operator counts
}

type StoppingCriterion

type StoppingCriterion interface {
	IsDone(rnd *rand.Rand, best, current State) (bool, error)
}

Directories

Path Synopsis
accept
examples
tsp command
internal
select
stop

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL