@@ -50,7 +50,7 @@ class CityPriorityGene(BaseGeneClass):
50
50
"""
51
51
randMin = geneRandMin
52
52
randMax = geneRandMax
53
-
53
+
54
54
mutProb = geneMutProb
55
55
mutAmt = geneMutAmt
56
56
@@ -67,15 +67,15 @@ def __init__(self, name, x=None, y=None):
67
67
self .name = name
68
68
69
69
# constrain city coords so they're no closer than 50 pixels
70
- # to any edge, so the city names show up ok in the gui version
70
+ # to any edge, so the city names show up ok in the gui version
71
71
if x == None :
72
72
x = random () * (width - 100 ) + 50
73
73
if y == None :
74
74
y = random () * (height - 100 ) + 50
75
-
75
+
76
76
self .x = x
77
77
self .y = y
78
-
78
+
79
79
def __sub__ (self , other ):
80
80
"""
81
81
compute distance between this and another city
@@ -133,7 +133,7 @@ class TSPSolution(OrganismClass):
133
133
the TSP
134
134
"""
135
135
genome = genome
136
-
136
+
137
137
mutateOneOnly = mutateOneOnly
138
138
139
139
crossoverRate = crossoverRate
@@ -152,7 +152,7 @@ def fitness(self):
152
152
# start at first city, compute distances to last
153
153
for i in xrange (cityCount - 1 ):
154
154
distance += sortedCities [i ] - sortedCities [i + 1 ]
155
-
155
+
156
156
# and add in the return trip
157
157
distance += sortedCities [0 ] - sortedCities [- 1 ]
158
158
@@ -172,7 +172,7 @@ def getCitiesInOrder(self):
172
172
173
173
# now sort them, the priority elem will determine order
174
174
sorter .sort ()
175
-
175
+
176
176
# now extract the city objects
177
177
sortedCities = [tup [1 ] for tup in sorter ]
178
178
@@ -188,21 +188,21 @@ def normalise(self):
188
188
sorter = [(genes [name ][i ], name ) for name in cityNames ]
189
189
sorter .sort ()
190
190
sortedGenes = [tup [1 ] for tup in sorter ]
191
-
192
-
191
+
192
+
193
193
194
194
195
195
class TSPSolutionPopulation (Population ):
196
196
197
197
initPopulation = popInitSize
198
198
species = TSPSolution
199
-
199
+
200
200
# cull to this many children after each generation
201
201
childCull = popChildCull
202
-
202
+
203
203
# number of children to create after each generation
204
204
childCount = popChildCount
205
-
205
+
206
206
# number of best parents to add in with next gen
207
207
incest = popIncest
208
208
@@ -213,10 +213,11 @@ class TSPSolutionPopulation(Population):
213
213
mutateAfterMating = mutateAfterMating
214
214
215
215
def main ():
216
-
216
+ from time import time
217
+ s = time ()
217
218
# create initial population
218
219
pop = TSPSolutionPopulation ()
219
-
220
+
220
221
# now repeatedly calculate generations
221
222
i = 0
222
223
@@ -227,18 +228,17 @@ def main():
227
228
i += 1
228
229
except KeyboardInterrupt :
229
230
pass
230
-
231
-
231
+
232
+
232
233
# get the best solution
233
234
solution = pop .best ()
234
-
235
+
235
236
# and print out the itinerary
236
237
sortedCities = solution .getCitiesInOrder ()
237
- print "Best solution: total distance %04.2f:" % solution .fitness ()
238
+ print "Best solution: total distance %04.2f in %.3f seconds:" % (
239
+ solution .fitness (), time () - s )
238
240
for city in sortedCities :
239
241
print " x=%03.2f y=%03.2f %s" % (city .x , city .y , city .name )
240
242
241
243
if __name__ == '__main__' :
242
244
main ()
243
-
244
-
0 commit comments