我需要使用matlab为组长选择实现WSN模拟器

时间:2014-04-30 06:46:36

标签: matlab simulation

我正在尝试在matlab上实现无线传感器网络模拟器,我需要你的帮助。

这正是我想要做的事情:

  1. 在2D平面中随机部署节点。
  2. 使用两个条件对组长选举算法进行建模:

    a)能量:生成与每个传感器相关的随机能量值,具有最大能量的传感器节点被选为领导者的概率更高。

    b)邻近:主要由相邻节点包围的传感器节点被选中的概率较高。

  3. 因此,对于具有最大能量的随机节点,可以选择更多邻居作为领导者,并以其他颜色绘制其余节点。

    我一直在研究这个问题,试图开发我的代码都无济于事。我对matlab编码不太满意,我还在学习。

    拜托,伙计们,我需要任何帮助,我的截止日期迫在眉睫。

    谢谢, 艾克

2 个答案:

答案 0 :(得分:2)

首先,使用matlab构建无线网络模拟器到目前为止还不是最佳选择。最好使用专用的模拟器,如NS2或Omnet ++。但是,我已经开发了一个简单的m文件代码来随机部署正方形中的点,并尝试根据这些点之间的距离在这些点之间进行链接。这些点被假定为传感器节点。

尝试对其进行修改以获得所需内容。如果这个答案对你有用,请投票赞成:

 W=0;X=0;Y=0;Z=0;
 nodedistance = zeros();
 maxx = 400; maxy=400; maxn = 50;
 q = zeros(maxn);
 e = rand(maxn,1)*100;
 nodeloc = rand(maxn, 2)* maxx;
 node(maxn) = struct('NodeNum',zeros(maxn),'nEnergy',zeros(maxn),'Loc',[zeros(maxn,1), zeros(maxn,2)]);
 rss(maxn,maxn) = struct('NodeNumber',zeros(maxn),'NodeDistance',zeros(maxn));
 for a = 1:100,

 m = 2;
 node = struct();
 rss = struct();
 nodedistance = zeros();
 maxx = 400; maxy=400; maxn = 50;
 q = zeros(maxn);
 e = rand(maxn,1)*100;
 nodeloc = rand(maxn, 2)* maxx;
     for  i = 1: maxn,
          node(i)=struct('NodeNum',i,'nEnergy',e(i),'Loc',[nodeloc(i, 1), nodeloc(i, 2)]);
     end
     for i = 1:maxn,
     for j = 1:maxn,
      rss(i,j) = struct('NodeNumber',i,'NodeDistance',sqrt((node(i).Loc(1)-  node(j).Loc(1))^2+(node(i).Loc(2)-node(j).Loc(2))^2));
     end
   end
 for i = 1:maxn,
 for j = 1:maxn,
    nodedistance(i,j)=rss(i,j).NodeDistance;
 end
 end
   for i = 1:maxn,
   for j = 1:maxn,
       if  (node(i).nEnergy > 0) &&  (node (j).nEnergy > 0) && (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)

       if (node(i).nEnergy < node (j).nEnergy) %|| ( (node(i).nEnergy == node (j).nEnergy )&& (0 < nodedistance(i,j) && nodedistance(i,j)) < 30) %|| (0 < nodedistance(i,j) && nodedistance(i,j) <= 30)

      q(i,j) = 1;

      elseif node(i).nEnergy == node (j).nEnergy && 0 < nodedistance(i,j) && nodedistance(i,j) < 30

       q(i,j) = 1;

      else if 0 < nodedistance(i,j) && nodedistance(i,j) <= 30

        q(i,j) = 1;

      else

        q(i,j) = 0;

      end
      end
      end
      end
      end
      end


        for i = 1:maxn,
          for j = 1:maxn,
              if q(i,j) == 1
                 q(j,i) = 1;
            end 
         end
       end

     colordef white,
     figure(1);
      axis equal
     for i = 1:maxn,
        hold on 
        box on;
        plot(nodeloc(i, 1), nodeloc(i, 2), 'k.', 'MarkerSize', 5);
        lscatter(nodeloc(i, 1),nodeloc(i, 2),i);
       grid on;
     end
       gplot(q,nodeloc,'r-');
      c = q;
      b = 0; zeros(maxn);
      check = 1;
      while (check)
        for m = 2:30
       p = size(b)
       b = zeros(maxn,maxn);
            for i = 1:maxn,
            for j = 1:maxn,
        if i ~= j
        if c(i,j) == 0
        for k = 1:maxn,
       if c(i,k) >0 && q(k,j) >0
       b(i,j) = m;
    break

     else b(i,j) = 0;
     end
     end
     end
     end
     end
     end
        f = size(b)
        c = c + b;
     if b ==0 
          check = 0;
       else b = 0;% while loop here the condition.
     m = m + 1; 
     end
     end
       k = 0;
         for i = 1:maxn,
         for j = 1:maxn,
          k = k + c(i,j);
        end
        end
           average_hop = k/(maxn*(maxn-1));

         o = 0;
         for i = 1:maxn,
         for j = 1:maxn,
         if c(i,j) ~= 0
         o = o + 1;
        end
        end
        end
           c(~c) = nan;
        r=max(max(c));
        t=min(min(c));
      clear
      end 

     formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
      formatSpec2 = 'The average number of hops is %.4f, the maximum hop-count is %d, the minimum hop-count is %d \n';
    formatSpec3 = 'The number of created paths is %d \n';
      fileID = fopen('Paths11.txt','w');
      fprintf(fileID,formatSpec1,i,j,c(i,j));
       fprintf(fileID,formatSpec2,average_hop,r,t);
       fprintf(fileID,formatSpec3,o);

      clear;
      X = X+average_hop;
         Y = Y+r;
      z = Z+t;
        W = W+o; 
         end;

        formatSpec1 = 'The number of hops from node %d to node %d is %d \n';
        formatSpec2 = 'The average number of hops is %.4f, the average maximum hop-count is %.2f, the minimum hop-count is %d \n';
        formatSpec3 = 'The average number of created paths is %.2f \n';
        fileID = fopen('Energy-50-AODV.txt','w');
        fprintf(fileID,formatSpec1,i,j,c(i,j));
        fprintf(fileID,formatSpec2,X/a,Y/a,t);
        fprintf(fileID,formatSpec3,W/a);

        clear;

答案 1 :(得分:0)

这不是一个为你的作业做免费网站;你必须自己完成大部分工作。以下是我的提示:

  • 请勿将作业留到最后一分钟。
  • 研究在您所在领域工作的其他人的MATLAB代码,并在遇到困难时参考MATLAB帮助文件(点击F1)。这是一个富有成效的搜索查询,可以为您指明正确的方向:wsn OR "sensor network" clustering matlab code site:edu