其他分享
首页 > 其他分享> > 多智能体实现带状栅栏覆盖——MATLAB代码

多智能体实现带状栅栏覆盖——MATLAB代码

作者:互联网

仔细阅读一篇关于栅栏覆盖的文献(文末会给出文献)之后把代码写出来了。还有点小bug,不过已经可以实现了。代码写得比较粗糙,读者可以自己改一改。

代码如下:

clc
clear 
close all
%%
%生成智能体和地标
s=0.5;
r=1;
tk=1;
T=1;
xc=0.00001;
v0=0.01;
vmax=0.01;
L1=[5*rand(1),5*rand(1)];
L2=L1+[5*rand(1),5*rand(1)];
delta=atan((L2(2)-L1(2))/(L2(1)-L1(1))); 
theta_av=delta+pi/2;
q0=[sin(theta_av) -cos(theta_av)]*(L1)';
qn_1=[sin(theta_av) -cos(theta_av)]*(L2)';
figure(10);
rectangle('Position', [L1-[0.6/2 0.6/2],0.6,0.6], 'Curvature', [0 0],'EdgeColor', 'k','facecolor','k');
rectangle('Position', [L2-[0.6/2 0.6/2],0.6,0.6], 'Curvature', [0 0],'EdgeColor', 'k','facecolor','k');
rectangle('Position', [L1-[s/2 s/2],s,s], 'Curvature', [1 1],'EdgeColor', 'g','facecolor','g');
rectangle('Position', [L2-[s/2 s/2],s,s], 'Curvature', [1 1],'EdgeColor', 'g','facecolor','g');
axis equal;
n=round(norm(L1-L2)/s);
a=[10*rand(1),10*rand(1)];
[y,x]=meshgrid(1,1:n);
P = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
theta = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
q = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
th = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
Q = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
vj = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
vb = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
v = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
F = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
[y,x]=meshgrid(1,1:n);
qi = arrayfun(@(x,y)[x,y],x,y, 'UniformOutput' , false );
sumthe=1:n;
num=1:n;
cij=1:n; 
H=1:n; 
M=1:n; 
P{n+1}=L1;
P{n+2}=L2;
theta{n+1}(1)=theta_av;
theta{n+2}(1)=theta_av;
for i=1:round(n/2)
    %生成智能体初始位置和初始航向角
    P{i}=L1+[(rand(1)-0.5)*0.4,(rand(1)-0.5)*0.4];
    th{i}(1)=pi*rand(1);
    theta{i}(1)=th{i}(1);
    F{i}(1)=[cos(theta{i}(1)) sin(theta{i}(1))]*(P{i})';
    a(i)=rectangle('Position', [P{i}-[s/2 s/2],s,s], 'Curvature', [1 1],'EdgeColor', 'g','facecolor','g');
end
for i=round(n/2)+1:n
    %生成智能体初始位置和初始航向角
    P{i}=L2+[(rand(1)-0.5)*0.4,(rand(1)-0.5)*0.4];
    th{i}(1)=pi*rand(1);
    theta{i}(1)=th{i}(1);
    F{i}(1)=[cos(theta{i}(1)) sin(theta{i}(1))]*(P{i})';
    a(i)=rectangle('Position', [P{i}-[s/2 s/2],s,s], 'Curvature', [1 1],'EdgeColor', 'g','facecolor','g');
end

%% 时间序列
for k=1:2500
theta{n+1}(k)=theta_av;
theta{n+2}(k)=theta_av;
%% 计算Hi(KT)Mi(KT) 更新Fi(KT) theta(KT)计算Qi(KT)
for i=1:n
sumthe(i)=0;   %D区域的角度和
num(i)=0;      %D区域的智能体数量
cij(i)=0;      %D区域的共识信息
    for io=1:n+2
        if(norm(P{i}-P{io})<=r)
            num(i)=num(i)+1;
            sumthe(i)=sumthe(i)+theta{io}(k);
            cij(i)=cij(i)+[cos(theta{i}(k)) sin(theta{io}(k))]*(P{io})';
            q{i}(num(i))=[sin(theta{i}(k)) -cos(theta{i}(k))]*(P{io})';
        end
    end
    if (num(i)>1) %存在α和β中的一个
        q{i}=sort(q{i});%排个序
        for i2=1:num(i)
            if (q{i}(i2)==[sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})')%找到i=i2
                if(i2==1)%只存在β
                    Q{i}=(q{i}(i2)-s+q{i}(2))/2;
                elseif(i2==num(i))%只存在α
                    Q{i}=(q{i}(i2-1)+q{i}(i2)+s)/2;
                else %α和β都存在
                    Q{i}=(q{i}(i2+1)+q{i}(i2-1))/2;
                end
            end
        end
    else %α和β都不存在
         Q{i}=q{i}(1);
    end
        if ((0<(q0-[sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})'))&&((q0-[sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})')<s+xc))
            qi{i}(k)=([sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})'+q0+v0*T)/2;
        elseif((0<([sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})'-qn_1))&&(([sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})'-qn_1)<s+xc))
            qi{i}(k)=([sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})'+qn_1-v0*T)/2;
        else 
            qi{i}(k)=Q{i};
        end 
 H(i)=sumthe(i)/num(i);
 theta{i}(k+1)=H(i);
 M(i)=cij(i)/num(i);
 F{i}(k+1)=M(i);
 F{i}(k)=[cos(theta{i}(k)) sin(theta{i}(k))]*(P{i})';%这一步太重要了
 vj{i}(k)=(F{i}(k+1)-F{i}(k))/T; 
 vb{i}(k)=(qi{i}(k)-[sin(theta{i}(k)) -cos(theta{i}(k))]*(P{i})')/T;
 v{i}(k)=sqrt((vj{i}(k))^2+(vb{i}(k))^2);
 if(vj{i}(k)<0)
        th{i}(k)=theta{i}(k)-acos(vb{i}(k)/v{i}(k))-pi/2;
 else
        th{i}(k)=theta{i}(k)+acos(vb{i}(k)/v{i}(k))-pi/2;
 end
end
%% 移动智能体
    pause(0.012);
for i=1:n
   delete(a(i));
   if v{i}(k)>vmax
       v{i}(k)=vmax;
   end
    P{i}=P{i}+(v{i}(k))*[cos(th{i}(k)) sin(th{i}(k))]*T;
    a(i)=rectangle('Position', [P{i}-[s/2 s/2],s,s], 'Curvature', [1 1],'EdgeColor', 'g','facecolor','g');
end
end

仿真图:

 

参考文献:

[1] Cheng T M, Savkin A V. A distributed self-deployment algorithm for the coverage of mobile        wireless sensor networks[J]. IEEE Communications Letters, 2009, 13(11): 877-879.

标签:rand,栅栏,end,带状,cos,num,MATLAB,theta,sin
来源: https://blog.csdn.net/FANYANGYANG_/article/details/123420875