-
Notifications
You must be signed in to change notification settings - Fork 21
/
aco.py
188 lines (160 loc) · 6.93 KB
/
aco.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import torch
from torch.distributions import Categorical
class ACO():
def __init__(self,
distances,
n_ants=20,
decay=0.9,
alpha=1,
beta=1,
elitist=False,
min_max=False,
pheromone=None,
heuristic=None,
min=None,
device='cpu'
):
self.problem_size = len(distances)
self.distances = distances
self.n_ants = n_ants
self.decay = decay
self.alpha = alpha
self.beta = beta
self.elitist = elitist
self.min_max = min_max
if min_max:
if min is not None:
assert min > 1e-9
else:
min = 0.1
self.min = min
self.max = None
if pheromone is None:
self.pheromone = torch.ones_like(self.distances)
if min_max:
self.pheromone = self.pheromone * self.min
else:
self.pheromone = pheromone
self.heuristic = 1 / distances if heuristic is None else heuristic
self.shortest_path = None
self.lowest_cost = float('inf')
self.device = device
@torch.no_grad()
def sparsify(self, k_sparse):
'''
Sparsify the TSP graph to obtain the heuristic information
Used for vanilla ACO baselines
'''
_, topk_indices = torch.topk(self.distances,
k=k_sparse,
dim=1, largest=False)
edge_index_u = torch.repeat_interleave(
torch.arange(len(self.distances), device=self.device),
repeats=k_sparse
)
edge_index_v = torch.flatten(topk_indices)
sparse_distances = torch.ones_like(self.distances) * 1e10
sparse_distances[edge_index_u, edge_index_v] = self.distances[edge_index_u, edge_index_v]
self.heuristic = 1 / sparse_distances
def sample(self):
paths, log_probs = self.gen_path(require_prob=True)
costs = self.gen_path_costs(paths)
return costs, log_probs
@torch.no_grad()
def run(self, n_iterations):
for _ in range(n_iterations):
paths = self.gen_path(require_prob=False)
costs = self.gen_path_costs(paths)
best_cost, best_idx = costs.min(dim=0)
if best_cost < self.lowest_cost:
self.shortest_path = paths[:, best_idx]
self.lowest_cost = best_cost
if self.min_max:
max = self.problem_size / self.lowest_cost
if self.max is None:
self.pheromone *= max/self.pheromone.max()
self.max = max
self.update_pheronome(paths, costs)
return self.lowest_cost
@torch.no_grad()
def update_pheronome(self, paths, costs):
'''
Args:
paths: torch tensor with shape (problem_size, n_ants)
costs: torch tensor with shape (n_ants,)
'''
self.pheromone = self.pheromone * self.decay
if self.elitist:
best_cost, best_idx = costs.min(dim=0)
best_tour= paths[:, best_idx]
self.pheromone[best_tour, torch.roll(best_tour, shifts=1)] += 1.0/best_cost
self.pheromone[torch.roll(best_tour, shifts=1), best_tour] += 1.0/best_cost
else:
for i in range(self.n_ants):
path = paths[:, i]
cost = costs[i]
self.pheromone[path, torch.roll(path, shifts=1)] += 1.0/cost
self.pheromone[torch.roll(path, shifts=1), path] += 1.0/cost
if self.min_max:
self.pheromone[(self.pheromone > 1e-9) * (self.pheromone) < self.min] = self.min
self.pheromone[self.pheromone > self.max] = self.max
@torch.no_grad()
def gen_path_costs(self, paths):
'''
Args:
paths: torch tensor with shape (problem_size, n_ants)
Returns:
Lengths of paths: torch tensor with shape (n_ants,)
'''
assert paths.shape == (self.problem_size, self.n_ants)
u = paths.T # shape: (n_ants, problem_size)
v = torch.roll(u, shifts=1, dims=1) # shape: (n_ants, problem_size)
assert (self.distances[u, v] > 0).all()
return torch.sum(self.distances[u, v], dim=1)
def gen_path(self, require_prob=False):
'''
Tour contruction for all ants
Returns:
paths: torch tensor with shape (problem_size, n_ants), paths[:, i] is the constructed tour of the ith ant
log_probs: torch tensor with shape (problem_size, n_ants), log_probs[i, j] is the log_prob of the ith action of the jth ant
'''
start = torch.randint(low=0, high=self.problem_size, size=(self.n_ants,), device=self.device)
mask = torch.ones(size=(self.n_ants, self.problem_size), device=self.device)
mask[torch.arange(self.n_ants, device=self.device), start] = 0
paths_list = [] # paths_list[i] is the ith move (tensor) for all ants
paths_list.append(start)
log_probs_list = [] # log_probs_list[i] is the ith log_prob (tensor) for all ants' actions
prev = start
for _ in range(self.problem_size-1):
actions, log_probs = self.pick_move(prev, mask, require_prob)
paths_list.append(actions)
if require_prob:
log_probs_list.append(log_probs)
mask = mask.clone()
prev = actions
mask[torch.arange(self.n_ants, device=self.device), actions] = 0
if require_prob:
return torch.stack(paths_list), torch.stack(log_probs_list)
else:
return torch.stack(paths_list)
def pick_move(self, prev, mask, require_prob):
'''
Args:
prev: tensor with shape (n_ants,), previous nodes for all ants
mask: bool tensor with shape (n_ants, p_size), masks (0) for the visited cities
'''
pheromone = self.pheromone[prev] # shape: (n_ants, p_size)
heuristic = self.heuristic[prev] # shape: (n_ants, p_size)
dist = ((pheromone ** self.alpha) * (heuristic ** self.beta) * mask) # shape: (n_ants, p_size)
dist = Categorical(dist)
actions = dist.sample() # shape: (n_ants,)
log_probs = dist.log_prob(actions) if require_prob else None # shape: (n_ants,)
return actions, log_probs
if __name__ == '__main__':
torch.set_printoptions(precision=3,sci_mode=False)
input = torch.rand(size=(5, 2))
distances = torch.norm(input[:, None] - input, dim=2, p=2)
distances[torch.arange(len(distances)), torch.arange(len(distances))] = 1e10
aco = ACO(distances)
aco.sparsify(k_sparse=3)
print(aco.run(20))