资讯详情

【路径规划】基于Djisktra算法实现路由无线传感器网络模拟随机路点运动模型含Matlab源码

1 简介

基于Djisktra路由无线传感器网络模拟随机路点运动模型

2 部分代码

clc;clear all;close all;global EexL X Y Xb Yb N nodesCH alpha beta A SNpause(4)rng default;stop1=0;RoutingId=3;%3 --- Modified Dijikstra  N=80; % No. of nodest1='800x800';t2=strsplit(t1,'x');min1=0;max1=str2double(t2(2));X = min1 (max1-min1)*rand(1,N);Y = min1 (max1-min1)*rand(1,N);%%figure,plot(X,Y,'o','LineWidth',1,...                    'MarkerEdgeColor','k',...                    'MarkerFaceColor','g',...                    'MarkerSize',7');           xlabel('X in m')          ylabel('Y in m')                    for i2 = 1:N                     text(X(i2), Y(i2), num2str(i2),'FontSize',10);           hold on;endhold onxlabel('x');ylabel('y');%% RWM model% velocity distribution min and max (0,2)minVel=-4;maxVel=4;% PauseTime (0,1)minPause=0;maxPause=1;simTime=350;% Round750Rc=150; % coverage Area%Rc=280; %sensor field Radius% position of  source nodetb='0x0';t2=strsplit(tb,'x');Xs =str2double(t2(1));Ys =str2double(t2(2));         hold on         plot(Xs,Ys,'s','LineWidth',1,...                    'MarkerEdgeColor','k',...                    'MarkerFaceColor','r',...                    'MarkerSize',12');          xlabel('X in m')         ylabel('Y in m')         text(Xs, Ys, 'Source','FontSize',10);          hold on;                                      % position of  sink node%t1='410x410';t2=strsplit(t1,'x');Xb =str2double(t2(1));Yb =str2double(t2(2));         hold on         plot(Xb,Yb,'s','LineWidth',1,...                    'MarkerEdgeColor','k',...                    'MarkerFaceColor','y',...                    'MarkerSize',12');          xlabel('X in m')         ylabel('Y in m')         text(Xb, Yb, 'Base','FontSize',10);          hold on;                    %%alpha=0.01;%0.001 energy per distance% 0.001                                 %% node to CH power Ratiobeta=0.045;%0.0015                                                           %% CH to sink distance power ratioSector1=1;nodes=N;% Total No. of Nodes%% Energy mi1=0; mi2=25; mi3=50;ma1=50;ma2=75; ma3=100;Ep =4*1e3; % 4000 mWE1=Ep.*(mi1 (ma1-mi1).*rand(1,round(nodes/3)))./100 E2=Ep.*(mi2 (ma2-mi2).*rand(1,round(nodes/3)))./100 E3=Ep.*(mi3 (ma3-mi3).*rand(1,nodes-2*round(nodes/3)))./100 E=[E1 E2 E3]%E=4.*ones(1,nodes);  % intial Energy 4WPackSize=2; % 2Mb/secnodesCH=10;% no of cluster headEexL=E;E1=E;%% Ch selectionchalg1='chselalg.m'chalg=strrep(chalg1,'.m,;varName=matlab.lang.makeValidName(chalg);chselalg=str2func(varName)%% Routing         Route_type = 'custom';        %% EnergyFunctionEfun1='Energyfun.m'Efun=strrep(Efun1,'.m,;varName=matlab.lang.makeValidName(Efun);Efun=str2func(varName)A=3;if(strcmp(Route_type,'custom'))    Ralg1='DjisktraRoute.m';    Ralg=strrep(Ralg1,'.m,;    varName=matlab.lang.makeValidName(Ralg);    Ralgfun=str2func(varName)        end%%%% ThroughputFnThfun1='Throughputfun.m';Thfun=srrep(Thfun1,'.m','');varName=matlab.lang.makeValidName(Thfun);Thfun=str2func(varName)%% Simulation Startsround=simTime;roundDelay=0.05;%%ipp=1;  Alivenodes=zeros(1,round);AvgEc=zeros(1,round);Throughput=zeros(1,round);d=1;apL=1;%%X1=[Xs X Xb];Y1=[Ys Y Yb];matrizP=pdist2([X1 ;Y1]',[X1; Y1]');%%%N=nodes;%N1=1:nodes;figure,slider1_data.val=40;% 110 percent mobilityPauseTime=minPause+(maxPause-minPause).*rand(1,N);ipppause=zeros(1,N);while(ipp<round)                 %% Mobility Range    cla;       axis([min1-30 max1+30 min1-30 max1+30])     hold on      Velocity=minVel+(maxVel-minVel).*rand(2,N);            [valPause,indexPause]=find(abs(PauseTime-ipppause)>0);    ipppause(indexPause)=0;        ipppause=ipppause+1;              m1=10;%0.01+(slider1_data.val/100);      aa=-1;ba=1;      delx=Velocity(1,:);      dely=Velocity(2,:);                  delx(indexPause) = Velocity(1,indexPause);      dely(indexPause) = Velocity(2,indexPause);          X(indexPause)=X(indexPause)+delx.*m1;        Y(indexPause)=Y(indexPause)+dely.*m1;        X(X<min1 | X>max1)=X(X<min1 | X>max1)-delx(X<min1 | X>max1).*m1;         Y(Y<min1 | Y>max1)=Y(Y<min1 | Y>max1)-dely(Y<min1 | Y>max1).*m1;     %%    X1=[Xs X Xb];    Y1=[Ys Y Yb];    matrizP=pdist2([X1 ;Y1]',[X1; Y1]');                %Ah=[ipp X Y]       plot(X,Y,'o','LineWidth',1,...                    'MarkerEdgeColor','k','Linewidth', 0.5,...                    'MarkerFaceColor','g',...                    'MarkerSize',8');           xlabel('X in m')          ylabel('Y in m')    for i2 = 1:N           text(X(i2), Y(i2), num2str(i2),'FontSize',10);           hold on;    end    hold on    xlabel('x');    ylabel('y');           hold on         plot(Xb,Yb,'s','LineWidth',1,...                    'MarkerEdgeColor','k',...                    'MarkerFaceColor','y',...                    'MarkerSize',12');          xlabel('X in m')         ylabel('Y in m')         text(Xb, Yb, 'Base','FontSize',10);          hold on;                                      % Source         plot(Xs,Ys,'s','LineWidth',1,...                    'MarkerEdgeColor','k',...                    'MarkerFaceColor','r',...                    'MarkerSize',12');          xlabel('X in m')         ylabel('Y in m')         text(Xs, Ys, 'Source','FontSize',10);          hold on;                               %%      EneExL(ipp)=0;            A1=randperm(N);                                                      % Randomly select Source node       ind=A1(3);       pathL=[];                   %% Custom Adhoc Routing       if(A==3)                     Source =1;          Dest=numel(X1);          Rc1=Rc;%4.*           matrizP(matrizP>Rc1)=inf;          [pathP,cost]=Ralgfun(Source,Dest,matrizP);          costN=cost.*length(pathP);                    dist1L=costN;          dist2L=0;          apL=0;          if(~isempty(pathP))                          if(pathP(end)~=Dest)                 pathP=[pathP Dest];                   end                                             if(EexL(ind)~=0 )              pathP(pathP==Source)=-2;              pathP(pathP==Dest)=-1;              pathL=pathP;%[ind -1];               path11=pathP;              path11(path11==-1)=[];              path11(path11==-2)=[];              path11=path11;              % Sink Node Assign as PathL=-1              apL=apL+1;              path11=path11-1              EexL(path11)=EexL(path11)-Efun(alpha,beta,dist1L,dist2L,A);              EneExL(ipp)=Efun(alpha,beta,dist1L,dist2L,A).*numel(path11);            end            end                     end       %%                                   %EexL(ind)=EexL(ind)-alpha.*dist1L -(beta+0.5).*dist2L;                                                     %id1(ind)            %EexL(id1(ind))                                    if(EexL(ind)<=0)               EexL(ind)=0;             end             %% if(Sector1==1) if(~isempty(pathL))     path1=pathL;    for p =1:(size(path1,2))-1            if(path1(p+1)==-1)                line([X1(path1(p)) Xb], [Y1(path1(p)) Yb], 'Color','c','LineWidth',2, 'LineStyle','-')                 arrow([X1(path1(p)) Y1(path1(p)) ], [Xb Yb ])            elseif(path1(p)==-2)                line([Xs X1(path1(p+1))], [Ys Y1(path1(p+1))], 'Color','c','LineWidth',2, 'LineStyle','-')                 arrow([Xs Ys ],[X1(path1(p+1)) Y1(path1(p+1)) ])                            else                line([X1(path1(p)) X1(path1(p+1))], [Y1(path1(p)) Y1(path1(p+1))], 'Color','c','LineWidth',2, 'LineStyle','-')                 arrow([X1(path1(p)) Y1(path1(p)) ], [X1(path1(p+1)) Y1(path1(p+1))])            end            hold on    end      end end                  EexL(EexL<=0)=0;  indg3=find(EexL<=0);  %  if(A==2 || A==4 || A==5) %     [CH]=chselalg(EexL,nodesCH,RoutingId)%  end    %% Store Parameters    Ec2L(ipp)=sum(EexL);                                                     % Energy Consumption    REc2L(ipp)=sum(E1) - sum(EexL);                                           % Remaining Energy Level                        % Throughput Calc             RxData1L=apL;                          if(ipp>1)                  ThroughputL(ipp)=Thfun(ThroughputL,RxData1L,ipp-1);              else                  ThroughputL(ipp)=RxData1L*PackSize;              end                        % AliveNodes Calc            hold on            plot(X(indg3),Y(indg3),'o','LineWidth',1,...                     'MarkerEdgeColor','k',...                     'MarkerFaceColor','k',...                     'MarkerSize',8');                     xlabel('X in m')                  ylabel('Y in m')                  hold on            AlivenodesL(ipp)=N-numel(indg3);            %Avg Energy Consumed            AvgEcL(ipp)=mean(EneExL);                                ipp=ipp+1;txt = {['Time in Sec-> ' num2str(ipp)]};text(10,600,txt)%set(h,'string',['Time in Sec-> ' num2str(ipp)]);pause(roundDelay)if(stop1==1)    stop1=0;    break;endend%%    inde=find(Ralg1=='.');    Az=Ralg1(1:inde-1);    fname=['-' Az];save(['Result\Alivenodes' fname '.mat'],'AlivenodesL')save(['Result\AvgEc' fname '.mat'],'AvgEcL')save(['Result\Ec2' fname '.mat'],'Ec2L')save(['Result\REc2' fname '.mat'],'REc2L')save(['Result\Throughput',fname,'.mat'],'ThroughputL')% %%figure,plot(1:numel(AlivenodesL)-1,AlivenodesL(1:end-1),'-*k')xlabel('Rounds')ylabel('AliveNodes')title('Alivenodes')%figure,plot(1:numel(AvgEcL)-1,AvgEcL(1:end-1),'-*k')xlabel('Rounds')ylabel('AvgEnergyConsumption')title('AvgEnergyConsumption')%%figure,plot(1:numel(Ec2L),Ec2L(1:end),'-*k')xlabel('Rounds')ylabel('Remaining Energy Level')title('Remaining Energy Level')%%figure,plot(1:numel(REc2L),REc2L(1:end),'-*k')xlabel('Rounds')ylabel('Energy Consumption Level')title('Energy Consumption Level')%%% Throughput  figure,plot(1:numel(ThroughputL)-1,ThroughputL(1:end-1),'-*k')xlabel('Rounds') ylabel('Recieved-Packets')title('Recieved-Packets')

3 仿真结果

4 参考文献

[1]王菁华, 张翠敏. 智能机器人综合路径规划算法在Matlab中的实现[J]. 天津工程师范学院学报, 2006, 16(3):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

标签: e0e1e2传感器无线传感器网络中pathyb传感器n传感器e3x

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台