A list,

The application of virtual force coverage algorithm to sensor networks has attracted more and more attention in recent years. Firstly, Zou et al. introduced virtual force into sensor networks. It is obvious that sensor nodes and robots have a lot in common. Therefore, based on the application of virtual force in the field of robotics, they regard sensors as particles in potential fields. Since the Earth is a huge magnetic field and there are electromagnetic fields around the earth, nodes can be regarded as particles in potential fields. In this way, there is mutual connection between sensor nodes.

Ii. Source code

clear all
clc
N=40; % Number of sensor nodes XMAX=900; % Total area length XMIN=100;
YMAX=800; % Total area width YMIN=100; X =zeros(N,2);
x(:,1)=(XMAX-XMIN)*rand(N,1)+XMIN; % make the coordinates of nodes randomly distributed x(:,2)=(YMAX-YMIN)*rand(N,1)+YMIN; figure, xm=[XMIN YMIN;XMIN YMAX;XMAX YMAX;XMAX YMIN]; Fill (xm(:,1),xm(:,2),0.8.0.8.0.8]);
hold on
plot(x(:,1),x(:,2),'r.'.'linewidth'.5);
r=90; % Perception radius of the sensor node w=0:pi/50:2*pi;
for i=1:N
    x1=x(i,1)+r*cos(w);
    y1=x(i,2)+r*sin(w); % means that each Angle of the circle corresponds to one point, and then connect these points to form a circlehold on
    plot(x1,y1,'g'); % Draw the sensing range of each sensor node text(x(I,1) +3,x(i,2),'\fontsize{8}\rm',num2str(i)]);
    hold on
    fill(x1,y1,'b')
end
axis([0 1000 0 900]); % set coordinate range xlabel('X/m'); ylabel('Y/m');
%legend('Areas to monitor'['Sensor node (').'\fontsize{12}\bf',num2str(N),'\fontsize{10}\rm) position '].'Sensing range of sensor nodes');
hold on
plot([XMIN XMAX],[YMIN YMIN],'k'.'linewidth'.1.5);
hold on
plot([XMIN XMIN],[YMIN YMAX],'k'.'linewidth'.1.5);
hold on
plot([XMAX XMAX],[YMIN YMAX],'k'.'linewidth'.1.5);
hold on
plot([XMIN XMAX],[YMAX YMAX],'k'.'linewidth'.1.5); % connects the four vertices form a monitoring area % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- virtual force algorithm -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- %------------ Discretize the region first --------- deta=2; % network size x1=XMIN:deta:XMAX; y1=YMIN:deta:YMAX; [xx,yy]=meshgrid(x1,y1); [m,n]=size(xx); K=m*n; % Total number of grid points xx1= 0 (xx,K, 0)1); Y = 0 (yy,K, 0, 0)1); Hold on %plot(xx1,yy1, xx1)'g*') % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- computing network coverage at first -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [no_cover,summ,k1]=compute_cover(xx1,yy1,x,r); %no_cover stores the locations of uncovered lattice points. K1 is the lattice points to be covered. Summ is the number of lattice points to be covered. q(1.1)=summ/K;
%-------------------------------------------------------
figure,
fill(xm(:,1),xm(:,2),0.8.0.8.0.8]); % fills the monitoring areahold on
plot(x(:,1),x(:,2),'ro'.'markerfacecolor'.'r'.'linewidth'.3); % Position of the sensor nodefor i=1:N
    text(x(i,1) +3,x(i,2),'\fontsize{8}\rm',num2str(i)]); % Indicates the position of the sensor nodeend
axis([0 1000 0 900]);
xlabel('X/m'); ylabel('Y/m');
%----------------------
R=2*r; % Communication radius of the sensor node maxiter=100; % Maximum number of iterations max_step=2.5; % Maximum step moved by a sensor node (maximum step moved by a grid point) max_sensor=3.5; % of mobile sensor nodes of the large step (under the action of a sensor node is the most large step) % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- KP =1;
xp{kp,1}=x;
for t=1:maxiter
    %k3=1;
    F=0; tx_old=x; % Position of the sensor node in the previous iterationfor i=1:N
        k2=1;
        flag1=0;
        for k=1:(k1- 1)
            dik=sqrt((x(i,1)-no_cover(k,1)) ^2+(x(i,2)-no_cover(k,2)) ^2); %x is the location of the sensor node, and no_cover is the location of the grid point not covered by the networkif((dik>r)&(dik<= r)) % r is the communication radius, r is the perception radius F(k2,1)=no_cover(k,1)-x(i,1);
                 F(k2,2)=no_cover(k,2)-x(i,2);
                k2=k2+1;
                %flag1=flag1+1;
            end
        end
        Fx=0;
        Fy=0;
        for j=1:(k2- 1)
           Fx=Fx+F(j,1); % horizontal force Fy=Fy+F(j,2); % vertical force end Fxy=sqrt(Fx^2+Fy^2);
        if Fxy==0
            x(i,1)=x(i,1) +0;
            x(i,2)=x(i,2) +0;
        else
            x(i,1)=x(i,1)+Fx/Fxy*max_step*exp(- 1/Fxy); % node position update x(I,2)=x(i,2)+Fy/Fxy*max_step*exp(- 1/Fxy);
        end
        %if flag1~=0
        %   k3=k3+1; % % end end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- repulsion between sensor -- -- -- -- -- -- -- the Fs =0;
    for i=1:N
        ks=1;
        for j=1:N
            if i~=j
                ds=sqrt((x(i,1)-x(j,1)) ^2+(x(i,2)-x(j,2)) ^2);
                if ds<(1.4*r)
                    Fs(ks,1)=x(i,1)-x(j,1);
                    Fs(ks,2)=x(i,2)-x(j,2);
                    ks=ks+1;
                end
            end
        end
        Fsx=0;
        Fsy=0;
        for j=1:(ks- 1)
           Fsx=Fsx+Fs(j,1); % horizontal force Fsy=Fsy+Fs(j,2); % vertical force end Fsxy=sqrt(Fsx^2+Fsy^2);
        if Fsxy==0
            x(i,1)=x(i,1) +0;
            x(i,2)=x(i,2) +0;
        else
            x(i,1)=x(i,1)+Fsx/Fsxy*max_sensor*exp(- 1/Fsxy);
            x(i,2)=x(i,2)+Fsy/Fsxy*max_sensor*exp(- 1/Fsxy); End %----------------- cannot go outside the boundary ------if x(i,1)<XMIN
            x(i,1)=XMIN;
        end
        if x(i,1)>XMAX
            x(i,1)=XMAX;
        end
         if x(i,2)<YMIN
            x(i,2)=YMIN;
        end
        if x(i,2)>YMAX
            x(i,2)=YMAX; end end %----------------------------------------------------------- [no_cover,summ,k1]=compute_cover(xx1,yy1,x,r); % Calculate coverage %------------------for i=1:N
        hold on
        plot([tx_old(i,1) x(i,1)],[tx_old(i,2) x(i,2)].The '-'); % Draw the motion trajectory diagram of sensor node end %-------------------if t==(kp*round(maxiter/4)) % in order to obtain KP = KP + at every quarter of the total iteration step size1;
        xp{kp,1}=x;
    end
    q(t+1.1)=summ/K;
    if q(t+1.1)>=1
       break; End end function [no_cover,summ,k1]=compute_cover(xx1,yy1,x,r) %% (xx1,yy1); %% x is the coordinate of the sensor node, r is the sensing radius of the sensor node %% no_cover stores the location of the uncovered grid point, k1 is the covered grid point, and summ is the number of covered grids. no_cover=0; % Stores the location of the grid point that is not overwritten flag=0;
k1=1;
summ=0;
K=length(xx1);
[N,N1]=size(x);
for k=1:K %K is the total number of grid points flag=0;
    for j=1:N % To find the grid points not in the covered area, the condition is :x-r<=xx1<=x+r D=sqrt((xx1(k,1)-x(j,1)) ^2+(yy1(k,1)-x(j,2)) ^2); % Distance between grid points and sensor nodesif(D<=r) % Grid points in the disk range are used as overridden grid points flag=flag+1;
        end
        if flag==1
            summ=summ+flag;
            break;
        end
    end
    if flag==0
        no_cover(k1,1)=xx1(k,1); % stores uncovered grid position no_cover(k1,2)=yy1(k,1);
        %text(no_cover(k1,1) +1,no_cover(k1,2),'\fontsize{8}\rm',num2str(k1)]);
        k1=k1+1;  
    end
end
k1=k1- 1;
Copy the code

Third, the operation result

Fourth, note

Version: 2014 a