Documentation
¶
Overview ¶
Package examplan generates the exam schedule (Terminplan): it assigns every exam (unit) to a time slot on top of the generic simulated-annealing core in plexams/optimize. It is deliberately DB-agnostic — the caller builds a *Problem from the domain data and maps the resulting slot assignment back to plan entries.
The primary objective is to spread each student's exams as far apart as possible. Hard constraints: fixed placements stay put, a student never has two exams in the same slot (unless the pair is declared shareable), per-slot EXaHM and total seat capacities. Soft objective (weighted): the spread (sum over students + a convex worst-case term so no single student gets a bad schedule), "attract" pairs that should sit together (parallel sections / small exams of the same examer), and a slot-load term keeping the seats/rooms per slot moderate.
Index ¶
- func IntersectAllowed(sets [][]int) []int
- func IntersectSlots(a, b []int) []int
- type AttractPair
- type Diagnostics
- type Pair
- type Problem
- type Slot
- type SlotRef
- type State
- func (st *State) Converged() bool
- func (st *State) Cost() float64
- func (st *State) Detail() string
- func (st *State) Diagnostics() Diagnostics
- func (st *State) Propose(rng *rand.Rand) func()
- func (st *State) Restore(a any)
- func (st *State) Snapshot() any
- func (st *State) TbauUsage() (bookedExahm, usedExahm, bookedSeb, usedSeb int)
- func (st *State) TopLoadedSlots(n int) [][3]int
- func (st *State) UnplacedAncodes() []int
- type Student
- type Unit
- type Weights
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IntersectAllowed ¶ added in v1.213.3
IntersectAllowed intersects the allowed-slot index sets of a same-slot group's members. An empty member set means "all slots allowed" and is skipped; if every member allows all slots the result is nil (= all allowed). The result is sorted.
func IntersectSlots ¶ added in v1.213.3
IntersectSlots returns the slot indices in both a and b. An empty a means "all slots allowed", so the result is a copy of b. The result may be empty (no overlap).
Types ¶
type AttractPair ¶
AttractPair are two units that should sit close (ideally same slot): parallel sections of the same module, or small exams of the same examer.
func AttractPairs ¶ added in v1.213.3
func AttractPairs(units []Unit, smallExamThreshold int) []AttractPair
AttractPairs computes the attract pairs among the movable (non-fixed) units: parallel sections (same module+program, different examer) and small exams (at most smallExamThreshold seats) of the same examer. Fixed units are ignored. The result is deduplicated on the unit-index pair and sorted (A<B) so a re-run is deterministic. A and B are indices into units.
type Diagnostics ¶
type Diagnostics struct {
Students int // students with at least two placed exams
Pairs int // counted conflict pairs (placed, both)
SameSlot int // both exams in the same slot (a hard violation — should be 0)
Adjacent int // same day, directly consecutive slot
SameDay int // same day, not consecutive
NextDay int // consecutive calendar day (weekend gap makes Fri->Mon land higher)
Within3 int // 2-3 calendar days apart
Further int // more than 3 calendar days apart
StudentsWithAdjacent int
StudentsWithSameDay int
WorstStudentID string
WorstStudentPenalty float64
WorstStudentPairs [6]int // [sameSlot, adjacent, sameDay, nextDay, within3, further]
MaxSlotSeats int
SlotsUsed int // slots holding at least one of OUR exams (foreign-only slots excluded)
SlotsOverThreshold int
MaxExamsPerSlot int
InteriorHoles int // empty slots between occupied ones on the same day (bad for invigilation)
}
Diagnostics is a human-readable quality report of a solved schedule: how close students' exams ended up, the worst-off student, and per-slot load. It is used to judge and calibrate a run (the raw cost alone is not interpretable).
type Pair ¶
type Pair struct {
A, B int
Weight float64
// CrossLoc: the two exams are at different campuses (extra travel-gap penalty when
// they end up on the same day).
CrossLoc bool
}
Pair is a conflict between two of a student's units (A<B), already weighted by the shared-student contribution and any per-student repeat discount. Pairs a student may legitimately never sit together (canShareSlot) are omitted at build time.
type Problem ¶
type Problem struct {
Slots []Slot
Units []Unit
Students []Student
Attract []AttractPair
W Weights
// TimeSeverity is the per-slot start-time avoidance severity (index-aligned with
// Slots): how far, in hours, the slot's start time lies outside the wanted window
// (0 = inside / no penalty). Semester-dependent and set by the caller via
// SetTimeSeverity; combined with W.TimeOfDay and a unit's seats into timePenalty.
// nil (or W.TimeOfDay == 0) disables the term.
TimeSeverity []float64
// contains filtered or unexported fields
}
Problem is the immutable input to the solver.
func NewProblem ¶
func NewProblem(slots []Slot, units []Unit, students []Student, attract []AttractPair, w Weights) *Problem
NewProblem validates the input and precomputes derived structures.
func (*Problem) NumNTAOverruns ¶ added in v1.209.0
NumNTAOverruns returns the number of installed NTA time-overrun adjacency pairs.
func (*Problem) Registry ¶
Registry returns the self-describing hard/soft constraints for reporting and the read-only "which constraints are applied" view.
func (*Problem) SetNTAOverruns ¶ added in v1.209.0
SetNTAOverruns installs the consecutive-exam-gap constraints: for each ordered pair (a, b), unit b must not be placed in the slot immediately following a on the same day, because a student sharing both is still occupied with a (its duration — extended for that student's NTA — plus the travel buffer) when that next slot starts. Both-fixed pairs are ignored (neither can be moved to satisfy it). Idempotent; replaces any previously installed pairs.
func (*Problem) SetTimeSeverity ¶ added in v1.221.0
SetTimeSeverity installs the per-slot start-time avoidance severity (see Problem.TimeSeverity). len(sev) must equal len(Slots); a shorter/nil slice or a zero W.TimeOfDay simply disables the penalty. Call before Solve.
type Slot ¶
type Slot struct {
SlotRef
Seats int // total room seats available this slot (global capacity)
ExahmSeats int // booked (T-building) EXaHM seats this slot
SebSeats int // booked (T-building) SEB seats this slot
}
Slot is a candidate slot with its capacities. Seats/ExahmSeats of 0 mean "no limit known" and are not enforced.
type SlotRef ¶
type SlotRef struct {
Day int // 1-based exam day number
Slot int // 1-based slot number within the day
Start time.Time
}
SlotRef identifies a slot in the exam period; Start is its absolute start time, used so temporal distance naturally includes weekends (Fri->Mon is farther than Tue->Wed even though the day numbers are consecutive).
type State ¶
State is the mutable assignment: SlotOf maps each unit to a slot index (-1 = unplaced). It implements optimize.Model (+ Converger + Detailer).
The soft objective is maintained incrementally: slotSeats/slotExahm and the running cost totals (spread, attract, slot-load, unplaced) are updated per move, so Cost() is O(1) and a full pass over all conflict pairs is only needed once (initCost) and for reporting. pS[s] holds a student's current spread penalty P_s.
func Solve ¶
Solve builds a start assignment and improves it with simulated annealing. With warmStart the start is the exams' current plan (Unit.StartSlot) so a re-run only improves and keeps churn low; otherwise it is a fresh most-constrained-first construction. The returned State is left at the best assignment found.
func (*State) Converged ¶
Converged reports whether every movable unit is placed (the schedule is complete).
func (*State) Diagnostics ¶
func (st *State) Diagnostics() Diagnostics
Diagnostics computes the quality report for the current assignment.
func (*State) Propose ¶
Propose applies a random hard-feasible move (relocate 70%, swap 30%) and returns its undo, or nil to skip this step.
func (*State) TbauUsage ¶
TbauUsage reports how many of the booked T-building EXaHM/SEB seats are used (capped at the booking; SEB overflow to R-rooms does not count).
func (*State) TopLoadedSlots ¶
TopLoadedSlots returns the (day, slot, seats) of the n most loaded slots, for reporting.
func (*State) UnplacedAncodes ¶
UnplacedAncodes returns the ancodes of units left without a slot (for reporting).
type Unit ¶
type Unit struct {
ID int // representative ancode
Ancodes []int // all ancodes in this (same-slot) unit
Seats int // students to seat this slot (sum over members)
Exahm bool
Seb bool
Examer int // main examer id, for same-examer clustering (0 = unknown)
Module string // for section clustering
Program string
Allowed []int // allowed slot indices; empty = all slots allowed
Fixed bool
FixedSlot int
// StartSlot is the warm-start slot (this exam's current plan entry), -1 if none.
// Used only when solving with warmStart to begin from the existing plan.
StartSlot int
// Location is the exam's campus (empty = default). Exams at different campuses need
// travel time, so a same-day pair across campuses is penalized extra.
Location string
// Foreign: planned by another faculty (external / notPlannedByMe). A conflict
// between two Foreign exams is neither optimizable nor our problem, so it is left
// out of the objective and the diagnostics; our own fixed (pre-planned) exams are
// NOT Foreign.
Foreign bool
// contains filtered or unexported fields
}
Unit is one schedulable item. A "same slot" group is pre-merged into a single Unit (its Ancodes then holds all members). Fixed units (locked / external / notPlannedByMe) keep FixedSlot and are never moved.
type Weights ¶
type Weights struct {
Adjacent float64 // same day, adjacent slot (very bad)
SameDay float64 // same day, non-adjacent
DayFactor float64 // different days: DayFactor / calendarDaysBetween
WorstCase float64 // convex per-student term: + WorstCase * P_s^2
RepeatFactor float64 // multiplier applied (at build time) to repeat-tolerable pairs
Attract float64 // per distance rank for attract pairs (want them close)
SlotLoad float64 // convex penalty per seat over LoadThreshold in a slot
LoadThreshold int // soft seat threshold per slot
Unplaced float64 // penalty per unplaced unit (dominant)
CrossCampus float64 // extra penalty for a same-day student pair across campuses (travel gap)
TbauFill float64 // per unused booked T-building seat (EXaHM/SEB phase A: fill the rooms)
Hole float64 // per empty slot that lies between two occupied slots on the same day
// TimeOfDay scales the start-time avoidance penalty: per seat, per hour a slot's start
// time lies outside the wanted window (see Problem.TimeSeverity). 0 = off. The window
// is semester-dependent (winter: avoid early starts; summer: avoid late starts) and is
// baked into TimeSeverity by the caller, so the solver only needs the scalar weight.
TimeOfDay float64
}
Weights scale the soft objective. Placeholder values — to be calibrated against real data (Test26SS).
func DefaultWeights ¶
func DefaultWeights() Weights
DefaultWeights returns the calibrated weights (tuned against real data, Test26SS, 2026-07-02: Adjacent/SameDay high enough to push directly-consecutive to 0 and same-day exams down markedly, with a mild worst-case term protecting the least well-spread students). Ordered so the spread dominates and clustering / slot-load are tie-breakers; Unplaced dominates everything so the solver places all exams first.