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.
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
// 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 ¶
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 ¶
Describe lists every constraint (hard first, then soft) for the read-only view.
func (Registry[S]) HardViolations ¶
HardViolations returns all hard-constraint violations of the given state.
type Result ¶
Result reports what the run achieved. Per-constraint cost and violations are obtained separately from the generator's Registry on the final state.
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.