class="hide-preCode-box">
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
(5)更新信息素
每迭代一次之后,更新所有城市之间的信息素。首先建一个信息素的增加量矩阵,存放该次迭代后每条路径的信息素增量。每条路径上的信息素等于信息素自身挥发后剩余的信息素加上每只蚂蚁经过城市i到城市j留下的信息素。
注意,更新信息素其实有三种方式。我选择的是第一种:△τ=Q/L,Q为常数,描述蚂蚁释放信息素的浓度,L为每只蚂蚁周游中所走路径的总长度。即每只蚂蚁从城市i到城市j之间信息素的增量等于Q除以周游中所走路径的长度。对于同一只蚂蚁,所有的路径上(任意两个城市之间)△τ是一样的。
changepheromonetable = np. zeros( ( city_count, city_count) )
for i in range ( AntCount) :
for j in range ( city_count - 1 ) :
changepheromonetable[ candidate[ i, j] ] [ candidate[ i] [ j + 1 ] ] += Q / length[ i]
changepheromonetable[ candidate[ i, j + 1 ] ] [ candidate[ i, 0 ] ] += Q / length[ i]
pheromonetable = ( 1 - rho) * pheromonetable + changepheromonetable
iter += 1
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
(6)30城市的坐标.txt
下面是30城市的坐标,大家建一个‘30城市的坐标.txt’文件然后放到和代码同目录下就可以啦。
1,41,94 2,37,84 3,53,67 4,25,62 5,7,64 6,2,99 7,68,58 8,71, 44 9,54,62 10,83,69 11,64,60 12,18,54 13,22,60 14,83,46 15,91,38 16,25,38 17,24,42 18,58,69 19,71,71 20,74,78 21,87,76 22,18,40 23,13,40 24,82,7 25,62,32 26,58,35 27,45,21 28,41,26 29,44,35 30,4,50
(7) 完整代码
import numpy as np
import matplotlib. pyplot as plt
import matplotlib
import math
import random
matplotlib. rcParams[ 'font.family' ] = 'STSong'
city_name = [ ]
city_condition = [ ]
with open ( '30城市的坐标.txt' , 'r' , encoding= 'UTF-8' ) as f:
lines = f. readlines( )
for line in lines:
line = line. split( '\n' ) [ 0 ]
line = line. split( ',' )
city_name. append( line[ 0 ] )
city_condition. append( [ float ( line[ 1 ] ) , float ( line[ 2 ] ) ] )
city_condition = np. array( city_condition)
city_count = len ( city_name)
Distance = np. zeros( ( city_count, city_count) )
for i in range ( city_count) :
for j in range ( city_count) :
if i != j:
Distance[ i] [ j] = math. sqrt( ( city_condition[ i] [ 0 ] - city_condition[ j] [ 0 ] ) ** 2 + ( city_condition[ i] [ 1 ] - city_condition[ j] [ 1 ] ) ** 2 )
else :
Distance[ i] [ j] = 100000
AntCount = 100
city_count = len ( city_name)
alpha = 1
beta = 2
rho = 0.1
iter = 0
MAX_iter = 200
Q = 1
pheromonetable = np. ones( ( city_count, city_count) )
candidate = np. zeros( ( AntCount, city_count) ) . astype( int )
path_best = np. zeros( ( MAX_iter, city_count) )
distance_best = np. zeros( MAX_iter)
etable = 1.0 / Distance
while iter < MAX_iter:
if AntCount <= city_count:
candidate[ : , 0 ] = np. random. permutation( range ( city_count) ) [ : AntCount]
else :
m = AntCount - city_count
n = 2
candidate[ : city_count, 0 ] = np. random. permutation( range ( city_count) ) [ : ]
while m > city_count:
candidate[ city_count* ( n - 1 ) : city_count* n, 0 ] = np. random. permutation( range ( city_count) ) [ : ]
m = m - city_count
n = n + 1
candidate[ city_count* ( n- 1 ) : AntCount, 0 ] = np. random. permutation( range ( city_count) ) [ : m]
length = np. zeros( AntCount)
for i in range ( AntCount) :
unvisit = list ( range ( city_count) )
visit = candidate[ i, 0 ]
unvisit. remove( visit)
for j in range ( 1 , city_count) :
protrans = np. zeros( len ( unvisit) )
for k in range ( len ( unvisit) ) :
protrans[ k] = np. power( pheromonetable[ visit] [ unvisit[ k] ] , alpha) * np. power(
etable[ visit] [ unvisit[ k] ] , ( alpha + 1 ) )
cumsumprobtrans = ( protrans / sum ( protrans) ) . cumsum( )
cumsumprobtrans -= np. random. rand( )
k = unvisit[ list ( cumsumprobtrans > 0 ) . index( True ) ]
candidate[ i, j] = k
unvisit. remove( k)
length[ i] += Distance[ visit] [ k]
visit = k
length[ i] += Distance[ visit] [ candidate[ i, 0 ] ]
"""
更新路径等参数
"""
if iter == 0 :
distance_best[ iter ] = length. min ( )
path_best[ iter ] = candidate[ length. argmin( ) ] . copy( )
else :
if length. min ( ) > distance_best[ iter - 1 ] :
distance_best[ iter ] = distance_best[ iter - 1 ]
path_best[ iter ] = path_best[ iter - 1 ] . copy( )
else :
distance_best[ iter ] = length. min ( )
path_best[ iter ] = candidate[ length. argmin( ) ] . copy( )
"""
信息素的更新
"""
changepheromonetable = np. zeros( ( city_count, city_count) )
for i in range ( AntCount) :
for j in range ( city_count - 1 ) :
changepheromonetable[ candidate[ i, j] ] [ candidate[ i] [ j + 1 ] ] += Q / length[ i]
changepheromonetable[ candidate[ i, j + 1 ] ] [ candidate[ i, 0 ] ] += Q / length[ i]
pheromonetable = ( 1 - rho) * pheromonetable + changepheromonetable
iter += 1
print ( "蚁群算法的最优路径" , path_best[ - 1 ] + 1 )
print ( "迭代" , MAX_iter, "次后" , "蚁群算法求得最优解" , distance_best[ - 1 ] )
fig = plt. figure( )
plt. title( "Best roadmap" )
x = [ ]
y = [ ]
path = [ ]
for i in range ( len ( path_best[ - 1 ] ) ) :
x. append( city_condition[ int ( path_best[ - 1 ] [ i] ) ] [ 0 ] )
y. append( city_condition[ int ( path_best[ - 1 ] [ i] ) ] [ 1 ] )
path. append( int ( path_best[ - 1 ] [ i] ) + 1 )
x. append( x[ 0 ] )
y. append( y[ 0 ] )
path. append( path[ 0 ] )
for i in range ( len ( x) ) :
plt. annotate( path[ i] , xy= ( x[ i] , y[ i] ) , xytext= ( x[ i] + 0.3 , y[ i] + 0.3 ) )
plt. plot( x, y, '-o' )
fig = plt. figure( )
plt. title( "Distance iteration graph" )
plt. plot( range ( 1 , len ( distance_best) + 1 ) , distance_best)
plt. xlabel( "Number of iterations" )
plt. ylabel( "Distance value" )
plt. show( )
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">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
data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/weixin_48241292/article/details/109312812","extend1":"pc","ab":"new"}">>
评论记录:
回复评论: