@@ -37,7 +37,7 @@ def getFitness(individual, X, y):
37
37
return (0 ,)
38
38
39
39
40
- def geneticAlgorithm (X , y ):
40
+ def geneticAlgorithm (X , y , n_population , n_generation ):
41
41
"""
42
42
Deap global variables
43
43
Initialize variables to use eaSimple
@@ -59,8 +59,6 @@ def geneticAlgorithm(X, y):
59
59
toolbox .register ("select" , tools .selTournament , tournsize = 3 )
60
60
61
61
# initialize parameters
62
- n_population = 10
63
- n_generation = 2
64
62
pop = toolbox .population (n = n_population )
65
63
hof = tools .HallOfFame (n_population * n_generation )
66
64
stats = tools .Statistics (lambda ind : ind .fitness .values )
@@ -92,9 +90,24 @@ def bestIndividual(hof, X, y):
92
90
return _individual .fitness .values , _individual , _individualHeader
93
91
94
92
93
+ def getArguments ():
94
+ """
95
+ Get argumments from command-line
96
+ If pass only dataframe path, pop and gen will be default
97
+ """
98
+ dfPath = sys .argv [1 ]
99
+ if (len (sys .argv ) == 4 ):
100
+ pop = int (sys .argv [2 ])
101
+ gen = int (sys .argv [3 ])
102
+ else :
103
+ pop = 10
104
+ gen = 2
105
+ return dfPath , pop , gen
106
+
107
+
95
108
if __name__ == '__main__' :
96
- # get dataframe path from command-line argument
97
- dataframePath = sys . argv [ 1 ]
109
+ # get dataframe path, population number and generation number from command-line argument
110
+ dataframePath , n_pop , n_gen = getArguments ()
98
111
# read dataframe from csv
99
112
df = pd .read_csv (dataframePath , sep = ',' )
100
113
@@ -110,7 +123,7 @@ def bestIndividual(hof, X, y):
110
123
str (getFitness (individual , X , y )) + "\n " )
111
124
112
125
# apply genetic algorithm
113
- hof = geneticAlgorithm (X , y )
126
+ hof = geneticAlgorithm (X , y , n_pop , n_gen )
114
127
115
128
# select the best individual
116
129
accuracy , individual , header = bestIndividual (hof , X , y )
0 commit comments