English | 日本語
Formulates the on-site shift schedule of the university festival café as an integer linear programming (ILP) problem and solves it by mathematical optimization.
Making the café's shift schedule by hand is hard. Each member can come only at certain times, and every service slot needs a set number of people per role. Some roles need someone who can work them alone, and members have preferred roles. On top of that, the load should be fair and nobody should work too many slots in a row.
This is a variant of the nurse scheduling problem (NSP), also called the nurse rostering problem (NRP). The NSP asks to assign staff to shifts so that staffing requirements, availability and work rules are met while following preferences as far as possible [1, 2]. It is NP-hard in general [3], and no algorithm is known that finds an optimal schedule efficiently for every input. Schedules made by hand or by simple rules can therefore easily miss a better schedule, or even one that meets every requirement.
This project therefore finds an optimal schedule with a general-purpose MIP solver. Using branch and bound, the solver returns an optimal schedule together with a proof that no better one exists. At the size of this café (tens of members, about ten service slots), this takes seconds to minutes. Even when not every requirement can be met, the model still returns a schedule and reports where people are missing.
| Term | Meaning |
|---|---|
| Raw data | The Google Form responses, downloaded as CSV |
| Processed data | One TSV per event, cleaned from the raw data. Organizers edit it by hand (skill levels, corrections) |
| Event definition | config/<event>/: the slots and roles of the event. The form is built from it |
config/<event>/ Event definition, edited by hand (tracked by git)
slots.csv Shift slots; labels are the options shown on the form
roles.csv Roles; labels are the options shown on the form
data/
*.csv Raw data (contains personal data; not tracked by git)
processed/<event>.tsv Processed data (contains names; not tracked by git)
dummy/small.tsv Dummy processed data for config/dummy, 40 members
dummy/medium.tsv Dummy processed data for config/dummy, 60 members
model/
schedule.mod Optimization model (GNU MathProg)
scripts/
check_processed.awk Checks processed data against the event definition
make_dat.awk Generates the model's data file from the event definition and processed data
check_assignment.awk Checks a solved assignment against the processed data
render_schedule.sh Draws a solved schedule as an image (SVG; bash + gawk)
generate_dummy.sh Dummy data generator (bash + awk)
docs/ Images for this README (made from dummy data)
out/ Data files and results (may contain names; not tracked by git)
A dataset for the optimizer is config/<event>/ + data/processed/<event>.tsv (or data/dummy/*.tsv).
Everything runs in a Unix shell; there is nothing to build. On Windows, use WSL.
| Tool | Used for | |
|---|---|---|
| bash | scripts/*.sh |
required |
GNU awk (gawk) 4.0 or later |
every .awk script, cleaning the raw data (FPAT), drawing the schedule |
required |
GLPK (glpsol) |
solving the model | required |
coreutils (sort, cut, paste, …) |
cleaning the raw data | required (usually preinstalled) |
column from util-linux |
viewing the processed data (-o needs the util-linux version) |
optional |
| Google Chrome | converting the schedule image to PNG | optional |
| another MIP solver | Other solvers | optional |
RHEL / AlmaLinux / Rocky Linux :
sudo dnf install epel-release
sudo dnf install gawk glpk-utils util-linuxDebian / Ubuntu :
sudo apt install gawk glpk-utils bsdextrautilsmacOS :
brew install gawk glpk util-linuxCheck the setup with the dummy data:
gawk -f scripts/check_processed.awk config/dummy/roles.csv config/dummy/slots.csv data/dummy/small.tsv # prints OK
mkdir -p out
gawk -f scripts/make_dat.awk config/dummy/roles.csv config/dummy/slots.csv data/dummy/small.tsv > out/dummy.dat
glpsol -m model/schedule.mod -d out/dummy.dat --cutsglpsol should end with a summary in which missing people is 0, and write out/assignment.csv.
Tested with the commands above on AlmaLinux 10 (gawk 5.3, GLPK 5.0, bash 5.2), Debian 13 and Ubuntu 24.04, and macOS (gawk 5.4.1, GLPK 5.0, util-linux 2.42.4).
- Define the event in
config/<event>/(Event definition) - Create the Google Form from that definition (Creating the form)
- Download the responses as CSV into
data/ - Clean them into
data/processed/<event>.tsv(Cleaning the raw data) - Fill in skill levels and fix anything needed by hand, then run the check (Editing the processed data)
- Solve the shift schedule (Solving)
UTF-8 CSV with LF line endings.
date,slot,kind,start,end,label
2026-10-09,loading,loading,13:00,,荷運び
2026-10-10,setup,setup,09:00,10:00,朝の準備
2026-10-10,s1,service,10:00,11:30,10:00~11:30
...
2026-10-10,s5,service,16:00,18:00,16:00~18:00
2026-10-10,cleanup,cleanup,18:00,19:00,片付け
2026-10-11,setup,setup,09:00,10:00,朝の準備
...
| Column | Content |
|---|---|
date |
YYYY-MM-DD. Any number of dates is allowed; the form gets one question per date |
slot |
Slot ID, unique within the date, no spaces. A slot is identified by (date, slot) |
kind |
loading (load-in), setup (before the café opens), service (café open; needs people per role), cleanup (after the café closes). Everyone who answered a loading / setup / cleanup slot works it |
start, end |
HH:MM. setup ends when the first service slot of the date starts; cleanup starts when the last one ends |
label |
Option text on the form. Must be unique within the date and must not contain ", " |
role,label,skill,need
tea,紅茶,1,2
coffee,コーヒー,1,2
latte,ラテ,1,3
cashier,会計,0,2
support,縁の下,0,3
water,水汲み,0,1
| Column | Content |
|---|---|
role |
Role ID, no spaces or = |
label |
Option text on the form (unique) |
skill |
1 if organizers rate members' skill level for this role, else 0 |
need |
Number of people needed for this role in each service slot |
Create a Google Form with exactly these question titles. The cleaning procedure finds columns by title, so the order does not matter.
| Question title | Type | Options |
|---|---|---|
氏名 |
Short answer | |
学年 |
Drop-down | B1, B2, B3, B4, M1, M2, D |
参加できますか |
Multiple choice | はい, いいえ |
<M/D> 参加できる時間帯 (one per date, e.g. 10/10 参加できる時間帯) |
Checkboxes, not required | label of the slots on that date, in file order |
第1希望, 第2希望, 第3希望 |
Drop-down | label of every role |
備考 |
Paragraph | (free text; dropped when cleaning) |
- Turn on Collect email addresses (used to keep only the latest response per person).
- Do not add the "Other" option to checkboxes; free text belongs in
備考. - Leaving every box of a date unchecked means "not available that day".
One TSV per event (UTF-8, LF). One row per participant (参加できますか = はい).
member_id name grade pref skill 2027-10-08 2027-10-09 2027-10-10
M001 林 直樹 B3 cashier coffee support tea=2 coffee=1 latte=2 s2 s3 s2 s3
M003 木村 悠真 M1 cashier support water tea=1 coffee=2 latte=2 loading setup s1 s2 s3 s1 s2
(rows from data/dummy/small.tsv; columns are separated by tabs, aligned here for reading)
| Column | Content |
|---|---|
member_id |
M001, M002, … in submission order |
name |
Name as answered |
grade |
B1–B4, M1, M2, D |
pref |
Preferred roles (role IDs), space-separated, most preferred first. May be empty |
skill |
Skill levels set by organizers, role=level space-separated. Only roles with skill=1. Roles not written are level 0. Empty after cleaning |
| one column per date | slot IDs the member can work on that date, space-separated, in slots.csv order. Empty = not available |
Skill levels: 0 no experience, 1 can assist, 2 can work the role alone, 3 can teach others.
Dropped from the raw data: timestamp (used only for de-duplication), email address, 参加できますか, 備考.
Free-text answers that match no label are dropped with a warning.
Done by hand in the shell. Example for 2026:
RAW="data/2026技科大祭当日提供シフトメンバー募集(回答) - フォームの回答 1.csv"
CONF=config/2026
OUT=data/processed/2026.tsv
WORK=data/processed/2026.work # intermediate files
mkdir -p $WORK
DATES=$(tail -n +2 $CONF/slots.csv | cut -d, -f1 | sort -u | paste -sd' ') # all dates of the eventThe exported CSV has CRLF line endings, quoted values containing commas ("朝の準備, 10:00~11:30"),
and may have newlines inside free text. gawk's FPAT splits the fields, and a line with an unclosed quote is joined with the next one.
gawk -v FPAT='([^,]*)|("([^"]|"")*")' '
{ sub(/\r$/, "")
buf = (buf == "" ? $0 : buf " " $0)
if (gsub(/"/, "&", buf) % 2) next # unclosed quote -> join with the next line
$0 = buf; buf = ""
for (i = 1; i <= NF; i++) {
f = $i
if (f ~ /^"/) { f = substr(f, 2, length(f) - 2); gsub(/""/, "\"", f) }
gsub(/\t/, " ", f)
printf "%s%s", f, (i < NF ? "\t" : "\n")
} }' "$RAW" > $WORK/raw.tsv
awk -F'\t' '{ print NF }' $WORK/raw.tsv | sort | uniq -c # every line should have the same field countawk -F'\t' -v OFS='\t' -v dates="$DATES" '
NR == 1 { # header: find the columns by title
for (i = 1; i <= NF; i++) col[$i] = i
split("タイムスタンプ メールアドレス 参加できますか 氏名 学年 第1希望 第2希望 第3希望", q, " ")
for (k = 1; k <= 8; k++) q[k] = col[q[k]] ? col[q[k]] : err(q[k])
n = split(dates, d, " ")
for (k = 1; k <= n; k++) { # 2026-10-10 -> "10/10 参加できる時間帯"
split(d[k], ymd, "-"); title = (ymd[2] + 0) "/" (ymd[3] + 0) " 参加できる時間帯"
day[k] = col[title] ? col[title] : err(title)
}
next }
function err(title) { print "question not found: " title > "/dev/stderr"; exit 1 }
{ split($q[1], t, /[\/ :]/) # 2026/09/02 7:59:48 -> 20260902075948 (sortable)
printf "%04d%02d%02d%02d%02d%02d", t[1], t[2], t[3], t[4], t[5], t[6]
printf "\t%s", tolower($q[2])
for (k = 3; k <= 8; k++) printf "\t%s", $q[k]
for (k = 1; k <= n; k++) printf "\t%s", $day[k]
print "" }' $WORK/raw.tsv \
| sort -t$'\t' -k1,1r \
| sort -t$'\t' -s -u -k2,2 \
| awk -F'\t' '$3 == "はい"' \
| sort -t$'\t' -k1,1 \
| cut -f4- > $WORK/answers.tsvsort -k1,1rorders newest first;sort -s -u -k2,2(stable, unique on email) then keeps only the first, i.e. latest, response per person- Filter participants after de-duplication, so a person whose latest answer is "no" is not kept by an older "yes"
- Finally restore submission order and drop timestamp, email and the join flag
Columns of answers.tsv: name, grade, pref 1, pref 2, pref 3, then one column per date in $DATES order.
Role labels are converted with roles.csv, slot labels with slots.csv (looked up by date and label).
Unknown labels are reported and dropped; repeated preferences keep only the first.
awk -F'\t' -v OFS='\t' -v dates="$DATES" '
BEGIN { n = split(dates, d, " ") }
FILENAME == ARGV[1] { if (FNR > 1) { split($0, a, ","); role[a[2]] = a[1] } next } # roles.csv: label -> role
FILENAME == ARGV[2] { if (FNR > 1) { split($0, a, ","); slot[a[1], a[6]] = a[2] # slots.csv: (date, label) -> slot
order[a[1]] = order[a[1]] " " a[2] } next }
FNR == 1 { printf "member_id\tname\tgrade\tpref\tskill"; for (k = 1; k <= n; k++) printf "\t%s", d[k]; print "" }
{ id = sprintf("M%03d", FNR)
name = $1; gsub(/^[ ]+|[ ]+$/, "", name)
pref = ""; split("", used)
for (i = 3; i <= 5; i++) {
if ($i == "") continue
if (!($i in role)) { print id ": unknown role: " $i > "/dev/stderr"; continue }
r = role[$i]; if (!(r in used)) { used[r]; pref = pref (pref == "" ? "" : " ") r }
}
printf "%s\t%s\t%s\t%s\t", id, name, $2, pref # skill is left empty
for (k = 1; k <= n; k++) {
split("", chosen); m = split($(5 + k), v, /, /)
for (j = 1; j <= m; j++)
if ((d[k], v[j]) in slot) chosen[slot[d[k], v[j]]]
else if (v[j] != "") print id ": unknown option on " d[k] ": " v[j] > "/dev/stderr"
out = ""; c = split(order[d[k]], o, " ") # write slots in slots.csv order
for (j = 1; j <= c; j++) if (o[j] in chosen) out = out (out == "" ? "" : " ") o[j]
printf "\t%s", out
}
print "" }' $CONF/roles.csv $CONF/slots.csv $WORK/answers.tsv > $WORK/new.tsvIf $OUT already exists (re-running after more responses came in), skill levels are carried over by name,
because member_id may change. Other hand edits are not carried over: make them after the form is closed.
touch $OUT
awk -F'\t' -v OFS='\t' '
FILENAME == ARGV[1] { if (FNR > 1 && $5 != "") skill[$2] = $5; next }
FNR > 1 && $5 == "" && ($2 in skill) { $5 = skill[$2]; delete skill[$2] }
{ print }
END { for (s in skill) print "skill not carried over (name not found): " s > "/dev/stderr" }' \
$OUT $WORK/new.tsv > $WORK/merged.tsv && mv $WORK/merged.tsv $OUT
gawk -f scripts/check_processed.awk $CONF/roles.csv $CONF/slots.csv $OUTThe intermediate files still contain email addresses and free text. Delete them once you have checked the output.
rm $WORK/raw.tsv $WORK/answers.tsv $WORK/new.tsv && rmdir $WORKThe cleaning may also be done by an AI coding agent (e.g. Claude Code) that can run shell commands in this repository. The raw data contains names and email addresses: use only an AI tool that your organization allows for personal data.
Example prompt:
Clean the Google Form responses (raw data) into processed data, following
"Cleaning the raw data" in README.md exactly.
- Raw data: data/<file name>.csv
- Event definition: config/2027/
- Output: data/processed/2027.tsv
Rules:
- Use the commands of the procedure as they are; do not add your own processing.
If something cannot be handled by the procedure, stop and ask me.
- At the end, run scripts/check_processed.awk and report the result.
- Report every warning (unknown role / unknown option / skill not carried over) as a list.
Do not interpret free-text answers or reflect them in the data.
- Do not print names, email addresses or other personal data beyond what the report needs.
- After I have confirmed the result, delete the intermediate files with step 5.
The file is plain TSV, so columns do not line up in an editor. Align them for viewing with column
(handles empty cells and full-width characters; the file is not changed):
column -t -s $'\t' data/processed/2026.tsv
column -t -s $'\t' -o ' │ ' data/processed/2026.tsv | less -S # with separators, scroll sidewaysA shell alias is handy: alias tsv="column -t -s $'\t' -o ' │ '", then tsv data/processed/2026.tsv.
Open data/processed/<event>.tsv in an editor or a spreadsheet and edit it directly.
Editor features that align columns by padding cells with spaces (e.g. "align" commands of CSV extensions) must be undone
before processing; the check below reports padded cells.
- Skill levels: write
role=levelinskill, e.g.tea=2 latte=3 - Availability: add or remove
slotIDs in the date columns (e.g. for answers like "same as the day before" that were dropped)
Then check it:
gawk -f scripts/check_processed.awk config/2026/roles.csv config/2026/slots.csv data/processed/2026.tsvIt reports unknown grades, roles, slots, skill levels outside 0–3, wrong column counts, duplicate IDs and cells padded with spaces.
model/schedule.mod (GNU MathProg) assigns members to roles in the service slots.
Load-in, setup and cleanup slots are worked by everyone who answered them, so they are not optimized.
| Symbol | Meaning |
|---|---|
|
|
members, roles, roles with skill levels |
service slots |
|
|
|
|
|
|
|
people needed per role in a service slot (roles.csv) |
|
preference score (pref_score): 3 / 2 / 1 for the 1st / 2nd / 3rd preference, else 0 |
|
| skill level (0–3), only for |
|
skill level that counts as "can work alone" (lead_level, default 2) |
|
people per service slot who can work role lead_need, default 1; must be |
|
| number of service slots member |
|
fewest service slots per member (min_load, default 1) |
|
most slots in a row (max_consecutive, default 3) |
Variables:
-
$x_{mtr} \in {0,1}$ for$(m, t) \in A^{svc}$ ,$r \in R$ : member$m$ works role$r$ in service slot$t$ -
$u_{tr} \ge 0$ for$t \in S^{svc}$ ,$r \in R$ : missing people -
$v_{tr} \ge 0$ for$t \in S^{svc}$ ,$r \in R^{skill}$ : missing can-work-alone people -
$z \ge 0$ : service slots of the busiest member -
$y_m \ge 0$ for$a_m > 0$ : service slots member$m$ gets below$\min(L, a_m)$
In the sums,
| Weight | Parameter | Default | Per |
|---|---|---|---|
w_short |
1000 | missing person | |
w_lead |
100 | missing can-work-alone person | |
w_pref |
1 | preference score point | |
w_fair |
10 | service slot of the busiest member | |
w_under |
50 | service slot a member gets below min_load
|
Weights are assumed to be non-negative.
| Constraint | |
|---|---|
| one role at a time | |
| people per role in each service slot | |
| at least |
|
| service slots of every member are at most |
|
| everyone who answered a service slot works at least |
|
|
|
at most |
In the last row, slots.csv on the same date.
Runs without a service slot are left out: their sum is a constant that the model cannot change, and if it exceeded
Missing people are allowed at a high cost, so the model always has a solution and reports where people are missing.
Leaving a member below min_load is also allowed (it can be unavoidable when there are more members than seats).
With the default weights, the model normally gives priority in this order: missing people, missing can-work-alone people, members below min_load, then preferences and fairness.
This order comes from the ratios of the weights and is not strictly guaranteed: for example, since w_lead is exactly twice w_under, one missing can-work-alone person and two members below min_load cost the same.
To make the order stricter, widen the gaps (e.g. w_lead more than twice w_under).
Weights (w_short, w_lead, w_pref, w_fair, w_under), preference scores (pref_score), the level that counts as "can work alone" (lead_level),
the number of such people (lead_need), the most slots in a row (max_consecutive) and the fewest service slots per member (min_load) are parameters with defaults; override them in an extra data file:
data;
param w_fair := 30;
param lead_need := latte 2;
param max_consecutive := 2;
end;
mkdir -p out
gawk -f scripts/make_dat.awk config/2026/roles.csv config/2026/slots.csv data/processed/2026.tsv > out/2026.dat
glpsol -m model/schedule.mod -d out/2026.dat --cuts # add -d weights.dat to override parameters
gawk -v max_consecutive=3 -f scripts/check_assignment.awk config/2026/slots.csv data/processed/2026.tsv out/assignment.csv--cuts turns on GLPK's cutting planes; without it, some data sets (2026) find the optimum quickly but take minutes to prove it.
glpsol prints the assignment per slot and a summary (missing people, busiest member, members with no service slot, preferences met),
and writes out/assignment.csv (member_id,name,date,slot,role; role is empty for load-in / setup / cleanup).
check_assignment.awk confirms that every assignment is in a slot the member answered, nobody is assigned twice to one slot,
and nobody works more than max_consecutive slots in a row (give the same value as the model); it also prints the longest run.
Draw the result as an image: one row per member, one column per slot, the role written in each cell.
scripts/render_schedule.sh --config config/2026 --processed data/processed/2026.tsv \
--assignment out/assignment.csv -o out/schedule.svg --title "シフト表 2026"Open out/schedule.svg in a browser (hovering a cell shows its details). For a PNG, let Chrome take a screenshot;
the size is printed in the SVG's width / height:
google-chrome --headless=new --hide-scrollbars --window-size=1224,1338 \
--screenshot=out/schedule.png "file://$PWD/out/schedule.svg"Example with data/dummy/small.tsv:
Cells: role (colored, with its name) · 参加 for load-in / setup / cleanup · dashed outline = available but not assigned ·
empty = not available. The right column counts service slots per member; the bottom row marks missing people with ▲.
To use another MIP solver (HiGHS, SCIP, CPLEX, Gurobi, …), write the model in LP format and give it to that solver:
glpsol -m model/schedule.mod -d out/2026.dat --check --wlp out/2026.lpscripts/generate_dummy.sh generates processed data for any event definition (bash + awk only).
The dates, slots and roles come from the config.
scripts/generate_dummy.sh --config config/dummy -o data/dummy/small.tsv -n 40 --seed 0
scripts/generate_dummy.sh --config config/dummy -o data/dummy/medium.tsv -n 60 --seed 1It prints the number of service seats (service slots × people needed per slot) and of members who can work a service slot, and warns when there are more members than seats: then someone gets no service slot and the solver slows down.
The same command with the same --seed gives the same data again, as long as the script and the awk implementation are the same.
The commands above show how the files in data/dummy/ were made, but those files are not guaranteed to match what the current script generates
(the script may have changed since, or the files may have been edited); data/dummy/small.tsv currently does not.
Names are random combinations of common Japanese family and given names.
For each date, a member is available for:
- each
loadingslot with probability 45% - every slot of the date (18%), none (12%), or a contiguous block of
serviceslots, occasionally with one slot missing in the middle. A block starting at the first service slot tends to includesetup; one ending at the last service slot tends to includecleanup
A date with the same slot kinds as the previous one copies its pattern with probability 35%.
Skill levels are drawn for every role with skill=1, higher for senior grades and often one level higher for the first preference.
F=data/dummy/small.tsv
# Long format: one line per (member, date, slot)
awk -F'\t' -v OFS='\t' 'NR == 1 { for (i = 6; i <= NF; i++) h[i] = $i; next }
{ for (i = 6; i <= NF; i++) { m = split($i, v, " "); for (j = 1; j <= m; j++) print $1, h[i], v[j] } }' $F > long.tsv
# Number of available members per slot
cut -f2,3 long.tsv | sort | uniq -c
# Distribution of available slots per member (how many members can work N slots)
cut -f1 long.tsv | uniq -c | awk '{ print $1 }' | sort -n | uniq -c
# Breakdown of first preferences
tail -n +2 $F | cut -f4 | cut -d' ' -f1 | sort | uniq -c | sort -rn
# Members who can make lattes alone (level 2 or more)
awk -F'\t' 'NR > 1 && $5 ~ /(^| )latte=[23]/ { print $1, $2, $5 }' $F- Wikipedia, "Nurse scheduling problem" (revision of 16 August 2026). https://en.wikipedia.org/w/index.php?title=Nurse_scheduling_problem&oldid=1369652809
- E. K. Burke, P. De Causmaecker, G. Vanden Berghe, H. Van Landeghem. The state of the art of nurse rostering. Journal of Scheduling 7(6): 441–499, 2004. doi:10.1023/B:JOSH.0000046076.75950.0b
- T. Osogami, H. Imai. Classification of various neighborhood operations for the nurse scheduling problem. In Algorithms and Computation (ISAAC 2000), LNCS 1969, pp. 72–83, Springer, 2000. doi:10.1007/3-540-40996-3_7