-
Notifications
You must be signed in to change notification settings - Fork 0
/
aco.py
204 lines (159 loc) · 6.14 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from collections import namedtuple
import sys
import time
import networkx as nx
import random
import constants
from chatbot_graph import ChatbotGraph
from graph import Graph
from transformation_graph import TransformationGraph
Edge = namedtuple("Edge", ["start", "end", "data"])
class Ant:
alpha = constants.ALPHA
beta = constants.BETA
evaporation_rate = constants.EVAPORATION_RATE
budget = constants.LENGTH_OF_PATH
def __init__(self, id):
self.id = id
self.current_node = "START"
self.route = []
self.travel_distance = 0
def __str__(self):
return f"{self.id}, {self.route}, {self.travel_distance}"
def reset(self):
self.current_node = "START"
self.route = []
self.travel_distance = 0
def traverse(self, G: nx.DiGraph):
count = 0
while count < self.budget:
count += 1
next_edge = self.select(G)
self.route.append(next_edge)
self.current_node = next_edge.end
self.travel_distance += next_edge.data["weight"]
def _calc_probabilities(self, edges):
"""
p_ij <- tau_ij^alpha * eta^beta / sigma p_ij
p_ij: probability of select node i to j
eta: 1 / c_ij
c_ij: weight of node i to j
"""
probabilities = []
for edge in edges:
pheromone = edge.data["pheromone"]
importance = 1 / edge.data["weight"]
probabilities.append(pheromone**self.alpha * importance**self.beta)
sum_p = sum(probabilities)
return [p / sum_p for p in probabilities]
def select(self, G) -> Edge:
edges = list(G.edges.data(data=True, nbunch=self.current_node))
edges = [
Edge(*e) for e in edges if e[1] != "START"
] # Exclude edge when end is "START".
if random.random() < constants.RANDOM_CHOICE_RATE:
probabilities = self._calc_probabilities(edges)
next_edge = random.choices(population=edges, weights=probabilities, k=1)[0]
else:
next_edge = random.choice(edges)
self.update_pheromone_locally(G, next_edge)
return next_edge
def update_pheromone_locally(self, G, edge):
"""
tau_xy <- (1-rho) * tau_xy + rho * tau_0
tau_xy: pheromone for node x to y
rho: coefficient of evaporation
"""
G[edge.start][edge.end]["pheromone"] = (1 - self.evaporation_rate) * G[
edge.start
][edge.end]["pheromone"]
G[edge.start][edge.end]["pheromone"] += (
self.evaporation_rate * AntColony.initial_pheromone
)
def update_pheromone_globally(self, G):
"""
tau_xy <- (1-rho) * tau_xy + delta tau_xy_k
delta tau_xy_k = Q / L_k
tau_xy: pheromone for node x to y
rho: coefficient of evaporation
"""
visited = [(edge.start, edge.end) for edge in self.route]
cost = sum([data["weight"] for _, _, data in self.route])
for start, end in G.edges():
G[start][end]["pheromone"] = (1 - self.evaporation_rate) * G[start][end][
"pheromone"
]
G[start][end]["pheromone"] += 1 / cost if (start, end) in visited else 0
print(f"ant: {self.id}")
for edge in self.route:
print(edge)
print(cost)
def convert_route_to_transformation_path(self):
"""
EX)
route:
[Edge(start='START', end='REMOVE_NODE I', data={'weight': 1, 'pheromone': 0.5464705452296552}),
Edge(start='REMOVE_NODE I', end='REMOVE_EDGE H J', data={'weight': 1, 'pheromone': 0.39553318404096}),
Edge(start='REMOVE_EDGE H J', end='REMOVE_NODE C', data={'weight': 1, 'pheromone': 0.3867572576256})]
->
transformation_path:
["REMOVE_NODE I", "REMOVE_EDGE H J", "REMOVE_NODE C]
"""
transformation_path = []
for path in self.route:
transformation_path.append(path.end)
return transformation_path
class AntColony:
initial_pheromone = constants.INITIAL_PHEROMONE
budget = constants.ITERATION_BUDGET
def __init__(self, CG: ChatbotGraph):
self.CG = CG
self.CG.evaluate()
self.TG = TransformationGraph(CG)
def init_pheromone(self):
for node in list(self.TG.graph.nodes()):
self.TG.graph.add_edge("START", node, weight=constants.WEIGHT)
for start, end in self.TG.graph.edges():
self.TG.graph[start][end]["pheromone"] = self.initial_pheromone
def aco(self):
self.init_pheromone()
ants = [Ant(i) for i in range(constants.ANT_COUNT)]
fitness = self.CG.fitness
shortest_path = []
count = 0
while count < self.budget:
count += 1
start = time.time()
best_ant = ants[0]
best_ant_fitness = self.CG.fitness
for ant in ants:
ant.traverse(self.TG.graph)
new_CG = self.CG.copy()
transformation_path = ant.convert_route_to_transformation_path()
new_CG.transform(transformation_path)
new_CG.evaluate()
if new_CG.fitness > best_ant_fitness:
# Best ant per iteration can update pheromone.
best_ant = ant
best_ant_fitness = new_CG.fitness
if new_CG.fitness > fitness:
# Best fitness per whole ACO.
fitness = new_CG.fitness
shortest_path = transformation_path
best_ant.update_pheromone_globally(self.TG.graph)
print("fitness:", fitness)
for ant in ants:
ant.reset()
print(f"{count}: elasped time: {time.time() - start:.4f}s")
print("shortest: ")
for edge in shortest_path:
print(edge)
print(fitness)
return shortest_path
if __name__ == "__main__":
CG = ChatbotGraph("lead-homepage")
ant_colony = AntColony(CG)
shortest_path = ant_colony.aco()
optimized_CG = CG.copy()
optimized_CG.transform(shortest_path)
Graph.draw_graphs(CG, optimized_CG)