|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
- L: s3 c$ e( G& C
一、简介8 c4 n( y, C. T2 \$ Y" C/ w
基于matlab蚁群算法的三维路径规划
1 R* o1 }7 E1 v# a
, B9 B+ h% ?; Q1 \0 @1 E N7 ?二、源代码
* L- t$ f! }8 X- I- H%% 该函数用于演示基于蚁群算法的三维路径规划算法
# V' t* @! D' X: i7 J7 n7 m
! l9 B5 \+ g+ b%% 清空环境
4 h, e: Q4 `! G; a' bclc
7 e u1 @& A# U# xclear
' p4 T- T* y$ ?* P4 D R: |5 E9 F
5 y( j$ M% `: L6 _& v%% 数据初始化
# D9 ^' F5 N6 p+ o3 I1 j, K
- Q& g! ~0 \& Q4 V%下载数据
; L- N% t7 N6 n8 g6 wload HeightData HeightData
/ _# k, F1 X* x! Q; U! V. _$ O i" A+ @- E" C1 Z
%网格划分
- C' k9 \& m4 O/ ?; I5 LLevelGrid=10;+ d8 ^2 f5 h9 `& O* ]; w
PortGrid=21;' j7 `8 D1 F' q4 D7 e( Y
5 w f$ j4 I' [. O9 [%起点终点网格点 1 f" P6 w P- ~- O1 Y
starty=10;starth=4;, A5 \7 A/ D8 W: N
endy=8;endh=5;
3 y2 W9 ~$ A1 u. W+ qm=1;
2 J- L5 x3 S8 r! D( X6 u( P%算法参数
' N( `! I# U: t- C( b0 SPopNumber=10; %种群个数) r8 X) w; M, ]7 U1 t4 ~( b
BestFitness=[]; %最佳个体
3 V9 {5 w m) m7 ?
+ V$ m0 b+ A# f1 W%初始信息素
$ n0 X" H8 r4 ]+ i7 ]4 xpheromone=ones(21,21,21);
- |4 w; u1 S& ]" s0 h0 F8 \! }
( _5 P) I7 t# ]3 d%% 初始搜索路径
h. y0 U0 S) `2 u) q[path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,pheromone, ...- A. O5 P4 _& [
HeightData,starty,starth,endy,endh); 3 E. o4 j: V7 c/ u1 N; P
fitness=CacuFit(path); %适应度计算1 |4 h# d% ^: V8 R6 S
[bestfitness,bestindex]=min(fitness); %最佳适应度
: }7 o9 Q0 v2 l+ C0 w* Tbestpath=path(bestindex,:); %最佳路径( \' c& h7 s1 p+ J C l: E5 N
BestFitness=[BestFitness;bestfitness]; %适应度值记录: Y) I2 l5 F3 d9 b( E
1 u3 d8 {; ?( w( b F
%% 信息素更新! [0 t* o9 x& `1 k/ x4 r. A1 A. Y. H
rou=0.2;
% `0 \3 i% q1 F- F# v" _/ Zcfit=100/bestfitness;5 p* E6 f/ J) ?2 E' ]
for i=2:PortGrid-1 A! x- p9 z' ^+ _- W5 }$ a
pheromone(i,bestpath(i*2-1),bestpath(i*2))= ...6 T2 K4 l5 i3 v: ~
(1-rou)*pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit;
! j2 L$ F7 j; |' t4 w$ Uend
|, c) T- W# I7 [. M% `9 u
) d) \3 l% @+ Z' |%% 循环寻找最优路径
& d$ @% X' H+ H6 U; i' afor kk=1:1003 F4 V! V# P6 |0 D" L1 X+ l
# \; G/ b( y7 j
%% 路径搜索
$ V- Z9 n( z. E I" K8 y2 M [path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,... H p) p. {; H& T) s4 B4 p
pheromone,HeightData,starty,starth,endy,endh); + P- s1 f# S( K" s
% M* A, y7 D4 |( k0 Q" i& q- R! N$ z
%% 适应度值计算更新
. v5 ?% k7 i# ?0 f fitness=CacuFit(path);
0 k0 s; w G5 l: M% J5 H' L [newbestfitness,newbestindex]=min(fitness); 4 \3 o8 N# g8 c) I) D, x
if newbestfitness<bestfitness5 i; [3 ?3 [7 ?, ~8 B
bestfitness=newbestfitness;- |7 j f. ~6 ?$ `7 k
bestpath=path(newbestindex,:);: O `! R8 J0 k3 U8 t8 X
end
1 S# B( A$ D+ ^0 _4 b BestFitness=[BestFitness;bestfitness];. C9 A# H( s9 e4 y$ B. L
, Z. A* k8 R( e7 E1 e+ y! J
%% 更新信息素3 |, P$ M- [) [% I
cfit=100/bestfitness;8 F. ^; V" }6 o- J2 @5 k
for i=2:PortGrid-1* |) R: X2 D# A! A3 u
pheromone(i,bestpath(i*2-1),bestpath(i*2))=(1-rou)* .../ q; O' d, g7 ~! d) ^- ~& o
pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit;
' B2 W# F+ f9 I8 j% O& A' o# T end
6 E0 Z' h i# a 3 r/ j" F5 U2 W) U. p! |
end
( f' o n2 ? M Y4 H
4 ?6 n6 E2 r+ j* ?0 s三、运行结果
3 y$ m" D( g& E$ Z2 T
|
|