Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Prayush Kumar
/
icts-housing
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a29ea1ca
authored
Jul 31, 2023
by
Prayush Kumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial draft of notebook for housing allocations this year
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
292 additions
and
0 deletions
2023__housing_allocations.ipynb
2023__housing_allocations.ipynb
0 → 100644
View file @
a29ea1ca
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# import numpy as np\n",
"# import pandas as pd\n",
"import random\n",
"import copy\n",
"import datetime\n",
"\n",
"# Set RNG seed using Aug 1, 2023 9am\n",
"random.seed(int(datetime.datetime(2023, 8, 1, 9, 0, 0, 0).timestamp()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" A simple CSV/XLSX file with at least the following columns (it can have additional columns, doesn't matter):\n",
"\n",
" Name, Year of study, Student or Postdoc, Female or not, Preferences, ..\n",
"\n",
" A sample entry would be:\n",
" Albert Einstein, 1, student, no, [0, 1, 2, 3, 4, 5, 6, 8, 7],\n",
"\n",
" Total number of available housing in each category.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# inputs = pd.read_csv(\"\")\n",
"inputs = {}\n",
"inputs[\"name\"] = [\n",
" \"person 1\",\n",
" \"person 2\",\n",
" \"person 3\"\n",
"]\n",
"inputs[\"year\"] = [\n",
" 1, 3, 2\n",
"]\n",
"inputs[\"student_or_postdoc\"] = [\n",
" \"student\", \"student\", \"postdoc\"\n",
"]\n",
"inputs[\"course\"] = [\n",
" \"iphd\", \"phd\", \"postdoc\"\n",
"]\n",
"\n",
"inputs[\"female\"] = [\n",
" 1, 0, 0\n",
"]\n",
"inputs[\"preferences\"] = [\n",
" [0,5,1,2,3],\n",
" [0,5,1,2,3],\n",
" [0,5,1,2,3]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"availability = {\n",
" 0: 15,\n",
" 1: 23,\n",
" 2: 25,\n",
" 3: 35,\n",
" 4: 40,\n",
" 5: 18\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def get_info_for_person(person_name, col_name):\n",
" if person_name not in inputs[\"name\"]:\n",
" raise IOError(f\"We do not recognize name: {person_name}\")\n",
" \n",
" if col_name not in inputs:\n",
" raise IOError(f\"We were not provided with info: {col_name}\")\n",
" \n",
" idx_of_person = inputs[\"name\"].index(person_name)\n",
" return inputs[col_name][idx_of_person]\n",
"\n",
"def get_pool_number(person_name):\n",
" pool_number = 3\n",
" if (get_info_for_person(person_name, \"female\") == 1) and (\n",
" get_info_for_person(person_name, \"year\") == 1\n",
" ):\n",
" pool_number = 1\n",
" elif (get_info_for_person(person_name, \"year\") == 1) or (\n",
" (get_info_for_person(person_name, \"year\") == 5 and get_info_for_person(\n",
" person_name, \"course\") == \"phd\") or (\n",
" get_info_for_person(person_name, \"year\") == 6 and get_info_for_person(\n",
" person_name, \"course\") == \"iphd\")\n",
" ):\n",
" pool_number = 2\n",
" return pool_number\n",
"\n",
"def get_possible_pools():\n",
" return [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inputs look good. We got 3 entries.\n"
]
}
],
"source": [
"total_number_of_people = len(inputs[\"name\"])\n",
"\n",
"for col_name in inputs:\n",
" if len(inputs[col_name]) != total_number_of_people:\n",
" raise IOError(\"There are {} entries for column {}. We need {}.\".format(\n",
" len(inputs[col_name]), col_name, total_number_of_people\n",
" ))\n",
"\n",
"print(f\"Inputs look good. We got {total_number_of_people} entries.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assign people to lottery pools"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here are our assignments:\n",
"In pool 1, we have:\n",
" ['person 1']\n",
"In pool 2, we have:\n",
" []\n",
"In pool 3, we have:\n",
" ['person 2', 'person 3']\n"
]
}
],
"source": [
"housing_pools = {}\n",
"\n",
"for pool_id in get_possible_pools():\n",
" _pool = []\n",
" for person_name in inputs[\"name\"]:\n",
" if get_pool_number(person_name) == pool_id:\n",
" _pool.append(person_name)\n",
" housing_pools[pool_id] = _pool\n",
"\n",
"print(\"Here are our assignments:\")\n",
"\n",
"for pool_id in housing_pools:\n",
" print(f\"In pool {pool_id}, we have:\\n {housing_pools[pool_id]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw lottery and assign housing"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"As per a randomized lottery, we will allocate in the following order:['person 1']\n",
"...assigning housing 0 to person 1 as it has 15 vacancies still.\n",
"\n",
"As per a randomized lottery, we will allocate in the following order:[]\n",
"\n",
"As per a randomized lottery, we will allocate in the following order:['person 2', 'person 3']\n",
"...assigning housing 0 to person 2 as it has 14 vacancies still.\n",
"...assigning housing 0 to person 3 as it has 13 vacancies still.\n"
]
}
],
"source": [
"housing_allocations = {}\n",
"current_availability = copy.deepcopy(availability)\n",
"\n",
"for pool_id in get_possible_pools():\n",
" # randomly assign ranks, and sort according to it\n",
" this_pool = housing_pools[pool_id]\n",
" random.shuffle(this_pool)\n",
" \n",
" print(f\"\\nAs per a randomized lottery, we will allocate in the following order:\"\n",
" f\"{this_pool}\") \n",
" \n",
" # now we go over the ranked list, and assign housing based on availability\n",
" for person_name in this_pool:\n",
" their_preferences = get_info_for_person(person_name, \"preferences\")\n",
" for pref in their_preferences:\n",
" if current_availability[pref] > 0:\n",
" # Allot NOW!\n",
" print(f\"\"\"...assigning housing {pref} to {\n",
" person_name} as it has {\n",
" current_availability[pref]} vacancies still.\"\"\")\n",
" housing_allocations[person_name] = pref\n",
" current_availability[pref] = current_availability[pref] - 1\n",
" break\n",
" else:\n",
" print(f\"For {person_name}, acco {pref} is full. Moving on...\")\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here are proposed housing allocations:\n",
"{'person 1': 0, 'person 2': 0, 'person 3': 0}\n"
]
}
],
"source": [
"print(f\"Here are proposed housing allocations:\\n{housing_allocations}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment