optimize

package
v1.240.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: BSD-3-Clause Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package optimize is a generic simulated-annealing engine shared by the schedule generators (exam schedule, pre-plan, invigilations). The engine (Anneal) is problem-agnostic: it drives a Model through a geometric cooling schedule with Metropolis acceptance and remembers the best state seen. Each generator supplies

  • a Model: its state plus hard-feasible moves (Propose/undo) and the soft cost;
  • a Registry of self-describing hard/soft Constraints, used both for reporting (per-constraint cost, violations) and for the read-only "which constraints are applied" view in the GUI.

The engine does not know about slots, exams or invigilators; the constraint evaluation lives in the Model/Registry the generator wires up.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Converger

type Converger interface {
	Converged() bool
}

Converger is an optional Model capability enabling early stopping: Converged reports whether the current state meets the generator's "good enough" criterion (e.g. everything placed and all near-hard goals met).

type Describable

type Describable interface {
	Info() Info
}

Describable is implemented by every constraint so it can be listed.

type Detailer

type Detailer interface {
	Detail() string
}

Detailer is an optional Model capability supplying a human-readable status line for progress reporting (evaluated on the best state).

type HardConstraint

type HardConstraint[S any] interface {
	Describable
	Check(state S) []Violation
}

HardConstraint must always hold. Check reports all violations of a given state (used for validation and reporting). The fast per-move feasibility test the optimizer uses to keep every state hard-feasible is generator-specific (it needs the move type) and therefore lives in the Model, not here.

type Info

type Info struct {
	// Name is a stable machine key (used as the map key for per-constraint cost).
	Name string
	// Title is a short human-readable (German) label.
	Title string
	// Description explains what the constraint enforces or optimizes.
	Description string
	Kind        Kind
	// Weight is the soft-constraint weight (0 for hard constraints).
	Weight float64
	// Tier orders constraints by priority for display (lower = more important);
	// optional (0 if unused).
	Tier int
}

Info is a self-description of a constraint for reporting and the read-only "which constraints are applied" view in the GUI. It carries no evaluation logic; it only explains what the constraint does.

type Kind

type Kind int

Kind distinguishes hard (must always hold) from soft (weighted objective) constraints.

const (
	KindHard Kind = iota
	KindSoft
)

func (Kind) String

func (k Kind) String() string

type Model

type Model interface {
	Propose(rng *rand.Rand) (undo func())
	Cost() float64
	Snapshot() any
	Restore(any)
}

Model is the problem-specific state the engine optimizes. Every state the engine visits must stay hard-feasible: Propose applies a random hard-feasible move in place and returns an undo closure (or nil to skip this step). Cost is the soft objective (lower is better). Snapshot/Restore capture and restore the full state so the engine can keep the best state seen.

type Options

type Options struct {
	Iterations int
	StartTemp  float64
	EndTemp    float64
	Seed       int64

	// StopWhenConverged ends the search early once the Model reports Converged()
	// AND the best state has not improved for StagnationLimit iterations AND the
	// temperature is near its floor (so "no improvement" is meaningful, not just
	// high-temperature wandering). Only applies when the Model implements Converger.
	StopWhenConverged bool
	StagnationLimit   int

	// StrictImprove turns the search into a greedy local search: only strictly cost-
	// reducing moves are accepted (no uphill, no equal-cost lateral moves). Used for a
	// warm start ("improve the current assignment") so the result stays as close to the
	// starting point as possible — nothing moves without lowering the cost. Temperature
	// is then irrelevant, and early stop triggers on stagnation alone.
	StrictImprove bool

	// Rng, if set, is used as the random source instead of seeding a fresh one from Seed.
	// This lets a caller share one RNG stream across a constructive pass and the anneal
	// (so the result is identical to an inline loop that used the same rng) — used by the
	// invigilation solver, whose greedy start draws from the same stream.
	Rng *rand.Rand

	// OnProgress, if set, is called every ProgressEvery iterations with a snapshot
	// of the current best. It is throttled on purpose: per-iteration calls would
	// dominate the runtime with I/O.
	OnProgress    func(Progress)
	ProgressEvery int
}

Options controls the annealing run.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults. Temperatures are scaled to the typical lower-tier soft-cost delta of a single move; a dominant near-hard penalty should be weighted large enough to be treated as effectively mandatory.

type Progress

type Progress struct {
	Iteration int
	Total     int
	BestCost  float64
	Detail    string
}

Progress is a throttled snapshot of the best state for UI feedback. Detail is a generator-supplied human-readable status (via the optional Detailer interface).

type Registry

type Registry[S any] struct {
	Hard []HardConstraint[S]
	Soft []SoftConstraint[S]
}

Registry bundles the active constraints for one generator over its state type S. It provides the soft objective (Cost), hard validation (HardViolations) and the read-only description (Describe).

func (Registry[S]) Cost

func (r Registry[S]) Cost(state S) (total float64, byConstraint map[string]float64, violations []Violation)

Cost is the summed (weighted) soft penalty; the per-constraint breakdown (keyed by Info().Name) and the individual violations are returned for reporting.

func (Registry[S]) Describe

func (r Registry[S]) Describe() []Info

Describe lists every constraint (hard first, then soft) for the read-only view.

func (Registry[S]) HardViolations

func (r Registry[S]) HardViolations(state S) []Violation

HardViolations returns all hard-constraint violations of the given state.

type Result

type Result struct {
	Cost         float64
	Iterations   int
	StoppedEarly bool
}

Result reports what the run achieved. Per-constraint cost and violations are obtained separately from the generator's Registry on the final state.

func Anneal

func Anneal(m Model, opts Options) Result

Anneal improves the Model's current state with simulated annealing and leaves the Model restored to the best state found. The Model is responsible for starting from (and only ever moving through) hard-feasible states.

type SoftConstraint

type SoftConstraint[S any] interface {
	Describable
	Cost(state S) (penalty float64, violations []Violation)
}

SoftConstraint should hold. Cost returns the total (weighted) penalty of a state together with the individual violations for reporting.

type Violation

type Violation struct {
	Constraint string
	Message    string
	Penalty    float64
	Refs       []int
}

Violation is a single broken constraint. For a hard constraint its presence means the state is infeasible; for a soft constraint Penalty is the (already weighted) cost contribution. Refs holds the involved entities (e.g. ancodes) for the UI.

Jump to

Keyboard shortcuts

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