i want to create clusters inside each grids using leach in wsn..........................what could i do in the following coding so that clusters would be made inside the grids
조회 수: 2 (최근 30일)
이전 댓글 표시
close all clc clear all; warning('off','all'); x_dimensions=200; y_dimensions=200; base_station.x=160; base_station.y=160; n =100; p=0.05; Initial_energy=0.1; transmiter_energy=50*0.000000001; receiver_energy=50*0.000000001; amplification_energy_min=10*0.0000000000001; %amplification energy when d is less than d0 amplification_energy_greater=0.0013*0.000000000001; %amplification energy when d is greater than d0 Data_Aggregation_Energy=5*0.000000001; maximum_lifetime=100; do=sqrt(amplification_energy_min/amplification_energy_greater); % minimum distance between cluster head and base station for energy consumption K_bit_data_packet=4000; remaining_energy(1)=0; for i=1:1:n leach(i).x_position=rand(1,1)*x_dimensions; %it will distribute the nodes in 1 dimension in x axis randomly. leach(i).y_position=rand(1,1)*y_dimensions; %it will distribute the nodes in 1 dimension in y axis randoml.
leach(i).G=0; % as the no of node that have been cluster head is zero 0
leach(i).E=Initial_energy%%*(1+rand*a);
leach(i).type='N';
end
leach(n+1).x_position=base_station.x; %assume that base station is also a node sp total no of nodes is n and with base station it is n+1
leach(n+1).y_position=base_station.y;
%figure(1); countCHs=0; %the number of Stateflow objects in the current context. cluster=1; %first cluster is selected flag_first_dead=0; %flag_teenth_dead=0; flag_all_dead=0;
dead=0; first_dead=0; %teenth_dead=0; all_dead=0;
allive=n; %counter for bit transmitted to Bases Station and to Cluster Heads packets_TO_BS=0; packets_TO_CH=0; % counter for sleep nodes s=0; for r=0:1:maximum_lifetime r if(mod(r, round(1/p) )==0) %remainder for i=1:1:n leach(i).G=0; % it will assign to the nodes that have not been cluster head . end end hold off; dead=0; figure(1);
for i=1:1:n
if (leach(i).E<=0)
dead=dead+1;
plot(leach(i).x_position,leach(i).y_position,'h','LineWidth',2, 'MarkerEdgeColor','r','MarkerFaceColor','y','MarkerSize',10)
hold on;
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
% if(dead==0.1*n)
%if(flag_teenth_dead==0)
% teenth_dead=r;
% flag_teenth_dead=1;
%end
% end
if(dead==n)
if(flag_all_dead==0)
all_dead=r;
flag_all_dead=1;
break;
end
end
end
if (leach(i).E>0)
leach(i).type='N';
plot(leach(i).x_position,leach(i).y_position,'o','LineWidth',2, 'MarkerEdgeColor','r','MarkerFaceColor','b','MarkerSize',10)
hold on;
end
end
plot(leach(n+1).x_position,leach(n+1).y_position,'d','LineWidth',2, 'MarkerEdgeColor','g','MarkerFaceColor','m','MarkerSize',12)
hold on;
DEAD(r+1)=dead;
countCHs=0;
cluster=1;
for i=1:1:n if(leach(i).E>=0)
random_number=rand;
if ( (leach(i).G)<=0)
%threshold function if(random_number<= (p/(1-p*mod(r,round(1/p)))))
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
leach(i).type='C';
leach(i).G=round(1/p)-1;
C(cluster).x_position=leach(i).x_position;
C(cluster).y_position=leach(i).y_position;
% plot(leach(i).x_position,leach(i).y_position,'k*');
X10=[leach(i).x_position,leach(n+1).x_position]; Y10=[leach(i).y_position,leach(n+1).y_position];
plot(X10,Y10,'-.r');
distance=sqrt( (leach(i).x_position-(leach(n+1).x_position) )^2 + (leach(i).y_position-(leach(n+1).y_position) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=leach(i).x_position;
Y(cluster)=leach(i).y_position;
cluster=cluster+1;
distance;
if (distance>do)
leach(i).E=leach(i).E- ( (transmiter_energy+Data_Aggregation_Energy)*(K_bit_data_packet) + amplification_energy_greater*K_bit_data_packet*(distance*distance*distance*distance ));
end
if (distance<=do)
leach(i).E=leach(i).E- ( (transmiter_energy+Data_Aggregation_Energy)*(K_bit_data_packet) + amplification_energy_min*K_bit_data_packet*(distance * distance ));
end
end
end
end
end
STATISTICS.COUNTCHleach(r+1)=countCHs;
% Association of nodes
for i=1:1:n
if countCHs==0
min_dis=sqrt( (leach(i).x_position-(leach(n+1).x_position) )^2 + (leach(i).y_position-(leach(n+1).y_position) )^2 );
min_dis;
if (min_dis>do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_greater*K_bit_data_packet*( min_dis *min_dis * min_dis * min_dis));
end
if (min_dis<=do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_min*K_bit_data_packet*( min_dis * min_dis));
end
leach(C(min_dis_cluster).id).E =leach(C(min_dis_cluster).id).E- ( (receiver_energy + Data_Aggregation_Energy)*K_bit_data_packet );
packets_TO_CH=packets_TO_CH+1;
elseif ( leach(i).type=='N' && leach(i).E>0)
if(cluster-1>=1)
min_dis=Inf;
min_dis_cluster=0;
for c=1:1:cluster-1
tamplification_energy_greater=min(min_dis,sqrt( (leach(i).x_position-C(c).x_position)^2 + (leach(i).y_position-C(c).y_position)^2 ) );
if ( tamplification_energy_greater<min_dis )
min_dis=tamplification_energy_greater;
min_dis_cluster=c;
end
end
min_dis;
if (min_dis>do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_greater*K_bit_data_packet*( min_dis *min_dis * min_dis * min_dis));
end
if (min_dis<=do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_min*K_bit_data_packet*( min_dis * min_dis));
end
leach(C(min_dis_cluster).id).E =leach(C(min_dis_cluster).id).E- ( (receiver_energy + Data_Aggregation_Energy)*K_bit_data_packet );
packets_TO_CH=packets_TO_CH+1;
leach(i).min_dis=min_dis;
leach(i).min_dis_cluster=min_dis_cluster;
else
min_dis=sqrt( (leach(i).x_position-leach(n+1).x_position)^2 + (leach(i).y_position-leach(n+1).y_position)^2 );
if (min_dis>do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_greater*K_bit_data_packet*( min_dis *min_dis * min_dis * min_dis));
end
if (min_dis<=do)
leach(i).E=leach(i).E- ( transmiter_energy*(K_bit_data_packet) + amplification_energy_min*K_bit_data_packet*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
end
end
end
hold on;
STATISTICS.PACKETS_TO_CH(r+1)=packets_TO_CH;
STATISTICS.PACKETS_TO_BS(r+1)=packets_TO_BS;
if dead==n; network_lifetime=r break; end % [vx,vy]=voronoi(X,Y); %plot(X,Y,'h',vx,vy,'m-','LineWidth',3, 'MarkerEdgeColor','g','MarkerFaceColor',[1 0.4 .3],'MarkerSize',10) %hold on;
%X10=[X,leach(n+1).x_position]; Y10=[Y,leach(n+1).y_position];
% plot(X10,Y10,'-.G');
hold on
%voronoi(X,Y);
%axis([0 x_dimensions 0 y_dimensions+60]);
for i=1:n dddd=leach(i).E; end remaining_energy(r+1)=dddd;
end first_dead figure plot(remaining_energy),title('remaining energy');
figure plot(DEAD),title('Dead nodes');
%sink.x=3.1; %sink.y=3.1; ngrid=4; x = linspace(0, 200, ngrid+1); [X,Y] = meshgrid(x); figure(1) plot(X,Y,'k') hold on plot(Y,X,'k') hold on %set(gca, 'XTick', 1:ngrid+1, 'YTick', 1:ngrid+1) axis([0 200 0 200]) axis square %plot(sink.x,sink.y,'r+', 'MarkerSize', 5, 'LineWidth', 4);
댓글 수: 0
답변 (1개)
Bhaumik chaudhari
2020년 2월 28일
hey buddy,can you find out the solution of this question ,,if yes then say me because i am face same problem of your question...
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!