-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantColony.py
226 lines (207 loc) · 9.41 KB
/
antColony.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import random as rn
import numpy as np
from numpy.random import choice as np_choice
from matplotlib import pyplot as plt
import time
SAVE_DATA = True
class AntColony(object):
def __init__(self, distances, n_ants, n_best, n_iterations, decay, uav_num, filename='result', alpha=1, beta=1,max_time=0):
"""
Args:
distances (2D numpy.array): Square matrix of distances. Diagonal is assumed to be np.inf.
n_ants (int): Number of ants running per iteration
n_best (int): Number of best ants who deposit pheromone
n_iteration (int): Number of iterations
decay (float): Rate it which pheromone decays. The pheromone value is multiplied by decay, so 0.95 will lead to decay, 0.5 to much faster decay.
alpha (int or float): exponenet on pheromone, higher alpha gives pheromone more weight. Default=1
beta (int or float): exponent on distance, higher beta give distance more weight. Default=1
uav_num(int): the number of the uav
filename(str): save the data into csv file
max_time(int): per uav execute the maxmium number of the tasks
Example:
ant_colony = AntColony(german_distances, 100, 20, 2000, 0.95, alpha=1, beta=2)
"""
self.distances = distances
self.max_num = distances[0][0]
self.pheromone = np.ones(self.distances.shape) / len(distances)
self.last_pheromone = np.copy(self.pheromone) # used to mutation
self.all_inds = range(len(distances))
self.n_ants = n_ants
self.n_best = n_best
self.n_iterations = n_iterations
self.decay = decay
self.alpha = alpha
self.beta = beta
self.uav_num = uav_num
if max_time == 0 or max_time > self.uav_num:
self.uav_visited_max_time = self.uav_num
else:
self.uav_visited_max_time = max_time
print(self.uav_visited_max_time)
# drawing
self.draw_x = np.arange(n_iterations)
self.draw_y = np.ones(n_iterations)
# save data
self.filename = filename
def run(self):
shortest_path = None
all_time_shortest_path = ("placeholder", np.inf)
start_time = time.time()
count_stable = 0
for i in range(self.n_iterations):
# reduce the number of the ants
# if i % (self.n_iterations/4) == 0 and i!=0:
# self.n_ants = int(self.n_ants/2)
# self.n_best = int(self.n_best/2)
all_paths = self.gen_all_paths()
self.spread_pheronome(all_paths, self.n_best, shortest_path=shortest_path)
shortest_path = min(all_paths, key=lambda x: x[1])
# print (shortest_path)
if shortest_path[1] < all_time_shortest_path[1]:
all_time_shortest_path = shortest_path
# print('last',self.last_pheromone)
self.last_pheromone = np.copy(self.pheromone) # used for mutation
# print('newest',self.last_pheromone)
count_stable = 0
self.pheromone = self.pheromone * 0.9
# self.draw_y[i] = all_time_shortest_path[1] - self.uav_num
self.draw_y[i] = all_time_shortest_path[1]
count_stable += 1
# if count_stable >= 100: # break if the shortest path keep stable more than 100 times
# self.draw_y[i:] = self.draw_y[i]
# break
if count_stable >= 50: # mutation if the shortest path keep stable more than 30 times
self.random_pick_mutation()
count_stable = 0
print('total time:',time.time()-start_time)
# if SAVE_DATA:
# self.saveData()
self.drawing()
return all_time_shortest_path
def random_pick_mutation(self):
""" mutation
"""
print('mutation')
# mutation_list = []
length = len(self.distances)
mutation_num = int(length*length/3)
# print('last',self.last_pheromone)
# print('newest',self.pheromone)
# print('===========compare============')
# print(self.pheromone-self.last_pheromone)
for i in range(mutation_num):
x,y = np.random.randint(self.uav_num),np.random.randint(length) # randomly pick one edge to mutation
# print('x:{},y:{}'.format(x,y))
# print('before:{},after:{}'.format(self.pheromone[x][y],self.last_pheromone[x][y]))
# if self.pheromone[x][y] != self.last_pheromone[x][y]:
# print('before:{},after:{}'.format(self.pheromone[x][y],self.last_pheromone[x][y]))
self.pheromone[x][y] = self.last_pheromone[x][y] # mutation
def spread_pheronome(self, all_paths, n_best, shortest_path):
sorted_paths = sorted(all_paths, key=lambda x: x[1])
for path, dist in sorted_paths[:n_best]:
for move in path:
# self.pheromone[move] += 1.0 / self.distances[move]
self.pheromone[move] = self.decay * self.pheromone[move] + 1.0/self.distances[move]
def gen_path_dist(self, path):
total_dist = 0
for ele in path:
total_dist += self.distances[ele]
return total_dist
def gen_all_paths(self):
"""generate paths for all ants
"""
all_paths = []
for i in range(self.n_ants):
start = np.random.randint(self.uav_num)
path = self.gen_path(0)
# path = self.gen_path(start) # randomly generate the start point
all_paths.append((path, self.gen_path_dist(path)))
return all_paths
def gen_path(self, start):
"""generate one path
"""
path = []
visited = set()
visited_task = set()
self.uav_visited_time = np.zeros([1,self.uav_num])
visited.add(start)
self.uav_visited_time[0][start] += 1
prev = start
# for i in range(len(self.distances) - 1):
while len(visited) < len(self.distances): # all the nodes should be visited at least once
# while True:
# print('visited',visited)
move = self.pick_move(self.pheromone[prev], self.distances[prev], visited)
path.append((prev, move))
prev = move
if move not in visited:
visited.add(move)
if move < self.uav_num:
self.uav_visited_time[0][move] += 1
# if move not in visited_task and move >= self.uav_num: # Chinese version
# visited_task.add(move)
# if len(visited_task) == len(self.distances)-self.uav_num:
# break
path.append((prev, start)) # going back to where we startqed
return path
def pick_move(self, pheromone, dist, visited):
pheromone = np.copy(pheromone)
for i in visited:
if i >= self.uav_num: # node i is the target
pheromone[i] = 0 # node i cannot be visited again
elif i < self.uav_num:# node i is the uav
if self.uav_visited_time[0][i] >= self.uav_visited_max_time:
pheromone[i] = 0
# pheromone[list(visited)] = 0
row = pheromone ** self.alpha * (( 1.0 / dist) ** self.beta)
norm_row = (row / row.sum())
# print('norm_row',norm_row)
move = np_choice(self.all_inds, 1, p=norm_row)[0]
return move
def drawing(self):
plt.title("Result")
plt.xlabel('iteration')
plt.ylabel('cost')
plt.plot(self.draw_x,self.draw_y)
if SAVE_DATA:
plt.savefig('{}_fig.png'.format(self.filename),dpi=450)
# plt.show()
def saveData(self):
np.save('{}_x.npy'.format(self.filename),self.draw_x)
np.save('{}_y.npy'.format(self.filename),self.draw_y)
print('data has been saved')
def printAllocation(self,path,cost_matrix,uav_num,uav_x=None,uav_y=None,target_x=None,target_y=None,draw_map=True):
"""print allocation according to the best path.
:param path: the best path calculated by the ant colony
:param cost_matrix: the cost matrix
:param uav_num: the number of the uav
"""
cost = 0
cost_matrix = np.array(cost_matrix)
path = path[0]
path.sort()
if draw_map:
plt.title('result')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([1,110])
plt.ylim([1,110])
for item in path:
i,j = item[0],item[1]
if i<j:
temp_i = i+1
temp_j = j+1-uav_num
print('第{}个人完成第{}个任务'.format(temp_i,temp_j))
cost += cost_matrix[i][j-uav_num]
if draw_map:
x1,y1,x2,y2 = uav_x[i],uav_y[i],target_x[j-uav_num],target_y[j-uav_num]
plt.scatter(x1,y1,marker='o',color='red',s=100)
plt.text(x1+1,y1+1,'uav'+str(temp_i),fontsize=10)
plt.scatter(x2,y2,marker='v',color='blue',s=100)
plt.plot([x1,x2],[y1,y2],label='task_{}'.format(i+1))
plt.text(x2+1,y2+1,'target'+str(temp_j),fontsize=10)
# plt.arrow(x1,y1,x2-x1,y2-y1,width=1)
print('总代价',cost)
plt.legend(loc='center left',bbox_to_anchor=(1,0.5))
# plt.savefig('result_{}_ant.png'.format(len(cost_matrix)),dpi=450,bbox_inches='tight')
# plt.show()