蚁群算法python实现
时间: 2023-07-27 17:07:06 浏览: 175
基于python的蚁群算法设计与实现
蚁群算法(Ant Colony Algorithm,简称 ACA)是一种基于“蚂蚁觅食”的行为特征而发展起来的一种启发式算法,适用于求解NP难问题。
下面是一个简单的 python 实现:
```python
import random
class Ant:
def __init__(self, node_num):
self.node_num = node_num # 节点数量
self.alpha = 1 # 信息素重要程度因子
self.beta = 2 # 启发式因子
self.pheromone_trace = [[1 / node_num for j in range(node_num)] for i in range(node_num)] # 初始化信息素矩阵
self.allowed_nodes = [i for i in range(node_num)] # 当前可选择的节点
self.visited_nodes = [] # 已经访问过的节点
self.current_node = random.choice(self.allowed_nodes) # 当前所在节点
def select_next_node(self):
# 计算可选择节点的概率
probability = [0 for i in range(self.node_num)]
total_probability = 0
for node in self.allowed_nodes:
probability[node] = pow(self.pheromone_trace[self.current_node][node], self.alpha) * \
pow(1 / self.distance_matrix[self.current_node][node], self.beta)
total_probability += probability[node]
# 选择下一个节点
if total_probability > 0:
selected_node = None
rand = random.uniform(0, total_probability)
for node, prob in enumerate(probability):
if rand <= prob:
selected_node = node
break
rand -= prob
if selected_node is None:
selected_node = random.choice(self.allowed_nodes)
else:
selected_node = random.choice(self.allowed_nodes)
# 更新信息素矩阵
self.pheromone_trace[self.current_node][selected_node] += 1 / self.distance_matrix[self.current_node][
selected_node]
self.pheromone_trace[selected_node][self.current_node] = self.pheromone_trace[self.current_node][selected_node]
# 移动到下一个节点
self.visited_nodes.append(selected_node)
self.allowed_nodes.remove(selected_node)
self.current_node = selected_node
def run(self, distance_matrix):
self.distance_matrix = distance_matrix
while self.allowed_nodes:
self.select_next_node()
# 计算路径长度
length = 0
for i in range(self.node_num):
length += distance_matrix[self.visited_nodes[i - 1]][self.visited_nodes[i]]
return self.visited_nodes, length
class ACO:
def __init__(self, ant_num, node_num, max_iteration):
self.ant_num = ant_num # 蚂蚁数量
self.max_iteration = max_iteration # 最大迭代次数
self.node_num = node_num # 节点数量
self.best_path = None # 最优路径
self.best_length = float('inf') # 最优路径长度
self.ants = [Ant(node_num) for i in range(ant_num)] # 初始化蚂蚁
def run(self, distance_matrix):
for iteration in range(self.max_iteration):
paths = []
lengths = []
# 所有蚂蚁搜索一遍
for ant in self.ants:
path, length = ant.run(distance_matrix)
paths.append(path)
lengths.append(length)
# 更新最优路径
if length < self.best_length:
self.best_path = path
self.best_length = length
# 更新信息素矩阵
pheromone_delta = [[1 / lengths[i] for j in range(self.node_num)] for i in range(self.ant_num)]
for i in range(self.ant_num):
for j in range(self.node_num - 1):
pheromone_delta[i][paths[i][j]][paths[i][j + 1]] += 1 / lengths[i]
pheromone_delta[i][paths[i][j + 1]][paths[i][j]] = pheromone_delta[i][paths[i][j]][paths[i][j + 1]]
for i in range(self.node_num):
for j in range(self.node_num):
self.ants[0].pheromone_trace[i][j] = (1 - 0.1) * self.ants[0].pheromone_trace[i][j] + \
sum([pheromone_delta[k][i][j] for k in range(self.ant_num)]) * 0.1
```
这里实现了两个类:`Ant` 和 `ACO`。`Ant` 表示一个蚂蚁,包括选择下一个节点、更新信息素矩阵等方法;`ACO` 表示整个蚁群算法,包括初始化蚂蚁、搜索路径、更新信息素矩阵等方法。
使用时,需要传入距离矩阵,示例代码如下:
```python
import numpy as np
distance_matrix = np.array([
[0, 1, 2, 3, 4, 5],
[1, 0, 1, 2, 3, 4],
[2, 1, 0, 1, 2, 3],
[3, 2, 1, 0, 1, 2],
[4, 3, 2, 1, 0, 1],
[5, 4, 3, 2, 1, 0]
])
aco = ACO(ant_num=10, node_num=6, max_iteration=100)
aco.run(distance_matrix)
print(aco.best_path) # [0, 1, 2, 3, 4, 5]
print(aco.best_length) # 15
```
其中 `distance_matrix` 表示节点之间的距离,`aco.best_path` 和 `aco.best_length` 分别表示最优路径和最优路径长度。
阅读全文