Skip to content

Commit

Permalink
ACO + insertion update
Browse files Browse the repository at this point in the history
  • Loading branch information
udeshmg committed May 8, 2022
1 parent 53a1793 commit 582cb78
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 12 deletions.
47 changes: 39 additions & 8 deletions problems/tsp/tsp_aco.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import random
import copy
from collections import deque

class Ant():

Expand All @@ -19,7 +20,7 @@ def __init__(self, alpha, beta, n, id):

def step(self, graph, first_iter, deterministic=False, best_tour=False):
if self.step_num == 0:
next_node = 0 #random.randint(0, self.n-1)
next_node = 0
elif first_iter:
next_node = self.random_next_node()
else:
Expand Down Expand Up @@ -94,13 +95,15 @@ def __init__(self, xy, p):
self.reciprocal_dist = np.reciprocal(dist)

self.xy = xy
self.phero_matrix = np.full(self.dist.shape, 20)
self.phero_matrix = np.full(self.dist.shape, 5)
self.best_phero_matrix = self.phero_matrix
self.p = p

self.phero_max = 10
self.phero_min = 0.1

self.best_k_paths = deque(maxlen=10)

def update_phero(self, path_costs):

cost_matrix = np.zeros(self.dist.shape)
Expand All @@ -112,6 +115,16 @@ def update_phero(self, path_costs):

self.phero_matrix = (1-self.p)*self.phero_matrix + cost_matrix

def override_phero(self, path_costs):
cost_matrix = np.zeros(self.dist.shape)

for cost, path in path_costs:
for i in range(len(path)-1):
cost_matrix[i][path[i]][path[i+1]] -= 1/cost
cost_matrix[len(path)-1][path[len(path)-1]][path[0]] -= 1/cost

self.phero_matrix = (1-self.p)*self.phero_matrix - cost_matrix

def clip_phero(self):
self.phero_matrix = np.clip(self.phero_matrix, self.phero_min, self.phero_max)

Expand Down Expand Up @@ -145,7 +158,7 @@ def __init__(self, xy, p):
self.reciprocal_dist = np.reciprocal(dist)

self.xy = xy
self.phero_matrix = np.full(self.dist.shape, 1)
self.phero_matrix = np.full(self.dist.shape, 20)
self.best_phero_matrix = self.phero_matrix
self.p = p

Expand Down Expand Up @@ -215,7 +228,25 @@ def update_phero(self):
lowest_path = path
ant.reset()
self.graph.update_phero([[lowest_cost, lowest_path]])
#self.graph.clip_phero()
self.graph.clip_phero()
elif self.type == 'population':
lowest_cost = np.inf
lowest_path = []
for ant in self.ants:
path_cost, path = ant.get_path_cost(self.graph)
if lowest_cost > path_cost:
lowest_cost = path_cost
lowest_path = path
ant.reset()

if len(self.graph.best_k_paths) == 10:
path_cost = self.graph.best_k_paths.popleft()
self.graph.override_phero(path_cost)

self.graph.best_k_paths.append([[lowest_cost, lowest_path]])
self.graph.update_phero([[lowest_cost, lowest_path]])
self.graph.clip_phero()

else:
path_costs = []
for ant in self.ants:
Expand Down Expand Up @@ -255,8 +286,8 @@ def solve_all_aco(dataset):
results = []
start_time = time.time()
for i, instance in enumerate(dataset):
aco = ACO(instance, iterations=200, alpha=1, beta=5, p=0.2, k=20,
type='min-max')
aco = ACO(instance, iterations=100, alpha=0.4, beta=1.2, p=0.8, k=20,
type='population')
aco.run()
result = aco.get_best_tour()
print("Solved instance {} with tour length {} : Solved in {} seconds".format(i, result, time.time()-start_time))
Expand All @@ -265,10 +296,10 @@ def solve_all_aco(dataset):

if __name__=="__main__":
np.random.seed(1234)
xy = np.random.uniform(0, 1, (50, 2))
xy = np.random.uniform(0, 1, (20, 2))
xy = get(20, 0.1)

aco = ACO(xy, iterations=1000, alpha=1, beta=2, p=0.8, k=20,
aco = ACO(xy, iterations=1000, alpha=0.4, beta=5, p=0.2, k=20,
type='min-max')
aco.run(log=True)
cost, tour = aco.get_best_tour()
Expand Down
8 changes: 4 additions & 4 deletions problems/tsp/tsp_gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ def load_from_path(filename):

if __name__=="__main__":
np.random.seed(1234)
xy = np.random.uniform(0, 1, (20, 2))
xy = get(20, 0.1)
xy = np.random.uniform(0, 1, (10, 2))
#xy = get(10, 0.1)
#print(xy)
tour_length, tour = solve_dynamic_euclidian_tsp(xy)
#tour_length, tour = solve_euclidian_tsp(xy)
#tour_length, tour = solve_dynamic_euclidian_tsp(xy)
tour_length, tour = solve_euclidian_tsp(xy)
print("Tour length: ", tour_length, " Tour: ", tour)

fig, ax = plt.subplots(figsize=(10, 10))
Expand Down
Loading

0 comments on commit 582cb78

Please sign in to comment.