Commit 99d582ac authored by Jack Otto's avatar Jack Otto
Browse files

11/18/21 making the scheduler_class class

parent 0dc5ef18
lkgejs1avt6va9ckl1mca1jrc8
9a0hrqcl6v8vm75o85u30i17ps
9uikf7gj7dfu06p6psp6kultlo
k8qmftpmqt733svhbav288sst0
ha10uvfsellmb4iujnggfjoe6k
k3cujd04k2onvtplodp6men1m4
imqes9jhubar1kfhroflfa7rt0
u51oodqumlhcne998vccru5pak
u2unmq6g01oujqffqjbf4kcvo0
ap0m28h1d2rp0jp31osptoj9h8
rtcva8b9et1mtheolune3l5jo0
9t978avbge42pk7bdoic5terpc
686n5omuf0asg9tnet9k5sf8nk
sru7bcdthu6afrbffbdi7uoe78
mgat7ckd280fuqrkiagobq926o
73j6lup99iavmrbvr5poabsfoc
70ll0rnsh4mbmrv9v3s6t11oe8
mn5f8atc3cgdn4dm7lrkom6i88
m0bf38db9a6fsmar8tr5iq7u40
d56j7fvf5g06uuu94p6pqng1s0
n2g245rp4vsag9m6b3t1kq0avk
sf14cbkj6ao8b240rd3b2femc4
lrg6qq75ve1u01kvh8esc5a178
rh05ses4nf68kmffj079fa82bg
d0e0k5itpgnavmaqtkmc05fdk4
l6o2mot84m0bjc4605ft34spv8
fggi7ah683mqks2t4mdcakos8g
7galh2t6qjqnstm26enmill8c8
b3p5c508378i8vsac0j709epno
r16404676bfvn7v920antehmtc
4hqkrppv8ur56rgurstrsiqpq0
vvkq0kvgisfp40l6bqqhe9rpo0
h517sks05saohdf5jfdhf5dr88
pj0svrhmoq5vi6b3ncopprece4
hjanjt1bkec3jtiflmbepus5j8
h638q8kbi5mkv33ripaui668ig
i83ep5nkes8npbjopit8987gcs
aptq4n96h0jdufmtq538dkg370
4bj47d7o0i336a91lql5r5bv7k
gb2e76lc15a59embinftr9gu00
7amo4ojmjnqd2bv3sbom2ho924
3oa30p6ik2pgjuu7mgkd7r7ijc
g6i4iel5n7kn037v7huoaa94nc
7riqo0533f2c1p1e6vgffmlp30
40hgl07ht2bv9ltinudnn63ar4
k1k6vtncn17b65eirqp0sjh7r4
lrluekocrfdmlgdf1qjs98ct7o
l9j3cgsebucplq11gb51k7n5ng
igkv5g9r3pmg7he58s5fvg4nlo
iv3jib6c2uu3hp6qmjlgf2up10
2nnlbka84l2426u2elhg5ctvps
pmvval1js7dvrn2n4m7da9lo9o
9n2a0a42kflq787hghrjfq6vu8
hbu67053g08j6pfhet8hmnkafc
ei21boahb38dlsme7jip60mq1g
3kceq88gom9n95akvejhaq7lv4
u9kosgqp37d4e100pn2i3ct1bc
3ijul13dig678o2f4gp3fnvuvc
esgqbhe9gd0uvf2lud72led8rk
q0i71eebhab1h62jvn33raujbs
j5bp4uhsl5hrukcuh42bkuh26o
0lvkkurr4tjf8a0b7knoif1nsk
kb9lr6qsaaqai2j5koc7e4a1k8
40mvp6tenfek2t0i879qombc0k
pu7v51km6cgvc64bk9s9sml1q0
fb4udao4mm32selu0a501l98b0
f3q9pjbfg4mveg2qu9nkph0650
r64pr6v6tpduvl2s9vq9sfiu0o
qq31q9v58o03rv6g0tavodaub0
9i5mlkph6b624u18vt559dki08
tvo64ooiqo26p2l088dc0lai6o
bbvu40fa5ulm9hm308so3dimv4
f8vea5704062pkdukv0flvqisk
htffebqdlv6rj2dr5asbh5fmls
h50obhjuor1ntd4dcna277u9mk
......@@ -203,17 +203,17 @@ def full_schedule():
model.Add(scheduled_classes[n, c, p, "Th", "13"] == 0)
model.Add(scheduled_classes[n, c, p, "Th", "14"] == 0)
# print("Adding constraint to not schedule classes of same color at the same times")
# for n in courses:
# for d in day_names:
# for h in hours:
# classes_of_same_year = []
# for c in rooms:
# for p in profs:
# #only want one class for each year to be taught at a time
# for color_year in [cy for cy in courses if cy.color == n.color]:
# classes_of_same_year.append(scheduled_classes[color_year, c, p, d, h])
# model.Add(sum(classes_of_same_year) <= 1)
print("Adding constraint to not schedule classes of same color at the same times")
for n in courses:
for d in day_names:
for h in hours:
classes_of_same_year = []
for c in rooms:
for p in profs:
#only want one class for each year to be taught at a time
for color_year in [cy for cy in courses if cy.color == n.color]:
classes_of_same_year.append(scheduled_classes[color_year, c, p, d, h])
model.Add(sum(classes_of_same_year) <= 1)
......
class Schedule:
def __init__(self, model, courses, rooms, profs, days, hours):
self.model = model
self.courses = courses
self.rooms = rooms
self.profs = profs
self.days = days
self.hours = hours
self.scheduled_classes = {}
for n in self.courses:
for day in self.days:
for hour in self.hours:
for c in self.rooms:
for p in self.profs:
self.scheduled_classes[(n, c, p, day, hour)] = self.model.NewBoolVar(
f'{n}_{c}_{p}_{day}_{hour}')
def initialize_constraints(self):
print("Adding Class must be taught constraint...")
## The class must be taught
for n in self.courses:
num_classes_taught = 0
for c in self.rooms:
for p in self.profs:
for h in self.hours:
for d in self.days:
num_classes_taught += self.scheduled_classes[(n, c, p, d, h)]
self.model.Add(num_classes_taught == len(n.days))
print("Adding same time/day across week constraint...")
## Must be taught at the same time/room/prof each day it needs to be taught
for n in self.courses:
for c in self.rooms:
for p in self.profs:
for h in self.hours:
days_class_is_taught = []
for d in n.days:
days_class_is_taught.append(self.scheduled_classes[(n, c, p, d, h)])
for i in range(len(days_class_is_taught) - 1):
self.model.Add(days_class_is_taught[i] == days_class_is_taught[i + 1])
print("Preventing impossible conditions...")
for n in self.courses:
for c in self.rooms:
for p in self.profs:
for d in self.days:
for h in self.hours:
# constraint to make sure the classes fit in the classroom and have the right features
if n.size > c.capacity or n.room_requirement != c.features:
self.model.Add(self.scheduled_classes[(n, c, p, d, h)] == 0)
## The class must be taught on the right days that is is offered
if d not in n.days:
self.model.Add(self.scheduled_classes[(n, c, p, d, h)] == 0)
## Makes sure the professor only teaches the right classes
if n.name not in p.teachable_classes:
self.model.Add(self.scheduled_classes[(n, c, p, d, h)] == 0)
if (d, h) in c.unavailable_times:
self.model.Add(self.scheduled_classes[(n, c, p, d, h)] == 0)
# ## Makes the teacher teach below or equal to their possible load
# for p in self.profs:
# courses_taught = 0
# true_load = p.teaching_load
# for n in self.courses:
# for c in self.rooms:
# for h in self.hours:
# for d in self.days:
# courses_taught += self.scheduled_classes[(n, c, p, d, h)]
# true_load += self.scheduled_classes[(n, c, p, n.days[0], h)] * (len(n.days) - 1)
# self.model.Add(courses_taught <= true_load)
print("Adding no overbooking classrooms Constraint...")
## No overbooking of a classroom for a specific time and day
for c in self.rooms:
for d in self.days:
for h in self.hours:
room_used = 0
for n in self.courses:
for p in self.profs:
room_used += self.scheduled_classes[n, c, p, d, h]
self.model.Add(room_used <= 1)
print("Adding no overbooking prof Constraint")
## No overbooking of a prof for a specific time and day
for p in self.profs:
for d in self.days:
for h in self.hours:
prof_used = 0
for n in self.courses:
for c in self.rooms:
prof_used += self.scheduled_classes[n, c, p, d, h]
self.model.Add(prof_used <= 1)
print("Adding no letting classes go past 5pm constraint")
## No scheduling classes if their length will take them past end of work day
for n in self.courses:
if n.length != 1:
for c in self.rooms:
for p in self.profs:
for d in n.days:
last_time = len(self.hours) - n.length + 1
for delta_t in range(last_time, len(self.hours)):
self.model.Add(self.scheduled_classes[(n, c, p, d, self.hours[delta_t])] == 0)
print("Adding constraint to not allow sections of same class to be taught together")
## Ensure Sections of each class are not taught at the same time
for n in self.courses:
for d in self.days:
for h in self.hours:
sections_taught_at_this_time = []
for c in self.rooms:
for p in self.profs:
classname = n.name.split(" ")
classname = classname[0] + classname[1]
for section in [sn for sn in self.courses if
sn.name.split(" ")[0] + sn.name.split(" ")[1] == classname]:
sections_taught_at_this_time.append(self.scheduled_classes[section, c, p, d, h])
self.model.Add(sum(sections_taught_at_this_time) <= 1)
print("Adding constraint for classes longer than an hour")
# Make sure if a class is longer than an hour that a class can not be taught in the time span of that class --> slow working version
for n in self.courses:
if n.length != 1:
for c in self.rooms:
for p in self.profs:
for d in n.days:
for h in self.hours[0:len(self.hours) - n.length + 1]:
subsequent_hours_of_classes = []
for delta_t in range(1, n.length):
dh = str(int(h) + delta_t)
dh = str(((2 - len(dh)) * "0") + dh)
# ALL classes with the prof and room need to be 0
for other_n in self.courses:
for other_p in self.profs:
subsequent_hours_of_classes.append(
self.scheduled_classes[other_n, c, other_p, d, dh])
self.model.Add(
self.scheduled_classes[n, c, p, d, h] + sum(subsequent_hours_of_classes) <= 1)
# # Classes Longer than an hour should block out the following classes --> fast meh version
# for n in [classes for classes in self.courses if classes.length > 1]:
# for c in [room for room in self.rooms if n.size <= room.capacity or n.room_requirement == room.features]:
# for p in [professor for professor in self.profs if n in professor.teachable_classes]:
# for d in n.days:
# for h in self.hours[0:len(self.hours) - n.length+1]:
# subsequent_hours_of_classes=[]
# for delta_t in range(1, n.length):
# dh = str(int(h) + delta_t)
# dh = str(((2-len(dh))*"0") + dh)
#
# ## TODO FIX THIS CONSTRAINT
#
# # ALL classes with the prof and room need to be 0
# for other_n in self.courses:
# for other_p in self.profs:
# subsequent_hours_of_classes.append(self.scheduled_classes[other_n, c, other_p, d, dh])
#
# self.model.Add(self.scheduled_classes[n, c, p, d, h] + sum(subsequent_hours_of_classes) <= 1)
print("Adding constraint for senior design common hour to be blocked off")
for p in self.profs:
if "MECH 402C 10" in p.teachable_classes:
for n in self.courses:
for c in self.rooms:
self.model.Add(self.scheduled_classes[n, c, p, "Th", "13"] == 0)
self.model.Add(self.scheduled_classes[n, c, p, "Th", "14"] == 0)
print("Adding constraint for senior design class room common hour to be blocked off")
for c in self.rooms:
if c == "DANA137" or c == "DANA115":
for n in self.courses:
for p in self.profs:
self.model.Add(self.scheduled_classes[n, c, p, "Th", "13"] == 0)
self.model.Add(self.scheduled_classes[n, c, p, "Th", "14"] == 0)
# print("Adding constraint to not schedule classes of same color at the same times")
# for n in self.courses:
# for d in self.days:
# for h in self.hours:
# classes_of_same_year = []
# for c in self.rooms:
# for p in self.profs:
# #only want one class for each year to be taught at a time
# for color_year in [cy for cy in self.courses if cy.color == n.color]:
# classes_of_same_year.append(self.scheduled_classes[color_year, c, p, d, h])
# self.model.Add(sum(classes_of_same_year) <= 1)
def add_hard_constraint(self, n, c, p, d, h):
try:
self.model.Add(self.scheduled_classes[n, c, p, d, h] == 1)
except:
print("Error adding previous hard constraint")
Supports Markdown
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