ant_move.m

来自「基于MATLAB蚁群算法LF解决聚类问题源程序代码」· M 代码 · 共 85 行

M
85
字号
%{
?	当前蚂蚁移到邻近区域内的没有被其他蚂蚁占据的节点
a)	输入参数
?	蚂蚁编号ant_no
?	S局部查找范围
?	ant_matrix(蚂蚁的窗格矩阵)
?	Z平面窗格总区域
b)	输出参数
?	蚂蚁前往的结点坐标

%}

function ant_matrix=ant_move(ant_no,S,ant_matrix,Z)
%1、获得ant_no点坐标
%[x,y,z]=ant_matrix(ant_no,:);
one_ant=ant_matrix(ant_no,:);
x=one_ant(1);
y=one_ant(2);
z=one_ant(3);

%2、获得上下界限
x_low=x-S/2;
x_high=x+S/2;
if(x_low<0)
    x_low=0;
end
if(x_high>Z)
    x_high=Z;
end

y_low=y-S/2;
y_high=y+S/2;
if(y_low<0)
    y_low=0;
end
if(y_high>Z)
    y_high=Z;
end

%获得所有邻接结点下标数组
%{
allneighbours=[];
[row,col]=size(ant_matrix);
for i=1:row
    if i~=ant_no
        [x,y,z]=ant_matrix(i,:);
        if x>=x_low && x<=x_high && y>=y_low && y<=y_high 
            allneighbours=[allneighbours i];
        end
    end
    
end
allneighbours=[allneighbours ant_no];
%}
[row,col]=size(ant_matrix);
positions=[];
%遍历所有上下界之间的点
for i=x_low:x_high
    for j=y_low:y_high
        status=0;
        for k=1:row
%            [x,y,z]=ant_matrix(k,:);
             one_ant=ant_matrix(k,:);
             x=one_ant(1);
             y=one_ant(2);
             z=one_ant(3);
            if x==i && y==j
                status=1;
            end
        end
        
        if status==0
            positions=[positions;i j];
        end
    end
end

%随机产生这个位置
[row,col]=size(positions);
i=ceil(rand*row);
position=positions(i,:);

ant_matrix(ant_no,1)=position(1);
ant_matrix(ant_no,2)=position(2);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?