1. 程式人生 > 其它 >【優化覆蓋】移動網格求解無線感測器網路節點覆蓋優化問題matlab原始碼

【優化覆蓋】移動網格求解無線感測器網路節點覆蓋優化問題matlab原始碼

一、二維移動網格法簡介


二、部分原始碼

%% 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;

三、執行結果



四、matlab版本及參考文獻

1 matlab版本
2014a

2 參考文獻
[1] 包子陽,餘繼周,楊杉.智慧優化演算法及其MATLAB例項(第2版)[M].電子工業出版社,2016.
[2]張巖,吳水根.MATLAB優化演算法原始碼[M].清華大學出版社,2017.