-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.py
More file actions
305 lines (260 loc) · 11.3 KB
/
Copy path1.py
File metadata and controls
305 lines (260 loc) · 11.3 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 29 02:04:38 2018
@author: Siddharth Singh
"""
import random
import xlrd
import xlsxwriter
workbook = xlrd.open_workbook('ttinput.xlsx')
worksheet = workbook.sheet_by_index(0)
#Reading Number of Sections
NumberOfSections=int(worksheet.cell(2, 2).value)
#print (NumberOfSections)
#Reading Number of Subjects
NumberOfSubjects=int(worksheet.cell(2, 7).value)
#print (NumberOfSubjects)
#Reading Number of Labs
NumberOfLabs=int(worksheet.cell(2, 12).value)
#print (NumberOfLabs)
#Reading Number of Batches per section
NumberOfBatches=int(worksheet.cell(4, 12).value)
#print (NumberOfBatches)
#Creating a dictionary that will hold Subject code as key and list of that subject's teachers as value
TeaDict = {}
for i in range (6,6+NumberOfSubjects):
j=4
temp = []
while (worksheet.cell(i, j).value!=0):
temp.append(worksheet.cell(i, j).value)
j+=1
TeaDict[worksheet.cell(i, 2).value] = temp
#print('Teachers For Each Subject:\n',TeaDict)
#Creating a dictionary that will store Subject code as key and subject credit as value
SubCredit = {worksheet.cell(i, 2).value:int(worksheet.cell(i, 3).value) for i in range (6,6+NumberOfSubjects)}
#print(SubCredit)
#Creating a list of subject Codes
SubList=[]
for i in range (0,NumberOfSubjects): SubList.append(worksheet.cell(6+i, 2).value)
#print(SubList)
#Creating a dictionary that will store Subject code as key and teacher as value for one section
ThisSection = {worksheet.cell(i, 2).value:'NULL' for i in range (6,6+NumberOfSubjects)}
#print(ThisSection)
#creating timetable table and initialising value as zeros except halfday
print('\n-----Initial-TIME-TABLE-----')
day = 6
hour = 7
table = [[0] * hour for i in range(day)]
table[5][4]=table[5][5]=table[5][6]='X' #halfday
for i in range (0,6): print(table[i])
print('-----------------------------')
def updateTTSecB(sheetno):
#for section B doing same thing again but with ability to check if lab-timing is clashing or not
worksheetSectionA = workbook.sheet_by_index(sheetno)
#updating TimeTable
#First setting lab timings. Each lab is of 3 hrs. each section has three lab day
#choosing random days from week to set lab timing. number of lab day is equal to NumberOfBatches
labDayFixed=[]
labHours=[1,4]
for i in range (0,NumberOfBatches):
labSet=0
while(labSet==0):
labDay=random.randint(0,5)
if labDay not in labDayFixed:
j=random.choice(labHours)
if labDay==5:
j=1 #on saturday lab can only be in first half
if (worksheetSectionA.cell(labDay, labHours).value!='LAB'):
labDayFixed.append(labDay)
table[labDay][j]=table[labDay][j+1]=table[labDay][j+2]='LAB'
labSet=1
#Using two loops to traverse through the time table
for i in range (0,6):
Sub = 'Blank'
for j in range (0,7):
if table[i][j]==0:
check=0
Secondpass=0
Firstpass=1
while(Secondpass==0):
#choosing a random subject from SubList that is not same as previous hour
if(j==0):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
else:
while(table[i][j-1]==Sub):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
#checking if the sub class is already taken in previous hours
for k in range (0,j):
if(Sub==table[i][k]):
Firstpass=0
#if passes first checkpost: check if subject's credit are remaining
if (Firstpass==1):
if (SubCredit[Sub]!=0):
Secondpass=1
SubCredit.update({Sub:SubCredit[Sub]-1})
else:
Secondpass=0
#Checking if number of trials are not more than number of subjects
check+=1
if(check==NumberOfSubjects):
Sub='Blank'
Secondpass=1
#if passes second check post
if (Secondpass==1):
table[i][j]=Sub
#selecting teacher for a section
if(Sub!='Blank'):
if(ThisSection[Sub]=='NULL'):
Teacher=TeaDict[Sub][random.randint(0,2)]
ThisSection[Sub]=Teacher
print(Sub, ':', ThisSection[Sub])
def updateTT():
#updating TimeTable
#First setting lab timings. Each lab is of 3 hrs. each section has three lab day
#choosing random days from week to set lab timing. number of lab day is equal to NumberOfBatches
labDayFixed=[]
labHours=[1,4]
for i in range (0,NumberOfBatches):
labSet=0
while(labSet==0):
labDay=random.randint(0,5)
if labDay not in labDayFixed:
labDayFixed.append(labDay)
j=random.choice(labHours)
if labDay==5:
j=1 #on saturday lab can only be in first half
table[labDay][j]=table[labDay][j+1]=table[labDay][j+2]='LAB'
labSet=1
#Using two loops to traverse through the time table
for i in range (0,6):
Sub = 'Blank'
for j in range (0,7):
if table[i][j]==0:
check=0
Secondpass=0
Firstpass=1
while(Secondpass==0):
#choosing a random subject from SubList that is not same as previous hour
if(j==0):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
else:
while(table[i][j-1]==Sub):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
#checking if the sub class is already taken in previous hours
for k in range (0,j):
if(Sub==table[i][k]):
Firstpass=0
#if passes first checkpost: check if subject's credit are remaining
if (Firstpass==1):
if (SubCredit[Sub]!=0):
Secondpass=1
SubCredit.update({Sub:SubCredit[Sub]-1})
else:
Secondpass=0
#Checking if number of trials are not more than number of subjects
check+=1
if(check==NumberOfSubjects):
Sub='Blank'
Secondpass=1
#if passes second check post
if (Secondpass==1):
table[i][j]=Sub
#selecting teacher for a section
if(Sub!='Blank'):
if(ThisSection[Sub]=='NULL'):
Teacher=TeaDict[Sub][random.randint(0,2)]
ThisSection[Sub]=Teacher
print(Sub, ':', ThisSection[Sub])
def printTable():
print('\n--------------------------TIME-TABLE--------------------------')
for i in range (0,6): print(table[i])
print('--------------------------------------------------------------\n')
print('Remaining Subject Credits:\n',SubCredit)
updateTTSecB(0)
#printTable()
#if Subject credits remains, find a blank spot such that subject is not in that day and replace it with that subject
for i in range (0,6):
day=0
while (SubCredit[SubList[i]]!=0 and day<6):
if SubList[i] not in table[day]:
hr=0
done=0
while (done==0 and hr<7):
if table[day][hr]=='Blank':
table[day][hr]=SubList[i]
SubCredit.update({SubList[i]:SubCredit[SubList[i]]-1})
done=1
hr+=1
else: day+=1
printTable()
#creating a workbook named workbookOutput and creating worksheet1 named SectionA
workbookOutput = xlsxwriter.Workbook('Timetable.xlsx')
worksheet1 = workbookOutput.add_worksheet('SectionA')
cell_format = workbookOutput.add_format({'bold': True, 'font_color': '#ff1814', 'bg_color':'#f4f6f9'})
cell_format2 = workbookOutput.add_format({'valign':'center'})
#creating table format
col = 1
for u in range(0,7):
hour= 'Hour ' + str(u+1)
worksheet1.write(0, col, hour, cell_format)
col += 1
worksheet1.write(1, 0, 'MONDAY', cell_format)
worksheet1.write(2, 0, 'TUESDAY', cell_format)
worksheet1.write(3, 0, 'WEDNESDAY', cell_format)
worksheet1.write(4, 0, 'THURSDAY', cell_format)
worksheet1.write(5, 0, 'FRIDAY', cell_format)
worksheet1.write(6, 0, 'SATURDAY', cell_format)
#writing content in table
row = 1
col = 1
for day in range(0,6):
for hour in range(0,7):
worksheet1.write(row, col, table[day][hour], cell_format2)
col+=1
row += 1
col=1
updateTTSecB(0)
#if Subject credits remains, find a blank spot such that subject is not in that day and replace it with that subject
for i in range (0,6):
day=0
while (SubCredit[SubList[i]]!=0 and day<6):
if SubList[i] not in table[day]:
hr=0
done=0
while (done==0 and hr<7):
if table[day][hr]=='Blank':
table[day][hr]=SubList[i]
SubCredit.update({SubList[i]:SubCredit[SubList[i]]-1})
done=1
hr+=1
else: day+=1
printTable()
worksheet2 = workbookOutput.add_worksheet('SectionB')
cell_format = workbookOutput.add_format({'bold': True, 'font_color': '#ff1814', 'bg_color':'#f4f6f9'})
cell_format2 = workbookOutput.add_format({'valign':'center'})
#creating table format
col = 1
for u in range(0,7):
hour= 'Hour ' + str(u+1)
worksheet2.write(0, col, hour, cell_format)
col += 1
worksheet2.write(1, 0, 'MONDAY', cell_format)
worksheet2.write(2, 0, 'TUESDAY', cell_format)
worksheet2.write(3, 0, 'WEDNESDAY', cell_format)
worksheet2.write(4, 0, 'THURSDAY', cell_format)
worksheet2.write(5, 0, 'FRIDAY', cell_format)
worksheet2.write(6, 0, 'SATURDAY', cell_format)
#writing content in table
row = 1
col = 1
for day in range(0,6):
for hour in range(0,7):
worksheet2.write(row, col, table[day][hour], cell_format2)
col+=1
row += 1
col=1
workbookOutput.close()