Conghao97's picture tcftrees's picture
ProgramGen release
d086025
|
Raw History Blame Contribute Delete
6.21 kB

sekigae3 — Usage Documentation (cleanroom)

sekigae3 is a seat-assignment optimization engine (席替え / seat-change solver). It takes a survey of which seats each student wants and which classmates they want to sit near, and computes an optimized seating arrangement as a distance-cost minimization problem.

Features

  • Per-student desired-seat preferences with weights.
  • Per-student pair preferences ("sit near this classmate") with weights. Negative weights are supported (keep-apart / disliked-seat preferences).
  • Pluggable distance function: build the problem as Problem<D> where D implements the DistanceFn trait. The default is Manhattan distance.

Build / install

The project is a Rust library crate named sekigae3 (edition 2024). Dependencies at runtime: getrandom, log.

cargo build --release

Library usage

The crate re-exports its main API at the root:

use sekigae3::{ILSA, Problem, Seat};
// Also available: Individual, WeightedSeatPref, DistanceFn

Core types and functions

Item Signature / Purpose
Seat Seat { x, y } seat coordinates (x = column, y = row).
WeightedSeatPref Type alias for (u16, f32) — an (id, weight) preference.
Problem::new(seats, want_seats, pair_edges) Build a problem with the default (Manhattan) distance function.
Problem::with_distance_fn(seats, want_seats, pair_edges, distance_fn) Build a problem with a custom DistanceFn.
problem.seat_count() Number of seats (usize).
problem.student_count() Number of students (currently assumes students = seats).
ILSA::new(&problem, seed: u64) Create a solver. Seed 0 draws randomness from the system (getrandom); any other value is used as a fixed seed.
solver.solve(budget) -> Individual Run the solve. budget is the maximum number of jumps/moves; a value around the seat count is a stable choice.
solver.solve_candidates(budget, max_candidates) -> Vec<Individual> Run the solve and return several candidate assignments.
best.cost() -> f32 Total cost of the assignment (lower is better).
best.by_seat() -> &[u16] Assignment indexed by seat: by_seat()[seat_id] = student_id.
best.seat_of() -> &[u16] Inverse mapping indexed by student: seat_of()[student_id] = seat_id.

Input format

  • seats: Vec<Seat> — the list of usable seats. The index in this vector is the SeatId.
  • want_seats: Vec<Vec<WeightedSeatPref>> — indexed by student (the index is the StudentId). Each entry lists that student's seat preferences as (seat_id, weight):
    • weight > 0: desired seat (larger = stronger preference)
    • weight = 0: indifferent
    • weight < 0: disliked seat An empty list means the student is indifferent about seats.
  • pair_edges: Vec<Vec<WeightedSeatPref>> — indexed by student. Each entry lists that student's pair preferences as (other_student_id, weight):
    • weight > 0: wants to sit near that classmate
    • weight = 0: indifferent
    • weight < 0: wants to be seated apart from that classmate A common convention is to enter only edges with a < b.

All vectors should be consistent with the seat/student counts (one entry per student; students = seats).

Output format

  • cost(): f32 — the cost of the found assignment; minimize this.
  • by_seat(): &[u16] — seat_id -> student_id mapping, e.g. [1, 2, 3, 5, 4, 0, 8, 7, 6].
  • seat_of(): &[u16] — student_id -> seat_id mapping.

Example

A 3x3 grid of nine seats and nine students with weighted pair preferences:

use sekigae3::{ILSA, Problem, Seat};

fn build_simple_problem() -> Problem {
    let seats = vec![
        Seat { x: 0, y: 0 },
        Seat { x: 1, y: 0 },
        Seat { x: 2, y: 0 },
        Seat { x: 0, y: 1 },
        Seat { x: 1, y: 1 },
        Seat { x: 2, y: 1 },
        Seat { x: 0, y: 2 },
        Seat { x: 1, y: 2 },
        Seat { x: 2, y: 2 },
    ];

    // want_seats[student] = [(seat_id, weight)]
    // Empty lists for everyone: all students are indifferent about seats
    // (useful for observing pair-preference behavior). A filled-in entry
    // would look like: vec![(8, 1.0), (4, 0.7)]
    let want_seats = (0..9).map(|_i| vec![]).collect();

    // pair_edges[student] = [(other_student_id, weight)]
    // Convention: only enter edges with a < b.
    let pair_edges = vec![
        vec![(1, 0.8), (3, 0.7), (4, 1.0)], // student 0 is close with 1, 3, 4
        vec![(2, 0.8), (4, 0.9)],
        vec![(5, 0.7), (4, 0.9)],
        vec![(6, 0.8), (4, 0.9)],
        vec![(5, 0.9), (7, 0.9), (8, 0.8)],
        vec![(8, 0.8)],
        vec![(7, 0.8)],
        vec![(8, 0.8)],
        vec![],                             // student 8 has no close friends
    ];

    Problem::new(seats, want_seats, pair_edges)
}

fn main() {
    let problem = build_simple_problem();

    let seat_count = problem.seat_count();

    let mut ilsa = ILSA::new(&problem, 42);
    let best = ilsa.solve(seat_count);

    println!("best cost: {:.3}", best.cost());
    println!("seat -> student: {:?}", best.by_seat());
    // Example output:
    // best cost: 18.200
    // seat -> student: [1, 2, 3, 5, 4, 0, 8, 7, 6]
}

Minimal single-seat smoke test:

use sekigae3::{ILSA, Problem, Seat};

let seats = vec![Seat { x: 0, y: 0 }];
let want_seats = vec![vec![(0u16, 1.0f32)]]; // student 0 wants seat 0
let pair_edges = vec![Vec::<(u16, f32)>::new()];

let problem = Problem::new(seats, want_seats, pair_edges);
let mut solver = ILSA::new(&problem, 0); // seed 0 -> system randomness
let best = solver.solve(1);              // budget ~ seat count

assert_eq!(best.by_seat().len(), 1);

Notes

  • Weights are relative: seat preferences and pair preferences are combined into one cost, so a strongly weighted preference can outweigh weaker ones (e.g. weighted seat wishes can take priority over pair cohesion when their weights are larger).
  • Same problem + same nonzero seed → reproducible result; seed 0 uses system randomness.
  • Distances between seats are computed with the configured DistanceFn; Manhattan distance is the default.