⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 routing.m

📁 无线传感器网络的可靠路由算法
💻 M
字号:
clear;
global neighbour;
global c;
global path_energy;
global p;
global link_energy;
global parent_reliability;
global parent_hops;
global parent_constraint;
global n;
global cost_constraint;
global flag;
global constraint_energy;
constraint_energy=zeros(100,1);
link_energy=zeros(100,1);
flag=zeros(100,1);
N=100;
M=100;
for i=1:N
    x(i)=rand(1)*M;
    y(i)=rand(1)*M;
end
x(1)=M*0.95;y(1)=M*0.95;
neighbour=zeros(N,N*0.2);
for i=1:N
    n(i)=1;
   for j=1:N
      if (x(i)-x(j))^2+(y(i)-y(j))^2<M^2*0.04 & i~=j
          neighbour(i,n(i))=j;
          n(i)=n(i)+1;
      end
   end
end
% d0=1;
% xi=0;
n0=2;
bn=1;
r=2;
rou=2/20/8;
f=20;
c=zeros(100,1);
path_energy=zeros(100,1);
c(1)=1;path_energy(1)=1;
for loop=1:20
    for i=1:100
        for m=1:n(i)-1
            j=neighbour(i,m);
            d(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2);
            g(i,j)=10+10*n0*log(10/d(i,j));
            pk(i,j)=(1-1/2*exp(-10^(0.1*g(i,j))*bn/2/r))^(rou*8*f);
            p(i,j)=1-(1-pk(i,j))^3;
            ctemp=p(i,j)*c(j);
            if c(i)<ctemp
               c(i)=ctemp;
               parent_reliability(i)=j;
            end
        end
    end
end
parent_reliability(1)=1;parent_hops(1)=1;
for j=1:3
    for i=1:100
        u(i,j)=x(10*j)+20*cos(2*pi*i/100);
        v(i,j)=y(10*j)+20*sin(2*pi*i/100);
    end
end
% plot(x,y,'o',x(1),y(1),'*');
% for i=1:N
%      cordinate_x(1)=x(i);
%      cordinate_x(2)=x(parent_reliability(i));
%      cordinate_y(1)=y(i);
%      cordinate_y(2)=y(parent_reliability(i));
%      line(cordinate_x,cordinate_y);     
%      text(x(i)+2,y(i)+2,num2str(i));
% end
hop=100*ones(N,1);hop(1)=0;k=0;computed=1;visited=zeros(N,1);
cost_constraint=zeros(100,1);
while computed<=N
    for i=1:N
        for j=1:n(i)-1
             if hop(neighbour(i,j))==k && visited(i)==0
                hop(i)=k+1;
                visited(i)=1;
                computed=computed+1;
             end
        end
    end     
    k=k+1;
end
parent_constraint=ones(100,1);children=zeros(N,N*0.2);ch=ones(100,1);hop(1)=0;path_energy=ones(100,1);
for i=1:N
    k=i;
    for j=1:n(i)-1
        temp=neighbour(i,j);
        if hop(temp)<hop(k) | (hop(temp)==hop(k) & ch(temp)<ch(k))
            k=temp;
        end
    end
    if k~=i
       parent_hops(i)=k;
       children(k,ch(k))=i;
       ch(k)=ch(k)+1;
    end
end
% plot(x,y,'o',x(1),y(1),'*');
% for i=1:N
%      cordinate_x(1)=x(i);
%      cordinate_x(2)=x(parent_hops(i));
%      cordinate_y(1)=y(i);
%      cordinate_y(2)=y(parent_hops(i));
%      line(cordinate_x,cordinate_y);     
%      text(x(i)+2,y(i)+2,num2str(i));
% end
for h=0:20
    for i=1:100
        if hop(i)==h
            j=parent_hops(i);
            if i~=j
               d(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2);
               g(i,j)=10+10*n0*log(10/d(i,j));
               pk1(i,j)=(1-1/2*exp(-10^(0.1*g(i,j))*bn/2/r))^(rou*8*f);
               link_energy(i,j)=1-(1-pk1(i,j))^3;
               path_energy(i)=link_energy(i,j)*path_energy(j);
            else 
               path_energy(i)=1;
            end
        end
    end
end           
axis([0,100,0,100]);
i=[1:100];
plot(i,c,'-o',i,path_energy,'-o');
legend reliability hops;
xlabel('node ID');
ylabel('probability');
for i=1:10
    count(i)=0;
    count1(i)=0;
    for j=1:100
        if c(j)>=(i-1)*0.1 && c(j)<i*0.1
            count(i)=count(i)+1;
        end
         if path_energy(j)>=(i-1)*0.1 && path_energy(j)<i*0.1
            count1(i)=count1(i)+1;
        end
    end
end
% i=[0:0.1:0.9];
%  plot(i,count,'-o',i,count1,'-o');
%  xlabel('probobility');
%  ylabel('node number');
po=ones(100,1);
e=ones(100,1);
for i=1:100
     j=i;
     sum(i)=0;
     while j~=1
         sum(i)=sum(i)+po(j)/e(j);
         j=parent_reliability(j);
     end
end
for i=1:100
     j=i;
     sum1(i)=0;
     while j~=1
         sum1(i)=sum1(i)+po(j)/e(j);
         j=parent_hops(j);
     end
  end
%   found(10);
%  i=[1:100];
%  plot(i,sum,'-o',i,sum1,'-o');
%  legend reliability hops
 for i=1:10
    count(i)=0;
    count1(i)=0;
    for j=1:100
        if sum(j)>=(i-1)*4 && sum(j)<i*4
            count(i)=count(i)+1;
        end
         if sum1(j)>=(i-1)*4 && sum1(j)<i*4
            count1(i)=count1(i)+1;
        end
    end
 end
% i=[0:4:36];
% plot(i,count,'-o',i,count1,'-o');
% legend reliability hops
for i=1:100
    found(i,0.5);
end
plot(x,y,'o',x(1),y(1),'*');
for i=1:N
     cordinate_x(1)=x(i);
     cordinate_x(2)=x(parent_constraint(i));
     cordinate_y(1)=y(i);
     cordinate_y(2)=y(parent_constraint(i));
     line(cordinate_x,cordinate_y);     
     text(x(i)+2,y(i)+2,num2str(i));
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -