Skip to content

Commit

Permalink
Lessen memory requirement of TSP by 50%.
Browse files Browse the repository at this point in the history
  • Loading branch information
skytreader committed Mar 19, 2017
1 parent 46d13cb commit 552c5dc
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions ai/ga/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class TSPSolver(GASolver):

def __init__(self, cities, max_iterations=100):
self.cities = cities
print("original pathlen %s" % self.path_cost(cities))
initial_pool_ = [cities]
initial_pool_ = [[i for i in range(len(self.cities))]]
print("original pathlen %s" % self.path_cost(initial_pool_[0]))
super().__init__(initial_pool_, max_iterations=max_iterations)

def euc_2d(self, p1, p2):
Expand All @@ -21,12 +21,11 @@ def euc_2d(self, p1, p2):
def path_cost(self, variation):
distance = 0
limit = len(variation)
i = 1

while i < limit:
distance += self.euc_2d(variation[i-1], variation[i])
i += 1


for i in range(limit):
c2 = variation[(i + 1) % limit]
distance += self.euc_2d(self.cities[variation[i]], self.cities[c2])

return distance

def compute_fitness(self, variation):
Expand Down

0 comments on commit 552c5dc

Please sign in to comment.