File size: 6,207 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | # 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`.
```sh
cargo build --release
```
## Library usage
The crate re-exports its main API at the root:
```rust
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:
```rust
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:
```rust
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.
|