Background: I am trying to allocate students to teams where each student will have a series of preferences to other students being in their teams. I have an objective function which I want to minimise along with 3 constraints for the function (written in the image below). In my DB I have a set of students along with their preferences (such as student i rating student j as their 3rd choice).
If student A rates student B as their 1st choice, that preference will have a weighting of 1 which is why the objective function is set to minimise.
Mathematical Formula:
Question: I am unsure whether I have written the constraints and variables correctly in PuLP, and I can't find any close resources that do team allocation with preferences. I'm very new to PuLP and am struggling to figure out if what I've written is correct syntatically, thanks for any help!
Here is the code that I have written in my file:
from pulp import *
model = LpProblem("Team Allocation Problem", LpMinimize)
############ VARIABLES ############
students = [1,...,20]
n = 20
# this will be imported from the database
r = [[...],...,[...]]
team_sizes = [5,5,5,5]
num_teams = len(z)
# x(ik) = 1 if student i in team k
x_vars = [[LpVariable("x%d%d" % (i,k), cat='Binary')
for k in range(num_teams)]
for i in range(num_students)]
# y(ijk) = 1 if student i and j are in team k
y_vars = [[[LpVariable("y%d%d%d" % (i,j,k), cat='Binary')
for k in range(num_teams)]
for j in range(num_students)]
for i in range(num_students)]
############ OBJECTIVE FUNCTION ############
for i in range(num_students):
for j in range(num_students):
if i!=j:
for k in range(num_teams):
model += r[i][j] * y_vars[i][j][k], "Minimize the sum of rank points in the team"
############ CONSTRAINTS ############
# C1: Every student is on exactly one team
for i in range(num_students):
for k in range(num_teams):
model += lpSum(x_vars[i][k]) == 1
# C2: Every team has the right size
for k in range(num_teams):
for i in range(num_students):
model += lpSum(x_vars[i][k]) == team_sizes[k]
# C3:
for i in range(num_students):
for j in range(num_students):
if i != j:
for k in range(num_teams):
model += 1 + y_vars[i][j][k] >= x_vars[i][k] + x_vars[j][k]
# Solve and Print
model.solve()
print("Status:", model.status)