forked from wncc/genetic-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.py~
68 lines (49 loc) · 1.57 KB
/
top.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
import fitnessFunction
def quickSort(sortBy,sortAlong):
pivot = sortBy[0]
pivotAlong = sortAlong[0]
length = len(sortBy)
lhsBy = []
rhsBy = []
lhsAlong = []
rhsAlong = []
for i in range(1,length):
if(sortBy[i] < pivot):
lhsBy.append(sortBy[i])
lhsAlong.append(sortAlong[i])
else:
rhsBy.append(sortBy[i])
rhsAlong.append(sortAlong[i])
quickSort(lhsBy, lhsAlong)
quickSort(rhsBy, rhsAlong)
sortBy = lhsBy + pivot + rhsBy
sortAlong = lhsAlong + pivotAlong + rhsAlong
def insertAtPosition(fitness, member, fitnessArr, memberArr):
length = len(fitnessArr)
for i in fitnessArr:
if fitness < i:
j = length
while (j > i):
fitnessArr[j] = fitnessArr[j - 1]
memberArr[j] = memberArr[j - 1]
j -= 1
fitnessArr[i]= fitness
memberArr[i]= member
def top(n,members,cityList):
fitnessList = []
for member in members:
fitnessList.append(fitnessFunction.fitnessFunction(member, cityList))
ans = []
ansFitness = []
insertedMembers = 0
memberCount = len(members)
for i in range(n):
ans.append(members[i])
ansFitness.append(fitnessList[i])
quickSort(ansFitness, ans)
threshold = ansFitness[n - 1]
for j in members[n: ]:
if fitnessList[j] < threshold:
insertAtPosition(fitnessList[j], members[j], ansFitness, ans)
threshold = ansFitness[n - 1]
return ans