Lab Partner and Version Assignment Solver

A constraint-satisfaction program using the Z3 SMT Solver to partition students into peer-review groups and assign lab versions.


Problem Overview

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.


What is Z3?

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:

  1. You define decision variables (placeholders for unknown answers).
  2. You declare rules and constraints that must hold true.
  3. You hand the rules to Z3, which mathematically searches for a combination of values that satisfies every rule simultaneously.

Quickstart & Setup with uv

uv is a fast Python package and project manager. It reads pyproject.toml and automatically installs the right Python version and packages for you.

1. Install uv

Windows (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.

2. Run the Program

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.

3. Run the Notebook (Optional)

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.


Input CSV Format

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
...

How the Code Works (Step-by-Step)

The Python program (assign_lab_partners.py) and notebook (Z3.ipynb) use the following workflow:

Step 1: Group Size Math (% 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

Step 2: Decision Variables (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):


Step 3: Domain & Range Constraints (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}$.


Step 4: Group Size Headcount (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)

Step 5: Version Diversity Constraints (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]))

Step 6: Solving & Extracting the Solution

if s.check() == sat:
    m = s.model()

File Manifest

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