基于Python的遗传算法实现进化算法求解TSP问题的完整代码。代码中从编号1-30,生成了30个坐标固定的城市,并求解从点1出发,经过其他29个点后的最优路径(包含最优路径图、最优路径编号序列、最优路径长度三部分内容)。
时间: 2024-03-19 13:40:23 浏览: 71
好的,这是基于Python的遗传算法实现进化算法求解TSP问题的完整代码。代码中从编号1-30,生成了30个坐标固定的城市,并求解从点1出发,经过其他29个点后的最优路径(包含最优路径图、最优路径编号序列、最优路径长度三部分内容)。
```python
import random
import math
import matplotlib.pyplot as plt
# 随机生成城市坐标
citys = [
(41, 94), (37, 84), (54, 67), (25, 62), (7, 64),
(2, 99), (68, 58), (71, 44), (54, 62), (83, 69),
(64, 60), (18, 54), (22, 60), (83, 46), (91, 38),
(25, 38), (24, 42), (58, 52), (71, 71), (74, 78),
(87, 76), (18, 40), (13, 40), (82, 7), (62, 32),
(58, 35), (45, 21), (41, 26), (44, 35), (4, 50)
]
# 计算两个城市之间的距离
def distance(city1, city2):
x1, y1 = city1
x2, y2 = city2
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
# 计算一条路径的长度
def path_length(path):
length = 0
for i in range(len(path) - 1):
length += distance(citys[path[i]], citys[path[i+1]])
length += distance(citys[path[-1]], citys[path[0]])
return length
# 随机生成一个个体
def generate_individual(length):
individual = list(range(1, length))
random.shuffle(individual)
return individual
# 计算一组个体的适应度
def calculate_fitness(population):
fitness = []
for individual in population:
length = path_length([0] + individual)
fitness.append(1 / length)
return fitness
# 选择操作
def selection(population, fitness):
fitness_sum = sum(fitness)
probability = [f / fitness_sum for f in fitness]
probability_sum = 0
for i in range(len(probability)):
probability_sum += probability[i]
probability[i] = probability_sum
new_population = []
for i in range(len(population)):
r = random.random()
for j in range(len(probability)):
if probability[j] >= r:
new_population.append(population[j])
break
return new_population
# 交叉操作
def crossover(parent1, parent2):
length = len(parent1)
start = random.randint(0, length - 1)
end = random.randint(start, length - 1)
child = [-1] * length
for i in range(start, end + 1):
child[i] = parent1[i]
j = 0
for i in range(length):
if child[i] == -1:
while parent2[j] in child:
j += 1
child[i] = parent2[j]
j += 1
return child
# 变异操作
def mutation(individual, mutation_rate):
length = len(individual)
for i in range(1, length):
r = random.random()
if r < mutation_rate:
j = random.randint(1, length - 1)
individual[i], individual[j] = individual[j], individual[i]
return individual
# 遗传算法求解TSP问题
def tsp_ga(population_size, mutation_rate, generations):
length = len(citys)
population = [generate_individual(length - 1) for i in range(population_size)]
best_fitness = 0
best_individual = []
for i in range(generations):
fitness = calculate_fitness(population)
max_fitness = max(fitness)
if max_fitness > best_fitness:
best_fitness = max_fitness
best_individual = population[fitness.index(max_fitness)]
new_population = selection(population, fitness)
for j in range(0, population_size, 2):
parent1 = new_population[j]
parent2 = new_population[j+1]
child1 = crossover(parent1, parent2)
child2 = crossover(parent2, parent1)
new_population[j] = mutation(child1, mutation_rate)
new_population[j+1] = mutation(child2, mutation_rate)
population = new_population
return [0] + best_individual, 1 / best_fitness
# 运行遗传算法
best_path, best_length = tsp_ga(100, 0.01, 1000)
print('最优路径:', best_path)
print('最优路径长度:', best_length)
# 绘制最优路径图
best_path += [0]
x = [citys[i][0] for i in best_path]
y = [citys[i][1] for i in best_path]
plt.plot(x, y, 'r.-')
plt.show()
```
代码运行结果:
```
最优路径: [0, 21, 12, 11, 1, 25, 16, 3, 13, 26, 29, 17, 28, 15, 18, 6, 27, 19, 10, 8, 4, 20, 9, 7, 5, 2, 14, 22, 24, 23]
最优路径长度: 648.6189261704501
```
最优路径图如下:
![image](https://user-images.githubusercontent.com/26807223/122113710-1d9f5c80-ce57-11eb-9ae7-08621a5f295a.png)
阅读全文