{
 "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
}
