{"modulePath":"github.com/obcode/plexams.go","version":"v1.247.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package optimize\n\nPackage 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\n\n  - a Model: its state plus hard-feasible moves (Propose/undo) and the soft cost;\n  - 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.\n\nThe engine does not know about slots, exams or invigilators; the constraint evaluation lives in the Model/Registry the generator wires up.\n\n## Types\n\n```go\ntype Converger interface {\n\tConverged() bool\n}\n```\nConverger 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).\n\n```go\ntype Describable interface {\n\tInfo() Info\n}\n```\nDescribable is implemented by every constraint so it can be listed.\n\n```go\ntype Detailer interface {\n\tDetail() string\n}\n```\nDetailer is an optional Model capability supplying a human-readable status line for progress reporting (evaluated on the best state).\n\n```go\ntype HardConstraint[S any] interface {\n\tDescribable\n\tCheck(state S) []Violation\n}\n```\nHardConstraint 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.\n\n```go\ntype Info struct {\n\t// Name is a stable machine key (used as the map key for per-constraint cost).\n\tName string\n\t// Title is a short human-readable (German) label.\n\tTitle string\n\t// Description explains what the constraint enforces or optimizes.\n\tDescription string\n\tKind        Kind\n\t// Weight is the soft-constraint weight (0 for hard constraints).\n\tWeight float64\n\t// Tier orders constraints by priority for display (lower = more important);\n\t// optional (0 if unused).\n\tTier int\n}\n```\nInfo 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.\n\n```go\ntype Kind int\n```\nKind distinguishes hard (must always hold) from soft (weighted objective) constraints.\n\n```go\nconst (\n\tKindHard Kind = iota\n\tKindSoft\n)\n```\n\n```go\nfunc (k Kind) String() string\n```\n\n```go\ntype Model interface {\n\tPropose(rng *rand.Rand) (undo func())\n\tCost() float64\n\tSnapshot() any\n\tRestore(any)\n}\n```\nModel 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.\n\n```go\ntype Options struct {\n\tIterations int\n\tStartTemp  float64\n\tEndTemp    float64\n\tSeed       int64\n\n\t// StopWhenConverged ends the search early once the Model reports Converged()\n\t// AND the best state has not improved for StagnationLimit iterations AND the\n\t// temperature is near its floor (so \"no improvement\" is meaningful, not just\n\t// high-temperature wandering). Only applies when the Model implements Converger.\n\tStopWhenConverged bool\n\tStagnationLimit   int\n\n\t// StrictImprove turns the search into a greedy local search: only strictly cost-\n\t// reducing moves are accepted (no uphill, no equal-cost lateral moves). Used for a\n\t// warm start (\"improve the current assignment\") so the result stays as close to the\n\t// starting point as possible — nothing moves without lowering the cost. Temperature\n\t// is then irrelevant, and early stop triggers on stagnation alone.\n\tStrictImprove bool\n\n\t// Rng, if set, is used as the random source instead of seeding a fresh one from Seed.\n\t// This lets a caller share one RNG stream across a constructive pass and the anneal\n\t// (so the result is identical to an inline loop that used the same rng) — used by the\n\t// invigilation solver, whose greedy start draws from the same stream.\n\tRng *rand.Rand\n\n\t// OnProgress, if set, is called every ProgressEvery iterations with a snapshot\n\t// of the current best. It is throttled on purpose: per-iteration calls would\n\t// dominate the runtime with I/O.\n\tOnProgress    func(Progress)\n\tProgressEvery int\n}\n```\nOptions controls the annealing run.\n\n```go\nfunc DefaultOptions() Options\n```\nDefaultOptions 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.\n\n```go\ntype Progress struct {\n\tIteration int\n\tTotal     int\n\tBestCost  float64\n\tDetail    string\n}\n```\nProgress is a throttled snapshot of the best state for UI feedback. Detail is a generator-supplied human-readable status (via the optional Detailer interface).\n\n```go\ntype Registry[S any] struct {\n\tHard []HardConstraint[S]\n\tSoft []SoftConstraint[S]\n}\n```\nRegistry 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).\n\n```go\nfunc (r Registry[S]) Cost(state S) (total float64, byConstraint map[string]float64, violations []Violation)\n```\nCost is the summed (weighted) soft penalty; the per-constraint breakdown (keyed by Info().Name) and the individual violations are returned for reporting.\n\n```go\nfunc (r Registry[S]) Describe() []Info\n```\nDescribe lists every constraint (hard first, then soft) for the read-only view.\n\n```go\nfunc (r Registry[S]) HardViolations(state S) []Violation\n```\nHardViolations returns all hard-constraint violations of the given state.\n\n```go\ntype Result struct {\n\tCost         float64\n\tIterations   int\n\tStoppedEarly bool\n}\n```\nResult reports what the run achieved. Per-constraint cost and violations are obtained separately from the generator's Registry on the final state.\n\n```go\nfunc Anneal(m Model, opts Options) Result\n```\nAnneal 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.\n\n```go\ntype SoftConstraint[S any] interface {\n\tDescribable\n\tCost(state S) (penalty float64, violations []Violation)\n}\n```\nSoftConstraint should hold. Cost returns the total (weighted) penalty of a state together with the individual violations for reporting.\n\n```go\ntype Violation struct {\n\tConstraint string\n\tMessage    string\n\tPenalty    float64\n\tRefs       []int\n}\n```\nViolation 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.\n\n","path":"github.com/obcode/plexams.go/plexams/optimize","name":"optimize","synopsis":"Package optimize is a generic simulated-annealing engine shared by the schedule generators (exam schedule, pre-plan, invigilations).","isRedistributable":true}
