-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolutionMultiProcessing.py
137 lines (114 loc) · 4.92 KB
/
EvolutionMultiProcessing.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
from sklearn.neighbors import LSHForest
from Common import offspringsInGeneration
import Common
import random
from Sets import Sets
import multiprocessing
def GetPrediction(y, distances, indices):
histo = dict()
sumOfDistances = dict()
for indexAndDistance in zip(indices, distances):
if histo.has_key(y[indexAndDistance[0]]):
histo[y[indexAndDistance[0]]] += 1
sumOfDistances[y[indexAndDistance[0]]] += indexAndDistance[1]
else:
histo[y[indexAndDistance[0]]] = 1
sumOfDistances[y[indexAndDistance[0]]] = indexAndDistance[1]
best = histo.keys()[0]
for key in histo.keys():
if (key != best):
if (histo[key] > histo[best]):
best = key
elif (histo[key] == histo[best]) & (sumOfDistances[key] < sumOfDistances[best]):
best = key
return best
trainingSetSize = 15
def FactorizeVectors(vectorized, factorVector):
enumerable = range(len(vectorized[0]))
result = [[(vector[i] - Common.meanVector[i])* factorVector[i] for i in enumerable] for vector in vectorized]
return result
def score(factors):
verifyCount = 3
X, y = Sets.trainingSet
test_set, databases = Sets.testSet
X = FactorizeVectors(X, factors)
test_set = FactorizeVectors(test_set, factors)
correctionAverage = 0
for i in range(verifyCount):
best_predictions = 0
clf = LSHForest(n_estimators = 10, n_candidates = 10)
clf.fit(X)
correct = 0
total = 0
for j in range(len(test_set)):
total += 1
actual = databases[j]
distances, indices = clf.kneighbors(test_set[j], n_neighbors=5)
predicted = GetPrediction(y, distances[0], indices[0])
if (actual == predicted):
correct += 1
if (correct > best_predictions):
best_predictions = correct
correctionAverage += best_predictions
correctionAverage = float(correctionAverage)/verifyCount
return correctionAverage
def mutate(vector, mutationFactor):
for i in range(len(vector)):
shouldMutate = random.choice([0, 1])
if (shouldMutate == 1):
mutation = random.uniform(mutationFactor*(-1), mutationFactor)
if (vector[i] < 0.1) | (vector[i] > 100000):
vector[i] = vector[i] - (abs(vector[i] - 0.1)/(vector[i] - 0.1)) * vector[i] * abs(mutation)
else:
vector[i] *= 1+mutation
return vector
def singleProcess(vector):
results = score(vector)
return results
def RunOneGeneration(startingVector, mutationFactor):
max_score = 0
leader = 0
newVectors = [startingVector] + [mutate(list(startingVector), mutationFactor) for i in range(offspringsInGeneration -1)]
pool = multiprocessing.Pool(processes=10)
scores = pool.map(singleProcess, newVectors)
pool.close()
pool.join()
for i in range(offspringsInGeneration):
if scores[i] >= max_score:
max_score = scores[i]
leader = i
print('Max Score: ' + max_score.__str__() + ' vector: ' + newVectors[leader].__str__())
return newVectors[leader], max_score
def Evolution(startingVector, startingMutationFactor):
lastBestVector = list(startingVector)
currentMutationFactor = startingMutationFactor
lastBestScore = 0
noChangeCounter = 0
secondBestScore, secondBestVector = 0, list()
while (True):
currentVector, currentScore = RunOneGeneration(lastBestVector, currentMutationFactor)
if (currentScore >= secondBestScore):
secondBestScore, secondBestVector = currentScore, currentVector
if currentScore <= lastBestScore:
noChangeCounter += 1
else:
currentMutationFactor = startingMutationFactor
noChangeCounter = 0
if (noChangeCounter == 10):
if (currentMutationFactor < 0.5):
currentMutationFactor += 0.1
print('Increasing mutation factor to: ' + currentMutationFactor.__str__())
noChangeCounter = 0
lastBestScore, lastBestVector = secondBestScore, secondBestVector
print('Changing to second best score: '+ secondBestScore.__str__() + ' second best vector: ' + secondBestVector.__str__())
secondBestScore = 0
secondBestVector = list()
if currentScore >= lastBestScore:
lastBestScore = currentScore
lastBestVector = currentVector
print('Taking the current score: ' + lastBestScore.__str__() + ' and vector: ' + lastBestVector.__str__())
startingVector = [268.2219308702712, 1175.543198488771, 1044.9510869045512, 1618.0474956879277, 2861.877445243491, 364.03686992286356, 708.0772292778498, 9323.2014983376, 953.151626413617, 1251.1187443314202]
startingMutationFactor = Common.defaultMutationFactor
if __name__=='__main__':
Evolution(startingVector, startingMutationFactor)
#print(score(startingVector))