一、二维移动网格法简介
二、部分源代码
%% 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.