Commit 544eea6a authored by Rajiv Vaidyanathan's avatar Rajiv Vaidyanathan

SPA algorithm implementation

parent 304031f4
from functools import reduce
# Part 1 (get these from the webapp)
# get student list
# get student project priorities and let them apply to their top priority
S = {"s1":["p1", "p3", "p2", "p4"], "s2":["p1", "p3", "p2", "p4"]}
# S = {"s1":["p1", "p2", "p3", "p4"], "s2":["p1", "p4", "p3", "p2"], "s3":["p3", "p1", "p2", "p4"], "s4":["p3", "p2", "p1", "p4"]}
# get project list from profs, per project capactiy and total capacity
L = {"l1":["p1", "p2"], "l2":["p3", "p4"]}
# L = {"l1":["p1", "p2"], "l2":["p3", "p4"]}
P2L = {"p1":"l1", "p2":"l1", "p3":"l2", "p4":"l2"}
# get priorities from profs about students
Lpref = {"l1": ["s1", "s2"], "l2": ["s2", "s1"]}
# Lpref = {"l1": ["s3", "s4", "s1", "s2"], "l2": ["s1", "s2", "s3", "s4"]}
# get max intake
Pc = {"p1":1, "p2":1, "p3":1, "p4":1}
# Pc = {"p1":2, "p2":1, "p3":2, "p4":1}
# matching structure
Ms = {"s1": [], "s2": []}
# Ms = {"s1": [], "s2": [], "s3": [], "s4": []}
Mp = {"p1": [], "p2": [], "p3": [], "p4": []}
# print("MS values: ", Ms.values())
# isStable = reduce(lambda x, y: (x and (len(y)>0), Ms.values(), True)
isStable = True
for i in Ms.values():
if len(i)==0:
isStable = False
break
# print(isStable)
# Part 2 (compute and return to the webapp)
# Run algo
while not isStable:
for stud in Ms.keys():
if len(Ms[stud])==0:
# provisionally asigning
# print("Current student: ", stud)
proj = S[stud][0]
lec = P2L[proj]
Ms[stud].append(proj)
Mp[proj].append(stud)
# check if project is oversubscibed
while len(Mp[proj]) > Pc[proj]:
# find the least priority students assigned to the project
prefList = Lpref[lec] # pref order of lec
idx = len(prefList)-1
found = False
while not found:
if prefList[idx] in Mp[proj]:
Mp[proj].remove(prefList[idx])
Ms[prefList[idx]].remove(proj)
S[prefList[idx]].remove(proj)
prefList.remove(prefList[idx])
# print("Removing project: ", proj)
# print("from student: ", prefList[idx])
found = True
else:
idx -= 1
# isStable = reduce(lambda x, y: (x and (len(y)>0), Ms.values(), True)
isStable = True
for i in Ms.values():
if len(i)==0:
isStable = False
break
print(Ms)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment