|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
' {+ l3 R) A |/ [0 L! u一、简介3 S2 r( u3 d; r E4 h y9 M
基于matlab蚁群算法的三维路径规划
$ n; o- ?7 }- @/ {
( l! }4 @, e/ P. P! Z二、源代码
- P( o4 d6 v3 g. w+ N% {* |5 {, |%% 该函数用于演示基于蚁群算法的三维路径规划算法3 {0 X. Q3 @+ H/ V7 q- @5 s
) B' D& v7 e4 ]# W
%% 清空环境! R! ]3 ~, Q% f) m% X* r) U: }8 ^. G
clc
1 F! Q7 ~6 [1 M$ A% [3 Vclear0 Q! ` |4 g5 s6 t3 N( h
# L( z$ T. @4 N. b4 o) |6 J
%% 数据初始化6 P" X* T. w+ h. B2 V3 `2 ^9 l
: Y' y- W W" H. H: g3 _! u/ _4 Q& K3 }%下载数据2 l; R1 ?# y0 b: q& _: T% Z
load HeightData HeightData
9 H; m# ?) {1 ^7 U
( c Y% z7 A: ]( U0 W1 H%网格划分
& J' J \7 z" e% |! m( qLevelGrid=10;
" D- K: G3 _3 R; t' O7 ^PortGrid=21;
}; C9 c8 ~- o
+ b; L( ]) U% e6 A* g, S( @' g%起点终点网格点
# \0 h4 s9 ?2 \( D" Gstarty=10;starth=4;
, n l3 l w0 gendy=8;endh=5;( O( @7 S L! U+ K& n; O
m=1;) ^- l* F/ |2 P0 T L
%算法参数
" [, t* K/ n4 t" R( D; ^PopNumber=10; %种群个数* e% V: ?, h6 m* m1 [$ ~
BestFitness=[]; %最佳个体
! o, E3 I4 c3 g: i7 a K8 X' x# y* n3 P" A
%初始信息素
7 k0 V/ H# Z- C4 Q8 t' hpheromone=ones(21,21,21);
I5 E( W9 s( w7 w4 ~9 G" A' G) M! q- F! C
%% 初始搜索路径
& C3 C3 w$ I6 n[path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,pheromone, ...; A& h) _' j% m' x, W7 j1 T
HeightData,starty,starth,endy,endh); 5 q+ Y5 z! x# ?9 x
fitness=CacuFit(path); %适应度计算' H9 [3 d- r' q
[bestfitness,bestindex]=min(fitness); %最佳适应度3 J% ~7 [ |) a; o
bestpath=path(bestindex,:); %最佳路径
, q) O, X- k! r8 ^9 f* EBestFitness=[BestFitness;bestfitness]; %适应度值记录0 t$ V* V; r' \% {) N" G
- d9 ], A; Z7 d' z+ [$ W%% 信息素更新
6 c! s' O- [0 y: n6 mrou=0.2;
( K: Y7 |5 @$ @3 R& H5 Rcfit=100/bestfitness;! H% v9 E4 W, v& Y5 g1 p0 i
for i=2:PortGrid-17 d1 l- x) }( @
pheromone(i,bestpath(i*2-1),bestpath(i*2))= ...) [% p8 h5 V, A9 f- T; _
(1-rou)*pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit; O$ _! K# c r
end- G8 y0 O) V$ J* f
& |4 `: t! k, M- D# K& [: M# }%% 循环寻找最优路径2 D* e2 w4 o+ {! H! Z$ T
for kk=1:100& b% U' F4 Z3 C) o. _
5 `( c' K: {" O9 P, @1 D# f! s7 B
%% 路径搜索
, ^0 L5 Z7 S1 x [path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,..., e' Z) R; t& ^3 ~- d# x
pheromone,HeightData,starty,starth,endy,endh); ! @# s# A/ V8 V2 [/ T+ }
# l7 ]! J. G1 o+ u7 P* h6 j* c1 r %% 适应度值计算更新6 i, L6 q5 o' |
fitness=CacuFit(path);
/ d0 F9 b% a) A Y1 F% g$ j. N [newbestfitness,newbestindex]=min(fitness); , Q) Z$ h6 ?3 p
if newbestfitness<bestfitness9 N1 O" Q2 h8 N
bestfitness=newbestfitness;
' H8 o7 L0 D: B# a bestpath=path(newbestindex,:);! t$ E6 v" A4 R) E, X i/ ]
end 7 G s) J% Z7 x- }8 c
BestFitness=[BestFitness;bestfitness];
- e w5 F x$ C2 x6 w ! D' `: x, Y0 b! G2 S( H
%% 更新信息素
- E( p5 ^' a% k( k- o% T2 S3 q- D+ x cfit=100/bestfitness; f) ~1 i9 f! q' \6 a& a3 r
for i=2:PortGrid-1; f4 D# Q y, C4 C
pheromone(i,bestpath(i*2-1),bestpath(i*2))=(1-rou)* ...
$ f! h e$ N# y# c pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit;% X& O" y0 ]9 k3 Y, ~
end5 K/ @- d& p( \3 M7 `" [
% ~! I$ C/ S! H4 E( w3 N
end
( @0 l. X: s# y' ?8 |* R7 { l( G7 Z5 E) L/ V
三、运行结果
/ \# F2 x7 O. t) l# p: `5 p, o6 ~
|
|