-
Notifications
You must be signed in to change notification settings - Fork 12
/
Work_Scheduling_our_alg_new.py
1684 lines (1536 loc) · 70.2 KB
/
Work_Scheduling_our_alg_new.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re, time
import numpy as np
import pandas as pd
import random as rd
import tool.functions.gene_alg_new as gen
#from tool.functions.CSR_order import CSR_ORDER
from tool.functions.LIMIT_ORDER import LIMIT_ORDER
from tool.functions.CONFIRM import confirm
from tool.final_score import final_score
from tool.tool import ERROR
import tool.tool as tl
import datetime, calendar, sys, copy
#========================================================================#
# Global Variables
#========================================================================#
# 產生親代的迴圈數
parent = 100 # int
ordernum = 1 #limit_order的排序數量
cutpoint = 5 #不同安排方式的切分點
#基因演算法的世代數量
generation = 1000
mutate_prob = 0.05
shuffle = False
LACK = tl.nE
SURPLUS = tl.nE * 0.5
NIGHT = tl.P2_t
BREAKCOUNT = tl.nE * tl.nW
NOON = tl.P4_t
IGNORE = True
# 生成Initial pool的100個親代
INITIAL_POOL = []
#=======================================================================================================#
#=======================================================================================================#
tstart_0 = time.time() #計時用
#測試檔案檔名 - 沒有要測試時請將TestPath留空白
# TestPath = ""
EmployeeTest = tl.EmployeeTest
AssignTest = tl.AssignTest
NeedTest = tl.NeedTest
U_ttest = tl.U_ttest
# =============================================================================#
# Parameters
# =============================================================================#
dir_name = tl.DIR
# -------Time-------#
year = tl.YEAR
month = tl.MONTH
# -------number-------#
nEMPLOYEE = tl.nE #總員工人數
nDAY = tl.nD #總日數
nK = tl.nK #班別種類數
nSK = tl.nSK #技能種類數
nT = tl.nT #總時段數
nR = tl.nR #午休種類數
nW = tl.nW #總週數
mDAY = tl.mDAY
miniresult = 100000000*nEMPLOYEE*nDAY*nT #親代最佳分數
# -----基礎項目---------#
P0, P1, P2, P3, P4 = tl.P #目標式中的調整權重(lack, surplus, nightCount, breakCount, noonCount)
timelimit = tl.TIME_LIMIT
#timelimit = 1000000000
Posi = tl.POSI_list
# -------表格---------#
CONTAIN = tl.CONTAIN #CONTAIN_kt - 1表示班別k包含時段t,0則否
DEMAND = tl.DEMAND #DEMAND_jt - 日子j於時段t的需求人數
ASSIGN = tl.ASSIGN #ASSIGN_ijk - 員工i指定第j天須排班別k,形式為 [(i,j,k)]
assign_par = tl.assign_par
EMPLOYEE_t = tl.Employee_t
E_NAME = tl.NAME_list
# -------list---------#
LMNIGHT = tl.LastWEEK_night #LMNIGHT_i - 表示員工i在上月終未滿一週的日子中曾排幾次晚班
FRINIGHT = tl.LastDAY_night #FRINIGHT_i - 1表示員工i在上月最後一工作日排晚班,0則否
nightdaylimit = EMPLOYEE_t['night_perWeek']
Shift_name = tl.CLASS_list
SKILL_list = tl.SKILL_list
# -----排班特殊限制-----#
LOWER = tl.LOWER #LOWER - 日期j,班別集合ks,職位p,上班人數下限
UPPER = tl.UPPER #UPPER - 員工i,日子集合js,班別集合ks,排班次數上限
PERCENT = tl.PERCENT #PERCENT - 日子集合,班別集合,要求占比,年資分界線
SKILL = tl.NOTPHONE_CLASS
SKILL_SPECIAL = tl.NOTPHONE_CLASS_special
# -----新 特殊班別一定人數--------------#
NOTPHONE_CLASS = tl.NOTPHONE_CLASS # 特殊班別每天人數相同
NOTPHONE_CLASS_special = tl.NOTPHONE_CLASS_special # 特殊班別假日後一天人數不同
Upper_shift = tl.Upper_shift # 特殊班別每人排班上限
# =============================================================================#
# Sets
# =============================================================================#
EMPLOYEE = [tmp for tmp in range(nEMPLOYEE)] #EMPLOYEE - 員工集合,I=0,…,nI
DAY = [tmp for tmp in range(nDAY)] #DAY - 日子集合,J=0,…,nJ-1
TIME = [tmp for tmp in range(nT)] #TIME - 工作時段集合,T=1,…,nT
BREAK = [tmp for tmp in range(nR)] #BREAK - 午休方式,R=1,…,nR
WEEK = [tmp for tmp in range(nW)] #WEEK - 週次集合,W=1,…,nW
SHIFT = [tmp for tmp in range(nK)] #SHIFT - 班別種類集合,K=1,…,nK ;0代表休假
# -------員工集合-------#
E_POSITION = tl.E_POSI_set #E_POSITION - 擁有特定職稱的員工集合,POSI=1,…,nPOSI
E_SKILL = tl.E_SKILL_set #E_SKILL - 擁有特定技能的員工集合,SKILL=1,…,nSKILL
E_SENIOR = tl.E_SENIOR_set #E_SENIOR - 達到特定年資的員工集合
# -------日子集合-------#
DATES = tl.DATE_list
DAYset = tl.D_WDAY_set #DAYset - 通用日子集合 [all,Mon,Tue...]
D_WEEK = tl.D_WEEK_set
WEEK_of_DAY = tl.WEEK_list #WEEK_of_DAY - 日子j所屬的那一週
VACnextdayset = tl.AH_list #VACnextdayset - 假期後或週一的日子集合
NOT_VACnextdayset = tl.NAH_list
# -------班別集合-------#
SHIFTset= tl.K_CLASS_set #SHIFTset - 通用的班別集合,S=1,…,nS
SKILLset= tl.SK_CLASS_set #SKILLset - 技能與班別的對應集合,SK=1,…,nSK
S_NIGHT = SHIFTset['night'] #S_NIGHT - 所有的晚班
S_NOON = SHIFTset['noon'] #S_NOON - 所有的午班
S_BREAK =tl.K_BREAK_set
#============================================================================#
#Variables
work = {} #work_ijk - 1表示員工i於日子j值班別為k的工作,0 則否 ;workij0=1 代表員工i在日子j休假
for i in range(nEMPLOYEE):
for j in range(nDAY):
for k in range(nK):
work[i, j, k] = False
#Test Variables
lack = {} #y_jt - 代表第j天中時段t的缺工人數
for j in range(nDAY):
for t in range(nT):
lack[j, t] = 0
surplus = 0 #每天每個時段人數與需求人數的差距中的最大值
nightCount = 0 #員工中每人排晚班總次數的最大值
breakCount = {} #breakCount_iwr - 1表示員工i在第w周中在午休時段r有午休,0則否
for i in range(nEMPLOYEE):
for w in range(nW):
for r in range(nR):
breakCount[i, w, r] = False
noonCount = 0 #員工中每人排午班總次數的最大值
#========================================================================#
# class
#========================================================================#
class Pool():
def __init__(self, result, df_x1):
#result: 目標式結果
self.result = result
#df_x1 : 員工班表(整數班別)
self.df_x1 = df_x1
#========================================================================#
# ABLE(i,j,k): 確認員工i在日子j是否可排班別k
#========================================================================#
def ABLE(this_i,this_j,this_k,consider=False):
ans = True
#only one work a day
for k in SHIFT:
if(work[this_i,this_j,k] == 1) and (k != this_k):
ans = False
return ans
#被指定的排班及當天被排除的排班
for tmp in ASSIGN:
if(tmp[0]==this_i):
if(tmp[1]==this_j):
#被指定的排班
if(tmp[2]==this_k):
return ans
else:
ans = False
return ans
#判斷是否正在排晚班
arrangenightshift = False
for tmp in S_NIGHT:
if(this_k == tmp):
arrangenightshift = True
#正在排晚班才進去判斷
if(arrangenightshift == True):
#no continuous night shift:
if(this_j!=0 and this_j!=nDAY-1): #非第一天或最後一天
for tmp in S_NIGHT:
if(work[this_i,this_j-1,tmp] == 1):
ans = False
return ans
if(work[this_i,this_j+1,tmp] == 1):
ans = False
return ans
elif (this_j==nDAY-1): #最後一天
for tmp in S_NIGHT:
if(work[this_i,this_j-1,tmp] == 1):
ans = False
return ans
else: #第一天
if(FRINIGHT[this_i] == 1):
ans = False
return ans
for tmp in S_NIGHT:
if(work[this_i,this_j+1,tmp] == 1):
ans = False
return ans
#no too many night shift a week:
whichweek = WEEK_of_DAY[this_j]
#非第一週
if(whichweek!=0):
countnightshift = 0
for theday in D_WEEK[whichweek]:
for tmp in S_NIGHT:
if(work[this_i,theday,tmp] == 1):
if(theday == this_j and tmp == this_k):
countnightshift += 0
else:
countnightshift += 1
if(countnightshift >= nightdaylimit[this_i]):
ans = False
return ans
#第一週
else:
countnightshift = 0
for theday in D_WEEK[0]:
for tmp in S_NIGHT:
if(work[this_i,theday,tmp] == 1):
if(theday == this_j and tmp == this_k):
countnightshift += 0
else:
countnightshift += 1
if(countnightshift+LMNIGHT[this_i] >= nightdaylimit[this_i]):
ans = False
return ans
#排班的上限
for item in UPPER:
if(this_i == item[0] and this_j in DAYset[item[1]] and this_k in SHIFTset[item[2]]):
tmpcount = 0
for whichday in DAYset[item[1]]:
for tmp in SHIFTset[item[2]]:
if(work[this_i,whichday,tmp]==1):
if(whichday == this_j and tmp == this_k):
tmpcount+=0
else:
tmpcount+=1
if(tmpcount>=item[3]):
ans = False
return ans
# 若在非ASSIGN情況下不能排AS、MS、O班
if this_k in SHIFTset['not_assigned']:
if(assign_par[this_i][this_j][this_k]==0):
ans = False
return ans
#排特殊技能班別的上限
#有每月總數上限
for item in Upper_shift:
if(this_k in SHIFTset[item[0]]):
if(this_i in E_SKILL['phone']):
tmpcount = 0
for whichday in DAY:
if(work[this_i,whichday,this_k]==1):
if(whichday == this_j):
tmpcount+=0
else:
tmpcount+=1
if(tmpcount>=item[1]):
ans = False
return ans
else: #無此技能
ans = False
return ans
#特殊技能排班
for item in NOTPHONE_CLASS:
if(this_k in SHIFTset[item[0]]):
if(this_i in E_SKILL[item[2]]):
tmpcount = 0
for people in EMPLOYEE:
if(work[people,this_j,this_k]==1):
if(people == this_i):
tmpcount+=0
else:
tmpcount+=1
if(tmpcount>=item[1]):
ans = False
return ans
else: #無此技能
ans = False
return ans
#特殊技能排班(有假日後要多人的限制)
for item in NOTPHONE_CLASS_special:
if(this_k in SHIFTset[item[0]]):
if(this_i in E_SKILL[item[2]]):
tmpcount = 0
for people in EMPLOYEE:
if(work[people,this_j,this_k]==1):
if(people == this_i):
tmpcount+=0
else:
tmpcount+=1
if(this_j in VACnextdayset):
if(tmpcount>=item[3]):
ans = False
return ans
else:
if(tmpcount>=item[1]):
ans = False
return ans
else: #無此技能
ans = False
return ans
#無特殊技能者不得排特殊技能班
for item in SKILL_list:
if (this_k in SKILLset[item]):
if (this_i not in E_SKILL[item]):
ans = False
else:
ans = True
break
if ans == False:
return ans
#年資限制
if consider == True:
for i in range(len(PERCENT)):
item = PERCENT[i]
if (this_k in SHIFTset[item[1]]):
if (this_j in DAYset[item[0]]):
if (this_i not in E_SENIOR[i]):
countS = 0
countT = 0
for s in E_SENIOR[i]:
if work[s,this_j,this_k] == True:
countS += 1
for t in EMPLOYEE:
if work[t,this_j,this_k] == True:
countT += 1
if (countS/(countT+1) < item[2]):
ans = False
return ans
return ans
def REPEAT(this_i,this_j,this_k): #一次安排可滿足多條限制式時使用
ans = False
for k in SHIFT:
if(work[this_i,this_j,k] == 1) and (k == this_k):
ans = True
return ans
#========================================================================#
# GENE(): 切分並交配的函數
#========================================================================#
def GENE(timelimit, available_sol, fix, generation, per_month_dir=tl.DIR_PER_MONTH, posibility = mutate_prob):
return gen.gene_alg(timelimit, available_sol, fix, generation, per_month_dir, posibility = posibility)
#========================================================================#
# SHIFT_ORDER(): 班別排序的函數
#========================================================================#
def takeNeck(alist):
try:
return alist[-1]
except:
print('找不到項目 ',end='')
print(alist,end='')
print(' 的瓶頸程度參數')
return None
def SHIFT_ORDER(demand, shift, day, sumlack_t, maxsurplus, maxnight, sumbreak_t, maxnoon, csr, arranged):
ans = []
for c in csr:
for a in range(len(day)):
if arranged[c][day[a]] == True:
continue
else:
for i in shift:
if ABLE(c,day[a],i)==False:
continue
sumlack = copy.deepcopy(sumlack_t)
sumbreak = copy.deepcopy(sumbreak_t)
maxsurplus_t = copy.deepcopy(maxsurplus)
maxnight_t = copy.deepcopy(maxnight)
maxnoon_t = copy.deepcopy(maxnoon)
demand_t = []
demand_t.extend(demand[a])
for t in range(nT):
if CONTAIN[i][t] == 1:
demand_t[t] -=1
if demand_t[t] >= 0:
sumlack -= 1
dem_s = np.array(demand_t)
for j in range(len(dem_s)):
if dem_s[j] > 0:
dem_s[j] = 0
if min(dem_s)*(-1) >= maxsurplus:
maxsurplus_t += 1
if i in S_NIGHT:
if nightdaylimit[c] > 0:
ni = 0
for y in range(nDAY):
for z in S_NIGHT:
if work[c,y,z] == True:
ni += 1
break
ni = ni / nightdaylimit[c]
if ni >= maxnight:
maxnight_t += 1
else:
maxnight_t = 1000000*nEMPLOYEE*nDAY*nT
elif i in S_NOON:
no = 0
for y in range(nDAY):
for z in S_NOON:
if work[c,y,z] == True:
no += 1
break
if no >= maxnoon:
maxnoon_t += 1
for w in WEEK:
if day[a] in D_WEEK[w]:
for br in BREAK:
if i in S_BREAK[br]:
if breakCount[c,w,br] == False:
sumbreak += 1
break
break
d = P0 * (sumlack+1000000*(sumlack>LACK)*(sumlack-LACK)) + \
P1 * (maxsurplus_t+1000000*(maxsurplus_t>SURPLUS)*(maxsurplus_t-SURPLUS)) + \
P2 * (maxnight_t+1000000*(maxnight_t>NIGHT)*(maxnight_t-NIGHT)) +\
P3 * (sumbreak+1000000*(sumbreak>BREAKCOUNT)*(sumbreak-BREAKCOUNT)) +\
P4 * (maxnoon_t+1000000*(maxnoon_t>NOON)*(maxnoon_t-NOON))
ans.append([c,day[a],i,d])
ans.sort(key=takeNeck, reverse=False)
return ans
def LIMIT_CSR_SHIFT_ORDER(TYPE, demand, shift_list, day, sumlack_t, maxsurplus, maxnight, sumbreak_t, maxnoon, csr_list, skilled):
ans = []
rd.shuffle(csr_list)
for i in csr_list:
for s in shift_list:
sumlack = copy.deepcopy(sumlack_t)
sumbreak = copy.deepcopy(sumbreak_t)
maxsurplus_t = copy.deepcopy(maxsurplus)
maxnight_t = copy.deepcopy(maxnight)
maxnoon_t = copy.deepcopy(maxnoon)
demand_t = []
demand_t.extend(demand)
for t in range(nT):
if CONTAIN[s][t] == 1:
demand_t[t] -=1
if demand_t[t] >= 0:
sumlack -= 1
dem_s = np.array(demand_t)
for j in range(len(dem_s)):
if dem_s[j] > 0:
dem_s[j] = 0
if min(dem_s)*(-1) >= maxsurplus:
maxsurplus_t += 1
if s in S_NIGHT:
if nightdaylimit[i] > 0:
ni = 0
for y in range(nDAY):
for z in S_NIGHT:
if work[i,y,z] == True:
ni += 1
break
ni = ni / nightdaylimit[i]
if ni >= maxnight:
maxnight_t += 1
else:
maxnight_t = 1000000*nEMPLOYEE*nDAY*nT
elif s in S_NOON:
no = 0
for y in range(nDAY):
for z in S_NOON:
if work[i,y,z] == True:
no += 1
break
if no >= maxnoon:
maxnoon_t += 1
for w in WEEK:
if day in D_WEEK[w]:
for br in BREAK:
if s in S_BREAK[br]:
if breakCount[i,w,br] == False:
sumbreak += 1
break
break
d = P0 * (sumlack+1000000*(sumlack>LACK)*(sumlack-LACK)) + \
P1 * (maxsurplus_t+1000000*(maxsurplus_t>SURPLUS)*(maxsurplus_t-SURPLUS)) + \
P2 * (maxnight_t+1000000*(maxnight_t>NIGHT)*(maxnight_t-NIGHT)) +\
P3 * (sumbreak+1000000*(sumbreak>BREAKCOUNT)*(sumbreak-BREAKCOUNT)) +\
P4 * (maxnoon_t+1000000*(maxnoon_t>NOON)*(maxnoon_t-NOON))
for oth in SKILL:
if s == SHIFTset[oth[0]][0]:
if i in E_SKILL[oth[2]]:
d = 0
break
for oth in SKILL_SPECIAL:
if s == SHIFTset[oth[0]][0]:
if i in E_SKILL[oth[2]]:
d = 0
break
ans.append([i,s,d])
ans.sort(key=takeNeck, reverse=False)
return ans
def SPECIAL_CSR_ORDER(shift, day, maxnight, sumbreak_t, maxnoon, csr_list):
ans = []
rd.shuffle(csr_list)
for i in csr_list:
sumbreak = copy.deepcopy(sumbreak_t)
maxnight_t = copy.deepcopy(maxnight)
maxnoon_t = copy.deepcopy(maxnoon)
if shift in S_NIGHT:
if nightdaylimit[i] > 0:
ni = 0
for y in range(nDAY):
for z in S_NIGHT:
if work[i,y,z] == True:
ni += 1
break
ni = ni / nightdaylimit[i]
if ni >= maxnight:
maxnight_t += 1
else:
maxnight_t = 1000000*nEMPLOYEE*nDAY*nT
elif shift in S_NOON:
no = 0
for y in range(nDAY):
for z in S_NOON:
if work[i,y,z] == True:
no += 1
break
if no >= maxnoon:
maxnoon_t += 1
for w in WEEK:
if day in D_WEEK[w]:
for br in BREAK:
if shift in S_BREAK[br]:
if breakCount[i,w,br] == False:
sumbreak += 1
break
break
d = P2 * (maxnight_t+1000000*(maxnight_t>NIGHT)*(maxnight_t-NIGHT)) +\
P3 * (sumbreak+1000000*(sumbreak>BREAKCOUNT)*(sumbreak-BREAKCOUNT)) +\
P4 * (maxnoon_t+1000000*(maxnoon_t>NOON)*(maxnoon_t-NOON))
ans.append([i,d])
ans.sort(key=takeNeck, reverse=False)
if shuffle == True:
rd.shuffle(ans)
return ans
def DAY_ORDER(day, demand_list):
ans = []
for i in range(len(day)):
#sumlack = copy.deepcopy(sumlack_t)
#maxsurplus_t = copy.maxsurplus
demand_t = []
demand_t.extend(demand_list[i])
dem_l = np.array(demand_t)
dem_s = np.array(demand_t)
dem_su = 0
for j in range(len(dem_l)):
if dem_l[j] < 0:
dem_l[j] = 0
for j in range(len(dem_s)):
if dem_s[j] > 0:
dem_s[j] = 0
if min(dem_s)*(-1) == maxsurplus:
dem_su = 0
else:
dem_su = 1
d = P0 * np.sum(dem_l) + P1 * (1-dem_su)
ans.append([day[i],d])
ans.sort(key=takeNeck, reverse=True)
return ans
def CSR_ORDER(what_order,CSR_List,EMPLOYEE, Posi, nightbound, day=-1, shift=-1, lower_not_ended=True):
EMPLOYEE_t = EMPLOYEE.copy()
## 以員工技能少、年資低、職位低為優先
if (what_order == "lower"):
nEMPLOYEE = EMPLOYEE_t.shape[0]
available_CSR = len(CSR_List)
index = range(0,1)
small_dataframe = pd.DataFrame(index=index,columns=EMPLOYEE_t.columns)
## 把職位轉為數字以便排優先順序
for i in range (nEMPLOYEE) :
if nightbound == True:
EMPLOYEE_t.at[i,'night_perWeek'] = 7 - EMPLOYEE_t.iloc[i,9]
for j in range(len(Posi)):
if (EMPLOYEE_t.iloc[i,4] == Posi[j]):
EMPLOYEE_t.at[i,'Position'] = j
break
temp_dataframe = EMPLOYEE_t.iloc[:,[0,3,4,5,6,7,8,9]]
for i in range (nEMPLOYEE) :
for j in range (available_CSR):
if (CSR_List[j] == int(temp_dataframe.index.values[i])):
small_dataframe = pd.concat([temp_dataframe.iloc[[i],:],small_dataframe],sort = False)
sort_order = []
existed = False
for i in SKILL:
for j in sort_order:
if 'skill-'+str(i[2]) == j[0]:
existed = True
j[1] = j[1] + 1
if existed == True:
existed = False
continue
sort_order.append(['skill-'+str(i[2]),i[1],len(E_SKILL[i[2]])])
for i in SKILL_SPECIAL:
for j in sort_order:
if 'skill-'+str(i[2]) == j[0]:
existed = True
j[1] = j[1] + 1
if existed == True:
existed = False
continue
sort_order.append(['skill-'+str(i[2]),i[1]*0.8+i[3]*0.2,len(E_SKILL[i[2]])])
for j in sort_order:
j[2] = j[2]/j[1]
sort_order.sort(key=takeNeck, reverse=False)
sort_t = []
for j in sort_order:
sort_t.append(j[0])
sort_t.extend(['night_perWeek','Senior','Position'])
small_dataframe = small_dataframe.dropna(thresh=2)
sorted_dataframe = small_dataframe.sort_values(sort_t,ascending = True)
newCSR_List = list()
for i in range (len(sorted_dataframe)):
newCSR_List.append(int(sorted_dataframe.index.values[i]))
return (newCSR_List)
## 以技能少、職位低、年資低為優先
elif (what_order == "ratio"):
nEMPLOYEE = EMPLOYEE_t.shape[0]
available_CSR = len(CSR_List)
for i in range (nEMPLOYEE) :
if nightbound == True:
EMPLOYEE_t.at[i,'night_perWeek'] = 7 - EMPLOYEE_t.iloc[i,9]
temp_dataframe = EMPLOYEE_t.iloc[:,[0,3,4,5,6,7,8,9]]
index = range(0,1)
small_dataframe = pd.DataFrame(index=index,columns=EMPLOYEE_t.columns)
for i in range (nEMPLOYEE) :
for j in range (available_CSR):
if (CSR_List[j] == int(temp_dataframe.index.values[i])):
small_dataframe = pd.concat([temp_dataframe.iloc[[i],:],small_dataframe],sort = False)
sort_order = []
existed = False
for i in SKILL:
for j in sort_order:
if 'skill-'+str(i[2]) == j[0]:
existed = True
j[1] = j[1] + 1
if existed == True:
existed = False
continue
sort_order.append(['skill-'+str(i[2]),i[1],len(E_SKILL[i[2]])])
for i in SKILL_SPECIAL:
for j in sort_order:
if 'skill-'+str(i[2]) == j[0]:
existed = True
j[1] = j[1] + 1
if existed == True:
existed = False
continue
sort_order.append(['skill-'+str(i[2]),i[1]*0.8+i[3]*0.2,len(E_SKILL[i[2]])])
for j in sort_order:
j[2] = j[2]/j[1]
sort_order.sort(key=takeNeck, reverse=False)
sort_t = []
for j in sort_order:
sort_t.append(j[0])
sort_t.extend(['night_perWeek','Senior','Position'])
small_dataframe = small_dataframe.dropna(thresh=2)
sorted_dataframe = small_dataframe.sort_values(sort_t,ascending = True)
newCSR_List = list()
for i in range (len(sorted_dataframe)):
newCSR_List.append(int(sorted_dataframe.index.values[i]))
return (newCSR_List)
## 技能員工當中先排年資淺再排職位低的員工
elif (what_order == "skill" or what_order == "skill_special"):
nEMPLOYEE = EMPLOYEE_t.shape[0]
available_CSR = len(CSR_List)
#temp_dataframe = EMPLOYEE_t.iloc[:,[0,3]]
index = range(0,1)
small_dataframe = pd.DataFrame(index=index,columns=EMPLOYEE_t.columns)
## 把職位轉為數字以便排優先順序
for i in range (nEMPLOYEE) :
#if nightbound == True:
# EMPLOYEE_t.at[i,'night_perWeek'] = 7 - EMPLOYEE_t.iloc[i,9]
for j in range(len(Posi)):
if (EMPLOYEE_t.iloc[i,4] == Posi[j]):
EMPLOYEE_t.at[i,'Position'] = j
break
temp_dataframe = EMPLOYEE_t.iloc[:,[0,3,4,6,7,8,9]]
for i in range (nEMPLOYEE) :
for j in range (available_CSR):
if (CSR_List[j] == int(temp_dataframe.index.values[i])):
small_dataframe = pd.concat([temp_dataframe.iloc[[i],:],small_dataframe],sort = False)
asc = False
if lower_not_ended:
asc = True
elif shift in SKILLset['phone']:
asc = True
else:
for i in Upper_shift:
for j in range(len(Shift_name)):
if i[0]==Shift_name[j] and j==shift:
for l in CSR_List:
for k in LOWER:
if l in E_POSITION[k[2]] and int(k[0])==day and (len(E_POSITION[k[2]])-int(k[3])==0):
asc = True
break
if asc == True:
break
break
small_dataframe = small_dataframe.dropna(thresh=2)
sorted_dataframe = small_dataframe.sort_values(['Position','Senior','night_perWeek'],ascending = asc)
newCSR_List = list()
for i in range (len(sorted_dataframe)):
newCSR_List.append(int(sorted_dataframe.index.values[i]))
return (newCSR_List)
#=======================================================================================================#
#====================================================================================================#
#=================================================================================================#
# main function
#=================================================================================================#
#====================================================================================================#
#=======================================================================================================#
LIMIT_MATRIX = LIMIT_ORDER(ordernum, IGNORE) #生成多組限制式matrix
#print(LIMIT_MATRIX)
sequence = 0 #限制式順序
#char = 'a' #CSR沒用度順序
fix = [] #存可行解的哪些部分是可以動的
#迴圈計時
tStart = time.time()
#成功數計算
success = 0
#產生100個親代的迴圈
for p in range(parent):
ordercount = (p)%1+1 #每重算一次SHIFT_SET的排序數
maxnight = 0
maxnoon = 0
maxsurplus = 0
unconfimed = False
lne = True
skilled = {}
for j in DAY:
for k in SHIFTset['all']:
skilled[Shift_name[k],j] = []
#初始缺工人數
sumlack_t = 0
for j in range(nDAY):
for t in range(nT):
sumlack_t += DEMAND[j][t]
#初始午休次數
sumbreak_t = 0
#動態需工人數
CURRENT_DEMAND = [tmp for tmp in range(nDAY)]
for j in DAY:
CURRENT_DEMAND[j] = []
for t in range(nT):
CURRENT_DEMAND[j].append(DEMAND[j][t])
#指定班別
for c in ASSIGN:
if ABLE(c[0],c[1],c[2]) == False:
unconfimed = True
work[c[0],c[1],c[2]] = True
for w in WEEK:
if c[1] in D_WEEK[w]:
for br in BREAK:
if c[2] in S_BREAK[br]:
if breakCount[c[0],w,br] == False:
breakCount[c[0],w,br] = True
sumbreak_t += 1
break
break
if c[2] in SHIFTset['phone']: #非其他班別時扣除需求
for t in range(nT):
if CONTAIN[c[2]][t] == 1:
CURRENT_DEMAND[c[1]][t] -= 1
if CURRENT_DEMAND[c[1]][t] >= 0:
sumlack_t -= 1
demand = []
demand.extend(CURRENT_DEMAND[c[1]])
for q in range(len(demand)):
if demand[q] > 0:
demand[q] = 0
if min(demand)*(-1) > maxsurplus:
maxsurplus = min(demand)*(-1)
if c[2] in S_NIGHT:
ni = 0
for j in range(nDAY):
for k in S_NIGHT:
if work[c[0],j,k] == True:
ni += 1
break
ni = ni / nightdaylimit[c[0]]
if ni > maxnight:
maxnight = ni
elif c[2] in S_NOON:
no = 0
for y in range(nDAY):
for z in S_NOON:
if work[c[0],y,z] == True:
no += 1
break
if no > maxnoon:
maxnoon = no
#瓶頸排班
LIMIT_LIST = LIMIT_MATRIX[sequence] #一組限制式排序
LIMIT = [] #一條限制式
CSR_LIST = [] #可排的員工清單
BOUND = [] #限制人數
for l in range(len(LIMIT_LIST)):
LIMIT = LIMIT_LIST[l]
nightbound = False
#print(LIMIT)
for n in S_NIGHT:
if LIMIT[3][0] == n:
nightbound = True
break
CSR_LIST = CSR_ORDER(LIMIT[0], LIMIT[1], EMPLOYEE_t, Posi, nightbound) #員工沒用度排序
#rd.shuffle(CSR_LIST)
demand_list = []
for m in LIMIT[2]:
demand_list.append(CURRENT_DEMAND[m])
DAY_SET = DAY_ORDER(LIMIT[2], demand_list)
DAY_LIST = []
for h in range(len(DAY_SET)):
DAY_LIST.append(DAY_SET[h][0])
for j in DAY_LIST:
if LIMIT[0] == 'lower' :
lne = False
BOUND = LIMIT[4]
#for i in CSR_LIST:
DAY_DEMAND = []
DAY_DEMAND.extend(CURRENT_DEMAND[j])
LOWER_SET = LIMIT_CSR_SHIFT_ORDER(LIMIT[0], DAY_DEMAND, LIMIT[3], j, sumlack_t, maxsurplus, maxnight, sumbreak_t, maxnoon, CSR_LIST, skilled)
for x in range(len(LOWER_SET)):
if BOUND <= 0:
break
i = LOWER_SET[x][0]
k = LOWER_SET[x][1]
if k in SHIFTset['not_assigned']:
continue
for oth in SKILL:
if k == SHIFTset[oth[0]][0]:
if skilled[Shift_name[k],j] != []:
for sk in skilled[Shift_name[k],j]:
if sk in CSR_LIST:
BOUND -= 1
break
for oth in SKILL_SPECIAL:
if k == SHIFTset[oth[0]][0]:
if skilled[Shift_name[k],j] != []:
for sk in skilled[Shift_name[k],j]:
if sk in CSR_LIST:
BOUND -= 1
break
for ra in PERCENT:
if j in DAYset[ra[0]]:
if k in SHIFTset[ra[1]]:
if skilled[Shift_name[k],j] != []:
for sk in skilled[Shift_name[k],j]:
if sk in CSR_LIST:
BOUND -= 1
if BOUND <= 0:
break
if ABLE(i, j, k) == True: #若此人可以排此班,就排
repeat = False
if REPEAT(i, j, k) == True:
repeat = True
work[i, j, k] = True
for w in WEEK:
if j in D_WEEK[w]:
for br in BREAK:
if k in S_BREAK[br]:
if breakCount[i,w,br] == False:
breakCount[i,w,br] = True
sumbreak_t += 1
break
break
if k in SHIFTset['phone'] and repeat == False: #非其他班別時扣除需求
for t in range(nT):
if CONTAIN[k][t] == 1:
CURRENT_DEMAND[j][t] -= 1
if CURRENT_DEMAND[j][t] >= 0:
sumlack_t -= 1
demand = []
demand.extend(CURRENT_DEMAND[j])
for q in range(len(demand)):
if demand[q] > 0:
demand[q] = 0
if min(demand)*(-1) > maxsurplus:
maxsurplus = min(demand)*(-1)
if k in S_NIGHT:
ni = 0
for y in range(nDAY):
for z in S_NIGHT:
if work[i,y,z] == True:
ni += 1
break
ni = ni / nightdaylimit[i]
if ni > maxnight:
maxnight = ni
elif k in S_NOON:
no = 0
for y in range(nDAY):
for z in S_NOON:
if work[i,y,z] == True:
no += 1
break
if no > maxnoon:
maxnoon = no
BOUND -= 1
skilled[Shift_name[k],j].append(i)
else:
continue
if BOUND > 0:
unconfimed = True
continue
elif LIMIT[0] == 'ratio':
DAY_DEMAND = []
DAY_DEMAND.extend(CURRENT_DEMAND[j])
RATIO_SET = LIMIT_CSR_SHIFT_ORDER(LIMIT[0], DAY_DEMAND, LIMIT[3], j, sumlack_t, maxsurplus, maxnight, sumbreak_t, maxnoon, CSR_LIST, skilled)