Assign projects to students using linear sum assignment

This commit is contained in:
2026-01-08 11:09:46 +01:00
parent 53a67e1ac4
commit d328e16a7c
2 changed files with 126 additions and 0 deletions

5
.gitignore vendored
View File

@@ -1,3 +1,8 @@
# Data and results
*.csv
*.pdf
*.html
# ---> JupyterNotebooks # ---> JupyterNotebooks
# gitignore template for Jupyter Notebooks # gitignore template for Jupyter Notebooks
# website: http://jupyter.org/ # website: http://jupyter.org/

121
Project assignments.ipynb Normal file
View File

@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "49839009",
"metadata": {},
"source": [
"# Import dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb8a880c",
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import pandas as pd\n",
"from scipy.optimize import linear_sum_assignment\n",
"import prettytable as pt"
]
},
{
"cell_type": "markdown",
"id": "8f2e14e2",
"metadata": {},
"source": [
"# Read data\n",
"Read the CSV file where I stored Google Form responses. The matrix has been\n",
"transposed (rows = projects, columns = people) and the Borda count matrix score\n",
"was evaluated subtracting 1 to poll responses (a score of 0 corresponds to the\n",
"most desirable assignation)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf13fa73",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"form_responses.csv\", index_col=0)\n",
"cost = df.values"
]
},
{
"cell_type": "markdown",
"id": "391fd82c",
"metadata": {},
"source": [
"# Solve the assignment problem using scipy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7efa127e",
"metadata": {},
"outputs": [],
"source": [
"row_ind, col_ind = linear_sum_assignment(cost)\n",
"assignment = pd.DataFrame({\n",
" \"Project\": df.index[row_ind],\n",
" \"Assignee\": df.columns[col_ind],\n",
" \"Cost\": cost[row_ind, col_ind],\n",
"})"
]
},
{
"cell_type": "markdown",
"id": "b5e6f5a2",
"metadata": {},
"source": [
"# Show results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4563429",
"metadata": {},
"outputs": [],
"source": [
"print(\"These are the best assignments:\")\n",
"assignment[[\"Assignee\", \"Cost\", \"Project\"]].reset_index(drop=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac221b1f",
"metadata": {},
"outputs": [],
"source": [
"print(f\"The total cost of these assignments was {assignment['Cost'].sum()}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pytorch",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}