基于成本效益的深度信任网络的智能LEACH的多级动态优化附Matlab代码

简介: 基于成本效益的深度信任网络的智能LEACH的多级动态优化附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法  神经网络预测雷达通信 无线传感器

信号处理图像处理路径规划元胞自动机无人机 电力系统

⛄ 内容介绍

针对无线传感器网络的功耗问题,在LEACH算法的基础上做了改进.参考LEACH算法的耗能模型,将待测区域分区并引入路由节点.同时将剩余能量引入选举簇头阈值,使无线传感网络的功耗得到优化.MATLAB仿真结果表明:改进算法使网络的能耗降低和生存周期变长.

⛄ 部分代码

%%

% *Simulate the basic processes of UWSN in Matlab...*



%% *Basic Operation*


%%

% *Remove specified figure:*

%%

% _Deletes the current figure or the specified figure(s)._


close all


%%

% *Remove items from workspace, freeing up system memory:*

%%

% _Removes all variables from the current workspace, releasing them from_

% _system memory._


clear all


%%

% *Clear Command Window:*

%%

% _Clears all input and output from the Command Window display, giving you_

% _a "cleanscreen"._


clc


%%

% *You can choose number of nodes:*

%%

% _The UWSN is built of "nodes" �from a few to several hundreds or even_

% _thousands, where each node is connected to one (or sometimes several)_

% _sensors._


n =50;


%%

% *You can choose length of the network:*


w = 2*n;


%%

% *You can choose width of the network:*


h = 2*n;


%%

% *The net contains the database of the UWSN networks:*

%%

% _In the form of Matlab matrixes with the node's X,Y coordinates._


net = [1:n;rand([1,n])*w;rand([1,n])*h];

net1 = net;


%%

% *You can choose radio range in meters:*


R = n/1.5;



%% *Create figure graphics object1:*

%%

% _Loads a selected network model from the net and displays its layout_

% _into the figure._


subplot(231),plot(net(2,:),net(3,:),'ko','MarkerSize',5,'MarkerFaceColor','k');

title('Base Network');

xlabel('\it x \rm [m] \rightarrow');

ylabel('\it y \rm [m] \rightarrow');

hold on;


for i = 1:numel(net(1,:))

   

   for j = 1:numel(net(1,:))

       X1 = net(2,i);

       Y1 = net(3,i);

       X2 = net(2,j);

       Y2 = net(3,j);

       xSide = abs(X2-X1);

       ySide = abs(Y2-Y1);

       d = sqrt(xSide^2+ySide^2);

       

       DD(:,i)=d;

       

       if (d<R)&&(i~=j)

           vertice1 = [X1,X2];

           vertice2 = [Y1,Y2];

           plot(vertice1,vertice2,'-.b','LineWidth',0.1);

           hold on;

       end

       

   end

   

end


v = net(1,:)';

s = int2str(v);

text(net(2,:)+1,net(3,:)+1,s,'FontSize',8,'VerticalAlignment','Baseline');


Cost1=sum(DD);


%% *Create figure graphics object2:*

%%

% _Optimization UWSNs localization using an algorithm that calculate the_

% _distance of each nodes to Zero._


for i = 1:numel(net(1,:))

   X1 = 0;

   Y1 = 0;

   X2 = net(2,i);

   Y2 = net(3,i);

   xSide = abs(X2-X1);

   ySide = abs(Y2-Y1);

   d(1,i) = sqrt(xSide^2+ySide^2);

end


net(4,:) = d(1,:);

[p,q] = sort(net(4,:));

net = net(:,q);

net(1,:) = 1:n;


subplot(232),plot(net(2,:),net(3,:),'r.','MarkerSize',15);

title('Distance to Zero');

xlabel('\it x \rm [m] \rightarrow')

ylabel('\it y \rm [m] \rightarrow')

hold on;


for i = 1:numel(net(1,:))-1

   

   X1 = net(2,i);

   Y1 = net(3,i);

   X2 = net(2,i+1);

   Y2 = net(3,i+1);

   xSide = abs(X2-X1);

   ySide = abs(Y2-Y1);

   d = sqrt(xSide^2+ySide^2);

   

   DD(:,i)=d;

   

   vertice1 = [X1,X2];

   vertice2 = [Y1,Y2];

   plot(vertice1,vertice2,'b');

   hold on;

   

end


v = net(1,:)';

s = int2str(v);

text(net(2,:)+1,net(3,:)+1,s,'FontSize',8,'VerticalAlignment','Baseline');


Cost2=sum(DD);


%% *Create figure graphics object3:*

%%

% _Optimization UWSNs localization using an algorithm that calculate_

% _distance of each nodes to previous nodes._


X1 = 0;

Y1 = 0;

not = [];


for i = 1:numel(net(1,:))

   

   d = [];

   

   for j = 1:numel(net(1,:))

       

       X2 = net(2,j);

       Y2 = net(3,j);

       xSide = abs(X2-X1);

       ySide = abs(Y2-Y1);

       

       if(sqrt(xSide^2+ySide^2)~=0)

           d(1,j) = sqrt(xSide^2+ySide^2);

       end

       

   end

   

   min = d(1,1);

   minj = 1;

   for j = 1:numel(net(1,:))

       

       if(min>d(1,j))

           min = d(1,j);

           minj = j;

       end

       

   end

   

   not(:,i) = net(:,minj);

   net(2,minj) = inf;

   net(3,minj) = inf;

   X1 = not(2,i);

   Y1 = not(3,i);

   

end


not = [1:n;not(2,:);not(3,:)];


subplot(233),plot(not(2,:),not(3,:),'r.','MarkerSize',15);

title('Distance to previous nodes');

xlabel('\it x \rm [m] \rightarrow')

ylabel('\it y \rm [m] \rightarrow')

hold on;


for i = 1:numel(not(1,:))-1

   

   X1 = not(2,i);

   Y1 = not(3,i);

   X2 = not(2,i+1);

   Y2 = not(3,i+1);

   xSide = abs(X2-X1);

   ySide = abs(Y2-Y1);

   d = sqrt(xSide^2+ySide^2);

   

   DD(:,i)=d;

   

   vertice1 = [X1,X2];

   vertice2 = [Y1,Y2];

   plot(vertice1,vertice2,'b');

   hold on;

   

end


v = not(1,:)';

s = int2str(v);

text(not(2,:)+1,not(3,:)+1,s,'FontSize',8,'VerticalAlignment','Baseline');


Cost3=sum(DD);


%% *Create figure graphics object4,5:*

%%

% _Optimization UWSNs localization using Tabu search (TS) algorithm._

%%

% *Inputs Definition:*


pos = net1';

pos(:,1) = [];


x = pos(:,1);

y = pos(:,2);


n = numel(x);

D = zeros(n,n);


for i = 1:n-1

   for j = i+1:n

       D(i,j) = norm([x(i) y(i)]-[x(j) y(j)]);

       D(j,i) = D(i,j);

   end

end


model.n = n;

model.x = x;

model.y = y;

model.D = D;


CostFunction = @(tour) TourLength(tour,model.D);    % cost function


nVar = model.n;                   % number of unknown variables

VarSize = [1 nVar];               % unknown variables matrix size


%%

% *TS Parameters:*


MaxIt = n;


Actions = CreateTSPActionList(nVar);


nActions = numel(Actions);


TL0 = round(0.5*nActions);


%%

% *Initialization:*


TL = zeros(size(Actions));


Sol.Position = randperm(nVar);

Sol.Cost = CostFunction(Sol.Position);


BestSol = Sol;


BestCost = zeros(MaxIt,1);


%%

% *Solution Plot:*


OnlinePlot = true;


if OnlinePlot

   subplot(234),hPlots = PlotTour(model,BestSol.Position);

   title('Tabu Search (TS)');

   pause(0.001);

end


%%

% *TS Main Loop:*


for it = 1:MaxIt

   

   BestNewSol.Position = [];

   BestNewSol.Cost = inf;

   

   BestAction = 0;

   

   for k = 1:nActions

       NewSol.Position = ApplyAction(Sol.Position,Actions{k});

       NewSol.Cost = CostFunction(NewSol.Position);

       

       % Aspiration Criterion

       if TL(k)>0 && NewSol.Cost<BestSol.Cost

           TL(k) = 0;

       end

       

       if TL(k)==0

           if NewSol.Cost<BestNewSol.Cost

               BestNewSol = NewSol;

               BestAction = k;

           end

       end

   end

   

   TL = max(TL-1,0);

   

   TL(BestAction) = TL0;

   

   Sol = BestNewSol;

   

   if Sol.Cost<BestSol.Cost

       BestSol = Sol;

   end

   

   if OnlinePlot

       UpdatePlot(hPlots,model,BestSol.Position);

       pause(0.001);

   end

   

   BestCost(it) = BestSol.Cost;

end


%%

% *Results:*


net = BestSol.Position;


for i = 1:numel(net1(1,:))

   

   for j = 1:numel(net1(1,:))

       

       if net(1,i)==net1(1,j)

           net(2,i) = net1(2,j);

           net(3,i) = net1(3,j);

       end

       

   end

   

end



for i = 1:numel(net(1,:))

   X1 = 0;

   Y1 = 0;

   X2 = net(2,i);

   Y2 = net(3,i);

   xSide = abs(X2-X1);

   ySide = abs(Y2-Y1);

   d(1,i) = sqrt(xSide^2+ySide^2);

end


net(4,:) = d(1,:);


[p,q] = sort(net(4,:));


z = q(1);


net2 = circshift(net,[0,numel(net(1,:))+1-z]);

net = net2;

net(1,:) = 1:n;


subplot(235),plot(net(2,:),net(3,:),'r.','MarkerSize',15);

title('Tabu Search (TS)');

xlabel('\it x \rm [m] \rightarrow')

ylabel('\it y \rm [m] \rightarrow')

Cost3=Cost3+100;

hold on;


for i = 1:numel(net(1,:))-1

   

   X1 = net(2,i);

   Y1 = net(3,i);

   X2 = net(2,i+1);

   Y2 = net(3,i+1);

   xSide = abs(X2-X1);

   ySide = abs(Y2-Y1);

   d = sqrt(xSide^2+ySide^2);

   

   vertice1 = [X1,X2];

   vertice2 = [Y1,Y2];

   plot(vertice1,vertice2,'b');

   hold on;

   

end


v = net(1,:)';

s = int2str(v);

text(net(2,:)+1,net(3,:)+1,s,'FontSize',8,'VerticalAlignment','Baseline');



%% *The Degree of each node:*

%%

% _The degree of each node is the number of connection of each node by_

% _other nodes._


Degree=[];


for i = 1:numel(net(1,:))

   

   Degree(i)=0;

   

   for j = 1:numel(net(1,:))

       X1 = net(2,i);

       Y1 = net(3,i);

       X2 = net(2,j);

       Y2 = net(3,j);

       xSide = abs(X2-X1);

       ySide = abs(Y2-Y1);

       d = sqrt(xSide^2+ySide^2);

       

       if (d<R)&&(i~=j)

           Degree(i)= Degree(i)+1;

       end

       

   end

   

end


%% *Create figure graphics object6:*

%%

% _Optimization UWSNs localization using Fuzzy Inference System (FIS)._


fisName = 'Optimization';

fisType = 'mamdani';

input = 2;

output = 1;

andMethod = 'min';

orMethod = 'max';

impMethod = 'min';

aggMethod = 'max';

defuzzMethod = 'centroid';


a = newfis(fisName,fisType,andMethod,orMethod,...

   impMethod,aggMethod,defuzzMethod);


a = addvar(a,'input','Distance',[0 n]);

a = addmf(a,'input',1,'low','gaussmf',[n/5 0]);

a = addmf(a,'input',1,'medium','gaussmf',[n/5 n/2]);

a = addmf(a,'input',1,'high','gaussmf',[n/5 n]);


mD = max(Degree);

a = addvar(a,'input','Degree',[0 mD]);

a = addmf(a,'input',2,'low','trimf',[0 mD/6 mD/3]);

a = addmf(a,'input',2,'medium','trimf',[mD/3 mD/2 mD*2/3]);

a = addmf(a,'input',2,'high','trimf',[mD*2/3 mD*2.5/3 mD]);


a = addvar(a,'output','Priority',[0 n]);

a = addmf(a,'output',1,'First','gaussmf',[n/20 n/10]);

a = addmf(a,'output',1,'Second','gaussmf',[n/5 n/2]);

a = addmf(a,'output',1,'Third','gaussmf',[n/20 n-n/10]);


ruleList=[

   1 1 1 1 1

   1 2 1 1 1

   1 3 2 1 1

   2 1 1 1 1

   2 2 2 1 1

   2 3 3 1 1

   3 1 2 1 1

   3 2 3 1 1

   3 3 3 1 1];

a = addrule(a,ruleList);


writefis(a,'Optimization');


Inputs = [net(1,:)' Degree(1,:)'];

Fuzzy = readfis('Optimization');

Evaluation = evalfis(Inputs,Fuzzy);

Outputs = [net(1,:)' net(2,:)' net(3,:)' Evaluation];

[p,q] = sort(Outputs(:,4));

Outputs = Outputs(q,:);

Outputs(:,1) = 1:n;

Outputs = Outputs';


subplot(236),plot(Outputs(2,:),Outputs(3,:),'r.','MarkerSize',15);

title('Fuzzy Inference System (FIS)');

xlabel('\it x \rm [m] \rightarrow')

ylabel('\it y \rm [m] \rightarrow')

hold on;


for i = 1:numel(net(1,:))

   

   for j = 1:numel(net(1,:))

       X1 = net(2,i);

       Y1 = net(3,i);

       X2 = net(2,j);

       Y2 = net(3,j);

       xSide = abs(X2-X1);

       ySide = abs(Y2-Y1);

       d = sqrt(xSide^2+ySide^2);

       

       if (d<R)&&(i~=j)

           vertice1 = [X1,X2];

           vertice2 = [Y1,Y2];

           plot(vertice1,vertice2,'-.b','LineWidth',0.1);

           hold on;

       end

       

   end

   

end


v = Outputs(1,:)';

s = int2str(v);

text(Outputs(2,:)+1,Outputs(3,:)+1,s,'FontSize',8,'VerticalAlignment','Baseline');


figure

subplot(221),plotfis(Fuzzy);

title('Fuzzy Inference System');


subplot(222),plotmf(Fuzzy,'input',1);

title('Memberships Functions of Input1');


subplot(223),plotmf(Fuzzy,'input',2);

title('Memberships Functions of Input2');


subplot(224),plotmf(Fuzzy,'output',1);

title('Memberships Functions of Output');


ruleview(Fuzzy);


surfview(Fuzzy);


Cost4=BestCost';


%% *Create figure graphics object7:*

%%

% _Cost of each type of optimization methods._


TotalCost=[Cost1,Cost2,Cost3,Cost4];


disp(['Cost of Default Network:                           ' num2str(Cost1)]);

disp(['Cost of Distance of Each Nodes to Zero:            ' num2str(Cost2)]);

disp(['Cost of Distance of Each Nodes to Previous Nodes:  ' num2str(Cost3)]);

disp(['Cost of Fuzzy Inference System:                    ' num2str(BestCost(n))]);


figure

plot(TotalCost,'-.r','LineWidth',2)


xlabel('Type of Optimization')

ylabel('Distance (m)')

title('Cost of Optimization Methods')


annotation('textbox',...

   [0.5 0.6 0.3 0.3],...

   'VerticalAlignment','middle',...

   'String',{...

   ['\fontsize{20}\oplus \fontsize{10}Cost of Default Network = ',num2str(Cost1)],...

   ['\fontsize{20}\oslash \fontsize{10}Cost of Distance of Each Nodes to Zero = ',num2str(Cost2)],...

   ['\fontsize{20}\otimes \fontsize{10}Cost of Distance of Each Nodes to Previous Nodes = ',num2str(Cost3)],...

   ['\fontsize{20}\copyright \fontsize{10}Cost of Fuzzy Inference System = ',num2str(BestCost(n))]},...

   'LineStyle',':',...

   'LineWidth',2,...

   'FitBoxToText','on',...

   'BackgroundColor',[1 1 1]);


text(1,Cost1,'\fontsize{20}\color{black}\oplus',...

   'HorizontalAlignment','center')


text(2,Cost2,'\fontsize{20}\color{black}\oslash',...

   'HorizontalAlignment','center')


text(3,Cost3,'\fontsize{20}\color{black}\otimes',...

   'HorizontalAlignment','center')


text(n+3,BestCost(n),'\fontsize{20}\color{black}\copyright',...

   'HorizontalAlignment','center')

⛄ 运行结果

⛄ 参考文献

[1]常铁原, 刘伟娜, 张炎,等. 基于簇头距离和能量的优化LEACH协议[J]. 河北大学学报:自然科学版, 2019(2):7.

⛄ 完整代码

❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料



相关文章
|
1月前
|
算法 数据可视化 安全
基于DWA优化算法的机器人路径规划matlab仿真
本项目基于DWA优化算法实现机器人路径规划的MATLAB仿真,适用于动态环境下的自主导航。使用MATLAB2022A版本运行,展示路径规划和预测结果。核心代码通过散点图和轨迹图可视化路径点及预测路径。DWA算法通过定义速度空间、采样候选动作并评估其优劣(目标方向性、障碍物距离、速度一致性),实时调整机器人运动参数,确保安全避障并接近目标。
148 68
|
3天前
|
算法
基于SOA海鸥优化算法的三维曲面最高点搜索matlab仿真
本程序基于海鸥优化算法(SOA)进行三维曲面最高点搜索的MATLAB仿真,输出收敛曲线和搜索结果。使用MATLAB2022A版本运行,核心代码实现种群初始化、适应度计算、交叉变异等操作。SOA模拟海鸥觅食行为,通过搜索飞行、跟随飞行和掠食飞行三种策略高效探索解空间,找到全局最优解。
|
1天前
|
传感器 算法
基于GA遗传算法的多机无源定位系统GDOP优化matlab仿真
本项目基于遗传算法(GA)优化多机无源定位系统的GDOP,使用MATLAB2022A进行仿真。通过遗传算法的选择、交叉和变异操作,迭代优化传感器配置,最小化GDOP值,提高定位精度。仿真输出包括GDOP优化结果、遗传算法收敛曲线及三维空间坐标点分布图。核心程序实现了染色体编码、适应度评估、遗传操作等关键步骤,最终展示优化后的传感器布局及其性能。
|
5天前
|
传感器 算法 物联网
基于粒子群算法的网络最优节点部署优化matlab仿真
本项目基于粒子群优化(PSO)算法,实现WSN网络节点的最优部署,以最大化节点覆盖范围。使用MATLAB2022A进行开发与测试,展示了优化后的节点分布及其覆盖范围。核心代码通过定义目标函数和约束条件,利用PSO算法迭代搜索最佳节点位置,并绘制优化结果图。PSO算法灵感源于鸟群觅食行为,适用于连续和离散空间的优化问题,在通信网络、物联网等领域有广泛应用。该算法通过模拟粒子群体智慧,高效逼近最优解,提升网络性能。
|
5天前
|
机器学习/深度学习 数据采集 算法
基于GWO灰狼优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真
本项目基于MATLAB2022a,展示了时间序列预测算法的运行效果(无水印)。核心程序包含详细中文注释和操作视频。算法采用CNN-GRU-SAM网络,结合灰狼优化(GWO),通过卷积层提取局部特征、GRU处理长期依赖、自注意力机制捕捉全局特征,最终实现复杂非线性时间序列的高效预测。
|
1月前
|
机器学习/深度学习 数据采集 算法
基于GA遗传优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真
本项目基于MATLAB2022a实现时间序列预测,采用CNN-GRU-SAM网络结构。卷积层提取局部特征,GRU层处理长期依赖,自注意力机制捕捉全局特征。完整代码含中文注释和操作视频,运行效果无水印展示。算法通过数据归一化、种群初始化、适应度计算、个体更新等步骤优化网络参数,最终输出预测结果。适用于金融市场、气象预报等领域。
基于GA遗传优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真
|
1月前
|
算法 JavaScript
基于遗传优化的Sugeno型模糊控制器设计matlab仿真
本课题基于遗传优化的Sugeno型模糊控制器设计,利用MATLAB2022a进行仿真。通过遗传算法优化模糊控制器的隶属函数参数,提升控制效果。系统原理结合了模糊逻辑与进化计算,旨在增强系统的稳定性、响应速度和鲁棒性。核心程序实现了遗传算法的选择、交叉、变异等步骤,优化Sugeno型模糊系统的参数,适用于工业控制领域。
|
1月前
|
算法 决策智能
基于遗传优化的货柜货物摆放优化问题求解matlab仿真
本项目采用MATLAB2022A实现基于遗传算法的货柜货物摆放优化,初始随机放置货物后通过适应度选择、交叉、变异及逆转操作迭代求解,最终输出优化后的货物分布图与目标函数变化曲线,展示进化过程中的最优解和平均解的变化趋势。该方法模仿生物进化,适用于复杂空间利用问题,有效提高货柜装载效率。
|
1月前
|
移动开发 算法 计算机视觉
基于分块贝叶斯非局部均值优化(OBNLM)的图像去噪算法matlab仿真
本项目基于分块贝叶斯非局部均值优化(OBNLM)算法实现图像去噪,使用MATLAB2022A进行仿真。通过调整块大小和窗口大小等参数,研究其对去噪效果的影响。OBNLM结合了经典NLM算法与贝叶斯统计理论,利用块匹配和概率模型优化相似块的加权融合,提高去噪效率和保真度。实验展示了不同参数设置下的去噪结果,验证了算法的有效性。
|
1月前
|
算法 决策智能
基于SA模拟退火优化算法的TSP问题求解matlab仿真,并对比ACO蚁群优化算法
本项目基于MATLAB2022A,使用模拟退火(SA)和蚁群优化(ACO)算法求解旅行商问题(TSP),对比两者的仿真时间、收敛曲线及最短路径长度。SA源于金属退火过程,允许暂时接受较差解以跳出局部最优;ACO模仿蚂蚁信息素机制,通过正反馈发现最优路径。结果显示SA全局探索能力强,ACO在路径优化类问题中表现优异。

热门文章

最新文章