Skip to content

Commit

Permalink
remove comments, use util.path_cost
Browse files Browse the repository at this point in the history
  • Loading branch information
rameziophobia committed Jun 19, 2020
1 parent ca12798 commit bc3d90b
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 313 deletions.
11 changes: 6 additions & 5 deletions anneal.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ def run(self):
print("Best fitness obtained: ", self.best_fitness)

def visualize_routes(self):
visualize_tsp('simulated annealing', self.route)
visualize_tsp('simulated annealing TSP', self.route)

def plot_learning(self):
fig = plt.figure(1)
plt.plot([i for i in range(len(self.progress))], self.progress)
plt.ylabel("Fitness")
plt.xlabel("Iteration")
plt.show()
plt.ylabel("Distance")
plt.xlabel("Iterations")
plt.show(block=False)


if __name__ == "__main__":
cities = read_cities(64)
sa = SimAnneal(cities, stopping_iter=15000)
sa.run()
sa.visualize_routes()
sa.plot_learning()
sa.visualize_routes()
256 changes: 0 additions & 256 deletions cities_256.data

This file was deleted.

2 changes: 0 additions & 2 deletions divide_and_conquer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def split_longer_dim(cities):

def merge(self, graph_1, graph_2):
if isinstance(graph_1, City):
# if graph_1.distance(graph_2[0][0]) < graph_1.distance(graph_2[0][1]):
graph_2.append((graph_1, graph_2[0][0]))
# else:
graph_2.append((graph_1, graph_2[0][1]))
return graph_2
min_cost = math.inf
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def solve_tsp_dynamic(cities):

if __name__ == "__main__":

cities = read_cities(8)
cities = read_cities(16)
g = solve_tsp_dynamic(cities)
sol = [cities[gi] for gi in g]
print(path_cost(sol))
Expand Down
23 changes: 1 addition & 22 deletions genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from util import City, read_cities, write_cities_and_return_them, generate_cities




class Fitness:
def __init__(self, route):
self.route = route
Expand Down Expand Up @@ -106,8 +104,6 @@ def generate_population(self):

def mutate(self, individual):
for index, city in enumerate(individual):
# mutation_rate = self.mutation_rate * individual[index - 1].distance(city) / self.average_path_cost
# if individual[index - 1].distance(city) > self.average_path_cost *
if random.random() < max(0, self.mutation_rate):
sample_size = min(min(max(3, self.population_size // 5), 100), len(individual))
random_sample = random.sample(range(len(individual)), sample_size)
Expand All @@ -131,9 +127,6 @@ def run(self):
for ind in range(0, self.iterations):
self.next_generation()
self.progress.append(self.best_distance())
# if i > self.iterations / 2:
# self.population[self.elites_num // 2:] = self.initial_population()[self.elites_num // 2:]
# self.roulette_selection = False
if self.plot_progress and ind % 10 == 0:
self.plot()
elif not self.plot_progress and ind % 10 == 0:
Expand Down Expand Up @@ -177,25 +170,11 @@ def greedy_route(start_index, cities):


if __name__ == '__main__':
x = 0
# for i in range(10):
cities = read_cities(64)
# cc = [(int(random.random() * 1000), int(random.random() * 1000)) for _ in range(16)]
# cities = [City(x=x, y=y) for (x, y) in cc]
# lilwel = [[] for _ in range(len(cc))]
# for index, city in enumerate(cc):
# for city_2 in cc:
# if city_2[0] == city[0] and city_2[1] == city[1]:
# lilwel[index].append(sys.maxsize)
# else:
# lilwel[index].append(math.hypot(city_2[0] - city[0], city_2[1] - city[0]))
# print(lilwel)
genetic_algorithm = GeneticAlgorithm(cities=cities, iterations=1500, population_size=100,
genetic_algorithm = GeneticAlgorithm(cities=cities, iterations=1200, population_size=100,
elites_num=20, mutation_rate=0.008, greedy_seed=1,
roulette_selection=True, plot_progress=True)
genetic_algorithm.run()
print(genetic_algorithm.best_distance())
x += genetic_algorithm.best_distance()
genetic_algorithm.plot()
plt.show(block=True)
# print(x)
11 changes: 2 additions & 9 deletions greedy_tsp.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import matplotlib.pyplot as plt
from util import City, read_cities, write_cities_and_return_them, generate_cities


def path_cost(route):
distance = 0
for index, city in enumerate(route):
distance += city.distance(route[(index + 1) % len(route)])
return distance
from util import City, read_cities, write_cities_and_return_them, generate_cities, path_cost


class Greedy:
Expand Down Expand Up @@ -35,7 +28,7 @@ def plot_interactive(self, block):
plt.plot([x1, x2], [y1, y2], 'ro')
plt.plot([x1, x2], [y1, y2], 'g')
plt.draw()
plt.pause(0.3)
plt.pause(0.07)
plt.show(block=block)

def init_plot(self):
Expand Down
Loading

0 comments on commit bc3d90b

Please sign in to comment.