From d328e16a7cac1e1bde810a383e97ebb013d4ec44 Mon Sep 17 00:00:00 2001 From: Davte Date: Thu, 8 Jan 2026 11:09:46 +0100 Subject: [PATCH] Assign projects to students using linear sum assignment --- .gitignore | 5 ++ Project assignments.ipynb | 121 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Project assignments.ipynb diff --git a/.gitignore b/.gitignore index 0c8bc9a..b61b2f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# Data and results +*.csv +*.pdf +*.html + # ---> JupyterNotebooks # gitignore template for Jupyter Notebooks # website: http://jupyter.org/ diff --git a/Project assignments.ipynb b/Project assignments.ipynb new file mode 100644 index 0000000..df89acb --- /dev/null +++ b/Project assignments.ipynb @@ -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 +}