Skip to content

Commit bdea826

Browse files
author
Tomasz bla Fortuna
committed
Small optimisations + whitespace cleanup
Single BUG fixed (gene1, gene1, instead of gene1, gene2) mostly irrelevant. Times [s] before: Salesman 10gens: 10.997, 10.974, 11.047 Quadratic 11gens: 0.954, 0.958, 0.951 after: Salesman 10gens: 10.595s, 10.654s Quadratic 11gens: 0.8634, 0.8645, 0.8634
1 parent 6b90b22 commit bdea826

File tree

6 files changed

+337
-356
lines changed

6 files changed

+337
-356
lines changed

demo_config_genome.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class QPopulation(Population):
110110

111111
# now a func to run the population
112112
def main():
113+
from time import time
114+
s = time()
113115
try:
114116
generations = 0
115117
while True:
@@ -127,7 +129,7 @@ def main():
127129

128130
except KeyboardInterrupt:
129131
pass
130-
print "Executed", generations, "generations"
132+
print "Executed", generations, "generations in", time() - s, "seconds"
131133

132134

133135
if __name__ == '__main__':

demo_salesman.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CityPriorityGene(BaseGeneClass):
5050
"""
5151
randMin = geneRandMin
5252
randMax = geneRandMax
53-
53+
5454
mutProb = geneMutProb
5555
mutAmt = geneMutAmt
5656

@@ -67,15 +67,15 @@ def __init__(self, name, x=None, y=None):
6767
self.name = name
6868

6969
# 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
7171
if x == None:
7272
x = random() * (width - 100) + 50
7373
if y == None:
7474
y = random() * (height - 100) + 50
75-
75+
7676
self.x = x
7777
self.y = y
78-
78+
7979
def __sub__(self, other):
8080
"""
8181
compute distance between this and another city
@@ -133,7 +133,7 @@ class TSPSolution(OrganismClass):
133133
the TSP
134134
"""
135135
genome = genome
136-
136+
137137
mutateOneOnly = mutateOneOnly
138138

139139
crossoverRate = crossoverRate
@@ -152,7 +152,7 @@ def fitness(self):
152152
# start at first city, compute distances to last
153153
for i in xrange(cityCount - 1):
154154
distance += sortedCities[i] - sortedCities[i+1]
155-
155+
156156
# and add in the return trip
157157
distance += sortedCities[0] - sortedCities[-1]
158158

@@ -172,7 +172,7 @@ def getCitiesInOrder(self):
172172

173173
# now sort them, the priority elem will determine order
174174
sorter.sort()
175-
175+
176176
# now extract the city objects
177177
sortedCities = [tup[1] for tup in sorter]
178178

@@ -188,21 +188,21 @@ def normalise(self):
188188
sorter = [(genes[name][i], name) for name in cityNames]
189189
sorter.sort()
190190
sortedGenes = [tup[1] for tup in sorter]
191-
192-
191+
192+
193193

194194

195195
class TSPSolutionPopulation(Population):
196196

197197
initPopulation = popInitSize
198198
species = TSPSolution
199-
199+
200200
# cull to this many children after each generation
201201
childCull = popChildCull
202-
202+
203203
# number of children to create after each generation
204204
childCount = popChildCount
205-
205+
206206
# number of best parents to add in with next gen
207207
incest = popIncest
208208

@@ -213,10 +213,11 @@ class TSPSolutionPopulation(Population):
213213
mutateAfterMating = mutateAfterMating
214214

215215
def main():
216-
216+
from time import time
217+
s = time()
217218
# create initial population
218219
pop = TSPSolutionPopulation()
219-
220+
220221
# now repeatedly calculate generations
221222
i = 0
222223

@@ -227,18 +228,17 @@ def main():
227228
i += 1
228229
except KeyboardInterrupt:
229230
pass
230-
231-
231+
232+
232233
# get the best solution
233234
solution = pop.best()
234-
235+
235236
# and print out the itinerary
236237
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)
238240
for city in sortedCities:
239241
print " x=%03.2f y=%03.2f %s" % (city.x, city.y, city.name)
240242

241243
if __name__ == '__main__':
242244
main()
243-
244-

0 commit comments

Comments
 (0)