A brief introduction of two-dimensional moving grid method

Two, some source code

%% Program Start % clear all; close all; % Sets the communication radius to5
global Rc;
Rc = 5; % sets the coverage area to L=40Square global L; L =5*Rc*sqrt(2);
S = L^2; % initializes one5*5the0Initnode_num = zeros(5.5);
node_num = 0; % to save the number of nodes % to set the grid weight grid_weight = [2.1.3.1.1;3.2.2.1.2;4.1.2.2.1;1.2.2.3.1;2.5.1.2.1]; % Calculate total weight total_weight =0;
for i = 1:5
    for j = 1:5total_weight_temp = grid_weight(i,j); total_weight = total_weight + total_weight_temp; Total_weight grid_weight %% Random Deployment % Random_x = randi([0.ceil(L)],1,(total_weight+10));
random_y = randi([0.ceil(L)],1,(total_weight+10));
figure(1); draw_grid(L); % griddingfor i = 1:(total_weight+10) x1 = random_x(i); y1 = random_y(i); draw_round(x1,y1); End %% Count Initial Nodes % Counts the number of Nodes in each cell after initializationfor i = 1:(total_weight+10) temp_x = random_x(i); temp_y = random_y(i); First determine the points on the axesif temp_x == 0% of the y axisif temp_y == 0
            initnode_num(1.1) = initnode_num(1.1) + 1;
        else
            for n = 1:L/(Rc*sqrt(2))
                if (temp_y>(n- 1)*Rc*sqrt(2))&&((temp_y<n*Rc*sqrt(2))||(temp_y==n*Rc*sqrt(2)))
                    initnode_num(1,n) = initnode_num(1,n)+1;
                end
            end
        end
    end
    
    if temp_y == 0Points on the x axisfor n = 1:L/(Rc*sqrt(2))
            if (temp_x>(n- 1)*Rc*sqrt(2))&&((temp_x<n*Rc*sqrt(2))||(temp_x==n*Rc*sqrt(2)))
                initnode_num(n,1) = initnode_num(n,1) +1; End end end % Is not a point on the coordinate axisif (temp_x~=0)||(temp_y~=0)
        for m = 1:L/(Rc*sqrt(2))
            for n = 1:L/(Rc*sqrt(2))
                if ((temp_x>(m- 1)*Rc*sqrt(2))&&((temp_x<m*Rc*sqrt(2))||(temp_x==m*Rc*sqrt(2))))&&((temp_y>(n- 1)*Rc*sqrt(2))&&((temp_y<n*Rc*sqrt(2))||(temp_y==n*Rc*sqrt(2))))
                 initnode_num(m,n) = initnode_num(m,n)+1; End end end end initnode_num %% Reject Force % Calculate Reject Force %5.5); % initialization5*5Is used to store grid repulsionfor i = 1:5
    for j = 1:5% If the number of nodes in the grid is greater than the weight of the grid, the repulsion is equal to the difference between the two, otherwise the repulsion is0
        if initnode_num(i,j)>grid_weight(i,j)
           rejectforce(i,j) = initnode_num(i,j)-grid_weight(i,j); 
        else 
            rejectforce(i,j) = 0; end %rejectforce(i,j) = initnode_num(i,j)-grid_weight(i,j); End grid_weight initnode_num rejectForce %% Calculate Attractive Force attractiveForce = zeros(25.4); % initialization25*4Since there are four grids around each grid % column coordinates from small to large represent left, up, right and down. If there are no other grids around the edge, use0Represents, that is, is not affected by the gravity in this direction. In addition, the first line represents (1.1) grid, the second row represents (1.2),... , the sixth line represents % (2.1) grid, the seventh row represents (2.2) grid... , the last line represents (5.5) gridfor m = 1:5%m is the x coordinatefor n = 1:5%n is the y coordinate j =1; i = add1; % I add automatically1, from1to25
        if (m- 1) >0% counts as the left gridif grid_weight(m- 1,n)>initnode_num(m- 1,n)
               attractiveforce(i,j) = grid_weight(m- 1,n)-initnode_num(m- 1,n);
            else
               attractiveforce(i,j) = 0;
            end
        else
           attractiveforce(i,j) = 0; End % calculates the upper grid j = j+1;
        if (n+1) <6
            if grid_weight(m,n+1)>initnode_num(m,n+1)
               attractiveforce(i,j) = grid_weight(m,n+1)-initnode_num(m,n+1);
            else
               attractiveforce(i,j) = 0;
            end
        else
           attractiveforce(i,j) = 0; End % calculates the right-hand grid j = j+1;
        if (m+1) <6
            if grid_weight(m+1,n)>initnode_num(m+1,n)
               attractiveforce(i,j) = grid_weight(m+1,n)-initnode_num(m+1,n);
            else
               attractiveforce(i,j) = 0;
            end
        else
           attractiveforce(i,j) = 0; End % calculates the lower grid j = j+1;
        if (n- 1) >0
            if grid_weight(m,n- 1)>initnode_num(m,n- 1)
               attractiveforce(i,j) = grid_weight(m,n- 1)-initnode_num(m,n- 1);
            else
               attractiveforce(i,j) = 0;
            end
        else
           attractiveforce(i,j) = 0; 
        end
    end
end
attractiveforce

%% Calculate Moving Probability andMove Nodes % Computes the movement probability and moves Nodes to the center of the gridfor m = 1:5
    for n = 1:5
        k = add1;
        j = 1;
        while ((rejectforce(m,n)>0)&&((attractiveforce(k,1) ~ =0)||(attractiveforce(k,2) ~ =0)||(attractiveforce(k,3) ~ =0)||(attractiveforce(k,4) ~ =0))) % when the central grid repulsion is greater than0There is at least one non around gravity0J =1;
            attractiveforce_max = attractiveforce(k,j);
            row = k;
            col = j;
            for j = 2:4% to find the maximum gravity, that is, to find the maximum probability of movementif attractiveforce(k,j)>attractiveforce_max
                    attractiveforce_max = attractiveforce(k,j);
                    row = k;
                    col = j;
                end
            end
            initnode_num(m,n) = initnode_num(m,n)- 1; % corresponding grid node minus1
            rejectforce(m,n) = rejectforce(m,n)- 1; The corresponding repulsive force decreases1
            attractiveforce(row,col) = attractiveforce(row,col)- 1; % decreases the grid gravity1% The coordinate transformation is performed below to increase the number of grid nodes obtained by gravity1
            m1 = m;
            n1 = n;
            if col == 1
                m1 = m1- 1;
            elseif col == 2
                n1 = n1+1;
            elseif col == 3
                m1 = m1+1;
            elseif col == 4
                n1 = n1- 1;
            end
            initnode_num(m1,n1) = initnode_num(m1,n1)+1;
        end
    end
end
grid_weight
initnode_num
rejectforce

figure(2); Draw_grid (L) for dynamically adjusted node deployment; % griddingfor i = 1:5
    for j = 1:5
        if initnode_num(i,j)~=0
        draw_round(((i0.5)*Rc*sqrt(2)),((j0.5)*Rc*sqrt(2))); End end end % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % program name: I = add1 () % parameters description: no parameter % functions: add1Function % call way: I = add1% -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the function I = add1 persistent () aifIsempty (a) % determines whether a has been assigned (initialized) a=0;
end
    a=a+1;
    if a >25;
        a = 1;
    end
  i=a;

Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

[1] Yang Baoyang, YU Jizhou, Yang Shan. Intelligent Optimization Algorithm and Its MATLAB Example (2nd Edition) [M]. Publishing House of Electronics Industry, 2016. [2] ZHANG Yan, WU Shuigen. MATLAB Optimization Algorithm source code [M]. Tsinghua University Press, 2017.