-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinitialization.py
More file actions
330 lines (274 loc) · 11.2 KB
/
initialization.py
File metadata and controls
330 lines (274 loc) · 11.2 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import random
import time
import pandas as pd
import numpy as np
st = time.time()
fp = pd.read_csv('faculty.csv')
cp = pd.read_csv('courses.csv')
total_subject_list = []
total_teacher_list = []
total_batch_list = set()
day_timeslot_dict = {'mon': [1, 2, 3, 4, 5, 6], 'tue': [7, 8, 9, 10, 11, 12],
'wed': [13,14, 15, 16, 17, 18], 'thu': [19, 20, 21, 22, 23, 24],
'fri': [ 25, 28, 29, 30, 31, 32]}
lab_alloted = {2:5,4:7,6:7,8:6}
subject_lab_credithour_dict = {}
subject_credithour_dict = {}
subject_batch_dict = {}
no_class_hours_dict = {}
subject_batch_ind_dict = {}
subject_teacher_dict = {}
course_type_dict = {}
#--------------------------------------------#
# Initialize all table values
def initializeTables():
global total_teacher_list, total_subject_list, total_batch_list
global subject_lab_credithour_dict, subject_credithour_dict, subject_batch_dict
global no_class_hours_dict, subject_batch_ind_dict, subject_teacher_dict, course_type_dict
total_teacher_list = list(fp['Faculty_Name'])
total_subject_list = list(cp['Course_Name'])
total_batch_list = set(cp['Semester'])
subject_lab_credithour_dict = dict(zip(cp.loc[cp['Type'] == 'L', 'Course_Code'], cp.loc[cp['Type'] == 'L', 'NOCW']))
subject_credithour_dict = dict(zip(cp.loc[cp['Type'] == 'N', 'Course_Code'], cp.loc[cp['Type'] == 'N', 'NOCW']))
subject_batch_dict = {i: list(tdf['Course_Code']) + ['NC'+str(i)]
for i, tdf in cp.groupby('Semester')}
no_class_hours_dict = {'NC'+str(i): 30 - tdf['NOCW'].sum()
for i, tdf in cp.groupby('Semester')}
course_type_dict = {'NC'+str(i): 'NC' for i in total_batch_list}
for i, j in zip(cp['Course_Code'], cp['Semester']):
subject_batch_ind_dict[i] = j
subject_teacher_dict = dict(zip(cp['Course_Code'], cp['Faculty_id']))
course_type_dict.update(dict(zip(cp['Course_Code'], cp['Type'])))
#---------------------------------------------------------#
def weektosubs(week):
oweek = []
for day in week:
for slot in day:
for sub in slot:
oweek.append(sub)
return oweek
def substoweek(week):
cweek = []
slotting = []
for i in range(0,len(week),4):
slotting.append(week[i:i+4])
for i in range(0,len(slotting),6):
cweek.append(slotting[i:i+6])
return cweek
def weektoslots(week):
poweek = []
for day in week:
for slot in day:
poweek.append(slot)
return poweek
def slotstoweek(slotting):
pcweek = []
for i in range(0,len(slotting),6):
pcweek.append(slotting[i:i+6])
return pcweek
# Intialization
# Initialize a week chromosome
# Chromosome skeleton
def initializeChromosome():
initializeTables()
week = []
for i in range(5):
day = []
for j in range(6):
slots = ['' for k in range(4)]
day.append(slots)
week.append(day)
for day in week:
for slot in day:
for i in range(len(slot)):
rn = random.randint(0, len(subject_batch_dict[(i*2)+2])-1)
sub = subject_batch_dict[(i*2)+2][rn]
if course_type_dict[sub] == 'NC':
if no_class_hours_dict[sub]>0:
slot[i] += ''
no_class_hours_dict[sub]-=1
if no_class_hours_dict[sub] == 0:
subject_batch_dict[(i*2)+2].remove(sub)
elif course_type_dict[sub] == 'N':
if subject_credithour_dict[sub]>0:
slot[i] += sub
subject_credithour_dict[sub]-=1
if subject_credithour_dict[sub] == 0:
subject_batch_dict[(i*2)+2].remove(sub)
elif course_type_dict[sub] == "L":
if subject_lab_credithour_dict[sub]>0:
slot[i] += sub
subject_lab_credithour_dict[sub]-=1
if subject_lab_credithour_dict[sub] == 0:
subject_batch_dict[(i*2)+2].remove(sub)
return week
def allEmpty():
for i in subject_batch_dict:
if subject_batch_dict[i]!=[]:
return False
return True
def initializeChromosomeRandom():
initializeTables()
week = []
for i in range(5):
day = []
for j in range(6):
slots = ['' for k in range(4)]
day.append(slots)
week.append(day)
week = weektoslots(week)
bl = [2,4,6,8]
while subject_batch_dict!={}:
rns = random.randint(0,29)
rnb = random.choice(bl)
if week[rns][(rnb//2)-1] == '':
sub = random.choice(subject_batch_dict[rnb])
if course_type_dict[sub] == 'NC':
if no_class_hours_dict[sub]>0:
week[rns][(rnb//2)-1] += ''
no_class_hours_dict[sub]-=1
if no_class_hours_dict[sub] == 0:
subject_batch_dict[rnb].remove(sub)
elif course_type_dict[sub] == 'N':
if subject_credithour_dict[sub]>0:
week[rns][(rnb//2)-1] += sub
subject_credithour_dict[sub]-=1
if subject_credithour_dict[sub] == 0:
subject_batch_dict[rnb].remove(sub)
elif course_type_dict[sub] == "L":
if subject_lab_credithour_dict[sub]>0:
week[rns][(rnb//2)-1] += sub
subject_lab_credithour_dict[sub]-=1
if subject_lab_credithour_dict[sub] == 0:
subject_batch_dict[rnb].remove(sub)
if subject_batch_dict[rnb] == []:
del subject_batch_dict[rnb]
bl.remove(rnb)
else:
continue
return (slotstoweek(week))
# CREATE A VERY SPECIFIC SELECTIVE INITIALIZATION WITH 2 HOURS CLASS PUT TO PLACE AND
# CLASSES TODAY NOT PLACED TMRW UNLESS NEEDED SO
# ALSO NOT FILL FIRST AND LAST SLOT IF NEEDED SO
# Create a population
popz = 100
pop = []
for i in range(popz):
pop.append(initializeChromosome())
# popz = 100
# pop = {}
# for i in range(popz):
# chromosome = weektosubs(initializeChromosomeRandom())
# pop[tuple(chromosome)] = -1
# import random
# import time
# import pandas as pd
# import numpy as np
# st = time.time()
# fp = pd.read_csv('faculty.csv')
# cp = pd.read_csv('courses.csv')
# # print(cp)
# # print(fp)
# total_subject_list = []
# total_teacher_list = []
# total_batch_list = set()
# day_timeslot_dict = {'mon': [1, 2, 3, 4, 5, 6], 'tue': [7, 8, 9, 10, 11, 12],
# 'wed': [13,14, 15, 16, 17, 18], 'thu': [19, 20, 21, 22, 23, 24],
# 'fri': [ 25, 28, 29, 30, 31, 32]}
# lab_alloted = {2:6,4:7,6:7,8:6}
# subject_lab_credithour_dict = {}
# subject_credithour_dict = {}
# subject_batch_dict = {}
# no_class_hours_dict = {}
# subject_batch_ind_dict = {}
# subject_teacher_dict = {}
# course_type_dict = {}
# #--------------------------------------------#
# # Initialize all table values
# def initializeTables():
# global total_teacher_list, total_subject_list, total_batch_list
# global subject_lab_credithour_dict, subject_credithour_dict, subject_batch_dict
# global no_class_hours_dict, subject_batch_ind_dict, subject_teacher_dict, course_type_dict
# total_teacher_list = list(fp['Faculty_Name'])
# total_subject_list = list(cp['Course_Name'])
# total_batch_list = set(cp['Semester'])
# subject_lab_credithour_dict = dict(zip(cp.loc[cp['Type'] == 'L', 'Course_Code'], cp.loc[cp['Type'] == 'L', 'NOCW']))
# subject_credithour_dict = dict(zip(cp.loc[cp['Type'] == 'N', 'Course_Code'], cp.loc[cp['Type'] == 'N', 'NOCW']))
# subject_batch_dict = {i: list(tdf['Course_Code']) + ['NC'+str(i)]
# for i, tdf in cp.groupby('Semester')}
# no_class_hours_dict = {'NC'+str(i): 30 - tdf['NOCW'].sum()
# for i, tdf in cp.groupby('Semester')}
# course_type_dict = {'NC'+str(i): 'NC' for i in total_batch_list}
# for i, j in zip(cp['Course_Code'], cp['Semester']):
# subject_batch_ind_dict[i] = j
# subject_teacher_dict = dict(zip(cp['Course_Code'], cp['Faculty_id']))
# course_type_dict.update(dict(zip(cp['Course_Code'], cp['Type'])))
# #---------------------------------------------------------#
# # Intialization
# # Initialize a week chromosome
# # look into this later
# # Chromosome skeleton
# def initializeChromosome():
# initializeTables()
# week = []
# for i in range(5):
# day = []
# for j in range(6):
# slots = ['' for k in range(4)]
# day.append(slots)
# week.append(day)
# for day in week:
# for slot in day:
# for i in range(len(slot)):
# rn = random.randint(0, len(subject_batch_dict[(i*2)+2])-1)
# sub = subject_batch_dict[(i*2)+2][rn]
# if course_type_dict[sub] == 'NC':
# if no_class_hours_dict[sub]>0:
# slot[i] += ''
# no_class_hours_dict[sub]-=1
# if no_class_hours_dict[sub] == 0:
# subject_batch_dict[(i*2)+2].remove(sub)
# elif course_type_dict[sub] == 'N':
# if subject_credithour_dict[sub]>0:
# slot[i] += sub
# subject_credithour_dict[sub]-=1
# if subject_credithour_dict[sub] == 0:
# subject_batch_dict[(i*2)+2].remove(sub)
# elif course_type_dict[sub] == "L":
# if subject_lab_credithour_dict[sub]>0:
# slot[i] += sub
# subject_lab_credithour_dict[sub]-=1
# if subject_lab_credithour_dict[sub] == 0:
# subject_batch_dict[(i*2)+2].remove(sub)
# return week
# def openChromosome(week):
# oweek = []
# for day in week:
# for slot in day:
# for sub in slot:
# oweek.append(sub)
# return oweek
# def closeChromosome(week):
# cweek = []
# slotting = []
# for i in range(0,len(week),4):
# slotting.append(week[i:i+4])
# for i in range(0,len(slotting),6):
# cweek.append(slotting[i:i+6])
# return cweek
# def popenChromosome(week):
# poweek = []
# for day in week:
# for slot in day:
# poweek.append(slot)
# return poweek
# def pcloseChromosome(slotting):
# pcweek = []
# for i in range(0,len(slotting),6):
# pcweek.append(slotting[i:i+6])
# return pcweek
# # Create a population
# popz = 100
# pop = []
# for i in range(popz):
# pop.append(initializeChromosome())