-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.py
1284 lines (1242 loc) · 59.2 KB
/
generate.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
import os
import math
import random
engine = create_engine("postgres:///tat")
#engine = create_engine("postgres://qqiqlhzydfpsxs:26c1fca3380b9d7b46fd0925b1b1e58fc995d6cccd1f926e9c9c83eda30b5c13@ec2-75-101-131-79.compute-1.amazonaws.com:5432/d1ak4rtao1o18l")
db = scoped_session(sessionmaker(bind=engine))
faculty_lecture_final_map = {}
class Faculty:
def __init__(self):
self.preferences_map = {}
self.max_lectures = {}
self.faculty_slot_map = {}
def fill_the_map(self,faculty_id,preference):
if faculty_id not in self.preferences_map:
self.preferences_map[faculty_id]=[]
self.preferences_map[faculty_id].append(preference)
else:
self.preferences_map[faculty_id].append(preference)
#print(name,row,column)
def printxyz(self):
print(self.preferences_map)
def set_max_lectures(self,faculty_id,max_lec):
self.max_lectures[faculty_id] = max_lec
def get_max_lectures(self,faculty_id):
return self.max_lectures[faculty_id]
def add_faculty_and_slot(self,faculty_id,slot):
self.faculty_slot_map[faculty_id] = slot
class Time_table:
def __init__(self):
self.time_table_map = {}
self.faculties_name = []
self.final_time_table = {}
self.counter = {}
self.max_lec = {}
self.blocked_slot = []
self.faculty_lectures = {}
self.batch = 0
self.unsuccess = 0
def set_max_lectures(self,name,max_lecs):
self.max_lec[name] = max_lecs
def fill_the_map(self,faculty_id,preference):
if preference not in self.time_table_map:
self.time_table_map[preference]=[]
self.time_table_map[preference].append(faculty_id)
else:
self.time_table_map[preference].append(faculty_id)
#print(name,row,column)
def fill_counter(self,name):
self.counter[name] = 0
def add_faculty(self,name):
if name not in self.faculties_name:
self.faculties_name.append(name)
self.faculty_lectures[name] = []
def make_time_table(self):
while_count = 0
while 1:
delete = []
while_count += 1
if while_count >= 100:
#print("Click on generate button again please")
self.unsuccess += 1
break
if while_count >= 10:
#while_count = 0
for name in self.faculties_name:
if self.max_lec[name] == self.counter[name]:
continue
for i in range(1,6):
if self.max_lec[name] == self.counter[name]:
break
for j in range(1,6):
if self.max_lec[name] == self.counter[name]:
break
day = random.randint(1,5)
time = random.randint(1,5)
preference = (day-1)*5 + time
#print(preference)
if preference in self.blocked_slot:
continue
binary = 0
if name in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[name]:
if slot==preference:
binary = 1
break
for k in range(1,6):
temp_preference = (day-1)*5 + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == name:
binary = 1
break
if binary == 0:
if preference not in self.blocked_slot:
self.faculty_lectures[name].append(preference)
self.final_time_table[preference] = name
if name not in faculty_lecture_final_map:
faculty_lecture_final_map[name] = []
faculty_lecture_final_map[name].append((preference,self.batch))
else:
faculty_lecture_final_map[name].append((preference,self.batch))
self.counter[name] += 1
temp = name
self.blocked_slot.append(preference)
#print(temp,self.counter[temp],preference)
for k in range(1,6):
temp_preference = (day-1)*5 + k
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
while_count = 0
delete.append(preference)
cnt = 0
for name in self.faculties_name:
if self.max_lec[name] == self.counter[name]:
cnt += 1
for arr in self.time_table_map:
if name in self.time_table_map[arr]:
self.time_table_map[arr].remove(name)
if cnt == len(self.faculties_name):
break
smallest_first = 0
for arr in self.time_table_map:
if len(self.time_table_map[arr]) == 1:
smallest_first = 1
for arr in self.time_table_map:
if smallest_first == 1:
if len(self.time_table_map[arr]) != 1:
continue
if self.counter[self.time_table_map[arr][0]] == self.max_lec[self.time_table_map[arr][0]]:
continue
binary = 0
if self.time_table_map[arr][0] in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[self.time_table_map[arr][0]]:
if slot==arr:
binary = 1
break
if binary==1:
continue
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == self.time_table_map[arr][0]:
binary = 1
break
if binary==1:
continue
self.faculty_lectures[self.time_table_map[arr][0]].append(arr)
self.final_time_table[arr] = self.time_table_map[arr][0]
self.counter[self.time_table_map[arr][0]] += 1
temp = self.time_table_map[arr][0]
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
else:
faculty_lecture_final_map[temp].append((arr,self.batch))
self.blocked_slot.append(arr)
#while_count = 0
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
delete.append(arr)
else:
flag = 0
minimum = 20
for prof in self.time_table_map[arr]:
if self.counter[prof] == self.max_lec[prof]:
continue
binary = 0
if prof in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[prof]:
if slot==arr:
binary = 1
break
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == prof:
binary = 1
break
if binary==1:
continue
if self.counter[prof] < minimum:
minimum = self.counter[prof]
flag = 1
temp = prof
elif self.counter[prof] == minimum and random.randint(1,5) < 3:
flag = 1
temp = prof
if flag == 1:
#while_count = 0
self.faculty_lectures[temp].append(arr)
self.final_time_table[arr] = temp
self.counter[temp] += 1
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
#print(temp,self.counter[temp],arr)
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
delete.append(arr)
self.blocked_slot.append(arr)
for x in delete:
if x in self.time_table_map:
del self.time_table_map[x]
def set_batch(self,batch):
self.batch = batch
class Time_table_with_slots:
def __init__(self):
self.time_table_map = {}
self.slot_faculty_map = {}
self.slots_name = []
self.final_time_table = {}
self.counter = {}
self.max_lec = {}
self.blocked_slot = []
self.faculty_lectures = {}
self.batch = 0
self.unsuccess = 0
self.preferences = {}
self.weighted_preferences = {}
for i in range(1,6):
for j in range(1,6):
preference = (i-1)*5 + j
self.final_time_table[preference] = []
def set_max_lectures(self,name,max_lecs):
self.max_lec[name] = max_lecs
def fill_the_map(self):
for slot in self.weighted_preferences:
for preference,weight in self.weighted_preferences[slot]:
if preference not in self.time_table_map:
self.time_table_map[preference] = []
self.time_table_map[preference].append((slot,weight))
else:
self.time_table_map[preference].append((slot,weight))
#print(self.time_table_map)
def fill_counter(self,name):
self.counter[name] = 0
def add_slots(self,slot_id):
if slot_id not in self.slots_name:
self.slots_name.append(slot_id)
if slot_id in self.slot_faculty_map:
for faculty in self.slot_faculty_map[slot_id]:
self.faculty_lectures[faculty] = []
#print(self.faculty_lectures)
def make_time_table(self):
#print(self.slots_name)
#print(self.slot_faculty_map)
while_count = 0
while 1:
delete = []
while_count += 1
#print(while_count)
if while_count >= 100:
self.unsuccess += 1
break
if while_count >= 10:
#while_count = 0
for slot in self.slots_name:
#print(slot)
#print(self.counter[slot],slot)
if self.max_lec[slot] == self.counter[slot]:
continue
for i in range(1,6):
if self.max_lec[slot] == self.counter[slot]:
break
for j in range(1,6):
if self.max_lec[slot] == self.counter[slot]:
break
day = random.randint(1,5)
time = random.randint(1,5)
preference = (day-1)*5 + time
#print(preference)
if preference in self.blocked_slot:
continue
binary = 0
for faculty in self.slot_faculty_map[slot]:
if faculty in faculty_lecture_final_map:
for fixed_preference,batch in faculty_lecture_final_map[faculty]:
if fixed_preference==preference:
binary = 1
break
#print(binary)
for k in range(1,6):
temp_preference = (day-1)*5 + k
#print(binary)
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.slot_faculty_map[slot][0] in self.final_time_table[temp_preference]:
binary = 1
#print(self.slot_faculty_map[slot][0],self.final_time_table[temp_preference])
break
#print(binary)
if binary == 0:
if preference not in self.blocked_slot:
for faculty in self.slot_faculty_map[slot]:
self.faculty_lectures[faculty].append(preference)
self.final_time_table[preference].append(faculty)
self.counter[slot] += 1
for faculty in self.slot_faculty_map[slot]:
if faculty not in faculty_lecture_final_map:
faculty_lecture_final_map[faculty] = []
faculty_lecture_final_map[faculty].append((preference,self.batch))
for l in range(1,6):
temp_preference = (day-1)*5 + l
if temp_preference not in self.time_table_map:
continue
for slots,weight in self.time_table_map[temp_preference]:
if slots == slot:
self.time_table_map[temp_preference].remove((slots,weight))
self.blocked_slot.append(preference)
while_count = 0
delete.append(preference)
cnt = 0
for name in self.slots_name:
if self.max_lec[name] == self.counter[name]:
cnt += 1
for arr in self.time_table_map:
for slot,weight in self.time_table_map[arr]:
if name == slot:
self.time_table_map[arr].remove((slot,weight))
if cnt == len(self.slots_name):
break
smallest_first = 0
for arr in self.time_table_map:
if len(self.time_table_map[arr]) == 1:
smallest_first = 1
for arr in self.time_table_map:
if smallest_first == 1:
if len(self.time_table_map[arr]) != 1:
continue
if self.counter[self.time_table_map[arr][0][0]] == self.max_lec[self.time_table_map[arr][0][0]]:
continue
binary = 0
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
if faculty in faculty_lecture_final_map:
for fixed_preference,batch in faculty_lecture_final_map[faculty]:
if fixed_preference==arr:
binary = 1
break
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == faculty:
binary = 1
break
if binary==1:
continue
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
self.faculty_lectures[faculty].append(arr)
self.final_time_table[arr].append(faculty)
self.counter[self.time_table_map[arr][0][0]] += 1
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
temp = faculty
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
temp = self.time_table_map[arr][0][0]
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
for slot,weight in self.time_table_map[temp_preference]:
if temp == slot:
self.time_table_map[temp_preference].remove((slot,weight))
self.blocked_slot.append(arr)
#while_count = 0
delete.append(arr)
else:
flag = 0
temp = -1
maximum = 0
#print(arr)
for slot,weight in self.time_table_map[arr]:
if self.counter[slot] == self.max_lec[slot]:
continue
binary = 0
for faculty in self.slot_faculty_map[slot]:
if faculty in faculty_lecture_final_map:
for fixed_preference,batch in faculty_lecture_final_map[faculty]:
if fixed_preference==arr:
binary = 1
break
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == faculty:
binary = 1
break
if binary==1:
continue
if weight > maximum:
#print(slot)
maximum = weight
flag = 1
temp = slot
elif weight == maximum and random.randint(1,4) < 3:
flag = 1
temp = slot
if flag == 1:
#print(temp)
if temp==-1:
continue
for faculty in self.slot_faculty_map[temp]:
self.faculty_lectures[faculty].append(arr)
self.final_time_table[arr].append(faculty)
self.counter[temp] += 1
for faculty in self.slot_faculty_map[temp]:
if faculty not in faculty_lecture_final_map:
faculty_lecture_final_map[faculty] = []
faculty_lecture_final_map[faculty].append((arr,self.batch))
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
for slot,weight in self.time_table_map[temp_preference]:
if slot == temp:
self.time_table_map[temp_preference].remove((slot,weight))
self.blocked_slot.append(arr)
#while_count = 0
delete.append(arr)
for x in delete:
if x in self.time_table_map:
del self.time_table_map[x]
def set_batch(self,batch):
self.batch = batch
def add_preference(self,slot_id,preference):
if slot_id not in self.preferences:
self.preferences[slot_id] = []
self.preferences[slot_id].append(preference)
def make_weighted_preferences(self):
for slot in self.preferences:
self.weighted_preferences[slot] = []
for slot in self.preferences:
weights_of_preferences = {}
for preference in self.preferences[slot]:
if preference not in weights_of_preferences:
weights_of_preferences[preference] = 1
else:
weights_of_preferences[preference] += 1
for preference in weights_of_preferences:
self.weighted_preferences[slot].append((preference,weights_of_preferences[preference]/len(self.slot_faculty_map[slot])))
def fill_slot_to_faculty_map(self,slot,faculty):
if slot not in self.slot_faculty_map:
self.slot_faculty_map[slot] = []
self.slot_faculty_map[slot].append(faculty)
else:
self.slot_faculty_map[slot].append(faculty)
#print(self.slot_faculty_map)
def btech1():
faculty_map_1 = Faculty()
time_table_1 = Time_table()
unsuccessful = 0
rows = db.execute("SELECT * FROM offers WHERE batch = 1").fetchall()
total_lec = 0
for row in rows:
fac_id = row[0]
lec = db.execute("SELECT lecture FROM courses join offers ON courses.id = offers.course_id AND user_id =:id AND batch = 1",{"id":fac_id}).fetchone()
if lec is None:
continue
lec = lec[0]
total_lec += lec
faculty_map_1.set_max_lectures(fac_id,lec)
time_table_1.set_max_lectures(fac_id,lec)
time_table_1.fill_counter(fac_id)
time_table_1.add_faculty(fac_id)
time_table_1.set_batch(1)
preferences = db.execute("SELECT * FROM preferences WHERE user_id=:id",{"id":fac_id}).fetchall()
#print(preferences)
for preference in preferences:
pref = preference[1]
time_table_1.fill_the_map(fac_id,pref)
faculty_map_1.fill_the_map(fac_id,pref)
time_table_1.make_time_table()
unsuccessful += time_table_1.unsuccess
#print(unsuccessful)
return unsuccessful
def btech2():
faculty_map_2 = Faculty()
time_table_2 = Time_table()
unsuccessful = 0
rows = db.execute("SELECT * FROM offers WHERE batch = 2").fetchall()
total_lec = 0
for row in rows:
fac_id = row[0]
lec = db.execute("SELECT lecture FROM courses join offers ON courses.id = offers.course_id AND user_id =:id AND batch = 2",{"id":fac_id}).fetchone()
if lec is None:
continue
lec = lec[0]
total_lec += lec
faculty_map_2.set_max_lectures(fac_id,lec)
time_table_2.set_max_lectures(fac_id,lec)
time_table_2.fill_counter(fac_id)
time_table_2.add_faculty(fac_id)
time_table_2.set_batch(2)
preferences = db.execute("SELECT * FROM preferences WHERE user_id=:id",{"id":fac_id}).fetchall()
#print(preferences)
for preference in preferences:
pref = preference[1]
time_table_2.fill_the_map(fac_id,pref)
faculty_map_2.fill_the_map(fac_id,pref)
time_table_2.make_time_table()
unsuccessful += time_table_2.unsuccess
#print(unsuccessful)
return unsuccessful
def btech3():
faculty_map_3 = Faculty()
time_table_3 = Time_table_with_slots()
unsuccessful = 0
rows = db.execute("SELECT user_id,slot FROM slots join offers ON slots.course_id = offers.course_id and batch = 3").fetchall()
max_lec = 0
for row in rows:
fac_id = row[0]
slot_id = row[1]
lec = db.execute("SELECT lecture FROM courses join offers ON courses.id = offers.course_id AND user_id =:id AND batch = 3",{"id":fac_id}).fetchone()
if lec is None:
continue
lec = lec[0]
#print(slot_id,fac_id,lec)
max_lec = max(lec,max_lec)
time_table_3.set_max_lectures(slot_id,max_lec)
time_table_3.fill_counter(slot_id)
time_table_3.set_batch(3)
time_table_3.fill_slot_to_faculty_map(slot_id,fac_id)
faculty_map_3.add_faculty_and_slot(fac_id,slot_id)
time_table_3.add_slots(slot_id)
rows = db.execute("SELECT * FROM preferences NATURAL JOIN offers WHERE batch = 3").fetchall()
#print(rows)
for row in rows:
fac_id = row[0]
pref = row[1]
if fac_id not in faculty_map_3.faculty_slot_map:
continue
slot_id = faculty_map_3.faculty_slot_map[fac_id]
time_table_3.add_preference(slot_id,pref)
time_table_3.make_weighted_preferences()
time_table_3.fill_the_map()
time_table_3.make_time_table()
unsuccessful += time_table_3.unsuccess
#print(unsuccessful)
return unsuccessful
def btech4():
faculty_map_4 = Faculty()
time_table_4 = Time_table_with_slots()
unsuccessful = 0
rows = db.execute("SELECT user_id,slot FROM slots join offers ON slots.course_id = offers.course_id and batch = 4").fetchall()
max_lec = 0
for row in rows:
fac_id = row[0]
slot_id = row[1]
lec = db.execute("SELECT lecture FROM courses join offers ON courses.id = offers.course_id AND user_id =:id AND batch = 4",{"id":fac_id}).fetchone()
if lec is None:
continue
lec = lec[0]
print(slot_id,fac_id,lec)
max_lec = max(lec,max_lec)
time_table_4.set_max_lectures(slot_id,max_lec)
time_table_4.fill_counter(slot_id)
time_table_4.set_batch(4)
time_table_4.fill_slot_to_faculty_map(slot_id,fac_id)
faculty_map_4.add_faculty_and_slot(fac_id,slot_id)
time_table_4.add_slots(slot_id)
rows = db.execute("SELECT * FROM preferences NATURAL JOIN offers WHERE batch = 4").fetchall()
#print(rows)
for row in rows:
fac_id = row[0]
pref = row[1]
if fac_id not in faculty_map_4.faculty_slot_map:
continue
slot_id = faculty_map_4.faculty_slot_map[fac_id]
time_table_4.add_preference(slot_id,pref)
time_table_4.make_weighted_preferences()
time_table_4.fill_the_map()
time_table_4.make_time_table()
unsuccessful += time_table_4.unsuccess
#print(time_table_4.unsuccess)
#print(unsuccessful)
return unsuccessful
class Faculty2:
def __init__(self):
self.preferences_map = {}
self.max_lectures = {}
self.faculty_slot_map = {}
def fill_the_map(self,faculty_id,preference):
if faculty_id not in self.preferences_map:
self.preferences_map[faculty_id]=[]
self.preferences_map[faculty_id].append(preference)
else:
self.preferences_map[faculty_id].append(preference)
#print(name,row,column)
def printxyz(self):
print(self.preferences_map)
def set_max_lectures(self,faculty_id,max_lec):
self.max_lectures[faculty_id] = max_lec
def get_max_lectures(self,faculty_id):
return self.max_lectures[faculty_id]
def add_faculty_and_slot(self,faculty_id,slot):
self.faculty_slot_map[faculty_id] = slot
class Time_table2:
def __init__(self):
self.time_table_map = {}
self.faculties_name = []
self.final_time_table = {}
self.counter = {}
self.max_lec = {}
self.blocked_slot = []
self.faculty_lectures = {}
self.batch = 0
self.unsuccess = 0
def set_max_lectures(self,name,max_lecs):
self.max_lec[name] = max_lecs
def fill_the_map(self,faculty_id,preference):
if preference not in self.time_table_map:
self.time_table_map[preference]=[]
self.time_table_map[preference].append(faculty_id)
else:
self.time_table_map[preference].append(faculty_id)
#print(name,row,column)
def fill_counter(self,name):
self.counter[name] = 0
def add_faculty(self,name):
if name not in self.faculties_name:
self.faculties_name.append(name)
self.faculty_lectures[name] = []
def make_time_table(self):
while_count = 0
while 1:
delete = []
while_count += 1
if while_count >= 100:
#print("Click on generate button again please")
self.unsuccess += 1
break
if while_count >= 10:
#while_count = 0
for name in self.faculties_name:
if self.max_lec[name] == self.counter[name]:
continue
for i in range(1,6):
if self.max_lec[name] == self.counter[name]:
break
for j in range(1,6):
if self.max_lec[name] == self.counter[name]:
break
day = random.randint(1,5)
time = random.randint(1,5)
preference = (day-1)*5 + time
#print(preference)
if preference in self.blocked_slot:
continue
binary = 0
if name in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[name]:
if slot==preference:
binary = 1
break
for k in range(1,6):
temp_preference = (day-1)*5 + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == name:
binary = 1
break
if binary == 0:
if preference not in self.blocked_slot:
self.faculty_lectures[name].append(preference)
self.final_time_table[preference] = name
if name not in faculty_lecture_final_map:
faculty_lecture_final_map[name] = []
faculty_lecture_final_map[name].append((preference,self.batch))
else:
faculty_lecture_final_map[name].append((preference,self.batch))
self.counter[name] += 1
temp = name
self.blocked_slot.append(preference)
#print(temp,self.counter[temp],preference)
for k in range(1,6):
temp_preference = (day-1)*5 + k
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
while_count = 0
delete.append(preference)
cnt = 0
for name in self.faculties_name:
if self.max_lec[name] == self.counter[name]:
cnt += 1
for arr in self.time_table_map:
if name in self.time_table_map[arr]:
self.time_table_map[arr].remove(name)
if cnt == len(self.faculties_name):
break
smallest_first = 0
for arr in self.time_table_map:
if len(self.time_table_map[arr]) == 1:
smallest_first = 1
arr = random.choice(list(self.time_table_map.keys()))
if smallest_first == 1:
if len(self.time_table_map[arr]) != 1:
continue
if self.counter[self.time_table_map[arr][0]] == self.max_lec[self.time_table_map[arr][0]]:
continue
binary = 0
if self.time_table_map[arr][0] in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[self.time_table_map[arr][0]]:
if slot==arr:
binary = 1
break
if binary==1:
continue
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == self.time_table_map[arr][0]:
binary = 1
break
if binary==1:
continue
self.faculty_lectures[self.time_table_map[arr][0]].append(arr)
self.final_time_table[arr] = self.time_table_map[arr][0]
self.counter[self.time_table_map[arr][0]] += 1
temp = self.time_table_map[arr][0]
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
else:
faculty_lecture_final_map[temp].append((arr,self.batch))
self.blocked_slot.append(arr)
while_count = 0
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
delete.append(arr)
else:
flag = 0
minimum = 20
for prof in self.time_table_map[arr]:
if self.counter[prof] == self.max_lec[prof]:
continue
binary = 0
if prof in faculty_lecture_final_map:
for slot,batch in faculty_lecture_final_map[prof]:
if slot==arr:
binary = 1
break
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == prof:
binary = 1
break
if binary==1:
continue
if self.counter[prof] < minimum:
minimum = self.counter[prof]
flag = 1
temp = prof
elif self.counter[prof] == minimum and random.randint(1,5) < 3:
flag = 1
temp = prof
if flag == 1:
while_count = 0
self.faculty_lectures[temp].append(arr)
self.final_time_table[arr] = temp
self.counter[temp] += 1
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
#print(temp,self.counter[temp],arr)
for i in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + i
if temp_preference not in self.time_table_map:
continue
if temp in self.time_table_map[temp_preference]:
self.time_table_map[temp_preference].remove(temp)
delete.append(arr)
self.blocked_slot.append(arr)
for x in delete:
if x in self.time_table_map:
del self.time_table_map[x]
def set_batch(self,batch):
self.batch = batch
class Time_table_with_slots2:
def __init__(self):
self.time_table_map = {}
self.slot_faculty_map = {}
self.slots_name = []
self.final_time_table = {}
self.counter = {}
self.max_lec = {}
self.blocked_slot = []
self.faculty_lectures = {}
self.batch = 0
self.unsuccess = 0
self.preferences = {}
self.weighted_preferences = {}
for i in range(1,6):
for j in range(1,6):
preference = (i-1)*5 + j
self.final_time_table[preference] = []
def set_max_lectures(self,name,max_lecs):
self.max_lec[name] = max_lecs
def fill_the_map(self):
for slot in self.weighted_preferences:
for preference,weight in self.weighted_preferences[slot]:
if preference not in self.time_table_map:
self.time_table_map[preference] = []
self.time_table_map[preference].append((slot,weight))
else:
self.time_table_map[preference].append((slot,weight))
#print(self.time_table_map)
def fill_counter(self,name):
self.counter[name] = 0
def add_slots(self,slot_id):
if slot_id not in self.slots_name:
self.slots_name.append(slot_id)
if slot_id in self.slot_faculty_map:
for faculty in self.slot_faculty_map[slot_id]:
self.faculty_lectures[faculty] = []
#print(self.faculty_lectures)
def make_time_table(self):
#print(self.slots_name)
#print(self.slot_faculty_map)
while_count = 0
while 1:
delete = []
while_count += 1
#print(while_count)
if while_count >= 100:
self.unsuccess += 1
break
if while_count >= 10:
#while_count = 0
for slot in self.slots_name:
#print(slot)
#print(self.counter[slot],slot)
if self.max_lec[slot] == self.counter[slot]:
continue
for i in range(1,6):
if self.max_lec[slot] == self.counter[slot]:
break
for j in range(1,6):
if self.max_lec[slot] == self.counter[slot]:
break
day = random.randint(1,5)
time = random.randint(1,5)
preference = (day-1)*5 + time
#print(preference)
if preference in self.blocked_slot:
continue
binary = 0
for faculty in self.slot_faculty_map[slot]:
if faculty in faculty_lecture_final_map:
for fixed_preference,batch in faculty_lecture_final_map[faculty]:
if fixed_preference==preference:
binary = 1
break
#print(binary)
for k in range(1,6):
temp_preference = (day-1)*5 + k
#print(binary)
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.slot_faculty_map[slot][0] in self.final_time_table[temp_preference]:
binary = 1
#print(self.slot_faculty_map[slot][0],self.final_time_table[temp_preference])
break
#print(binary)
if binary == 0:
if preference not in self.blocked_slot:
for faculty in self.slot_faculty_map[slot]:
self.faculty_lectures[faculty].append(preference)
self.final_time_table[preference].append(faculty)
self.counter[slot] += 1
for faculty in self.slot_faculty_map[slot]:
if faculty not in faculty_lecture_final_map:
faculty_lecture_final_map[faculty] = []
faculty_lecture_final_map[faculty].append((preference,self.batch))
for l in range(1,6):
temp_preference = (day-1)*5 + l
if temp_preference not in self.time_table_map:
continue
for slots,weight in self.time_table_map[temp_preference]:
if slots == slot:
self.time_table_map[temp_preference].remove((slots,weight))
self.blocked_slot.append(preference)
while_count = 0
delete.append(preference)
cnt = 0
for name in self.slots_name:
if self.max_lec[name] == self.counter[name]:
cnt += 1
for arr in self.time_table_map:
for slot,weight in self.time_table_map[arr]:
if name == slot:
self.time_table_map[arr].remove((slot,weight))
if cnt == len(self.slots_name):
break
smallest_first = 0
for arr in self.time_table_map:
if len(self.time_table_map[arr]) == 1:
smallest_first = 1
arr = random.choice(list(self.time_table_map.keys()))
if smallest_first == 1:
if len(self.time_table_map[arr]) != 1:
continue
if self.counter[self.time_table_map[arr][0][0]] == self.max_lec[self.time_table_map[arr][0][0]]:
continue
binary = 0
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
if faculty in faculty_lecture_final_map:
for fixed_preference,batch in faculty_lecture_final_map[faculty]:
if fixed_preference==arr:
binary = 1
break
for k in range(1,6):
temp_preference = (arr-1)/5
temp_preference = temp_preference*5
temp_preference = temp_preference + k
if temp_preference in self.blocked_slot and temp_preference in self.final_time_table:
if self.final_time_table[temp_preference] == faculty:
binary = 1
break
if binary==1:
continue
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
self.faculty_lectures[faculty].append(arr)
self.final_time_table[arr].append(faculty)
self.counter[self.time_table_map[arr][0][0]] += 1
for faculty in self.slot_faculty_map[self.time_table_map[arr][0][0]]:
temp = faculty
if temp not in faculty_lecture_final_map:
faculty_lecture_final_map[temp] = []
faculty_lecture_final_map[temp].append((arr,self.batch))
temp = self.time_table_map[arr][0][0]
for i in range(1,6):