From 6e3dde7b834f60811a47cb696b10fe2d31d1494c Mon Sep 17 00:00:00 2001 From: BAAAKA Date: Sun, 29 Oct 2023 11:24:47 +0100 Subject: [PATCH] added remove_previous_mnatches, not active yet tho --- functions/matching_users.py | 51 +--- notebooks/playground_roy.ipynb | 522 +++++++++++++++++++++++---------- 2 files changed, 391 insertions(+), 182 deletions(-) diff --git a/functions/matching_users.py b/functions/matching_users.py index 67ac6a9..f0e03cb 100644 --- a/functions/matching_users.py +++ b/functions/matching_users.py @@ -5,7 +5,7 @@ import numpy as np from sklearn.metrics.pairwise import cosine_similarity import collections -from data import User +from functions.data import User def get_sorted_list_func(user_ids, user_preferences): def get_sorted_list(user_index): @@ -37,6 +37,14 @@ def convert_preferences_to_matrix(users_preferences): return pref_matrix +def remove_previous_matches(preference_order, previous_matches): + + for match in previous_matches: + preference_order[match[0]].pop(preference_order[match[0]].index(match[1])) + preference_order[match[1]].pop(preference_order[match[1]].index(match[0])) + + return preference_order + def create_matches_from_users(users: list[User]): # now we need to build the matrix names_to_player = {x.id: Player(x.id) for x in users} @@ -45,10 +53,16 @@ def create_matches_from_users(users: list[User]): names = [x.id for x in users] pref_matrix = convert_preferences_to_matrix([x.preferences for x in users]) + + #print(pref_matrix) preference_order = get_preference_lists(names, pref_matrix) #print(preference_order) + # Uncomment line below if we want to remove previous matches, just need list of touples of previous matches + # like [(1,2),(3,4)] + # preference_order = remove_previous_matches(preference_order, previous_matches) + for name,pref in zip(names,preference_order): player = names_to_player[name] player.set_prefs([names_to_player[x] for x in pref]) @@ -71,37 +85,4 @@ def create_matches_from_users(users: list[User]): return user_tuples -if __name__ == "__main__": - users = [User("1", {"preferences": { - "guns": 5, - "dogs": 5 - }, - "days": [ - "mon", - "fri" - ], - "name": "John Wick", - "id": "1" - }), - User("2", {"preferences": { - "guns": 5, - "dogs": 3 - }, - "days": [ - "mon", - "fri" - ], - "name": "John Shirt", - "id": "2" - }), - User("3", {"preferences": { - - }, - "days": [ - "mon", - "fri" - ], - "name": "John Pants", - "id": "3" - }), ] - print([(match1.id,match2.id) for match1,match2 in create_matches_from_users(users)]) \ No newline at end of file + diff --git a/notebooks/playground_roy.ipynb b/notebooks/playground_roy.ipynb index 30ca9df..816f519 100644 --- a/notebooks/playground_roy.ipynb +++ b/notebooks/playground_roy.ipynb @@ -2,14 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "initial_id", "metadata": { + "collapsed": true, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.377018257Z", - "start_time": "2023-10-28T18:28:12.277786742Z" - }, - "collapsed": true + "end_time": "2023-10-29T09:57:19.110211910Z", + "start_time": "2023-10-29T09:57:17.587060842Z" + } }, "outputs": [], "source": [ @@ -23,28 +23,28 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "3b50d41a9f16ce5b", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.377499356Z", - "start_time": "2023-10-28T18:28:13.375857450Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.112294698Z", + "start_time": "2023-10-29T09:57:19.071128937Z" + } }, "outputs": [], "source": [] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "65d0fd8af0d27c74", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.377705620Z", - "start_time": "2023-10-28T18:28:13.376246262Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.113036020Z", + "start_time": "2023-10-29T09:57:19.072023045Z" + } }, "outputs": [], "source": [] @@ -73,14 +73,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "id": "26b37c1f39e49260", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.378126071Z", - "start_time": "2023-10-28T18:28:13.376429681Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.114204690Z", + "start_time": "2023-10-29T09:57:19.072849157Z" + } }, "outputs": [ { @@ -116,14 +116,14 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "id": "c317ebd2293a265e", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.378431973Z", - "start_time": "2023-10-28T18:28:13.376627094Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.114622405Z", + "start_time": "2023-10-29T09:57:19.082159857Z" + } }, "outputs": [], "source": [ @@ -152,14 +152,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "id": "42c007d8220de103", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.424885426Z", - "start_time": "2023-10-28T18:28:13.378278069Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.114977334Z", + "start_time": "2023-10-29T09:57:19.093553365Z" + } }, "outputs": [], "source": [ @@ -193,21 +193,21 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "id": "18d5b71c249a74dc", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.554721553Z", - "start_time": "2023-10-28T18:28:13.389594964Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.362347786Z", + "start_time": "2023-10-29T09:57:19.101692525Z" + } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Elapsed time: 0.003764 seconds\n", + "Elapsed time: 0.002283 seconds\n", "[[1. 0.95470327 0.63543805 0.58609427]\n", " [0.95470327 1. 0.51663504 0.46709937]\n", " [0.63543805 0.51663504 1. 0.97990505]\n", @@ -241,14 +241,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "id": "4c43837fa4f45e75", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.559896782Z", - "start_time": "2023-10-28T18:28:13.422536885Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.364929894Z", + "start_time": "2023-10-29T09:57:19.149164194Z" + } }, "outputs": [ { @@ -291,14 +291,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "id": "f5af822fe25d115", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.589098761Z", - "start_time": "2023-10-28T18:28:13.438019719Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.365761893Z", + "start_time": "2023-10-29T09:57:19.150646799Z" + } }, "outputs": [], "source": [ @@ -330,14 +330,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "id": "5a714455fba00efa", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.589786351Z", - "start_time": "2023-10-28T18:28:13.479833902Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.379289936Z", + "start_time": "2023-10-29T09:57:19.189824573Z" + } }, "outputs": [], "source": [ @@ -359,14 +359,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "id": "bfe55b16c578ab43", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.590056277Z", - "start_time": "2023-10-28T18:28:13.480175539Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.380537793Z", + "start_time": "2023-10-29T09:57:19.191263077Z" + } }, "outputs": [], "source": [ @@ -398,14 +398,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "id": "8a9f14fee1d3131b", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.590247334Z", - "start_time": "2023-10-28T18:28:13.480300448Z" - }, - "collapsed": false + "end_time": "2023-10-29T09:57:19.381116300Z", + "start_time": "2023-10-29T09:57:19.192613053Z" + } }, "outputs": [], "source": [] @@ -422,62 +422,55 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 37, "id": "7302285b5d9034d2", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.590500659Z", - "start_time": "2023-10-28T18:28:13.480430156Z" - }, - "collapsed": false + "end_time": "2023-10-29T10:15:37.618197471Z", + "start_time": "2023-10-29T10:15:37.469236159Z" + } }, "outputs": [], "source": [] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 37, "id": "ff595b57e15371f3", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.592658644Z", - "start_time": "2023-10-28T18:28:13.480515939Z" - }, - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[Snake, Panda, Tree, Dolphin]\n" - ] + "end_time": "2023-10-29T10:15:37.671088281Z", + "start_time": "2023-10-29T10:15:37.521162882Z" } - ], + }, + "outputs": [], "source": [] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 38, "id": "a8a6628dba1b9186", "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-29T07:54:56.702168114Z", - "start_time": "2023-10-29T07:54:56.107892608Z" + "end_time": "2023-10-29T10:15:37.672392654Z", + "start_time": "2023-10-29T10:15:37.521768458Z" } }, "outputs": [ { "ename": "ModuleNotFoundError", - "evalue": "No module named 'data'", + "evalue": "No module named 'firebase_admin'", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mModuleNotFoundError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[14], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfunctions\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmatching_users\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;241m*\u001B[39m\n\u001B[1;32m 4\u001B[0m user_ids \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mSnake\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mPanda\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mTree\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mDolphin\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mWater\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mBasel\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mTable 16\u001B[39m\u001B[38;5;124m'\u001B[39m]\n\u001B[1;32m 5\u001B[0m preference_matrix \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([\n\u001B[1;32m 6\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m],\n\u001B[1;32m 7\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m4\u001B[39m, \u001B[38;5;241m5\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m],\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 12\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m0\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m]\n\u001B[1;32m 13\u001B[0m ], dtype\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124muint8\u001B[39m\u001B[38;5;124m'\u001B[39m)\n", - "File \u001B[0;32m~/DataspellProjects/randoms/functions/matching_users.py:8\u001B[0m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01msklearn\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmetrics\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mpairwise\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m cosine_similarity\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mcollections\u001B[39;00m\n\u001B[0;32m----> 8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mdata\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m User\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_sorted_list_func\u001B[39m(user_ids, user_preferences):\n\u001B[1;32m 11\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_sorted_list\u001B[39m(user_index):\n", - "\u001B[0;31mModuleNotFoundError\u001B[0m: No module named 'data'" + "Cell \u001B[0;32mIn[38], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfunctions\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmatching_users\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;241m*\u001B[39m\n\u001B[1;32m 4\u001B[0m user_ids \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mSnake\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mPanda\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mTree\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mDolphin\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mWater\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mBasel\u001B[39m\u001B[38;5;124m'\u001B[39m,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mTable 16\u001B[39m\u001B[38;5;124m'\u001B[39m]\n\u001B[1;32m 5\u001B[0m preference_matrix \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([\n\u001B[1;32m 6\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m],\n\u001B[1;32m 7\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m4\u001B[39m, \u001B[38;5;241m5\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m],\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 12\u001B[0m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m0\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m]\n\u001B[1;32m 13\u001B[0m ], dtype\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124muint8\u001B[39m\u001B[38;5;124m'\u001B[39m)\n", + "File \u001B[0;32m~/DataspellProjects/randoms/functions/matching_users.py:8\u001B[0m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01msklearn\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmetrics\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mpairwise\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m cosine_similarity\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mcollections\u001B[39;00m\n\u001B[0;32m----> 8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfunctions\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdata\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m User\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_sorted_list_func\u001B[39m(user_ids, user_preferences):\n\u001B[1;32m 11\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_sorted_list\u001B[39m(user_index):\n", + "File \u001B[0;32m~/DataspellProjects/randoms/functions/data.py:6\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mrandom\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfaker\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Faker\n\u001B[0;32m----> 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfirebase_admin\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m firestore\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfirebase_admin\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m auth\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mUser\u001B[39;00m:\n", + "\u001B[0;31mModuleNotFoundError\u001B[0m: No module named 'firebase_admin'" ] } ], @@ -504,69 +497,324 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 76, "id": "9a29c81b1b0669ff", "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-29T08:04:50.833143676Z", - "start_time": "2023-10-29T08:04:50.781673733Z" + "end_time": "2023-10-29T10:22:52.995123710Z", + "start_time": "2023-10-29T10:22:52.509889524Z" } }, - "outputs": [ - { - "data": { - "text/plain": "'I really like Sports!'" - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import json\n", "from datetime import date\n", "import random\n", "from faker import Faker\n", "\n", + "class User:\n", + " def __init__(self, id: str, doc: dict):\n", + " self.id = id\n", + " self.name = doc[\"name\"]\n", + " self.preferences = doc[\"preferences\"]\n", + " self.days = doc[\"days\"]\n", + " self.original = doc\n", + " self.original[\"id\"] = id\n", + "\n", + " def __str__(self):\n", + " return json.dumps(self.original)\n", "\n", "def get_workdays():\n", " random_array = [random.choice([False, True]) for _ in range(5)]\n", - " return [days for nr, days in enumerate(['Mon', 'Tue', 'Wed','Thu','Fri']) if random_array[nr]]\n", + " return [days for nr, days in enumerate(['Mon', 'Tue', 'Wed', 'Thu', 'Fri']) if random_array[nr]]\n", + "\n", "\n", "def get_preferences():\n", - " preference_topics = ['Lord of the Rings', 'Sports', 'Football', 'PC Gaming', 'Console Gaming', 'Sleeping', 'Hygiene', 'Cooking', 'Books', 'Anime/Manga']\n", - " score = [0,0,0,0,0,0,0,0,0,0,1,2,3,4,5]\n", - " return {preference:random.choice(score) for preference in preference_topics}\n", + " preference_topics = ['Sports', 'Eating', 'Board Games', 'Video Games', 'Coffee Enthusiasm', 'Tea Appreciation',\n", + " 'Nightlife and Parties', 'Nature and Greenery', 'Books', 'Anime and Manga']\n", + "\n", + " score = [0]\n", + " return {preference: random.choice(score) for preference in preference_topics}\n", + "\n", "\n", "def create_fake_users(amount):\n", - " fake = Faker(locale = \"en_GB\")\n", - " return [User(i, {'name':fake.name(),'days':get_workdays(),'preferences':get_preferences()}) for i in range(amount)]\n", + " fake = Faker(locale=\"en_GB\")\n", + " return [User(i, {'name': fake.name(), 'days': get_workdays(), 'preferences': get_preferences()}) for i in\n", + " range(amount)]\n", + "\n", "\n", "def get_funfact(preferences):\n", " max_key = max(preferences, key=preferences.get)\n", " return 'I really like {}!'.format(max_key)\n", "\n", - " \n", - " \n", + "def get_sorted_list_func(user_ids, user_preferences):\n", + " def get_sorted_list(user_index):\n", + " user_score_list = {user_ids[partner_id]:score for partner_id, score in enumerate(user_preferences[user_index]) if partner_id != user_index}\n", + " user_score_ordered = collections.OrderedDict(sorted(user_score_list.items(), key=lambda item: item[1], reverse=True))\n", + " return list(user_score_ordered)\n", + " return get_sorted_list\n", + "\n", + "def check_input(user_ids, preference_matrix):\n", + " user_limit = 10000\n", + " if len(user_ids)>user_limit or len(preference_matrix)>user_limit:\n", + " raise ValueError(f\"The user limit is {user_limit}\")\n", + " if len(user_ids) != len(preference_matrix):\n", + " raise ValueError(f\"user list and matrix length do not match\")\n", + "\n", + "def get_preference_lists(user_ids, preference_matrix):\n", + " check_input(user_ids, preference_matrix)\n", + "\n", + " user_preferences = cosine_similarity(preference_matrix)\n", + " get_sorted_list = get_sorted_list_func(user_ids, user_preferences)\n", + " return [get_sorted_list(user_index) for user_index in range(len(user_ids))]\n", + "\n", + "def convert_preferences_to_matrix(users_preferences):\n", + " # Get the list of all topics from the user preferences\n", + " all_topics = set().union(*users_preferences)\n", + "\n", + " # Create a NumPy matrix from the user preferences\n", + " pref_matrix = np.array([[user_pref.get(topic, 0) for topic in all_topics] for user_pref in users_preferences])\n", + "\n", + " return pref_matrix\n", + "\n", + "def create_matches_from_users(users: list[User]):\n", + " # now we need to build the matrix \n", + " names_to_player = {x.id: Player(x.id) for x in users}\n", + " id_to_user = {x.id: x for x in users}\n", + " # going to be unique for each user\n", + " names = [x.id for x in users]\n", + " pref_matrix = convert_preferences_to_matrix([x.preferences for x in users])\n", + "\n", + "\n", "\n", - "user = create_fake_users(1)\n", - "get_funfact(user[0].preferences)\n" + " #print(pref_matrix)\n", + " preference_order = get_preference_lists(names, pref_matrix)\n", + " #print(preference_order)\n", + "\n", + " for name,pref in zip(names,preference_order):\n", + " player = names_to_player[name]\n", + " player.set_prefs([names_to_player[x] for x in pref])\n", + "\n", + " players = [value for key,value in names_to_player.items()]\n", + "\n", + " matching = stable_roommates(players)\n", + " user_tuples = []\n", + " losers = []\n", + "\n", + " for match1, match2 in matching.items():\n", + " #print(match1.name)\n", + " if match2:\n", + " user_tuples.append((id_to_user[match1.name], id_to_user[match2.name]))\n", + " else:\n", + " losers.append(id_to_user[match1.name])\n", + "\n", + " if len(losers) >= 2:\n", + " user_tuples.extend(zip(losers[0::2], losers[1::2]))\n", + "\n", + " return user_tuples\n", + "\n", + "\n", + "users = create_fake_users(1000)\n", + "names_to_player = {x.id: Player(x.id) for x in users}\n", + "id_to_user = {x.id: x for x in users}\n" ] }, { "cell_type": "code", - "execution_count": 25, - "id": "37468ad06e3338a6", + "execution_count": 77, + "outputs": [], + "source": [ + "# going to be unique for each user\n", + "names = [x.id for x in users]\n", + "pref_matrix = convert_preferences_to_matrix([x.preferences for x in users])\n", + "preference_order = get_preference_lists(names, pref_matrix)\n" + ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-29T08:04:47.248009298Z", - "start_time": "2023-10-29T08:04:47.232445347Z" + "end_time": "2023-10-29T10:22:53.933799076Z", + "start_time": "2023-10-29T10:22:53.007911967Z" } }, + "id": "7e7cc451db51550c" + }, + { + "cell_type": "code", + "execution_count": 78, "outputs": [], "source": [ + "class Match:\n", + " def __init__(self, user1: User, user2: User, date: date = date.today()):\n", + " self.user1 = user1\n", + " self.user2 = user2\n", + " self.date = date\n", + "\n", + "\n", + "def match_users(users: list[User]):\n", + " return [Match(pair[0], pair[1]) for pair in create_matches_from_users(users)]\n", + "\n" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:22:53.986241145Z", + "start_time": "2023-10-29T10:22:53.944774066Z" + } + }, + "id": "ff47e0ae78bb6f08" + }, + { + "cell_type": "code", + "execution_count": 79, + "outputs": [], + "source": [ + "previous_matches = [(1,2),(1,3),(1,4),(2,5),(6,3),(5,8),(9,8),(9,3),(2,3),(12,4),(12,3),(12,2),(12,1)]\n", + "\n", + "def remove_previous_matches(preference_order, previous_matches):\n", + "\n", + " for match in previous_matches:\n", + " preference_order[match[0]].pop(preference_order[match[0]].index(match[1]))\n", + " preference_order[match[1]].pop(preference_order[match[1]].index(match[0]))\n", + "\n", + " return preference_order" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:22:54.001167319Z", + "start_time": "2023-10-29T10:22:53.961767785Z" + } + }, + "id": "ebb086ce689d0fc7" + }, + { + "cell_type": "code", + "execution_count": 79, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:22:54.004682044Z", + "start_time": "2023-10-29T10:22:53.984049416Z" + } + }, + "id": "1f27ef170f38dc02" + }, + { + "cell_type": "code", + "execution_count": 80, + "outputs": [], + "source": [ + "return_list = remove_previous_matches(preference_order, previous_matches)\n" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:22:54.415084080Z", + "start_time": "2023-10-29T10:22:54.394193531Z" + } + }, + "id": "24346cfead241aa9" + }, + { + "cell_type": "code", + "execution_count": 62, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:20:13.695231611Z", + "start_time": "2023-10-29T10:20:13.507044642Z" + } + }, + "id": "f6c02396f994b7a1" + }, + { + "cell_type": "code", + "execution_count": 62, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:20:13.695811979Z", + "start_time": "2023-10-29T10:20:13.549595326Z" + } + }, + "id": "4b43568fbe9967eb" + }, + { + "cell_type": "code", + "execution_count": 63, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "done\n" + ] + } + ], + "source": [ + "from matching.algorithms import stable_roommates\n", + "\n", + "matches = match_users(users)\n", + "matches2 = match_users(users)\n", + "\n", + "list_of_matches = []\n", + "\n", + "for i in range(len(matches)):\n", + " user1_same = matches[i].user1 == matches2[i].user1\n", + " user2_same = matches[i].user2 == matches2[i].user2\n", + " if user1_same and user2_same:\n", + " pass\n", + " else:\n", + " print('Something was different')\n", + " break\n", + "print('done')\n", + "\n", + "\n", + "\n" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:20:13.696853885Z", + "start_time": "2023-10-29T10:20:13.550990032Z" + } + }, + "id": "eb36b9313a18d88e" + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "37468ad06e3338a6", + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-10-29T10:20:13.763153068Z", + "start_time": "2023-10-29T10:20:13.559344290Z" + } + }, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'firebase_admin'", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mModuleNotFoundError\u001B[0m Traceback (most recent call last)", + "Cell \u001B[0;32mIn[64], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfunctions\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdata\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m User\n\u001B[1;32m 2\u001B[0m \u001B[38;5;66;03m# import random\u001B[39;00m\n\u001B[1;32m 3\u001B[0m \u001B[38;5;66;03m# from faker import Faker\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;66;03m# \u001B[39;00m\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 23\u001B[0m \u001B[38;5;66;03m# for u in fake_users:\u001B[39;00m\n\u001B[1;32m 24\u001B[0m \u001B[38;5;66;03m# print(str(u))\u001B[39;00m\n", + "File \u001B[0;32m~/DataspellProjects/randoms/functions/data.py:6\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mrandom\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfaker\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Faker\n\u001B[0;32m----> 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfirebase_admin\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m firestore\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mfirebase_admin\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m auth\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mUser\u001B[39;00m:\n", + "\u001B[0;31mModuleNotFoundError\u001B[0m: No module named 'firebase_admin'" + ] + } + ], + "source": [ + "\n", "from functions.data import User\n", "# import random\n", "# from faker import Faker\n", @@ -597,14 +845,13 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "834dfbfe0b60cb0b", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.789507080Z", - "start_time": "2023-10-28T18:28:13.665400467Z" - }, - "collapsed": false + "start_time": "2023-10-29T10:20:13.636155515Z" + } }, "outputs": [], "source": [ @@ -613,33 +860,15 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "4180d228b79f25f3", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.791683938Z", - "start_time": "2023-10-28T18:28:13.707846880Z" - }, - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{\"name\": \"Ms Lisa Griffiths\", \"days\": [\"Wen\", \"Fri\"], \"preferences\": {\"Lord of the Rings\": 3, \"Hackatons\": 0, \"Console Gaming\": 0, \"PC Gaming\": 1, \"Humans\": 2, \"Bouldering\": 0, \"Boxing\": 2, \"Football\": 0}, \"id\": 0}\n", - "{\"name\": \"Mr Tony Alexander\", \"days\": [\"Tue\", \"Thu\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 1, \"PC Gaming\": 0, \"Humans\": 3, \"Bouldering\": 3, \"Boxing\": 0, \"Football\": 0}, \"id\": 1}\n", - "{\"name\": \"Natalie Davies\", \"days\": [\"Mon\", \"Tue\", \"Fri\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 0, \"PC Gaming\": 1, \"Humans\": 2, \"Bouldering\": 0, \"Boxing\": 3, \"Football\": 4}, \"id\": 2}\n", - "{\"name\": \"Janice O'Connor\", \"days\": [\"Tue\", \"Wen\", \"Thu\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 2, \"Console Gaming\": 0, \"PC Gaming\": 5, \"Humans\": 0, \"Bouldering\": 1, \"Boxing\": 0, \"Football\": 0}, \"id\": 3}\n", - "{\"name\": \"Carl Richards\", \"days\": [\"Thu\", \"Fri\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 4, \"PC Gaming\": 0, \"Humans\": 0, \"Bouldering\": 0, \"Boxing\": 0, \"Football\": 1}, \"id\": 4}\n", - "{\"name\": \"Megan Moss\", \"days\": [\"Mon\", \"Tue\", \"Thu\", \"Fri\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 1, \"Console Gaming\": 0, \"PC Gaming\": 0, \"Humans\": 0, \"Bouldering\": 3, \"Boxing\": 0, \"Football\": 3}, \"id\": 5}\n", - "{\"name\": \"Dr Dennis Hughes\", \"days\": [\"Mon\", \"Tue\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 0, \"PC Gaming\": 1, \"Humans\": 0, \"Bouldering\": 0, \"Boxing\": 5, \"Football\": 5}, \"id\": 6}\n", - "{\"name\": \"Christopher Myers\", \"days\": [], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 0, \"PC Gaming\": 0, \"Humans\": 0, \"Bouldering\": 5, \"Boxing\": 0, \"Football\": 0}, \"id\": 7}\n", - "{\"name\": \"Russell Mills\", \"days\": [\"Mon\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 0, \"Console Gaming\": 1, \"PC Gaming\": 4, \"Humans\": 5, \"Bouldering\": 0, \"Boxing\": 3, \"Football\": 5}, \"id\": 8}\n", - "{\"name\": \"Joan Wilkinson\", \"days\": [\"Mon\", \"Tue\", \"Thu\", \"Fri\"], \"preferences\": {\"Lord of the Rings\": 0, \"Hackatons\": 5, \"Console Gaming\": 3, \"PC Gaming\": 5, \"Humans\": 0, \"Bouldering\": 4, \"Boxing\": 0, \"Football\": 0}, \"id\": 9}\n" - ] + "start_time": "2023-10-29T10:20:13.677452102Z" } - ], + }, + "outputs": [], "source": [ "fake_users = create_fake_users(10)\n", "\n", @@ -649,14 +878,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "96ddc60b5e5f3caa", "metadata": { + "collapsed": false, "ExecuteTime": { - "end_time": "2023-10-28T18:28:13.792009008Z", - "start_time": "2023-10-28T18:28:13.755810489Z" - }, - "collapsed": false + "start_time": "2023-10-29T10:20:13.678111526Z" + } }, "outputs": [], "source": []