找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6180|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

基于蟻群算法的路徑規(guī)劃三維

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:337125 發(fā)表于 2018-5-24 10:38 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
%% 該函數(shù)用于演示基于蟻群算法的三維路徑規(guī)劃算法

%% 清空環(huán)境
clc
clear

%% 數(shù)據(jù)初始化

%下載數(shù)據(jù)
load  HeightData HeightData

%網(wǎng)格劃分
LevelGrid=10;
PortGrid=21;

%起點(diǎn)終點(diǎn)網(wǎng)格點(diǎn)
starty=10;starth=4;
endy=8;endh=5;
m=1;
%算法參數(shù)
PopNumber=10;         %種群個(gè)數(shù)
BestFitness=[];    %最佳個(gè)體

%初始信息素
pheromone=ones(21,21,21);

%% 初始搜索路徑
[path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,pheromone, ...
    HeightData,starty,starth,endy,endh);
fitness=CacuFit(path);                          %適應(yīng)度計(jì)算
[bestfitness,bestindex]=min(fitness);           %最佳適應(yīng)度
bestpath=path(bestindex,:);                     %最佳路徑
BestFitness=[BestFitness;bestfitness];          %適應(yīng)度值記錄

%% 信息素更新
rou=0.2;
cfit=100/bestfitness;
for i=2:PortGrid-1
    pheromone(i,bestpath(i*2-1),bestpath(i*2))= ...
        (1-rou)*pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit;
end

%% 循環(huán)尋找最優(yōu)路徑
for kk=1:100

    %% 路徑搜索
    [path,pheromone]=searchpath(PopNumber,LevelGrid,PortGrid,...
        pheromone,HeightData,starty,starth,endy,endh);

    %% 適應(yīng)度值計(jì)算更新
    fitness=CacuFit(path);                              
    [newbestfitness,newbestindex]=min(fitness);     
    if newbestfitness<bestfitness
        bestfitness=newbestfitness;
        bestpath=path(newbestindex,:);
    end
    BestFitness=[BestFitness;bestfitness];

    %% 更新信息素
    cfit=100/bestfitness;
    for i=2:PortGrid-1
        pheromone(i,bestpath(i*2-1),bestpath(i*2))=(1-rou)* ...
            pheromone(i,bestpath(i*2-1),bestpath(i*2))+rou*cfit;
    end

end

%% 最佳路徑
for i=1:21
    a(i,1)=bestpath(i*2-1);
    a(i,2)=bestpath(i*2);
end
figure(1)
x=1:21;
y=1:21;
[x1,y1]=meshgrid(x,y);
mesh(x1,y1,HeightData)
axis([1,21,1,21,0,2000])
hold on
k=1:21;
plot3(k(1)',a(1,1)',a(1,2)'*200,'--o','LineWidth',2,...
                       'MarkerEdgeColor','k',...
                       'MarkerFaceColor','g',...
                       'MarkerSize',10)
plot3(k(21)',a(21,1)',a(21,2)'*200,'--o','LineWidth',2,...
                       'MarkerEdgeColor','k',...
                       'MarkerFaceColor','g',...
                       'MarkerSize',10)
                   text(k(1)',a(1,1)',a(1,2)'*200,'S');
text(k(21)',a(21,1)',a(21,2)'*200,'T');
xlabel('km','fontsize',12);
ylabel('km','fontsize',12);
zlabel('m','fontsize',12);
title('三維路徑規(guī)劃空間','fontsize',12)
set(gcf, 'Renderer', 'ZBuffer')
hold on
plot3(k',a(:,1)',a(:,2)'*200,'--o')

%% 適應(yīng)度變化
figure(2)
plot(BestFitness)
title('最佳個(gè)體適應(yīng)度變化趨勢(shì)')
xlabel('迭代次數(shù)')
ylabel('適應(yīng)度值')


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:519697 發(fā)表于 2019-4-23 16:18 | 只看該作者
你好 我想問您一下這個(gè)程序?yàn)槭裁疵恳淮芜\(yùn)行出來(lái)的結(jié)果都不一樣呀
回復(fù)

使用道具 舉報(bào)

板凳
ID:519697 發(fā)表于 2019-4-23 16:26 | 只看該作者
您好 我也運(yùn)行了這段代碼 我一點(diǎn)也沒改動(dòng)  但是每一次運(yùn)行出來(lái)的結(jié)果都不一樣 我試著更改了子群數(shù)也是這樣 我不知道是什么原因QAQ 如果您能看到這條消息 期待您的回復(fù)
回復(fù)

使用道具 舉報(bào)

地板
ID:83345 發(fā)表于 2019-10-13 17:22 | 只看該作者
不懂,沖標(biāo)題來(lái)到,看看
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表