Skip to content

Commit

Permalink
Added half-credit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
el-agua committed Oct 30, 2023
1 parent 37fc1f2 commit c25e348
Show file tree
Hide file tree
Showing 2 changed files with 655 additions and 12 deletions.
43 changes: 31 additions & 12 deletions backend/courses/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,9 @@ class Scheduler:
def __init__(self):
self.intervals = []

def add_interval(self, start_time, end_time, class_name, section):
def add_interval(self, start_time, end_time, class_name, section, credit):
# Adds an interval to the list of intervals
self.intervals.append((start_time, end_time, class_name, section))
self.intervals.append((start_time, end_time, class_name, section, credit))
return True

def find_optimal_schedule(self, unwanted_intervals=[]):
Expand Down Expand Up @@ -795,6 +795,7 @@ def find_lectures_on_day(lectures, day):
float(meeting["end"]) + 0.001 * random.random(),
"-".join(section["id"].split("-")[0:2]),
section["id"],
section["credits"]
)
)
return lectures_on_day
Expand All @@ -813,7 +814,7 @@ def schedule_to_section(schedules):
# Removes the time intervals from the schedule list
sections = []
for s in schedules:
sections.append(s[3])
sections.append((s[3], s[4]))
return sections

def remove_duplicates(given_l):
Expand All @@ -839,7 +840,7 @@ def choose_class_hash(given_l):
hash = {}
courses = []
for node in given_l:
class_name = "-".join(node.split("-")[0:2])
class_name = "-".join(node[0].split("-")[0:2])
if class_name not in hash.keys():
hash[class_name] = [node]
else:
Expand All @@ -849,6 +850,12 @@ def choose_class_hash(given_l):
courses.append(random.choice(hash[key]))
return courses

def credit_count(sections):
acc = 0
for section in sections:
acc += section[1]
return acc

def check_if_schedule_possible(schedule, courses, unwanted_intervals):
"""
Given a schedule and a list of courses,
Expand All @@ -867,13 +874,14 @@ def check_if_schedule_possible(schedule, courses, unwanted_intervals):
intervals = []
for course in courses:
for section in course:
if section["id"] in schedule:
if (section["id"],section["credits"]) in schedule:
for meeting in section["meetings"]:
intervals.append(
(
day_to_num[meeting["day"]] + 0.01 * float(meeting["start"]),
day_to_num[meeting["day"]] + 0.01 * float(meeting["end"]),
section["id"],
section["credits"]
)
)
intervals = sorted(intervals, key=lambda x: x[0])
Expand Down Expand Up @@ -912,7 +920,8 @@ def scheduler_for_day(lectures, day, breaks):
day_classes = find_lectures_on_day(lectures, day)
scheduler = Scheduler()
for day_class in day_classes:
scheduler.add_interval(day_class[0], day_class[1], day_class[2], day_class[3])
scheduler.add_interval(day_class[0], day_class[1], day_class[2],
day_class[3], day_class[4])
day_schedules = []
for _ in range(5):
day_schedules.append(scheduler.find_optimal_schedule(breaks[day]))
Expand All @@ -938,14 +947,18 @@ def add_recs_to_schedule(schedule, recs, lectures, breaks):
newschedule = schedule
for course in recs:
if course != []:
print(course)
print()
course_name = "-".join(course[0]["id"].split("-")[0:2])
schedule_names = list(map(lambda x: "-".join(x.split("-")[0:2]), schedule))
schedule_names = list(map(lambda x: "-".join(x[0].split("-")[0:2]), schedule))
if course_name in schedule_names:
for section in course:
print(section["id"])
if check_if_schedule_possible(
schedule + [section["id"]], recs + lectures, breaks
schedule + [(section["id"], section["credits"])], recs + lectures,
breaks
):
newschedule.append(section["id"])
newschedule.append((section["id"], section["credits"]))
break
return newschedule

Expand All @@ -963,7 +976,6 @@ def add_recs_to_schedule(schedule, recs, lectures, breaks):
wed_schedules = scheduler_for_day(lectures, "W", breaks)
thurs_schedules = scheduler_for_day(lectures, "R", breaks)
fri_schedules = scheduler_for_day(lectures, "F", breaks)

possible_mwf = []
for i in range(len(monday_schedules)):
for j in range(len(wed_schedules)):
Expand All @@ -979,7 +991,6 @@ def add_recs_to_schedule(schedule, recs, lectures, breaks):
for i in range(len(possible_tr)):
for j in range(len(possible_mwf)):
total_schedules.append(possible_tr[i] + possible_mwf[j])

courses = list(map(schedule_to_section, total_schedules))
courses_unique = list(map(remove_duplicates, courses))

Expand All @@ -998,8 +1009,16 @@ def add_recs_to_schedule(schedule, recs, lectures, breaks):
for c in itertools.combinations(choose[i], count)
if c not in combinations
]
"""
[
combinations.append(list(c))
for c in itertools.combinations(choose[i], count + 1)
if c not in combinations
]
"""
else:
combinations = choose
choose = [add_recs_to_schedule(schedule, recs, lectures, breaks) for schedule in combinations]
choose = sorted(choose, key=lambda x: len(x))
choose = [schedule for schedule in choose if credit_count(schedule) <= count]
choose = sorted(choose, key=lambda x: random.random())
return choose
Loading

0 comments on commit c25e348

Please sign in to comment.