forked from wncc/genetic-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.py
85 lines (63 loc) · 2.2 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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])
if(len(lhsBy) > 0):
(lhsBy, lhsAlong) = quickSort(lhsBy, lhsAlong)
if(len(rhsBy) > 0):
(rhsBy, rhsAlong) = quickSort(rhsBy, rhsAlong)
sortBy = lhsBy + [pivot] + rhsBy
sortAlong = lhsAlong + [pivotAlong] + rhsAlong
return (sortBy, sortAlong)
def insertAtPosition(fitness, member, fitnessArr, memberArr):
length = len(fitnessArr)
for i in range(length):
if fitness < fitnessArr[i]:
j = length - 1
while (j > i):
fitnessArr[j] = fitnessArr[j - 1]
memberArr[j] = memberArr[j - 1]
j -= 1
fitnessArr[i]= fitness
memberArr[i]= member
break
# this function gives you the n top most cities in the current new generation
# members is the new generation
# cities is the co ordinates
def top(n,members,cityList):
fitnessList = []
for member in members:
fitnessList.append(fitnessFunction.fitnessFunction(member, cityList))
# print fitnessFunction.fitnessFunction(member, cityList), " ", member
# print fitnessList
ans = []
ansFitness = []
insertedMembers = 0
memberCount = len(members)
for i in range(n):
ans.append(members[i])
ansFitness.append(fitnessList[i])
(ansFitness,ans) = quickSort(ansFitness, ans)
threshold = ansFitness[n - 1]
for j in range(n, memberCount):
if fitnessList[j] < threshold:
insertAtPosition(fitnessList[j], members[j], ansFitness, ans)
threshold = ansFitness[n - 1]
return ans
#print top(3,[[1,2,3,4],[1,3,2,4],[1,4,3,2],[1,2,4,3],[1,3,4,2],[1,4,2,3]],[[1,2],[3,4],[1,3],[3,5]])
l1= [1,2,4,2,1,0,-7]
l2= [2,3,4,5,5,7,8]
(l1,l2) = quickSort(l1,l2)
#print "qs", l1, l2