improve step 6
This commit is contained in:
1 file changed
+55
-136
+55
-136
@@ -271,6 +271,10 @@ def generateScheduleV3(
|
|||||||
# Return Error if code is altered to cause error
|
# Return Error if code is altered to cause error
|
||||||
else: raise SystemExit(f"Invalid 'stepType' in func 'stepIndex' line {getLineNumber()}")
|
else: raise SystemExit(f"Invalid 'stepType' in func 'stepIndex' line {getLineNumber()}")
|
||||||
|
|
||||||
|
# Create copies for step 6
|
||||||
|
allClassRunCountsCopy = list(allClassRunCounts)
|
||||||
|
courseRunInfoCopy = dict(courseRunInfo)
|
||||||
|
|
||||||
while len(allClassRunCounts) > 0:
|
while len(allClassRunCounts) > 0:
|
||||||
# Get highest resource class (most times run)
|
# Get highest resource class (most times run)
|
||||||
index = allClassRunCounts.index(min(allClassRunCounts))
|
index = allClassRunCounts.index(min(allClassRunCounts))
|
||||||
@@ -319,6 +323,7 @@ def generateScheduleV3(
|
|||||||
elif allClassRunCounts[index] == 1:
|
elif allClassRunCounts[index] == 1:
|
||||||
# Equally disperse into semesters classes
|
# Equally disperse into semesters classes
|
||||||
semBlocks = []
|
semBlocks = []
|
||||||
|
offset = 1
|
||||||
|
|
||||||
if sem1 <= sem2:
|
if sem1 <= sem2:
|
||||||
for block in sem1List:
|
for block in sem1List:
|
||||||
@@ -334,7 +339,7 @@ def generateScheduleV3(
|
|||||||
leastBlock = semBlocks.index(min(semBlocks))
|
leastBlock = semBlocks.index(min(semBlocks))
|
||||||
cname = f"{course}-0"
|
cname = f"{course}-0"
|
||||||
|
|
||||||
running[f"block{leastBlock+1}"][cname] = {
|
running[f"block{leastBlock+offset}"][cname] = {
|
||||||
"CrsNo": course,
|
"CrsNo": course,
|
||||||
"Description": emptyClasses[course][cname]["Description"],
|
"Description": emptyClasses[course][cname]["Description"],
|
||||||
"students": selectedCourses[cname]["students"],
|
"students": selectedCourses[cname]["students"],
|
||||||
@@ -363,9 +368,8 @@ def generateScheduleV3(
|
|||||||
studentsCritical, studentsAcceptable = 0, 0
|
studentsCritical, studentsAcceptable = 0, 0
|
||||||
|
|
||||||
for student in students:
|
for student in students:
|
||||||
initialBlocks = [student["schedule"][block] for block in student["schedule"]]
|
blocks = [student["schedule"][block] for block in student["schedule"]]
|
||||||
conflicts = sum(1 for b in initialBlocks if len(b)>1)
|
hasConflicts = True if sum(1 for b in blocks if len(b)>1) > 0 else False
|
||||||
hasConflicts = True if conflicts > 0 else False
|
|
||||||
|
|
||||||
# If there is no conflicts
|
# If there is no conflicts
|
||||||
# and classes inserted to is equal to expectedClasses
|
# and classes inserted to is equal to expectedClasses
|
||||||
@@ -384,146 +388,61 @@ def generateScheduleV3(
|
|||||||
"index": student["studentIndex"]
|
"index": student["studentIndex"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# If we are unable to solve, we add to exceptions
|
if hasConflicts:
|
||||||
# and keep trying to resolve the rest of the schedule
|
for block in student["schedule"]:
|
||||||
exceptions = []
|
[running[block][cname]["students"].remove(studentData) for cname in student["schedule"][block]]
|
||||||
|
student["schedule"][block] = []
|
||||||
|
|
||||||
while hasConflicts:
|
# Find what class in student schedule has least run time
|
||||||
# Check if conflicts have been resolved
|
classes = []
|
||||||
blocks = [student["schedule"][block] for block in student["schedule"]]
|
runCounts = []
|
||||||
# If there is exceptions, make them look normal in blocks
|
for block in blocks:
|
||||||
# list to ignore them when looking for clashes, and not to
|
for cname in block:
|
||||||
# accidently overwrite while evaluating other classes
|
classes.append(cname[:-2])
|
||||||
if len(exceptions) > 0:
|
runCounts.append(courseRunInfoCopy[cname[:-2]]["Total"])
|
||||||
for i in range(len(blocks)):
|
|
||||||
if i in exceptions: blocks[i] = ['EXPT']
|
|
||||||
conflicts = sum(1 for b in blocks if len(b)>1)
|
|
||||||
|
|
||||||
if conflicts == 0: break
|
availableBlocks = [f'block{i}' for i in range(1, 11)]
|
||||||
|
while len(classes) > 0:
|
||||||
|
index = runCounts.index(min(runCounts)) # Get class least run
|
||||||
|
|
||||||
elif conflicts > 0:
|
found = False
|
||||||
|
|
||||||
blockLens = [len(block) for block in blocks]
|
# Rebuild student schedule
|
||||||
freeBlocks = [index for index in range(len(blocks)) if len(blocks[index]) == 0]
|
for block in availableBlocks:
|
||||||
|
if found: break
|
||||||
|
for cname in running[block]:
|
||||||
|
if cname[:-2] == classes[index] and len(running[block][cname]["students"]) < classCap:
|
||||||
|
running[block][cname]["students"].append(studentData)
|
||||||
|
student["schedule"][block].append(cname)
|
||||||
|
availableBlocks.remove(block)
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
clashIndex = blockLens.index(max(blockLens))
|
# Remove class after inserted or failed to insert
|
||||||
blockOut = f"block{clashIndex+1}"
|
classes.remove(classes[index])
|
||||||
classIndex = 0
|
runCounts.remove(runCounts[index])
|
||||||
done, advancedConflict = False, False
|
|
||||||
|
|
||||||
while not done:
|
if not found:
|
||||||
classOut = blocks[clashIndex][classIndex]
|
# Try alternate
|
||||||
found = False
|
alternates = [alt["CrsNo"] for alt in students[student["studentIndex"]]["remainingAlts"] if alt["CrsNo"] not in flex and alt["CrsNo"] in courseRunInfoCopy]
|
||||||
for index in freeBlocks:
|
if len(alternates) == 0: # If not alternates, create critical error
|
||||||
if found: break
|
c_cr_count += 1
|
||||||
if index != clashIndex:
|
criticalCount += 1
|
||||||
blockIn = list(running)[index]
|
if not newConflict(student["Pupil #"], "", "Critical", "C-CR", "Couldn't Resolve", conflictLogs): studentsCritical += 1
|
||||||
for cname in running[blockIn]:
|
|
||||||
if cname[:-2] == classOut[:-2] and len(running[blockIn][cname]["students"]) < classCap:
|
|
||||||
|
|
||||||
# Update records
|
else:
|
||||||
student["schedule"][blockOut].remove(classOut)
|
# Get alternate least run
|
||||||
student["schedule"][blockIn].append(cname)
|
altRunCounts = [courseRunInfoCopy[alt]["Total"] for alt in alternates]
|
||||||
|
altIndex = altRunCounts.index(min(altRunCounts))
|
||||||
|
|
||||||
running[blockOut][classOut]["students"].remove(studentData)
|
# add alternate
|
||||||
running[blockIn][cname]["students"].append(studentData)
|
classes.append(alternates[altIndex])
|
||||||
|
runCounts.append(altRunCounts[altIndex])
|
||||||
|
|
||||||
found = True
|
# Remove alternate from remaining alternates
|
||||||
break
|
for remaining in students[student["studentIndex"]]["remainingAlts"]:
|
||||||
|
if remaining["CrsNo"] == alternates[altIndex]:
|
||||||
if not found:
|
students[student["studentIndex"]]["remainingAlts"].remove(remaining)
|
||||||
if classIndex < len(blocks[clashIndex])-1: classIndex += 1
|
|
||||||
elif classIndex == len(blocks[clashIndex])-1:
|
|
||||||
classIndex = 0
|
|
||||||
advancedConflict = True
|
|
||||||
done = True
|
|
||||||
else:
|
|
||||||
print(f"Fatal error ({getLineNumber()}): Impossible error")
|
|
||||||
continue
|
|
||||||
|
|
||||||
elif found: done = True
|
|
||||||
|
|
||||||
if advancedConflict:
|
|
||||||
attemptResolve = True
|
|
||||||
classOutIndex = 0
|
|
||||||
while attemptResolve:
|
|
||||||
foundSolution = False
|
|
||||||
newClassOut = blocks[clashIndex][classOutIndex]
|
|
||||||
for blockIndex in range(len(running)):
|
|
||||||
if foundSolution: break
|
|
||||||
if blockIndex != clashIndex and blockIndex not in exceptions:
|
|
||||||
for cname in running[list(running)[blockIndex]]:
|
|
||||||
if cname[:-2] == newClassOut[:-2]:
|
|
||||||
blockIn = f"block{blockIndex+1}"
|
|
||||||
if blockIndex in freeBlocks:
|
|
||||||
if len(running[blockIn][cname]["students"]) < classCap:
|
|
||||||
# In the rare or impossible case a student was not inserted to a class
|
|
||||||
# And it weren't full, insert them into the class
|
|
||||||
student["schedule"][blockOut].remove(newClassOut)
|
|
||||||
student["schedule"][blockIn].append(cname)
|
|
||||||
|
|
||||||
running[blockOut][newClassOut]["students"].remove(studentData)
|
|
||||||
running[blockIn][cname]["students"].append(studentData)
|
|
||||||
foundSolution = True
|
|
||||||
break
|
|
||||||
|
|
||||||
elif len(blocks[blockIndex]) == 1:
|
|
||||||
oClassOut = blocks[blockIndex][0]
|
|
||||||
found_oBlockSolution = False
|
|
||||||
for oBlockIndex in range(len(running)):
|
|
||||||
if found_oBlockSolution: break
|
|
||||||
if oBlockIndex != clashIndex and oBlockIndex not in exceptions and oBlockIndex in freeBlocks:
|
|
||||||
for ocname in running[list(running)[oBlockIndex]]:
|
|
||||||
if ocname[:-2] == oClassOut[:-2]:
|
|
||||||
oBlockIn = f"block{oBlockIndex+1}"
|
|
||||||
if len(running[oBlockIn][ocname]["students"]) < classCap:
|
|
||||||
student["schedule"][blockOut].remove(newClassOut)
|
|
||||||
student["schedule"][blockIn].append(cname)
|
|
||||||
student["schedule"][blockIn].remove(oClassOut)
|
|
||||||
student["schedule"][oBlockIn].append(ocname)
|
|
||||||
|
|
||||||
running[blockOut][newClassOut]["students"].remove(studentData)
|
|
||||||
running[blockIn][cname]["students"].append(studentData)
|
|
||||||
running[blockIn][oClassOut]["students"].remove(studentData)
|
|
||||||
running[oBlockIn][ocname]["students"].append(studentData)
|
|
||||||
|
|
||||||
found_oBlockSolution = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not found_oBlockSolution:
|
|
||||||
exceptions.append(clashIndex)
|
|
||||||
criticalCount += 1
|
|
||||||
c_cr_count += 1
|
|
||||||
if not newConflict(student["Pupil #"], "", "Critial", "C-CR", "Couldn't Resolve", conflictLogs): studentsCritical += 1
|
|
||||||
|
|
||||||
foundSolution = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not foundSolution:
|
|
||||||
if classOutIndex < len(blocks[clashIndex]) - 1: classOutIndex += 1
|
|
||||||
elif classOutIndex == len(blocks[clashIndex]) - 1:
|
|
||||||
if len(student["remainingAlts"]) > 0:
|
|
||||||
exceptions.append(clashIndex)
|
|
||||||
criticalCount += 1
|
|
||||||
c_cr_count += 1
|
|
||||||
if not newConflict(student["Pupil #"], "", "Critical", "C-CR", "Couldn't Resolve", conflictLogs): studentsCritical += 1
|
|
||||||
|
|
||||||
attemptResolve = False
|
|
||||||
# Attempt to use alt
|
|
||||||
elif len(student["remainingAlts"]) == 0:
|
|
||||||
exceptions.append(clashIndex)
|
|
||||||
criticalCount += 1
|
|
||||||
c_cr_count += 1
|
|
||||||
if not newConflict(student["Pupil #"], "", "Critical", "C-CR", "Couldn't Resolve", conflictLogs): studentsCritical += 1
|
|
||||||
|
|
||||||
attemptResolve = False
|
|
||||||
else: print("Impossible - Thanos")
|
|
||||||
elif foundSolution:
|
|
||||||
attemptResolve = False
|
|
||||||
|
|
||||||
else:
|
|
||||||
print(f"Fatal error ({getLineNumber()}): Impossible error")
|
|
||||||
continue
|
|
||||||
|
|
||||||
metSelfRequirements = True if student["classes"] == student["expectedClasses"] else False
|
metSelfRequirements = True if student["classes"] == student["expectedClasses"] else False
|
||||||
while not metSelfRequirements:
|
while not metSelfRequirements:
|
||||||
@@ -581,7 +500,7 @@ def generateScheduleV3(
|
|||||||
for student in students:
|
for student in students:
|
||||||
for block in student["schedule"]:
|
for block in student["schedule"]:
|
||||||
if len(student["schedule"][block]) == 0:
|
if len(student["schedule"][block]) == 0:
|
||||||
student["schedule"][block].append(flex[0]) if int(block[len(block)-1]) <= 5 else student["schedule"][block].append(flex[1])
|
student["schedule"][block].append(flex[0]) if int(block[5:]) <= 5 else student["schedule"][block].append(flex[1])
|
||||||
|
|
||||||
# Update Student records
|
# Update Student records
|
||||||
with open(studentsDir, "w") as outfile:
|
with open(studentsDir, "w") as outfile:
|
||||||
|
|||||||
Reference in new issue
Block a user