-
Notifications
You must be signed in to change notification settings - Fork 81
/
GA.py
131 lines (106 loc) · 5.13 KB
/
GA.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
"""
Stock Selection ----- Deep or Traditional Methods?
Authors:
XingYu Fu; JinHong Du; YiFeng Guo; MingWen Liu; Tao Dong; XiuWen Duan;
ZiYi Yang; MingDi Zheng; Yuan Zeng;
Institutions:
AI&Fintech Lab of Likelihood Technology;
Gradient Trading;
School of Mathematics, Sun Yat-sen University;
LingNan College, Sun Yat-sen University;
Contact:
All Rights Reserved.
"""
"""Import Libraries"""
import numpy as np
from evaluation import EvaluationClass
class GA:
def __init__(self, X_train, Y_train, X_test, Y_test, model_type, save_computaion):
self.X_train = X_train
self.Y_train = Y_train
self.X_test = X_test
self.Y_test = Y_test
self.model_type = model_type
self.save_computaion = save_computaion
self.num_features = np.shape(X_train)[1]
self.populationSize = 100
self.crossoverProb = 0.2
self.mutationProb = 0.1
self.warmupNum = 1
self.iteration = 50
def IntialPopulation(self):
for i in range(self.warmupNum):
chromosomes = np.random.randint(0, 2, (self.populationSize, self.num_features))
chromosomes[np.sum(chromosomes, axis=1)<50, :] = 1 - chromosomes[np.sum(chromosomes, axis=1)<50, :]
if i>0:
_, _, fitness1 = self.Fitness(self.population)
_, _, fitness2 = self.Fitness(chromosomes)
print( fitness1<fitness2 )
self.population[fitness1<fitness2,:] = chromosomes[fitness1<fitness2,:]
else:
self.population = chromosomes
prob, _, _ = self.Fitness(self.population)
self.bestFitnessList.append(np.max(prob))
self.bestSolutionsList.append(self.population[np.argmax(prob),:])
def CheckChromosome(self, chromosome):
if np.sum(chromosome)>=2:
return True
else:
return False
def Crossover(self, population):
is_crossover = 1-np.random.binomial(1, self.crossoverProb, self.populationSize//2)
for i in np.where(is_crossover)[0]:
while True:
chromosome1 = np.copy(population[2*i,:])
chromosome2 = np.copy(population[2*i+1,:])
where_crossover = np.random.randint(0, self.num_features, 1)
chromosome1[where_crossover] = np.copy(self.population[2*i+1,where_crossover])
chromosome2[where_crossover] = np.copy(self.population[2*i,where_crossover])
if self.CheckChromosome(chromosome1) and self.CheckChromosome(chromosome2):
population[[2*i,2*i+1],:] = np.array([chromosome1, chromosome2])
break
return population
def Mutation(self, population):
is_mutate = 1-np.random.binomial(1, self.mutationProb, self.populationSize)
for i in np.where(is_mutate)[0]:
while True:
chromosome = np.copy(population[i,:])
where_mutate = np.random.randint(0, self.num_features, 1)
chromosome[where_mutate] = 1 - chromosome[where_mutate]
if self.CheckChromosome(chromosome):
population[i,:] = chromosome
break
return population
def Fitness(self, population):
fitness = np.zeros(self.populationSize)
for i in range(self.populationSize):
X_train_masked = self.X_train[:,population[i,:]==1]
X_test_masked = self.X_test[:,population[i,:]==1]
eva = EvaluationClass( X_train_masked, self.Y_train, X_test_masked, self.Y_test, self.model_type, self.save_computaion)
fitness[i] = eva.evalu()
prob = fitness / np.sum(fitness)
cum_prob = np.cumsum(prob)
return prob, cum_prob, fitness
def Select(self, cum_fitness):
newpopulation = np.zeros_like(self.population)
randoms = np.random.rand(self.populationSize)
for i, randoma in enumerate(randoms):
index = np.where(cum_fitness >= randoma)
newpopulation[i, :] = self.population[index[0][0], :]
return newpopulation
def Search(self):
self.bestFitnessList = []
self.bestSolutionsList = []
self.IntialPopulation()
_, cum_fitness, _ = self.Fitness(self.population)
for iteration in range(self.iteration):
print(str(iteration+1)+"th Iteration of GA.")
population = self.Select(cum_fitness)
crossoverPopulation = self.Crossover(population)
newPopulation = self.Mutation(crossoverPopulation)
prob, cum_fitness, fitness = self.Fitness(newPopulation)
self.bestFitnessList.append(np.max(prob))
self.bestSolutionsList.append(newPopulation[np.argmax(prob), :])
self.bestFitness = np.max(self.bestFitnessList)
self.bestSolutions = self.bestSolutionsList[np.argmax(self.bestFitnessList)]