蚁群算法python代码
时间: 2023-09-11 13:04:54 浏览: 87
以下是蚁群算法的 Python 代码示例:
```
import random
class Ant:
def __init__(self, colony, alpha=1, beta=3):
self.colony = colony
self.alpha = alpha
self.beta = beta
self.reset()
def reset(self):
self.visited = set()
self.current_node = random.choice(self.colony.nodes)
self.visited.add(self.current_node)
def move(self):
available_nodes = self.colony.get_available_nodes(self.current_node, self.visited)
if not available_nodes:
return
probabilities = []
for node in available_nodes:
pheromone = self.colony.pheromone[self.current_node][node]
distance = self.colony.distance[self.current_node][node]
probability = (pheromone ** self.alpha) * ((1 / distance) ** self.beta)
probabilities.append((node, probability))
total_probability = sum(prob for node, prob in probabilities)
probabilities = [(node, prob / total_probability) for node, prob in probabilities]
next_node = self.choose_node(probabilities)
self.colony.add_pheromone(self.current_node, next_node)
self.current_node = next_node
self.visited.add(next_node)
def choose_node(self, probabilities):
probability_sum = 0
random_number = random.random()
for node, probability in probabilities:
probability_sum += probability
if random_number < probability_sum:
return node
class Colony:
def __init__(self, size, alpha=1, beta=3, evaporation=0.5, pheromone=1):
self.nodes = range(size)
self.alpha = alpha
self.beta = beta
self.evaporation = evaporation
self.pheromone = [[pheromone] * size for _ in range(size)]
self.distance = [[0] * size for _ in range(size)]
for i in range(size):
for j in range(i + 1, size):
distance = random.randint(1, 10)
self.distance[i][j] = distance
self.distance[j][i] = distance
self.reset()
def reset(self):
self.ants = [Ant(self, self.alpha, self.beta) for _ in range(len(self.nodes))]
def get_available_nodes(self, node, visited):
return [n for n in self.nodes if n not in visited and n != node]
def add_pheromone(self, node1, node2):
self.pheromone[node1][node2] *= (1 - self.evaporation)
self.pheromone[node1][node2] += (self.evaporation / self.distance[node1][node2])
def update_pheromone(self):
for i in range(len(self.nodes)):
for j in range(i + 1, len(self.nodes)):
self.pheromone[i][j] *= (1 - self.evaporation)
self.pheromone[i][j] += self.evaporation
def run(self, iterations):
best_path = None
best_distance = float('inf')
for i in range(iterations):
for ant in self.ants:
while len(ant.visited) < len(self.nodes):
ant.move()
distance = self.get_distance(ant.visited)
if distance < best_distance:
best_distance = distance
best_path = ant.visited
ant.reset()
self.update_pheromone()
return best_path, best_distance
def get_distance(self, path):
distance = 0
for i in range(len(path) - 1):
distance += self.distance[path[i]][path[i + 1]]
distance += self.distance[path[-1]][path[0]]
return distance
if __name__ == '__main__':
colony = Colony(10)
best_path, best_distance = colony.run(100)
print('Best path:', best_path)
print('Best distance:', best_distance)
```
代码实现了一个简单的蚁群算法,用于解决旅行商问题。其中,Ant 类代表一个蚂蚁,Colony 类代表蚁群,包含了蚂蚁和信息素的相关信息。整个算法的流程是,每个蚂蚁通过概率选择下一个节点进行移动,访问完所有节点后反馈信息素,更新信息素浓度,重复进行若干次迭代,最后得到最优解。
阅读全文