-
Notifications
You must be signed in to change notification settings - Fork 2
/
prs_day_by_csv.py
86 lines (76 loc) · 3.85 KB
/
prs_day_by_csv.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
import numpy as np
import pandas as pd
from libs import GA_dataframes as ga
# -----------------------Query for gene pivoted-------------------------------#
chromosom_df = pd.read_csv('C:/MyFiles/Projects/DM/sample files/output.csv')
chromosom_df = pd.pivot_table(chromosom_df, values='SHIFTID',
index=['PersonnelBaseId'],
columns=['PersianDayOfMonth'], aggfunc=np.sum)
# -----------------------Query for personnel info-----------------------------#
personnel_df = pd.read_csv('C:/MyFiles/Projects/DM/sample files/personnel.csv')
personnel_df = personnel_df.set_index('PersonnelBaseId')
# -----------------------Query for shift info---------------------------------#
shift_df = pd.read_csv('C:/MyFiles/Projects/DM/sample files/shifts.csv')
shift_df = shift_df.set_index('Code')
# -----------------------Randomize gene---------------------------------------#
for prs in chromosom_df.index :
chromosom_df.loc[prs] = np.random.choice(shift_df.index.values.tolist(),size=len(chromosom_df.columns))
chromosom_df = chromosom_df.astype(int)
#------------------------fitness function-------------------------------------#
def fitness (individual, meta_data):
prs_count,day_count = individual.shape
shift_prs = personnel_df.reset_index()
shift_prs['diff'] = 0
prs_count = 0
shift_lenght_diff = []
for prs in personnel_df.index:
shift_lenght = 0
for day in range(day_count):
# print('shiftcode:'+str(individual.loc[prs,day+1]))
shift_lenght += meta_data.loc[individual.loc[prs,day+1]][1]
shift_lenght_diff.append(abs((shift_lenght/shift_prs.iloc[prs_count,3]) - 1))
shift_prs.set_value(prs_count,'RequirementWorkMins_real',shift_lenght)
shift_prs.set_value(prs_count,'diff',
abs(shift_lenght - shift_prs.iloc[prs_count,3])
)
# print(shift_lenght - shift_prs.iloc[prs_count,3])
prs_count += 1
cost = np.mean(shift_lenght_diff)
# print('cost: ' + str(cost))
return cost
# -----------------------prs output function-------------------------------------#
def get_personnel_diff_len (individual, meta_data):
prs_count,day_count = individual.shape
shift_prs = personnel_df.reset_index()
shift_prs['diff'] = 0
prs_count = 0
shift_lenght_diff = []
for prs in personnel_df.index:
shift_lenght = 0
for day in range(day_count):
# print('shiftcode:'+str(individual.loc[prs,day+1]))
shift_lenght += meta_data.loc[individual.loc[prs,day+1]][1]
shift_lenght_diff.append(shift_lenght-shift_prs.iloc[prs_count,3])
shift_prs.set_value(prs_count,'RequirementWorkMins_real',shift_lenght)
shift_prs.set_value(prs_count,'diff',
abs(shift_lenght - shift_prs.iloc[prs_count,3])
)
# print(shift_lenght - shift_prs.iloc[prs_count,3])
prs_count += 1
cost = np.mean(shift_lenght_diff)
# print('cost: ' + str(cost))
return cost,shift_prs
# -----------------------Define GA--------------------------------------------#
ga = ga.GeneticAlgorithm( seed_data=chromosom_df,
meta_data=shift_df,
population_size=20,
generations=10,
crossover_probability=0.8,
mutation_probability=0.2,
elitism=True,
maximise_fitness=False)
# -----------------------run ga-----------------------------------------------#
ga.fitness_function = fitness # set the GA's fitness function
ga.run() # run the GA
sol_fitness, sol_df = ga.best_individual()
sol_fitness, prs_diff_df = get_personnel_diff_len(sol_df,shift_df)