如何形成三角形植绒结构?

时间:2019-03-27 04:34:29

标签: matlab triangular multi-agent

我正在使用MATLAB模拟多智能体系统的编队控制。目前,我正在使用植绒算法,并设法模拟了圆形植绒。如何模拟代理以形成三角形群?代理商将使用晶格类型的几何图案收敛为羊群

%Number of agents
prompt = 'Enter number of Agents: ';

Agents = input(prompt);
if Agents > 100
    error('Error! Too many agents, decrease the number of agents');
end
if Agents < 2
    error('Error! Too little agents, increase the number of agents');
end
Step_Num = 400;
%Number of iterations
%Initial condition
p = 10*randn(2,Agents);  %Vector Position p
v = 5*randn(2,Agents);   %Vector Velocity v
t = 0.01;                %Time
Li = 30;                 %Axis Limit
%Separation/Collision Avoidance
S = 1;        %Controls the distance between agents
              %Bigger number, further the agents are from each other
%Cohesion/Flock Centering
K = 1;        %Controls the collision between the agents
              %Bigger number the smaller the flock
%Alignment/Velocity Matching
M = 0;        %Controls the flock motion
%Separation/Collision avoidance gain
ca = 5;       %Larger gain -- agents more spread out
%Cohesion/Flock centering gain
fc = 0.1;     %Larger gain -- flock centering is smaller
%Flocking Model
for n=1:Step_Num           %For every iteration from 1 to desired step
    sv = zeros(2,Agents);  %Collision avoidance vector / Seperation Vector
    kv = zeros(2,Agents);  %Flock centering vector / Cohesion Vector
    for i = 1:Agents          
        for j = 1:Agents          
            if i~= j                         %Distance = 0 when i = j (ignore)
               r = (p(:,j)-p(:,i));          %Distance between two points i & j
               d = sqrt(r(1)^2+(r(2)^2));    %Euclidean distance
               sv(:,i) = sv(:,i) - ca*r/d^2; %Collision avoidance/Seperation
               kv(:,i) = kv(:,i) + fc*r;     %Flock centering/Cohesion
            end
        end
        v(:,i) = S*sv(:,i)+ K*kv(:,i)+ M;  %Total Velocity
        p(:,i) = p(:,i) + v(:,i)*t;        %New Position formula p = p+vt
    end
    %Plot simulations
    figure;
    plot(p(1,:),p(2,:),'k.','LineWidth',3,'Markersize',15);
    grid on;
    axis([-Li Li -Li Li]);   %Axis limit
    xlabel('X-axis')         %X-axis
    ylabel('Y-axis')         %Y-axis
    title(['Step Number :', num2str(n)]);
    F(:,n) = getframe;
    hold off;
end

0 个答案:

没有答案