-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeneSelector.py
230 lines (176 loc) · 8.89 KB
/
GeneSelector.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
import datetime
import random
import copy
from sklearn.linear_model import BayesianRidge, LinearRegression, Ridge
import GeneticsForSelection as gns
import sys
import numpy as np
from mUtils import regression_accuracy_scorer, LimitedSizeDict
class GeneSelector:
def __init__(self, n_process=8, feature_bound=5):
self.Memo = LimitedSizeDict(size_limit=1000)
self.FitnessCites = 1
self.SavedCalculationTimes = 1
self.Genetics = None
self.n_process = n_process
self.featureBound = feature_bound
def reset_mini_timer(self, recalculateFitnesses=True):
if not self.Genetics is None:
self.Genetics.reset_mini_timer(recalculateFitnesses)
def __get_fitness_instance(self, metric, bic, std, nog):
return Fitness(metric, bic, std, nog)
def __get_fitness(self, genes, x, y, initialFitness=None, runs=1):
nog = np.count_nonzero(genes)
if nog == 0:
return self.__get_fitness_instance(0, 1000, 1, len(genes))
hashedGenes = hash(genes.data.tobytes())
self.FitnessCites += 1
if initialFitness is not None and hashedGenes in self.Memo: #Tabu mem search
self.SavedCalculationTimes += 1
return self.__get_fitness_instance(initialFitness.Metric - 10, 1000, initialFitness.Std + 0.1,
initialFitness.NOG)
clf = BayesianRidge(n_iter=100)
clf.fit(x[:, np.where(genes == 1)[0]], y)
scores2, bic = regression_accuracy_scorer(clf, x[:, np.where(genes == 1)[0]], y)
fitness = self.__get_fitness_instance(scores2, bic, 0, nog)
self.Memo[hashedGenes] = fitness
return fitness
def __removeRedundantGenes(self, fnGetFitness, genes, fitness):
lastFitness = copy.deepcopy(fitness)
onedLoci = np.where(genes == 1)[0]
genesCopy = np.copy(genes)
for locus in onedLoci:
gensCopyCopy = np.copy(genesCopy)
gensCopyCopy[locus] = 0
newFitness = fnGetFitness(gensCopyCopy, initialFitness=fitness)
if newFitness > lastFitness:
lastFitness = newFitness
genesCopy[locus] = 0
return genesCopy, lastFitness
def __mutate(self, genes, initialFitness, fnGetFitness, x, votes):
bound = 2
maxTries = random.randint(1, 5)
genesCopy = np.copy(genes)
fitness = copy.deepcopy(initialFitness)
for _ in range(maxTries):
toAdd = random.randint(1, 2) == 1
if toAdd:
zeroedLoci = np.where((genesCopy == 0) & (votes != 0))[0]
if len(zeroedLoci) > 0:
p_index = np.random.choice(zeroedLoci, random.randint(1, min(bound, len(zeroedLoci)))
if len(zeroedLoci) > 1 else 1,
replace=False, p= None)
genesCopy[p_index] = 1
else:
onedLoci = np.where((genesCopy == 1) & (votes != 0))[0]
zeroIndices = np.random.choice(onedLoci, random.randint(1, min(bound, len(onedLoci - 1))),
replace=False, p=None)
genesCopy[zeroIndices] = 0
if np.count_nonzero(genesCopy) == 0:
genesCopy[zeroIndices] = 1
continue
fitness = fnGetFitness(genesCopy, initialFitness=initialFitness)
if fitness > initialFitness:
break
return genesCopy, fitness
def __crossover(self, parentGenes, donorGenes, initialFitness, donorFitness, fnGetFitness, votes):
# global limit
if np.array_equal(parentGenes, donorGenes):
return None, None
bound = 2
maxTries = random.randint(1, 5)
childGenes = np.copy(parentGenes)
fitness = copy.deepcopy(initialFitness)
for _ in range(maxTries):
candidateGeneIndices = np.argwhere((childGenes != donorGenes) & (votes!=0)).flatten()
indicesToChange = np.random.choice(candidateGeneIndices,
random.randint(1, min(bound, len(candidateGeneIndices)))
if len(candidateGeneIndices) > 1 else 1,
replace=False, p=None)
childGenes[indicesToChange] = donorGenes[indicesToChange]
if np.array_equal(childGenes, donorGenes) or np.count_nonzero(childGenes) == 0:
childGenes = np.copy(parentGenes)
continue
fitness = fnGetFitness(childGenes, initialFitness=initialFitness)
if initialFitness < fitness:
break
return childGenes, fitness
def __create(self, gene_len, probs, poolSize, index, votes):
valid_indices = np.where(votes != 0)[0]
count = self.featureBound
target_indices = np.random.choice(valid_indices, count, replace=False)
genes = np.zeros((gene_len,), dtype='int8')
genes[target_indices] = 1
return genes
def __display(self, candidate, pindex, startTime):
timeDiff = str(datetime.datetime.now() - startTime)
fitness = candidate.Fitness
sys.stdout.write(
# print(
# "\r" +
"Fitness(Strategy: %s[%d]), %s\t%s\n" % (
candidate.Strategy.name,
pindex,
fitness,
timeDiff
)
# , end='\r'
)
sys.stdout.flush()
def just_do_it(self, x, y, votes, maxAge=200, poolSize=200, maxSeconds= None, maxIdleRounds=None,
maxSecondsMiniStroke=None):
random.seed(2020)
startTime = datetime.datetime.now()
# self.Memo = np.array([hash(str(np.ones((x.shape[1])).data.tobytes())), ])
votesSum = np.sum(votes)
oneProbs = np.apply_along_axis(lambda x: x / votesSum, 0, votes)
def fnDisplay(candidate, pindex):
self.__display(candidate, pindex, startTime)
def fnGetFitness(genes, initialFitness=None):
return self.__get_fitness(genes, x, y, initialFitness)
def fnMutate(genes, fitness):
return self.__mutate(genes, fitness, fnGetFitness, x, votes)
def fnCreate(index):
return self.__create(x.shape[1], oneProbs, poolSize, index, votes)
def fnCrossover(parentGenes, donorGenes, parentFitness, donorFitness):
return self.__crossover(parentGenes, donorGenes, parentFitness, donorFitness, fnGetFitness, votes)
optimalFitness = self.__get_fitness_instance(1, -1000000, 0, 1)
self.Genetics = gns.Genetics(fnGetFitness)
for timedOut, best, percentage, NOG in self.Genetics.get_best(0, optimalFitness,
fnDisplay, custom_mutate=fnMutate, custom_create=fnCreate, maxAge=maxAge,
poolSize=poolSize, crossover=fnCrossover,
maxSeconds=maxSeconds, maxIdleRounds=maxIdleRounds,
maxSecondsMiniStroke=maxSecondsMiniStroke
# triggerLimit=limit
):
# bestGenes, fitness = self.__removeRedundantGenes(fnGetFitness, best.Genes, best.Fitness)
yield timedOut, best.Genes, percentage, NOG, datetime.datetime.now() - startTime, \
self.SavedCalculationTimes / self.FitnessCites # if percentage >= limit else SavedAssTimes1 / fitnessCites1
class Fitness:
def __init__(self, metric, bic, std, nog):
self.Metric = metric
self.Std = std
self.NOG = nog
self.BIC = bic
# self.CompoundScore = self.Metric * (1 + self.NOG * beta) + self.Std * alpha
self.CompoundScore = self.Metric - 2*self.Std
def __int__(self):
return int(self.Metric)
def __add__(self, other):
return self.Metric + other.Metric
def __float__(self):
return self.Metric
def __gt__(self, other):
return self.CompoundScore > other.CompoundScore if self.CompoundScore != other.CompoundScore \
else self.NOG < other.NOG
# else self.BIC < other.BIC if self.BIC != other.BIC\
# else self.Std < other.Std if self.Std != other.Std\
def __lt__(self, other):
return self.CompoundScore < other.CompoundScore if self.CompoundScore != other.CompoundScore \
else self.NOG > other.NOG
# else self.BIC > other.BIC if self.BIC != other.BIC \
# else self.Std > other.Std if self.Std != other.Std \
def __eq__(self, other):
return self.Metric == other.Metric and self.Std == other.Std and self.NOG == other.NOG
def __str__(self):
return "AdjR^2: {:e}\tBIC: {}\tSt.D.: {:e}\tNOG: {}".format(float(self.Metric), float(self.BIC), float(self.Std), self.NOG)