LEACH网络协议性能仿真包括能耗,死亡节点,剩余存活节点
Posted 51matlab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LEACH网络协议性能仿真包括能耗,死亡节点,剩余存活节点相关的知识,希望对你有一定的参考价值。
1.算法仿真效果
matlab2022a仿真结果如下:
2.算法涉及理论知识概要
LEACH协议,全称是“低功耗自适应集簇分层型协议” (Low Energy Adaptive Clustering Hierarchy),是一种无线传感器网络路由协议。基于LEACH协议的算法,称为LEACH算法。LEACH算法是一种无线传感器网络路由协议,来源于Wendi Rabiner Heinzelman, Anantha Chandrakasan, 和Hari Balakrishnan三人在2000年Proceedings of the 33rd Hawaii International Conference on System Sciences上的一篇文章Energy-Efficient Communication Protocol for Wireless Microsensor Networks。 该算法基本思想是:以循环的方式随机选择簇头节点,将整个网络的能量负载平均分配到每个传感器节点中,从而达到降低网络能源消耗、提高网络整体生存时间的目的。仿真表明,与一般的平面多跳路由协议和静态分层算法相比,LEACH分簇协议可以将网络生命周期延长15%。
LEACH在运行过程中不断的循环执行簇的重构过程,每个簇重构过程可以用回合的概念来描述。每个回合可以分成两个阶段:簇的建立阶段和传输数据的稳定阶段。为了节省资源开销,稳定阶段的持续时间要大于建立阶段的持续时间。簇的建立过程可分成4个阶段:簇头节点的选择、簇头节点的广播、簇头节点的建立和调度机制的生成。
簇头节点的选择依据网络中所需要的簇头节点总数和迄今为止每个节点已成为簇头节点的次数来决定。具体的选择办法是:每个传感器节点随机选择0-1之间的一个值。如果选定的值小于某一个阈值,那么这个节点成为簇头节点。
选定簇头节点后,通过广播告知整个网络。网络中的其他节点根据接收信息的信号强度决定从属的簇,并通知相应的簇头节点,完成簇的建立。最后,簇头节点采用TDMA方式为簇中每个节点分配向其传递数据的时间点。
稳定阶段中,传感器节点将采集的数据传送到簇头节点。簇头节点对簇中所有节点所采集的数据进行信息融合后再传送给汇聚节点,这是一种较少通信业务量的合理工作模型。稳定阶段持续一段时间后,网络重新进入簇的建立阶段,进行下一回合的簇重构,不断循环,每个簇采用不同的CDMA代码进行通信来减少其他簇内节点的干扰。
LEACH路由协议主要分为两个阶段:即簇建立阶段(setup phase)和稳定运行阶段(ready phase)。簇建立阶段和稳定运行阶段所持续的时间总和为一轮(round)。为减少协议开销,稳定运行阶段的持续时间要长于簇建立阶段。
在簇建立阶段,传感器节点随机生成一个0,1之间的随机数,并且与阈值T(n)做比较,如果小于该阈值,则该节点就会当选为簇头。T(n)按照下列公式计算:式中:P为节点成为簇头节点的百分数,r为当前轮数,G为在最近的1/p轮中未当选簇头的节点集合。簇头节点选定后,广播自己成为簇头的消息,节点根据接收到的消息的强度决定加入哪个簇,并告知相应的簇头,完成簇的建立过程。然后,簇头节点采用TDMA的方式,为簇内成员分配传送数据的时隙。
在稳定阶段,传感器节点将采集的数据传送到簇头节点。簇头节点对采集的数据进行数据融合后再将信息传送给汇聚节点,汇聚节点将数据传送给监控中心来进行数据的处理。稳定阶段持续一段时间后,网络重新进入簇的建立阶段,进行下一轮的簇重建,不断循环。
3.MATLAB核心程序
IniEng=1; NetSize=200; % Network Size NoOfNode=200; % Number of Node Iteration=2400; [R1,FD1,TD1,AD1]=leach(IniEng,NetSize,NoOfNode,Iteration,0.1);%% Leach [R2,FD5,TD5,AD5]=Leach_Centralized(IniEng,NetSize,NoOfNode,Iteration,0.1); %% Centralized Leach [R3,FD2,TD2,AD2]=Improved1_leach(IniEng,NetSize,NoOfNode,Iteration,0.1); %% Improved Leach 1 r=0:Iteration; figure plot(r(1:50:end),R1.DEAD(1:50:end),\'-bs\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.9,0.0,0.0]); hold on plot(r(1:50:end),R2.DEAD(1:50:end),\'-bo\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.0,0.9,0.0]); hold on plot(r(1:50:end),R3.DEAD(1:50:end),\'-b^\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.0,0.0,0.9]); legend(\'LEACH\',\'LEACH-C\',\'E-LEACH\'); xlabel(\'x(time)\'); ylabel(\'死亡节点\'); figure plot(r(1:50:end),R1.ALLIVE(1:50:end),\'-bs\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.9,0.0,0.0]); hold on plot(r(1:50:end),R2.ALLIVE(1:50:end),\'-bo\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.0,0.9,0.0]); hold on plot(r(1:50:end),R3.ALLIVE(1:50:end),\'-b^\',... \'LineWidth\',1,... \'MarkerSize\',6,... \'MarkerEdgeColor\',\'k\',... \'MarkerFaceColor\',[0.0,0.0,0.9]); legend(\'LEACH\',\'LEACH-C\',\'E-LEACH\'); xlabel(\'x(time)\'); ylabel(\'存活节点\');
基于MATLAB/GUI的自组网仿真平台,对比leach,ADOV协议
目录
一、理论基础
此仿真平台由控制中心和终端节点组成(GUI界面交互)。控制中心可以向节点发送控制命令,不可到达节点可以通过多跳方式到达;节点返回相关信息;通信方式为UDP。
控制中心主要包括消息收发模块、用户列表(相关节点信息,用户名和IP)、参数设置模块和功能性模块。
- 参数设置模块:可以实现节点个数、仿真场景大小、仿真时间、信道模型、路由协议、节点初始能量等的输入或者选择
- 功能性模块:显示拓扑结构图;计算网络平均吞吐量、平均端到端时延、丢包率、剩余节点个数、节点消耗能量等
数据吞吐:
这个指标的计算,通过一段时间N的仿真,然后除以N,得到整个网络的平均吞吐量。
网络能量消耗:
网络能量的计算,即当前时刻的能量为前一时刻的能量减去当前时刻的能量消耗。
在进行网络相关指标计算的时候,需要对每个时刻的网络剩余能量Ei进行判断,如果大于0,则继续循环进行计算,如果小于等于0,则停止计算。
负载均衡度对比:
通过计算网络最大负载,和最小负载,然后得到负载均衡度。
这个指标反映了在实际工作中,网络的负载分布情况。
丢包率:
这在整个网络中,通过发送bit数据包,然后统计丢包率,计算公式如下所示:
即统计每个时刻的丢包的bit数,除以当前时刻总的数据包,获得当前时刻的丢包率。
死亡节点数:
根据网络能耗指标:
二、核心程序
function varargout = tops(varargin)
% TOPS MATLAB code for tops.fig
% TOPS, by itself, creates a new TOPS or raises the existing
% singleton*.
%
% H = TOPS returns the handle to a new TOPS or the handle to
% the existing singleton*.
%
% TOPS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TOPS.M with the given input arguments.
%
% TOPS('Property','Value',...) creates a new TOPS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tops_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tops_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help tops
% Last Modified by GUIDE v2.5 05-Jun-2014 20:45:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tops_OpeningFcn, ...
'gui_OutputFcn', @tops_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin1)
gui_State.gui_Callback = str2func(varargin1);
end
if nargout
[varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
else
gui_mainfcn(gui_State, varargin:);
end
% End initialization code - DO NOT EDIT
% --- Executes just before tops is made visible.
function tops_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to tops (see VARARGIN)
% Choose default command line output for tops
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tops wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = tops_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout1 = handles.output;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%============================================================================场景初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================场景初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================场景初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================场景初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
SEEK = str2num(get(handles.edit7,'String'));
rand('state',SEEK);
SEEK2 = str2num(get(handles.edit8,'String'));
A = str2num(get(handles.edit1,'String'));
nodes_number = str2num(get(handles.edit2,'String'));
R = str2num(get(handles.edit3,'String'));
X = rand(1,nodes_number)*A/2;
Y = rand(1,nodes_number)*A/2;
views = get(handles.checkbox1,'Value');
%节点初始化,这里直接进行编程,不通过GUI界面设置
%节点状态的初始化
%节点状态的初始化
%负载
rand('state',SEEK2);
tmps = 0.7*rand(3,nodes_number);
%假设节点的负载时随机的
% Fload = tmps(1,:);%负载百分比0~1之间
[xs,ys,ds,iscom] = func_dist([X',Y'],nodes_number,R);
for i = 1:nodes_number
dd = ds(i,:);
NEAR = length(find(dd<R));
Fload(i) = 0.4+0.6*NEAR/nodes_number;
end
%剩余能量
PLest = min(2*tmps(2,:),1);%剩余能量百分比0~1之间
%损坏
BREAK = tmps(3,:) >= 0.2;%0表示的损坏的节点,1表示正常的节点
axes(handles.axes1);
cla reset;
plot(X,Y,'b s');
hold on;
xlabel('空间横坐标 x 单位:m');
ylabel('空间纵坐标 y 单位:m');
hold on;
for i = 1:nodes_number
%给节点标注号码
if views == 1
text(X(i)+2, Y(i)+2, num2str(i));
end
for j = 1:nodes_number
distance = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2);
if distance <= R
nodes_link(i, j) = 1;
grid on;
else
nodes_link(i, j) = inf;
end;
end;
end;
%============================================================================路径搜索<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================路径搜索<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================路径搜索<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================路径搜索<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
parameter;
if get(handles.radiobutton1,'value')
SEL1=1;
elseif get(handles.radiobutton2,'value')
SEL1=2;
elseif get(handles.radiobutton3,'value')
SEL1=3;
end
if get(handles.radiobutton4,'value')
SEL2=1;
elseif get(handles.radiobutton5,'value')
SEL2=2;
end
S=str2num(get(handles.edit5,'String'));
D=str2num(get(handles.edit6,'String'));
if S < 1 | S > nodes_number
msgbox('源节点错误,请重新输入');
end
if D < 1 | D > nodes_number
msgbox('目标节点错误,请重新输入');
end
axes(handles.axes1);
cla reset;
plot(X,Y,'b s');
hold on;
xlabel('空间横坐标 x 单位:m');
ylabel('空间纵坐标 y 单位:m');
hold on;
grid on;
for i = 1:nodes_number
%给节点标注号码
if views == 1
text(X(i)+2, Y(i)+2, num2str(i));
end
for j = 1:nodes_number
distance = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2);
if distance <= R
nodes_link(i, j) = 1;
grid on;
else
nodes_link(i, j) = inf;
end;
end;
end;
%%
%LEACH
if SEL1 == 1
[PATHS,hop] = leach_path_discovery(nodes_number,nodes_link,S,D);
l=length(PATHS);
end
%%
%AODV
if SEL1 == 2
[PATHS,hop] = aodv_path_discovery(nodes_number,nodes_link,S,D);
l=length(PATHS);
end
%%
%改进AODV
if SEL1 == 3
[PATHS,hop] = aodv_path_discovery_new(nodes_number,nodes_link,S,D,Fload,PLest,BREAK);
l = length(PATHS);
end
if l==0 & S~=D
fprintf('源节点 %d 到目的节点 %d 的路径为:空!\\n',S,D);
fprintf('\\n');
plot(X(S),Y(S),'rp','markersize',15);
plot(X(D),Y(D),'rp','markersize',15);
elseif l==0 & S==D
fprintf('源节点 %d 与目的节点 %d 为同一节点。\\n',S,D);
fprintf('跳数为 %d 。\\n',hop);
fprintf('\\n')
plot(X(D),Y(D),'rp','markersize',15);
else fprintf('源节点 %d 到目的节点 %d 的路径为:',S,D);
i=2;
fprintf('%d',S);
while i~=l+1
fprintf(' -> %d',PATHS(i));
i=i+1;
end;
fprintf('\\n');
fprintf('跳数为 %d 。\\n',hop);
fprintf('\\n');
end;
if l ~= 0
for i = 1:(l-1)
line([X(PATHS(i)) X(PATHS(i+1))],[Y(PATHS(i)) Y(PATHS(i+1))],'Color','r','LineWidth',1.50);
end;
end;
if SEL1 == 1
save data_save\\path1.mat l S D X Y PATHS
end
if SEL1 == 2
save data_save\\path2.mat l S D X Y PATHS
end
if SEL1 == 3
save data_save\\path3.mat l S D X Y PATHS
end
function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton4
set(handles.radiobutton4,'value',1);
set(handles.radiobutton5,'value',0);
% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton5
set(handles.radiobutton4,'value',0);
set(handles.radiobutton5,'value',1);
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
set(handles.radiobutton1,'value',1);
set(handles.radiobutton2,'value',0);
set(handles.radiobutton3,'value',0);
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
set(handles.radiobutton1,'value',0);
set(handles.radiobutton2,'value',1);
set(handles.radiobutton3,'value',0);
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.radiobutton1,'value',0);
set(handles.radiobutton2,'value',0);
set(handles.radiobutton3,'value',1);
% Hint: get(hObject,'Value') returns toggle state of radiobutton3
function edit7_Callback(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit7 as text
% str2double(get(hObject,'String')) returns contents of edit7 as a double
% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%============================================================================性能仿真<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================性能仿真<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================性能仿真<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
%============================================================================性能仿真<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
Times = str2num(get(handles.edit10,'String'));
PACK = str2num(get(handles.edit11,'String'));
if SEL1 == 1
[Throughput,Power,Loads,droprate,death] = func_leach_performance(A,PACK,nodes_number,Times,X,Y,SEL2);
end
if SEL1 == 2
[Throughput,Power,Loads,droprate,death] = func_AODV_performance(A,PACK,nodes_number,Times,X,Y,SEL2);
end
if SEL1 == 3
[Throughput,Power,Loads,droprate,death] = func_advance_AODV_performance(A,PACK,nodes_number,Times,X,Y,SEL2);
end
axes(handles.axes2);
cla reset;
plot(Throughput,'b','linewidth',2);
grid on;
xlabel('x(time)');
ylabel('y(数据吞吐)');
axes(handles.axes3);
cla reset;
plot(Power,'b','linewidth',2);
grid on;
xlabel('x(time)');
ylabel('y(网络能量消耗)');
axes(handles.axes4);
cla reset;
plot(Loads,'b','linewidth',2);
grid on;
xlabel('x(time)');
ylabel('y(负载均衡度对比)');
axes(handles.axes5);
cla reset;
plot(droprate,'b','linewidth',2);
grid on;
xlabel('x(time)');
ylabel('y(丢包率)');
axes(handles.axes6);
cla reset;
plot(death,'b','linewidth',2);
grid on;
xlabel('x(time)');
ylabel('y(死亡节点数)');
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
if SEL2 == 1
axes(handles.axes2);
cla reset;
load data_save\\r0_1.mat
plot(Throughput,'b','linewidth',2);
hold on
load data_save\\r1_1.mat
plot(Throughput,'r','linewidth',2);
hold on
load data_save\\r2_1.mat
plot(Throughput,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(数据吞吐)');
axes(handles.axes3);
cla reset;
load data_save\\r0_1.mat
plot(Power,'b','linewidth',2);
hold on
load data_save\\r1_1.mat
plot(Power,'r','linewidth',2);
hold on
load data_save\\r2_1.mat
plot(Power,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(网络能量消耗)');
axes(handles.axes4);
cla reset;
load data_save\\r0_1.mat
plot(Loads,'b','linewidth',2);
hold on
load data_save\\r1_1.mat
plot(Loads,'r','linewidth',2);
hold on
load data_save\\r2_1.mat
plot(Loads,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(负载均衡度对比)');
axes(handles.axes5);
cla reset;
load data_save\\r0_1.mat
plot(droprate,'b','linewidth',2);
hold on
load data_save\\r1_1.mat
plot(droprate,'r','linewidth',2);
hold on
load data_save\\r2_1.mat
plot(droprate,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(丢包率)');
axes(handles.axes6);
cla reset;
load data_save\\r0_1.mat
plot(death,'b','linewidth',2);
hold on
load data_save\\r1_1.mat
plot(death,'r','linewidth',2);
hold on
load data_save\\r2_1.mat
plot(death,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(死亡节点数)');
end
if SEL2 == 2
axes(handles.axes2);
cla reset;
load data_save\\r0_2.mat
plot(Throughput,'b','linewidth',2);
hold on
load data_save\\r1_2.mat
plot(Throughput,'r','linewidth',2);
hold on
load data_save\\r2_2.mat
plot(Throughput,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(数据吞吐)');
axes(handles.axes3);
cla reset;
load data_save\\r0_2.mat
plot(Power,'b','linewidth',2);
hold on
load data_save\\r1_2.mat
plot(Power,'r','linewidth',2);
hold on
load data_save\\r2_2.mat
plot(Power,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(网络能量消耗)');
axes(handles.axes4);
cla reset;
load data_save\\r0_2.mat
plot(Loads,'b','linewidth',2);
hold on
load data_save\\r1_2.mat
plot(Loads,'r','linewidth',2);
hold on
load data_save\\r2_2.mat
plot(Loads,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(负载均衡度对比)');
axes(handles.axes5);
cla reset;
load data_save\\r0_2.mat
plot(droprate,'b','linewidth',2);
hold on
load data_save\\r1_2.mat
plot(droprate,'r','linewidth',2);
hold on
load data_save\\r2_2.mat
plot(droprate,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(丢包率)');
axes(handles.axes6);
cla reset;
load data_save\\r0_2.mat
plot(death,'b','linewidth',2);
hold on
load data_save\\r1_2.mat
plot(death,'r','linewidth',2);
hold on
load data_save\\r2_2.mat
plot(death,'g','linewidth',2);
hold on
legend('leach','AODV','改进AODV');
grid on;
xlabel('x(time)');
ylabel('y(死亡节点数)');
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
clear;
close all;
warning off;
function edit8_Callback(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit8 as text
% str2double(get(hObject,'String')) returns contents of edit8 as a double
% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit9_Callback(hObject, eventdata, handles)
% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit9 as text
% str2double(get(hObject,'String')) returns contents of edit9 as a double
% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit10_Callback(hObject, eventdata, handles)
% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit10 as text
% str2double(get(hObject,'String')) returns contents of edit10 as a double
% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
function edit11_Callback(hObject, eventdata, handles)
% hObject handle to edit11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit11 as text
% str2double(get(hObject,'String')) returns contents of edit11 as a double
% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
三、仿真测试结果
A12-15
以上是关于LEACH网络协议性能仿真包括能耗,死亡节点,剩余存活节点的主要内容,如果未能解决你的问题,请参考以下文章