A constraint-satisfaction program using the Z3 SMT Solver to partition students into peer-review groups and assign lab versions.
In a programming course, students are assigned one of three versions of a lab assignment (Version A, B, or C). Students perform code reviews for their lab partners, but can only review work on a different version of the assignment.
Z3 is a high-performance SMT (Satisfiability Modulo Theories) solver developed by Microsoft Research.
Unlike traditional imperative programming where you must write algorithms (loops, backtracking, heuristics) to find a valid arrangement yourself, constraint programming is declarative:
uvuv is a fast Python package and project manager. It reads pyproject.toml and automatically installs the right Python version and packages for you.
uvWindows (PowerShell):
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
macOS: if you already use Homebrew, run brew install uv. Otherwise, use the installer below (no need to install Homebrew just for uv).
macOS / Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
Close and reopen your terminal afterward so it can find the uv command. Check it with uv --version.
Open a terminal in the ConstraintSolving folder you downloaded (for example, cd Downloads/ConstraintSolving), then run:
# Read students.csv and print the groups
uv run assign_lab_partners.py
# Use a different roster and save the results to a CSV file
uv run assign_lab_partners.py -i students.csv -o assignments.csv
The first uv run takes a minute: it downloads Python 3.14, creates a .venv folder, and installs z3-solver. After that, it starts right away.
Run uv sync in this folder to create the .venv folder. Then open Z3.ipynb in VS Code and choose the .venv Python environment as the kernel.
The input CSV file (students.csv) should contain student names in the first column. Headers like Name or Student are automatically recognized and skipped:
Name
Aaliyah Jackson
Hiroshi Tanaka
Mateo Rodriguez
Chloe Dubois
...
The Python program (assign_lab_partners.py) and notebook (Z3.ipynb) use the following workflow:
% Remainder Operator)To distribute any class size $N$ into groups of at most 3:
MAX_GROUP_SIZE = 3
num_groups = math.ceil(num_students / MAX_GROUP_SIZE)
remainder = num_students % MAX_GROUP_SIZE
if remainder == 0:
num_groups_of_2 = 0
elif remainder == 2:
num_groups_of_2 = 1
else: # remainder == 1 (e.g., 4 students remaining: split into two groups of 2)
num_groups_of_2 = 2
num_groups_of_3 = num_groups - num_groups_of_2
Int())group = []
version = []
for i in range(num_students):
group.append(Int(f"group_{i}"))
version.append(Int(f"version_{i}"))
The Int() function creates symbolic integer placeholders called decision variables (one per student):
group[i]: Represents the group index ($0 \le \text{group}_i < \text{num_groups}$) assigned to student $i$.version[i]: Represents the lab version ($0 = \text{A}$, $1 = \text{B}$, $2 = \text{C}$) assigned to student $i$.s.add())for i in range(num_students):
s.add(group[i] >= 0, group[i] < num_groups)
s.add(version[i] >= 0, version[i] < 3)
s.add() registers constraints with the solver. These rules restrict each student’s group number to the valid group range and version index to ${0, 1, 2}$.
If() & Overloaded +=)for g in range(num_groups):
group_size = 0
for i in range(num_students):
group_size += If(group[i] == g, 1, 0)
if g < num_groups_of_3:
s.add(group_size == 3)
else:
s.add(group_size == 2)
If(condition, then_value, else_value): Z3’s symbolic ternary operator. Because decision variables are unknown during code execution, standard Python if cannot inspect them. If(group[i] == g, 1, 0) acts as an indicator: evaluates to 1 if student $i$ is in group $g$, and 0 otherwise.+=: Z3 overloads the + operator. Rather than computing an immediate number, each += appends another term to the formula, building up group_size across all students.num_groups_of_3 groups must total 3 students; any remaining groups must total 2 students.Implies())for i in range(num_students):
for j in range(i + 1, num_students):
s.add(Implies(group[i] == group[j], version[i] != version[j]))
Implies(condition, result): Acts as an “if-then” rule. If student $i$ and student $j$ are assigned to the same group (group[i] == group[j]), then their versions must not match (version[i] != version[j]).if s.check() == sat:
m = s.model()
s.check(): Asks Z3 to find values that satisfy all constraints. It returns sat (satisfiable) if a valid arrangement exists.s.model(): Retrieves the concrete values assigned to each decision variable.0 -> 'A', 1 -> 'B', 2 -> 'C') and prints or exports the assignments.| File | Purpose |
|---|---|
assign_lab_partners.py |
Standalone command-line Python application. |
Z3.ipynb |
Interactive Jupyter Notebook version with step-by-step commentary. |
students.csv |
Sample input roster of 20 students. |
assignments.csv |
Sample output generated by the program. |
pyproject.toml |
Environment metadata and dependency specifications. |
README.md |
Documentation and setup guide. |
This readme file was generated by Claude Opus 5.5 and revised by Brian Bird 10/7/26