restart step6
This commit is contained in:
1 parent
b0832d70bd
commit
29d0456236
1 file changed
+3
-190
@@ -350,199 +350,12 @@ def generateScheduleV3(
|
||||
|
||||
for student in students:
|
||||
blocks = [student["schedule"][block] for block in student["schedule"]]
|
||||
origin = list(block)
|
||||
exceptions = []
|
||||
count, hasConflict = 0, True
|
||||
count, hasConflict = sum(1 for b in blocks if len(b)==1), True
|
||||
|
||||
initialCount = sum(1 for b in blocks if len(b)==1)
|
||||
if initialCount == student["classes"]: # If student has no issues and has been inserted into classes
|
||||
if initialCount < student["expectedClasses"]: # But student is missing some classes
|
||||
# Attempt to fix
|
||||
requests = [request for request in student["requests"] if not request["alt"] and request["CrsNo"] not in flex]
|
||||
activeClasses = []
|
||||
for block in blocks:
|
||||
for cname in block: activeClasses.append(cname[:-2])
|
||||
|
||||
remainingRequests = [request for request in requests if request["CrsNo"] not in activeClasses]
|
||||
freeBlocks = [blockIndex for blockIndex in range(len(blocks)) if len(blocks[blockIndex]) == 0]
|
||||
if len(remainingRequests) > 0:
|
||||
for reRequest in remainingRequests:
|
||||
usedBlock = False
|
||||
for freeIndex in freeBlocks:
|
||||
if usedBlock: break
|
||||
freeblock = f"block{freeIndex+1}"
|
||||
for cname in running[freeblock]:
|
||||
if cname[:-2] == reRequest["CrsNo"] and len(running[freeblock][cname]["students"]) < 30:
|
||||
running[freeblock][cname]["students"].append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"index": student["studentIndex"]
|
||||
})
|
||||
# Update current blocks to work with
|
||||
blocks[freeIndex].append(cname)
|
||||
|
||||
student["schedule"][freeblock].append(cname)
|
||||
activeClasses.append(cname)
|
||||
freeBlocks.remove(freeIndex)
|
||||
student["classes"] += 1
|
||||
usedBlock = True
|
||||
break
|
||||
|
||||
elif len(student["remainingAlts"]) > 0:
|
||||
for reRequest in student["remainingAlts"]:
|
||||
usedBlock = False
|
||||
for freeIndex in freeBlocks:
|
||||
if usedBlock: break
|
||||
freeblock = f"block{freeIndex+1}"
|
||||
for cname in running[freeblock]:
|
||||
if cname[:-2] == reRequest["CrsNo"] and len(running[freeblock][cname]["students"]) < 30:
|
||||
running[freeblock][cname]["students"].append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"index": student["studentIndex"]
|
||||
})
|
||||
student["schedule"][freeblock].append(cname)
|
||||
activeClasses.append(cname)
|
||||
freeBlocks.remove(freeIndex)
|
||||
student["classes"] += 1
|
||||
usedBlock = True
|
||||
break
|
||||
else:
|
||||
hasConflict = False
|
||||
conflicts.append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"Email": "",
|
||||
"Conflict": "Short of expected classes"
|
||||
})
|
||||
|
||||
while hasConflict:
|
||||
# Get clash
|
||||
if len(exceptions) > 0:
|
||||
for i in range(len(exceptions)):
|
||||
blocks[exceptions[i]] = ['nil']
|
||||
|
||||
count = sum(1 for b in blocks if len(b)==1)
|
||||
c = sum(1 for b in blocks if len(b)>1)
|
||||
if c == 0:
|
||||
if count < student["expectedClasses"] and count < student["classes"]:
|
||||
conflicts.append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"Email": "",
|
||||
"Conflict": "Missing classes"
|
||||
})
|
||||
hasConflict = False
|
||||
student["classes"] = c
|
||||
break
|
||||
|
||||
if count < student["expectedClasses"]:
|
||||
if count == student["classes"]:
|
||||
conflicts.append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"Email": "",
|
||||
"Conflict": "Less classes than expected"
|
||||
})
|
||||
hasConflict = False
|
||||
break
|
||||
|
||||
blockLens = [len(block) for block in blocks]
|
||||
index = blockLens.index(max(blockLens))
|
||||
|
||||
blockOut = f"block{index+1}"
|
||||
done = False
|
||||
moveIndex = 0
|
||||
freeBlocks = [blockIndex for blockIndex in range(len(blocks)) if len(blocks[blockIndex]) == 0]
|
||||
while not done:
|
||||
classOut = blocks[index][moveIndex]
|
||||
found = False
|
||||
for blockIndex in freeBlocks:
|
||||
if found:
|
||||
break
|
||||
if blockIndex != index:
|
||||
block = list(running)[blockIndex]
|
||||
for cname in running[block]:
|
||||
if cname[:-2] == classOut[:-2] and len(running[block][cname]["students"]) < classCap:
|
||||
studentData = {
|
||||
"Pupil #": student["Pupil #"],
|
||||
"index": student["studentIndex"]
|
||||
}
|
||||
|
||||
# Update current blocks to work with
|
||||
blocks[index].remove(classOut)
|
||||
blocks[blockIndex].append(cname)
|
||||
|
||||
# Update Final Records
|
||||
running[blockOut][classOut]["students"].remove(studentData) # Remove final
|
||||
running[block][cname]["students"].append(studentData)
|
||||
|
||||
found, done = True, True
|
||||
break
|
||||
|
||||
if not found:
|
||||
if moveIndex < len(blocks[index])-1:
|
||||
moveIndex += 1
|
||||
elif moveIndex == len(blocks[index])-1:
|
||||
classIndexSearch = 0
|
||||
existsIn = []
|
||||
for blockIndex in range(len(running)):
|
||||
for cname in running[list(running)[blockIndex]]:
|
||||
if cname[:-2] == blocks[index][classIndexSearch][:-2]:
|
||||
existsIn.append(blockIndex)
|
||||
|
||||
for blockIndex in existsIn:
|
||||
if len(blocks[blockIndex]) == 1:
|
||||
pass
|
||||
elif len(blocks[blockIndex]) == 0:
|
||||
for cname in running[list(running)[blockIndex]]:
|
||||
if cname[:-2] == blocks[blockIndex][classIndexSearch][:-2]:
|
||||
if len(running[list(running)[blockIndex]][cname]["students"]) < 30:
|
||||
studentData = {
|
||||
"Pupil #": student["Pupil #"],
|
||||
"index": student["studentIndex"]
|
||||
}
|
||||
# Update current blocks to work with
|
||||
blocks[index].remove(blocks[blockIndex][classIndexSearch])
|
||||
blocks[blockIndex].append(cname)
|
||||
|
||||
# Update Final Records
|
||||
running[f"block{index+1}"][blocks[blockIndex][classIndexSearch]]["students"].remove(studentData) # Remove final
|
||||
running[list(running)[blockIndex]][cname]["students"].append(studentData)
|
||||
break
|
||||
break
|
||||
# If there is no conflicts: continue
|
||||
if student["classes"] == student["expectedClasses"] and count == student["classes"]: continue
|
||||
|
||||
|
||||
if index not in exceptions: exceptions.append(index)
|
||||
done = True
|
||||
|
||||
else:
|
||||
print('impossible err 3')
|
||||
|
||||
elif found: done = True
|
||||
|
||||
else:
|
||||
print('impossible err 2')
|
||||
|
||||
elif count == student["expectedClasses"]:
|
||||
if len(exceptions) > 0:
|
||||
for i in range(len(exceptions)):
|
||||
blocks[i] = origin[i]
|
||||
|
||||
conflicts.append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"Email": "",
|
||||
"Conflict": "More than one class per block"
|
||||
})
|
||||
|
||||
hasConflict = False
|
||||
elif count > student["expectedClasses"]:
|
||||
conflicts.append({
|
||||
"Pupil #": student["Pupil #"],
|
||||
"Email": "",
|
||||
"Conflict": "More classes than expected"
|
||||
})
|
||||
hasConflict = False
|
||||
else:
|
||||
print('impossible err 1')
|
||||
|
||||
# Update Student Schedule
|
||||
student["schedule"] = blocks
|
||||
|
||||
# Update Student records
|
||||
with open(studentsDir, "w") as outfile:
|
||||
|
||||
Reference in new issue
Block a user