基于K-mean实现无线传感器网络布局优化附Matlab代码

📅 2026/7/20 19:07:51
基于K-mean实现无线传感器网络布局优化附Matlab代码
1 简介针对无线传感器网络的能耗问题,提出了一种基于K-means聚类的能耗均衡路由算法(KBECRA).​2 部分代码%************************************************************************************************clear allclcclose all%***************************** global variables ********************************************REPerB84*2;%resource elements per resourse blockCategory[1,2,3];% 1:urban, 2:suburban,3:ruralF_HATA1500;%MHzNoiceFigure10; %dBBI15;NoisePower -174 10*log10(180*10^3)NoiceFigureBI;SINRmin-9.3;%dLbody3;%dB body lossLbpl18;%dB building penetration lossLj2;%jumber loosGa5;% gain%***************************** end global variables ******************************************%***************************** configuration of MeNB ******************************************MeNB(1).x500;% x coordinateMeNB(1).y500;% y coordinateMeNB(1).ChannelBandwidth20;%MHzMeNB(1).carrier1.8*10^9;%MHzMeNB(1).takeBitPerMilisec100000;%MHzMeNB(1).powerTransmit45; %dBmMeNB(1).height100; %metresfor i1:20MeNB(1).UEdata(i)0;%MHzendMaxPathLossCalculate_MaxPathLoss(MeNB(1).powerTransmit,NoisePower,SINRmin);%Max Path Loss%***************************** End configuration of MeNB ******************************************%***************************** configuration of 20-UEs %*****************************************% Here we locate the Ues In the Gridfor i1:50UE(i).x400;% x coordinateUE(i).y500;% y coordinateUE(i).height1.7;UserLocationX(i) 0;UserLocationY(i) 0;UserLocationZ(i) 0;endi1x400;% x coordinatey500while i51xrandi(1000, 1, 1);yrandi(1000, 1, 1);if (x400 || x500) (y400 || y500)UE(i).xx;% x coordinateUE(i).yy;% y coordinateUserLocationX(i) UE(i).x;UserLocationY(i) UE(i).y;UserLocationZ(i) 1.7;ii1;endend%***************************** End configuration of 20-UEs %*****************************************%***************************** scatter plot of UEs and ENB %*****************************************figure(Color, white)plot(UserLocationX, UserLocationY, ^, MarkerSize, 5, LineWidth, 3), hold onBaseStationX 500;BaseStationY 500;plot(BaseStationX, BaseStationY, rs, MarkerSize, 5, LineWidth, 4), hold on, grid on, grid minorhleg legend(User Location, Base Station);set(hleg, Location, NorthEastOutside);xlabel(coordinate Χ);ylabel(coordinate Υ);%***************************** End scatter plot of UEs and ENB %*****************************************%***************************** initialization phase of UEs *****************************************% Here we calculate some parameters of the UEs before they moving(PL,SINR,CQI,Max throughput, max resurceBlock that the UE xan take )for i1:50UE(i).distancefrom_eNB_m(1)round(Distance(MeNB(1).x,MeNB(1).y,UE(i).x,UE(i).y));%calculate distance between each UE ad eNBUE(i).PL_dB(1) (HATA_Model((UE(i).distancefrom_eNB_m(1)/1000),F_HATA(1),MeNB(1).height, UE(i).height,Category(2)))LbodyLbplLj;%calculate Path Loss for each UEUE(i).PowerReceive_dBmW(1) MeNB(1).powerTransmit -UE(i).PL_dBGa;%calculate Power receive ( Pr) for each UEUE(i).SNR_dB(1)UE(i).PowerReceive_dBmW-NoisePower;%calculate SINR for each UE [dB]UE(i).SNR(1) 10^(UE(i).SNR_dB(1)/10);%calculate SINR for each UEUE(i).CQI(1)CQI_calculation(UE(i).SNR_dB(1));%calculate CQI for each UE [dB]UE(i).MODULATION(1) Modulation_calculation(UE(i).SNR_dB(1));%calculate Modulation scheme for each UE depending on CQI [dB]UE(i).resurceBlock(1)setRB(UE(i).SNR_dB(1));%calculate how many resource block can each UE receive [dB]% UE(i).throughput(1)throughput_calculation( UE(i).MODULATION,UE(i).resurceBlock(1),UE(i).SNR_dB(1));%calculate throughput each UE receive [dB]end%{Ues request service from providercalcualation of servise prioritysheduler give as many resources as needed min{ can_decode,service_requires}%}for i1:50UE(i).QCI(1)QCI_example_service();UE(i).ExampleServise(1)ExampleServise_calculation(UE(i).QCI);UE(i).PriorityPriority_calculation(UE(i).QCI);UE(i).Resource_Block_Allocation(1)Resource_Block_Allocation_calculation( UE(i).QCI(1), UE(i).resurceBlock(1));UE(i).throughput(1)throughput_calculation( UE(i).MODULATION(1),UE(i).Resource_Block_Allocation(1), UE(i).SNR_dB(1));end%%--------------- Selecting Data of interest ---------------------% ---------------------------- Code ------------------------------Table_UE struct2table(UE);data Table_UE(:,1:2);data table2array(data);%________________________________________________________________%________________________________________________________________%%------------ Selecting Optimal Number of Clusters ------------% -------------- Method 1: Using the Elbow Method ---------------% ---------------------------- Code -----------------------------WCSS [];for k 1:20sumd 0;[idx,C,sumd] kmeans(data,k);WCSS(k) sum(sumd);endfigureplot(1:20, WCSS);ylabel( WCSS,fontsize,14);xlabel( drones,fontsize,14);prompt How many cluster do you want ?: ;clstr input(prompt);%________________________________________________________________%________________________________________________________________%%--------------- Clustering data -------------------------------[idx,C] kmeans(data,clstr);for i1:50UE(i).cluster_id(1)idx(i);end%________________________________________________________________%________________________________________________________________for i1:clstrDrone(i).xC(i,1);% x coordinateDrone(i).yC(i,2);% y coordinateDrone(i).indexi;Drone(i).height100;Drone(i).powerTransmit20;%dbm;endfor i1:clstrfor j1:50if Drone(i).indexUE(j).cluster_idUE(j).distancefromDrone(1)round(Distance(Drone(i).x,Drone(i).y,UE(j).x,UE(j).y));if UE(j).distancefromDrone(1)0UE(j).distancefromDrone(1)2;end% UE(j).PL_Drone_dB(1) (HATA_Model((UE(j).distancefromDrone(1)/1000),F_HATA(1),Drone(i).height, UE(j).height,Category(2)))LbodyLbplLj;%calculate Path Loss for each UEUE(j).PL_Drone_dB(1)Air2Ground(UE(j).distancefromDrone(1)/1000,F_HATA(1),Drone(i).height)LbodyLj;UE(j).PowerReceiveFromDrone_dBmW(1) Drone(i).powerTransmit -UE(j).PL_Drone_dBGa;%calculate Power receive ( Pr) for each UEendendendfor j1:50UE(j).SNR_Drone_dB(1)UE(j).PowerReceiveFromDrone_dBmW-NoisePower;UE(j).resurceBlock_Drone(1)setRB(UE(j).SNR_Drone_dB(1));end%%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%% PLOTS %%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%%%%--------------- Visualizing the Results------------------------basicwaitbar();if clstr 3legends {Cluster 1, Cluster 2, Cluster 3};endif clstr 4legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4};endif clstr 5legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5};endif clstr 6legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6};endif clstr 7legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7};endif clstr 8legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8};endif clstr 9legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9};endif clstr 10legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9,Cluster 10};endif clstr 20legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9,Cluster 10,Cluster 11,Cluster 12,Cluster 13,Cluster 14,Cluster 15,Cluster 16,Cluster 17,Cluster 18,Cluster 19,Cluster 20};endfigure,%plot(UserLocationX, UserLocationY, ^, MarkerSize, 5, LineWidth, 3), hold ongscatter(data(:,1),data(:,2),idx);hold onfor i 1:clstrscatter(C(i,1),C(i,2) ,96, black,filled);end%{hold onplot(BaseStationX, BaseStationY, rs, MarkerSize, 5, LineWidth, 4);%}legend(legends)xlabel(coordinate Χ);ylabel(coordinate Υ);hold offfor i1:50SINR_Receive_Comparison_BarChar(i,1)UE(i).SNR_Drone_dB;SINR_Receive_Comparison_BarChar(i,2)UE(i).SNR_dB;Power_Receive_Comparison_BarChar(i,1) UE(i).PowerReceiveFromDrone_dBmW;Power_Receive_Comparison_BarChar(i,2)UE(i).PowerReceive_dBmW;endMean_Power_Receive_Comparison_BarChar[ mean(Power_Receive_Comparison_BarChar(:,1)) mean(Power_Receive_Comparison_BarChar(:,2)) ];Mean_SINR_Comparison_BarChar[ mean(SINR_Receive_Comparison_BarChar(:,1)) mean(SINR_Receive_Comparison_BarChar(:,2)) ];x[1:50];y[1,2];figureX categorical({Drone,Base Station});X reordercats(X,{Drone,Base Station});bar(X,Mean_SINR_Comparison_BarChar);ylabel( Average SINR[dB],fontsize,14);%{figurebar(x,SINR_Receive_Comparison_BarChar);figureX categorical({Drone,Base Station});X reordercats(X,{Drone,Base Station});bar(X,Mean_Power_Receive_Comparison_BarChar);for i1:50UserIndex(i)UE(i).cluster_id;end%}disntance0;PathLoss0;PowerReceive0;SINR0;Rb0;for i1:50if UE(i).distancefrom_eNB_mUE(i).distancefromDronedisntancedisntance1;endif UE(i).PL_Drone_dBUE(i).PL_dBPathLossPathLoss1;endif UE(i).PowerReceiveFromDrone_dBmWUE(i).PowerReceive_dBmWPowerReceivePowerReceive1;endif UE(i).SNR_Drone_dBUE(i).SNR_dBSINRSINR1;endif UE(i).resurceBlock_DroneUE(i).resurceBlockRbRb1;endend3 仿真结果4 参考文献[1] Wanlei, Zhao , et al. RB-K-means Based Adaptive Mean shift基于RB-K平均带宽设定的Adaptive Mean shift. 中国图象图形学报 11.4(2006):511-515.[2]张海燕, and 刘虹. 基于K-means聚类的WSN能耗均衡路由算法. 传感技术学报 024.011(2011):1639-1643.博主简介擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真相关matlab代码问题可私信交流。部分理论引用网络文献若有侵权联系博主删除。1 简介针对无线传感器网络的能耗问题,提出了一种基于K-means聚类的能耗均衡路由算法(KBECRA).​2 部分代码%************************************************************************************************clear allclcclose all%***************************** global variables ********************************************REPerB84*2;%resource elements per resourse blockCategory[1,2,3];% 1:urban, 2:suburban,3:ruralF_HATA1500;%MHzNoiceFigure10; %dBBI15;NoisePower -174 10*log10(180*10^3)NoiceFigureBI;SINRmin-9.3;%dLbody3;%dB body lossLbpl18;%dB building penetration lossLj2;%jumber loosGa5;% gain%***************************** end global variables ******************************************%***************************** configuration of MeNB ******************************************MeNB(1).x500;% x coordinateMeNB(1).y500;% y coordinateMeNB(1).ChannelBandwidth20;%MHzMeNB(1).carrier1.8*10^9;%MHzMeNB(1).takeBitPerMilisec100000;%MHzMeNB(1).powerTransmit45; %dBmMeNB(1).height100; %metresfor i1:20MeNB(1).UEdata(i)0;%MHzendMaxPathLossCalculate_MaxPathLoss(MeNB(1).powerTransmit,NoisePower,SINRmin);%Max Path Loss%***************************** End configuration of MeNB ******************************************%***************************** configuration of 20-UEs %*****************************************% Here we locate the Ues In the Gridfor i1:50UE(i).x400;% x coordinateUE(i).y500;% y coordinateUE(i).height1.7;UserLocationX(i) 0;UserLocationY(i) 0;UserLocationZ(i) 0;endi1x400;% x coordinatey500while i51xrandi(1000, 1, 1);yrandi(1000, 1, 1);if (x400 || x500) (y400 || y500)UE(i).xx;% x coordinateUE(i).yy;% y coordinateUserLocationX(i) UE(i).x;UserLocationY(i) UE(i).y;UserLocationZ(i) 1.7;ii1;endend%***************************** End configuration of 20-UEs %*****************************************%***************************** scatter plot of UEs and ENB %*****************************************figure(Color, white)plot(UserLocationX, UserLocationY, ^, MarkerSize, 5, LineWidth, 3), hold onBaseStationX 500;BaseStationY 500;plot(BaseStationX, BaseStationY, rs, MarkerSize, 5, LineWidth, 4), hold on, grid on, grid minorhleg legend(User Location, Base Station);set(hleg, Location, NorthEastOutside);xlabel(coordinate Χ);ylabel(coordinate Υ);%***************************** End scatter plot of UEs and ENB %*****************************************%***************************** initialization phase of UEs *****************************************% Here we calculate some parameters of the UEs before they moving(PL,SINR,CQI,Max throughput, max resurceBlock that the UE xan take )for i1:50UE(i).distancefrom_eNB_m(1)round(Distance(MeNB(1).x,MeNB(1).y,UE(i).x,UE(i).y));%calculate distance between each UE ad eNBUE(i).PL_dB(1) (HATA_Model((UE(i).distancefrom_eNB_m(1)/1000),F_HATA(1),MeNB(1).height, UE(i).height,Category(2)))LbodyLbplLj;%calculate Path Loss for each UEUE(i).PowerReceive_dBmW(1) MeNB(1).powerTransmit -UE(i).PL_dBGa;%calculate Power receive ( Pr) for each UEUE(i).SNR_dB(1)UE(i).PowerReceive_dBmW-NoisePower;%calculate SINR for each UE [dB]UE(i).SNR(1) 10^(UE(i).SNR_dB(1)/10);%calculate SINR for each UEUE(i).CQI(1)CQI_calculation(UE(i).SNR_dB(1));%calculate CQI for each UE [dB]UE(i).MODULATION(1) Modulation_calculation(UE(i).SNR_dB(1));%calculate Modulation scheme for each UE depending on CQI [dB]UE(i).resurceBlock(1)setRB(UE(i).SNR_dB(1));%calculate how many resource block can each UE receive [dB]% UE(i).throughput(1)throughput_calculation( UE(i).MODULATION,UE(i).resurceBlock(1),UE(i).SNR_dB(1));%calculate throughput each UE receive [dB]end%{Ues request service from providercalcualation of servise prioritysheduler give as many resources as needed min{ can_decode,service_requires}%}for i1:50UE(i).QCI(1)QCI_example_service();UE(i).ExampleServise(1)ExampleServise_calculation(UE(i).QCI);UE(i).PriorityPriority_calculation(UE(i).QCI);UE(i).Resource_Block_Allocation(1)Resource_Block_Allocation_calculation( UE(i).QCI(1), UE(i).resurceBlock(1));UE(i).throughput(1)throughput_calculation( UE(i).MODULATION(1),UE(i).Resource_Block_Allocation(1), UE(i).SNR_dB(1));end%%--------------- Selecting Data of interest ---------------------% ---------------------------- Code ------------------------------Table_UE struct2table(UE);data Table_UE(:,1:2);data table2array(data);%________________________________________________________________%________________________________________________________________%%------------ Selecting Optimal Number of Clusters ------------% -------------- Method 1: Using the Elbow Method ---------------% ---------------------------- Code -----------------------------WCSS [];for k 1:20sumd 0;[idx,C,sumd] kmeans(data,k);WCSS(k) sum(sumd);endfigureplot(1:20, WCSS);ylabel( WCSS,fontsize,14);xlabel( drones,fontsize,14);prompt How many cluster do you want ?: ;clstr input(prompt);%________________________________________________________________%________________________________________________________________%%--------------- Clustering data -------------------------------[idx,C] kmeans(data,clstr);for i1:50UE(i).cluster_id(1)idx(i);end%________________________________________________________________%________________________________________________________________for i1:clstrDrone(i).xC(i,1);% x coordinateDrone(i).yC(i,2);% y coordinateDrone(i).indexi;Drone(i).height100;Drone(i).powerTransmit20;%dbm;endfor i1:clstrfor j1:50if Drone(i).indexUE(j).cluster_idUE(j).distancefromDrone(1)round(Distance(Drone(i).x,Drone(i).y,UE(j).x,UE(j).y));if UE(j).distancefromDrone(1)0UE(j).distancefromDrone(1)2;end% UE(j).PL_Drone_dB(1) (HATA_Model((UE(j).distancefromDrone(1)/1000),F_HATA(1),Drone(i).height, UE(j).height,Category(2)))LbodyLbplLj;%calculate Path Loss for each UEUE(j).PL_Drone_dB(1)Air2Ground(UE(j).distancefromDrone(1)/1000,F_HATA(1),Drone(i).height)LbodyLj;UE(j).PowerReceiveFromDrone_dBmW(1) Drone(i).powerTransmit -UE(j).PL_Drone_dBGa;%calculate Power receive ( Pr) for each UEendendendfor j1:50UE(j).SNR_Drone_dB(1)UE(j).PowerReceiveFromDrone_dBmW-NoisePower;UE(j).resurceBlock_Drone(1)setRB(UE(j).SNR_Drone_dB(1));end%%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%% PLOTS %%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%% %%%%%%%%%%%%%--------------- Visualizing the Results------------------------basicwaitbar();if clstr 3legends {Cluster 1, Cluster 2, Cluster 3};endif clstr 4legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4};endif clstr 5legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5};endif clstr 6legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6};endif clstr 7legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7};endif clstr 8legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8};endif clstr 9legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9};endif clstr 10legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9,Cluster 10};endif clstr 20legends {Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5,Cluster 6,Cluster 7,Cluster 8,Cluster 9,Cluster 10,Cluster 11,Cluster 12,Cluster 13,Cluster 14,Cluster 15,Cluster 16,Cluster 17,Cluster 18,Cluster 19,Cluster 20};endfigure,%plot(UserLocationX, UserLocationY, ^, MarkerSize, 5, LineWidth, 3), hold ongscatter(data(:,1),data(:,2),idx);hold onfor i 1:clstrscatter(C(i,1),C(i,2) ,96, black,filled);end%{hold onplot(BaseStationX, BaseStationY, rs, MarkerSize, 5, LineWidth, 4);%}legend(legends)xlabel(coordinate Χ);ylabel(coordinate Υ);hold offfor i1:50SINR_Receive_Comparison_BarChar(i,1)UE(i).SNR_Drone_dB;SINR_Receive_Comparison_BarChar(i,2)UE(i).SNR_dB;Power_Receive_Comparison_BarChar(i,1) UE(i).PowerReceiveFromDrone_dBmW;Power_Receive_Comparison_BarChar(i,2)UE(i).PowerReceive_dBmW;endMean_Power_Receive_Comparison_BarChar[ mean(Power_Receive_Comparison_BarChar(:,1)) mean(Power_Receive_Comparison_BarChar(:,2)) ];Mean_SINR_Comparison_BarChar[ mean(SINR_Receive_Comparison_BarChar(:,1)) mean(SINR_Receive_Comparison_BarChar(:,2)) ];x[1:50];y[1,2];figureX categorical({Drone,Base Station});X reordercats(X,{Drone,Base Station});bar(X,Mean_SINR_Comparison_BarChar);ylabel( Average SINR[dB],fontsize,14);%{figurebar(x,SINR_Receive_Comparison_BarChar);figureX categorical({Drone,Base Station});X reordercats(X,{Drone,Base Station});bar(X,Mean_Power_Receive_Comparison_BarChar);for i1:50UserIndex(i)UE(i).cluster_id;end%}disntance0;PathLoss0;PowerReceive0;SINR0;Rb0;for i1:50if UE(i).distancefrom_eNB_mUE(i).distancefromDronedisntancedisntance1;endif UE(i).PL_Drone_dBUE(i).PL_dBPathLossPathLoss1;endif UE(i).PowerReceiveFromDrone_dBmWUE(i).PowerReceive_dBmWPowerReceivePowerReceive1;endif UE(i).SNR_Drone_dBUE(i).SNR_dBSINRSINR1;endif UE(i).resurceBlock_DroneUE(i).resurceBlockRbRb1;endend3 仿真结果4 参考文献[1] Wanlei, Zhao , et al. RB-K-means Based Adaptive Mean shift基于RB-K平均带宽设定的Adaptive Mean shift. 中国图象图形学报 11.4(2006):511-515.[2]张海燕, and 刘虹. 基于K-means聚类的WSN能耗均衡路由算法. 传感技术学报 024.011(2011):1639-1643.博主简介擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真相关matlab代码问题可私信交流。部分理论引用网络文献若有侵权联系博主删除。