-
Notifications
You must be signed in to change notification settings - Fork 0
/
ant_search.py
148 lines (105 loc) · 3.96 KB
/
ant_search.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
import numpy as np
import random
from functools import total_ordering
def ant_search(graph, start, n_ants=50, q=100, rho=0.99, alpha=1, beta=5):
cities_count = graph.shape[0]
ants = _init_ants(cities_count, n_ants)
phs = _init_pheromones(graph)
for i in range(cities_count):
_turn(ants, phs, graph, q, rho, alpha, beta)
best_ant = _find_best_ant(ants, start)
return best_ant.path, best_ant.cost, None
@total_ordering
class _Ant:
def __init__(self, city):
self.path = [city]
self.cost = 0
def __repr__(self):
return f'Ant(cost={self.cost}; path={self.path})'
def __lt__(self, other):
return self.cost < other.cost
@property
def city(self):
return self.path[-1]
@property
def start(self):
return self.path[0]
def go(self, next_city, graph):
self.cost += graph[self.city, next_city]
self.path.append(next_city)
def _init_ants(cities_count, n_ants):
return [_Ant(city) for city in range(cities_count) for _ in range(n_ants)]
def _init_pheromones(graph):
phs = graph.copy()
phs[phs > np.NINF] = 0
return phs
def _turn(ants, phs, graph, q, rho, alpha, beta):
_update_ants(ants, phs, graph, alpha, beta)
_update_pheromones(ants, phs, q, rho)
def _update_ants(ants, phs, graph, alpha, beta):
ants_to_kill = []
for ant in ants:
live = _update_ant(ant, graph, phs, alpha, beta)
if not live:
ants_to_kill.append(ant)
for ant in ants_to_kill:
ants.remove(ant)
def _update_ant(ant, graph, phs, alpha, beta):
next_paths, probabilities = _get_next_paths_and_probabilities(ant.path, graph, phs, alpha, beta)
if len(next_paths) == 0:
return False
selected_path = random.choices(next_paths, weights=probabilities, k=1)[0]
ant.go(selected_path[-1], graph)
return True
def _get_next_paths_and_probabilities(path, graph, phs, alpha, beta):
next_paths = _get_next_paths(path, graph)
numerators = [_get_probability_numerator(path, graph, phs, alpha, beta) for path in next_paths]
denominator = sum(numerators) if numerators else 0
if denominator == 0:
probabilities = np.ones((len(numerators,))) / len(numerators)
else:
probabilities = [num / denominator for num in numerators]
return next_paths, probabilities
def _get_probability_numerator(path, graph, phs, alpha, beta):
if len(path) == 2:
return 1
current_city, next_city = path[-2:]
visibility = 1 / graph[current_city, next_city]
numerator = phs[current_city, next_city]**alpha * visibility**beta
return numerator
def _update_pheromones(ants, phs, q, rho):
np.multiply(phs, rho, out=phs)
for ant in ants:
prev_city, next_city = ant.path[-2:]
delta_phs = q / ant.cost
phs[prev_city, next_city] += delta_phs
def _find_best_ant(ants, start):
start_ants = [ant for ant in ants if ant.start == start]
best_ants = sorted(start_ants)
return best_ants[0]
def _get_next_paths(current_path, graph):
cities_count = graph.shape[1]
current_path_length = len(current_path)
if current_path_length == cities_count:
return _get_next_paths_final_step(current_path, graph)
else:
return _get_next_paths_standard_step(current_path, graph)
def _get_next_paths_standard_step(current_path, graph):
next_paths = []
cities_count = graph.shape[1]
current_city = current_path[-1]
for next_city in range(cities_count):
weight = graph[current_city][next_city]
if next_city in current_path or weight == np.NINF:
continue
next_path = current_path + [next_city]
next_paths.append(next_path)
return next_paths
def _get_next_paths_final_step(current_path, graph):
first_city = current_path[0]
current_city = current_path[-1]
cost = graph[current_city][first_city]
if cost == np.NINF:
return []
new_path = current_path + [first_city]
return [new_path]