-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch.py
272 lines (248 loc) · 10.1 KB
/
scratch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import random
from datetime import datetime, timedelta
from copy import deepcopy
import math
import pandas
# ----------> reading files
x_courses = pandas.read_csv("actual_dataset/courses.csv", header=None)
courses = [list(row) for row in x_courses.values]
x_rooms = pandas.read_csv("actual_dataset/rooms.csv", header=None)
rooms = [list(row) for row in x_rooms.values]
x_studentCourse = pandas.read_csv("actual_dataset/studentCourses.csv", header=None)
studentCourse = [list(row) for row in x_studentCourse.values]
x_studentNames = pandas.read_csv("actual_dataset/studentNames.csv", header=0)
studentNames = [list(row) for row in x_studentNames.values]
x_teachers = pandas.read_csv("actual_dataset/teachers.csv", header=0)
teachers = [list(row) for row in x_teachers.values]
def randomSlotGenerator(slotsList):
for x in courses:
# =====> Saturday Sunday Clash Removal
while 1:
randDateIndex = random.randint(0,len(datesForTwoWeeks)-1)
day = dayNames[datesForTwoWeeks[randDateIndex].weekday()]
if day != 'Saturday' and day != 'Sunday':
break
# ======> Friday we have different time slot due to break
if day != 'Friday':
randTimeIndex = random.randint(0, len(slotsForTwoWeeks) - 1)
slotAssigned = slotsForTwoWeeks[randTimeIndex]
else:
slotsFriday = ['9A.M. - 12P.M.', '2P.M. - 5P.M.']
randTimeIndex = random.randint(0, len(slotsFriday) - 1)
slotAssigned = slotsFriday[randTimeIndex]
# ========> Select Random Teacher
randTeacherIndex = random.randint(0, len(teachers)-1)
totalStudentsForCourse = countCourseStudents[x[0]]
# ========> Select Rooms allocation
roomsAlloted = []
# =========> Room Allocation
while totalStudentsForCourse > 0:
randRoom = random.randint(0, len(rooms) - 1)
if rooms[randRoom][0] in roomsAlloted:
continue
roomsAlloted.append(rooms[randRoom][0])
totalStudentsForCourse -= 28
# =======> assigning date, day slot time ,teacher and rooms for a Course exam randomly
slotsList[x[0]]=[str(datesForTwoWeeks[randDateIndex]),
dayNames[datesForTwoWeeks[randDateIndex].weekday()],
slotAssigned,
teachers[randTeacherIndex][0],
roomsAlloted]
return slotsList
def CheckTeacherClash(slotsList):
teacherChecked = []
teachersClash = int(0)
for slot in slotsList:
teacher = slotsList[slot][3]
date = slotsList[slot][0]
if teacher in teacherChecked:
continue
teacherChecked.append(teacher)
for slot1 in slotsList:
if slot != slot1:
teacher1 = slotsList[slot1][3]
date1 = slotsList[slot1][0]
if teacher == teacher1 and date == date1:
print(slot, slot1, teacher, date)
teachersClash += 1
return teachersClash
def CheckRoomClash(slotsList):
roomChecked = []
roomClash = int(0)
for slot in slotsList:
room = slotsList[slot][4]
date = slotsList[slot][0]
time = slotsList[slot][2]
for r in room:
if room in roomChecked:
continue
roomChecked.append(room)
for slot1 in slotsList:
if slot != slot1:
room1 = slotsList[slot1][4]
date1 = slotsList[slot1][0]
time1 = slotsList[slot1][2]
if room == room1 and date == date1:
if time1 == time:
print(slot, slot1, room, date)
roomClash += 1
elif (time=='1P.M. - 4P.M.' and time1=='2P.M. - 5P.M.') or (time1 == '1P.M. - 4P.M.'and time=='2P.M. - 5P.M.'):
print(slot, slot1, room, date)
roomClash += 1
# print(date,end=", ")
return roomClash
def CheckCourseClash(slotsList):
dateChecked = []
dateTimeClash = int(0)
for slot in slotsList:
time = slotsList[slot][2]
date = slotsList[slot][0]
datetime = [date,time]
if datetime in dateChecked:
continue
dateChecked.append(datetime)
for slot1 in slotsList:
if slot != slot1:
time1 = slotsList[slot1][2]
date1 = slotsList[slot1][0]
if time == time1 and date == date1:
print(slot, slot1,time, time1, date)
dateTimeClash += 1
elif (time=='1P.M. - 4P.M.'and time1=='2P.M. - 5P.M.' and date == date1) or (time1=='1P.M. - 4P.M.'and time=='2P.M. - 5P.M.' and date == date1):
print(slot, slot1,time, time1, date)
dateTimeClash += 1
# print(date,end=", ")
return dateTimeClash
def CheckstudentClash(slotsList):
studentCourses = {}
for stu in studentNames:
temp = []
for stuCor in studentCourse:
if stu[0] == stuCor[1]:
temp.append(stuCor[2])
studentCourses[stu[0]] = temp
studentclashes = int(0)
for stu in studentCourses:
courselist = studentCourses[stu]
for i in range(0,len(courselist)):
date1 = slotsList[courselist[i]][0]
time1= slotsList[courselist[i]][2]
for j in range(i+1,len(courselist)):
date2=slotsList[courselist[j]][0]
time2=slotsList[courselist[j]][2]
#checking date and time courseList[i] and courseList[j]
if date1 == date2 and time1 == time2:
print(stu,courselist[i],courselist[j])
studentclashes+=1
elif (time2 == '1P.M. - 4P.M.' and time1 == '2P.M. - 5P.M.' and date2 == date1) or (time1 == '1P.M. - 4P.M.' and time2 == '2P.M. - 5P.M.' and date2 == date1):
print(stu, courselist[i], courselist[j])
studentclashes += 1
return studentclashes
def solutionCost(slotsList):
print()
#checking teacher clashes
print("\nchecking teachers clash : ")
totalTeachersClash = CheckTeacherClash(slotsList)
print("\ntotal teacher clashes = ",totalTeachersClash)
print("\nchecking rooms clash : ")
totalRoomClash = CheckRoomClash(slotsList)
print("\ntotal rooms clashes = ", totalRoomClash)
print("\nchecking course clash : ")
totalCourseClash = CheckCourseClash(slotsList)
print("\ntotal course clashes = ", totalCourseClash)
print("\nchecking Student clash : ")
totalStudentClash = CheckstudentClash(slotsList)
# print("\ntotal student clashes = ", totalStudentClash)
print("total clahes = ", totalRoomClash + totalTeachersClash)
return totalRoomClash+ totalTeachersClash
def randomNeighbour(slotList):
#print(read.courses)
newSol = deepcopy(slotList)
randCourseIndex = random.randint(0,len(courses)-1)
code = courses[randCourseIndex][0]
#print(code)
# selecting date randomly and checking day should not be sunday and saturday
while 1:
randDateIndex = random.randint(0, len(datesForTwoWeeks) - 1)
day = dayNames[datesForTwoWeeks[randDateIndex].weekday()]
if day != 'Saturday' and day != 'Sunday':
break
if day != 'Friday':
randTimeIndex = random.randint(0, len(slotsForTwoWeeks) - 1)
slotAssigned = slotsForTwoWeeks[randTimeIndex]
else:
slotsFriday = ['9A.M. - 12P.M.', '2P.M. - 5P.M.']
randTimeIndex = random.randint(0, len(slotsFriday) - 1)
slotAssigned = slotsFriday[randTimeIndex]
randTeacherIndex = random.randint(0, len(teachers) - 1)
totalStudentsForCourse = countCourseStudents[x[0]]
# Rooms allocation
roomsAlloted = []
while totalStudentsForCourse > 0:
randRoom = random.randint(0, len(rooms) - 1)
if rooms[randRoom][0] in roomsAlloted:
continue
roomsAlloted.append(rooms[randRoom][0])
totalStudentsForCourse -= 28
# assigning date, day slot time ,teacher and rooms for a Course exam randomly
newSol[code] = [str(datesForTwoWeeks[randDateIndex]),
dayNames[datesForTwoWeeks[randDateIndex].weekday()],
slotAssigned,
teachers[randTeacherIndex][0],
roomsAlloted]
#print(newSol)
return newSol
def simulatedAneal(slotList):
current = slotList
next = []
best = current
temperature = 5
coolingRate = 0.5
while temperature > 1:
next = randomNeighbour(current)
E = solutionCost(current) - solutionCost(next)
if E > 0:
current = next
print("Good Move : {}".format(current))
else:
probabilty = math.exp(E / temperature)
current = next
print("Bad Move : {}\n with probability {}".format(current, probabilty))
if solutionCost(current) < solutionCost(best):
best = current
temperature -= coolingRate
print("********************************\n\nBest solution is :")
for slot in slotList:
print(slot," = ",slotList[slot])
print("\nsolution cost is :")
solutionCost(best)
# count students in a course
slots = {}
dayNames = ["Monday", 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
slotsForTwoWeeks = ['9A.M. - 12P.M.', '1P.M. - 4P.M.', '2P.M. - 5P.M.']
datesForTwoWeeks = []
startDate = datetime(2022, 5, 20)
# ---------> Total 14 Days [ 2 Weeks]
# for i in range(14):
# datesForTwoWeeks.append(startDate.date())
# startDate += timedelta(days=1)
# countCourseStudents = {}
# ---------> Total 21 Days [3 Weeks]
for i in range(21):
datesForTwoWeeks.append(startDate.date())
startDate += timedelta(days=1)
countCourseStudents = {}
# how many students in 1 Slot
for x in courses:
count = int(0)
for y in studentCourse:
print("================>",y)
if y[2] == x[0]:
count += 1
countCourseStudents[x[0]] = count
print(countCourseStudents)
slots = randomSlotGenerator(slots)
for slot in slots:
print(slot, slots[slot])
solutionCost(slots)
simulatedAneal(slots)