How to Find a Best Match with Python | by Khuyen Tran | Towards Data Science

Congratulations! You are a new team leader of 8 talented employees. You want to split them into 4 pairs to work on 4 different projects. As an experienced leader, you know employees are the most productive when working with somebody they like. So you come up with a rating system for paring employees. The scales run from 1 to 10, with a 10 rating meaning the employee really likes the other employee and a 1 rating meaning the employer does not want to work with the other employee at all. After conducting the survey, you get this result:
Since the employees cannot rate themselves, the diagonal elements are zeros. The good news is that you know how much each employer like the others. The bad news is that you don’t know how to match them so that everybody is happy with their teammate! As data enthusiastic, you know that there must be some way you could solve this with data. And you are right.
This problem could easily be solved with mathematical programming. While machine learning is great in learning from data and making predictions, it has a limitation in making the optimal decision. When your problems involve multiple constraints and allow for many possible solutions, only mathematical programming has the power to find the optimal solution.
Let’s find out how to match your employees with mathematical programming!
If you are not familiar with PuLP, it is a python library that enables you to solve optimization problems. You could find the documentary about this library here. I will use this handy package to formulate and solve our problem.
!pip install pulpfrom pulp import *prob = LpProblem("Matching Employees", LpMaximize)
In LpProblem method, the first parameter is the name of the problem while the second parameter is your objective (maximize or minimize)
There are 8 employees that you want to match. The NumPy array is a preference matrix of 8 employees shown in the introduction.
Use LpVariables to create variable y, a decision variable to determine whether or not to match employee i with employee j.
y = LpVariable.dicts("pair", [(i,j) for i in employees for j in employees] ,cat='Binary')
Maximize the preference scores between employee i and j
prob += lpSum([(c[i][j] + c[j][i]) * y[(i,j)] for i in employees for j in employees])
for i in employees:    prob += lpSum(y[(i,j)] for j in employees) <= 1    prob += lpSum(y[(j,i)] for j in employees) <= 1    prob += lpSum(y[(i,j)] + y[(j,i)] for j in employees) <= 1prob += lpSum(y[(i,j)] for i in employees for j in employees) == 4
After defining the parameters, variables, objective function, and constraints, we are ready to solve the problem with one line of code!
>>> prob.solve()1
Print the solutions
print("Finish matching!\n")for i in employees:    for j in employees:        if y[(i,j)].varValue == 1:            print('{} and {} with preference score {} and {}. Total score: {}'.format(names[i],names[j],c[i,j], c[j,i], c[i,j] +c[j,i]))
Before using this result to match your employees, let’s take a careful look at the results to see if they are reasonable. Remember, the objective is to maximize the preference of both sides in a pair, not just one individual.
Kate has Francisco as her top choice and Francisco also has Kate as her top choice. So they are definitely a good pair.
Alfredo has Ben as his top choice. Even though Ben does not like Alfredo as much as Francisco, Ben still likes Alfredo (with a preference score of 7).
Jorge does not seem to like Chris, but Chris extremely likes Jorge. So maybe Chris will make Jorge like him more as they work together?
The pairing between Olivia and Thinh seems not to be desirable for Olivia. She does not like Thinh at all! But Thinh really likes Olivia so maybe he will be enthusiastic to help Olivia; thus, it will be beneficial for the whole team?
There are still some limitations to the result: Some people do not have their teammate as their top choice or even hate their teammate. But overall, the algorithm does a good job of splitting them into pairs so that the matching scores of the whole group are maximized. And better yet, you don’t need to deal with the headache of making an optimal decision because the algorithm has done that for you!
The notebook for this article can be found in my Github repository. If you are wondering how to group your employees, or friends into pairs, I encourage you to create a small survey of preference scores and try out this algorithm. Maybe maximizing the preference would create a productive working environment?
Feel free to fork and play with the code for this article in this Github repo.
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: