一、二维移动网格法简介

【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_03
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02

二、部分源代码

%% Program Start
%清零
clc ;clear all;close all;
%设定通信半径为5
global Rc;
Rc = 5;

%设定覆盖区域为L=40的正方形
global L;
L = 5*Rc*sqrt(2);
S = L^2;

%初始化一个5*5的0矩阵,用来保存随机部署后每个网格中的节点个数
initnode_num = zeros(5,5);
node_num = 0;    %用于保存节点个数


%设定网格权重
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];

%计算总权重
total_weight = 0;
for i = 1:5
    for j = 1:5
        total_weight_temp =  grid_weight(i,j);
        total_weight = total_weight + total_weight_temp;
    end
end

%输出总权重
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);  %画网格
for i = 1:(total_weight+10)
    x1 = random_x(i);
    y1 = random_y(i);
    draw_round(x1,y1);  %节点随机部署图
end

%% Count Initial Nodes
%计算初始化后每个单元格中的节点个数
for i = 1:(total_weight+10)
    temp_x = random_x(i);
    temp_y = random_y(i);
    %首先判断坐标轴上的点
    if temp_x == 0   %y轴上的点
        if 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 == 0   %x轴上的点
        for 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
    
    %非坐标轴上的点
    if (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
end
initnode_num

%% Calculate Reject Force
%计算斥力
rejectforce = zeros(5,5);   %初始化5*5的矩阵用于存放网格斥力
for i = 1:5
    for j = 1:5
        %如果网格中的节点数多于网格权重,则斥力等于两者之差,否则斥力为0
        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
end
grid_weight
initnode_num
rejectforce

%% Calculate Attractive Force
%计算引力
attractiveforce = zeros(25,4);  %初始化25*4的矩阵,存放每个网格受到的引力,由于每个网格周围有四个网格
                                %列坐标从小到大表示左、上、右、下,处在边缘的网格若周围没有其他网格,用0表示,即不受
                                %此方向的引力。另外,第一行代表(1,1)网格,第二行代表(1,2),……,第六行代表
                                %(2,1)网格,第七行代表(2,2)网格,……,最后一行代表(5,5)网格   
for m = 1:5             %m为x轴坐标
    for n = 1:5         %n为y轴坐标
        j = 1;
        i = add1;       %i自动加1,从1到25
        if (m-1)>0      %算左边网格
            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
        %算上边网格
        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
        %算右边网格
        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
        %算下边网格
        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 and Move Nodes
%计算移动概率并移动节点同时将网格中的节点移动到网格中心
for 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)))
            %当中心网格斥力大于0&&周围引力至少有一个非0时成立
            j = 1;
            attractiveforce_max = attractiveforce(k,j);
            row = k;
            col = j;
            for j = 2:4    %求最大引力,即求得最大移动概率
                if 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;  %相应网格节点减1
            rejectforce(m,n) = rejectforce(m,n)-1;    %相应的斥力也减1
            attractiveforce(row,col) = attractiveforce(row,col)-1; %相应网格引力减1
            %下面进行坐标转换,使通过引力获得节点的网格节点数加1
            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 i = 1:5
    for j = 1:5
        if initnode_num(i,j)~=0
        draw_round(((i-0.5)*Rc*sqrt(2)),((j-0.5)*Rc*sqrt(2)));
        end
    end
end
%--------------------------------------------
%程序名  :  i = add1()
%参数说明:  无参数
%功能    :  实现加1功能
%调用方式:  i = add1
%--------------------------------------------
function i = add1()
persistent a
if isempty(a)  %判断a是否已经赋值(初始化)
    a=0;
end
    a=a+1;
    if a >25;
        a = 1;
    end
  i=a;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02
  •  

三、运行结果

【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_06
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_08
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_10
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_02

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.

​​

【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_12
【优化覆盖】移动网格求解无线传感器网络节点覆盖优化问题matlab源码_布局优化_13