update work schedule generator

This commit is contained in:
SowinskiBraeden committed 2022-02-20 22:50:13 -08:00
1 parent 71388084e2
commit c23448a7d6
1 file changed
+31 -19
+31 -19
View File
@@ -3,6 +3,7 @@ import random
import names
import math
import json
from prettytable import PrettyTable
from courses import courses, activeCourses
'''
@@ -51,7 +52,7 @@ running: {
}
'''
minReq, classCap, blockClassLimit = 18, 30, 15
minReq, classCap, blockClassLimit = 18, 30, 24
mockStudents = []
running = {
"semester1": {
@@ -108,44 +109,52 @@ def generateSchedule():
# Add course to active list if enough requests
if courses[request]["totalrequests"] > minReq and courses[request]["code"] not in activeCourses: activeCourses[courses[request]["code"]] = courses[request]
# This is just for data testing/visualizations
# calculate # of times to run class
t = PrettyTable(["Class Name", "Class Runcount"])
for i in range(len(activeCourses)):
classRunCount = math.floor(activeCourses[list(activeCourses)[i]]["totalrequests"] / classCap)
# If there is 18+ requests left, 1 more class could be run
if (activeCourses[list(activeCourses)[i]]["totalrequests"] % classCap) > minReq: classRunCount += 1
activeCourses[list(activeCourses)[i]]["classRunCount"] = classRunCount
t.add_row([activeCourses[list(activeCourses)[i]]["code"], classRunCount])
print(t)
for student in mockStudents:
alternateOffset = len(student["requests"])-8
for i in range(len(student["requests"])-alternateOffset): # Subtract x classes as they are alternatives
currentCourse = student["requests"][i]
while True:
generate = True
while generate:
# If class is allowed to run
if currentCourse in activeCourses:
blockIndex = 1
semesterIndex = 1
while True:
getFreeBlock = True
while getFreeBlock:
block = f"block{blockIndex}"
semester = f"semester{semesterIndex}"
if student["schedule"][semester][block] != "":
if semesterIndex == 2 and blockIndex == 4:
# No available classes
print("No more available classes")
break
elif blockIndex == 4:
blockIndex = 1
semesterIndex += 1
else: blockIndex += 1
if currentCourse in running[semester][block]:
# Add student to class
if student["schedule"][semester][block] == "": # Add student to class
if len(running[semester][block][currentCourse]["students"]) < classCap:
running[semester][block][currentCourse]["students"].append(student["name"])
student["schedule"][semester][block] = running[semester][block][currentCourse]["name"]
break
student["schedule"][semester][block] = courses[currentCourse]["name"]
getFreeBlock = False
else: # Find next available class or create new one
if semesterIndex == 2 and blockIndex == 4:
# No available classes
print("No more available classes")
break
getFreeBlock = False
elif blockIndex == 4:
blockIndex = 1
semesterIndex += 1
else: blockIndex += 1
else:
if semesterIndex == 2 and blockIndex == 4:
# No available classes
print("No more available classes for that student --------------------")
# Resort to alternative
getFreeBlock = False
elif blockIndex == 4:
blockIndex = 1
semesterIndex += 1
@@ -164,6 +173,7 @@ def generateSchedule():
"name": courses[currentCourse]["name"],
"students": [student["name"]]
}
student["schedule"][newSemester][newBlock] = courses[currentCourse]["name"]
break
else:
if semesterNum == 2 and blockNum == 4:
@@ -184,14 +194,16 @@ def generateSchedule():
if alternateOffset == 0:
# TODO: No more alternatives, solve problem
print("No more alternatives, solve this problem somehow")
break
generate = False
currentCourse = student["requests"][len(student["requests"])-alternateOffset]
alternateOffset -= 1
if __name__ == '__main__':
generateMockStudents(400)
generateMockStudents(200)
generateSchedule()
with open("schedule.json", "w") as outfile:
json.dump(running, outfile, indent=2)
with open("students.json", "w") as outfile:
json.dump(mockStudents, outfile, indent=2)