Skip to content

Commit

Permalink
fix bug in lengths and remove prints
Browse files Browse the repository at this point in the history
  • Loading branch information
afourmy committed Feb 23, 2018
1 parent d410c53 commit d9b4154
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ i.e the city <b>k</b> which minimizes <b>d(i, k) + d(k, j) - d(i, j)</b> with <

<pre>
- Start from a random city.
- Find the city <b>k</b> farthest from any node in the tour (i.e the city <b>k</b> which maximizes <b>d(c, k)</b> with <b>c</b> a
city in the partial tour), and insert <b>k</b> where it causes the smallest increase in length
- Find the city <b>k</b> farthest from any node in the tour (i.e the city <b>k</b> which maximizes <b>d(c, k)</b> with <b>c</b>
a city in the partial tour), and insert <b>k</b> where it causes the smallest increase in length
(by minimizing <b>d(i, k) + d(k, j) - d(i, j)</b>, with <b>(i, j)</b> an edge in the partial tour).
- Repeat until every city has been visited.
</pre>
Expand Down
10 changes: 2 additions & 8 deletions algorithms/tour_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class TourConstructionHeuristics(BaseAlgorithm):
# returns the neighbor as well as the distance between the two
def closest_neighbor(self, tour, node, in_tour=False, farthest=False):
neighbors = self.distances[node]
print(tour, node, neighbors)
current_dist = [(c, d) for c, d in neighbors.items()
if (c in tour if in_tour else c not in tour)]
return sorted(current_dist, key=itemgetter(1))[-farthest]
Expand All @@ -35,9 +34,7 @@ def nearest_neighbor(self):
city = randrange(1, self.size)
current, tour, tour_length, tour_lengths = city, [city], 0, []
while len(tour) != len(self.cities):
print(tour)
arg_min, edge_length = self.closest_neighbor(tour, current)
print(arg_min, edge_length)
tour_length += edge_length
tour_lengths.append(tour_length)
tour.append(arg_min)
Expand All @@ -52,12 +49,11 @@ def nearest_neighbor(self):

def nearest_insertion(self, farthest=False):
city = randrange(1, self.size)
tour, tours, tour_lengths = [city], [], []
tour, tours = [city], []
# we find the closest node R to the first node
neighbor, length = self.closest_neighbor(tour, city, False, farthest)
tour.append(neighbor)
tour_length = length
tour_lengths.append(tour_length)
while len(tour) != len(self.cities):
best, dist = None, 0 if farthest else float('inf')
# (selection step) given a sub-tour,we find node r not in the
Expand All @@ -79,14 +75,12 @@ def nearest_insertion(self, farthest=False):
if add < dist:
idx, dist = i, add
tour_length += self.add(tour[idx], tour[idx + 1], best)
tour_lengths.append(tour_length)
tours.append(tour)
tour.insert(idx + 1, best)
tour = tour[:-1]
tour_length += self.distances[tour[0]][tour[-1]]
tour_lengths.append(tour_length)
best_lengths = list(map(self.compute_length, tours))
return [self.format_solution(step) for step in tours], tour_lengths
return [self.format_solution(step) for step in tours], best_lengths

def farthest_insertion(self):
return self.nearest_insertion(farthest=True)
Expand Down
2 changes: 1 addition & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def configure_socket(app):
def import_cities():
with open(join(path_app, 'data', 'cities.json')) as data:
for city_dict in load(data):
if int(city_dict['population']) < 1200000:
if int(city_dict['population']) < 900000:
continue
city = City(**city_dict)
db.session.add(city)
Expand Down

0 comments on commit d9b4154

Please sign in to comment.