How to get and residual energy for cluster head and cluster member ?
1 次查看(过去 30 天)
显示 更早的评论
Matlab111
2014-10-14
http://www.mathworks.in/matlabcentral/answers/153186#comment_235285 sir from your code i can get an cluster head and cluster head members. But i want to get their residual energy also ('energy'(S.E in my code)).For every node i want.(i need both cluster head and cluster member energy).
采纳的回答
Geoff Hayes
2014-10-14
Arul - why not just add a third column to the clusterHeads and clusterNodes matrices. From the link you provided, just modify the code to something like the following
clusterData = cell(size(X,2),1);
for k=1:size(clusterData)
clusterData{k}.clusterHead = [X(k) Y(k) E(k)];
clusterData{k}.clusterNodes = [];
end
for k=1:size(S,2)
x = S(k).xd;
y = S(k).yd;
energy = S(k).ENERGY;
minDist = Inf;
at = 0;
for u=1:size(X,2)
dist = sqrt((x-X(u))^2+(y-Y(u))^2);
if dist<minDist
minDist = dist;
at = u;
end
end
if at>0
clusterData{at}.clusterNodes = ...
[clusterData{at}.clusterNodes ; [x y energy] ];
end
end
Note that the above is untested, and assumes that you have created an E energy array in the manner that you created the X and Y arrays i.e.
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
E(cluster)=S(i).ENERGY; % <-- add this line
22 个评论
Geoff Hayes
2014-10-15
Why is this occurring? If you put a breakpoint at the line
clusterData{at}.clusterNodes = [clusterData{at}.clusterNodes ; [x y energy] ];
what do you see? (Hint - you are not setting the ENERGY for all nodes.)
Matlab111
2014-10-15
sir, after sending the packets to sink node energy will get vary's for all the nodes. Now i want to display the energy for all the nodes along with the cluster head and cluster head member.
Geoff Hayes
2014-10-15
Arul - put a breakpoint at the line
clusterData{at}.clusterNodes = [clusterData{at}.clusterNodes ; [x y energy] ];
and see what is going wrong. What is energy? Look again at the hint in the previous comment.
Matlab111
2014-10-15
sir, i executed it's showing wrong value like
"cluster head
ans =
clusterHead: [75.2022 104.7560 -2.4486]
clusterNodes: [69.3790 103.3981 0]"
cluster head energy should be more than '0' but it's showing negative value.
And one more thing while putting the break point it's coming why it's not coming while running the normal simulation?.
Geoff Hayes
2014-10-15
编辑:Geoff Hayes
2014-10-15
cluster head energy should be more than '0' but it's showing negative value.
You have not provided your code, so I can't guess as to why your code is setting the energy to be negative (or zero).
why it's not coming while running the normal simulation
Perhaps you have a clear all statement that is clearing your breakpoints. Again, without seeing your code, it is difficult to know exactly what the code is doing, how you are calling it, etc.
Matlab111
2014-10-15
k sir, here is my code
clc;
clear all;
close all;
n=100;
Eo=1;
xm=200;
ym=200;
sink.x=0.5*xm;
sink.y=0.5*ym;
p=0.1;
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;
a=0.5;
rmax=2; %New Metaheuristic Bat-Inspired Algorithm;
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
hold off;
for i=1:1:n
S(i).xd=rand(1,1)*xm;
XR(i)=S(i).xd;
S(i).yd=rand(1,1)*ym;
YR(i)=S(i).yd;
S(i).G=0;
%initially there are no cluster heads only nodes
S(i).type='N';
S(i).E=Eo;
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
%plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
figure(1);
%First Iteration
%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
dead1=0;
dead2=0;
for r=0:1:rmax
r
%Operation for epoch
if(mod(r, round(1/p) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
hold off;
%Number of dead nodes
dead=0;
%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
%counter for bit transmitted to Bases Station and to Cluster Heads
%per round
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;
figure(1);
for i=1:1:n
%checking if there is a dead node
if (S(i).E<=0)
if(dead1==0)
dead1=0;
%disp(dead1);
disp('1st node dead');
dead1=[S(i).xd,S(i).yd];
disp(dead1);
end
if(r==1)
if (S(i).E<=0)
dead2=[S(i).xd,S(i).yd];
disp('2st node dead');
c1 = dead2(~ismember(dead2, dead1));
disp(c1);
end
end
if(r==2)
if (S(i).E<=0)
dead3=[S(i).xd,S(i).yd];
disp('3st node dead');
c2 = dead3(~ismember(dead3, dead2));
disp(c2);
end
end
plot(S(i).xd,S(i).yd,'*r');
dead=dead+1;
hold on;
end
%dead2=[S(i-1).xd,S(i-1).yd];
if S(i).E>0
S(i).type='N';
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
%'o','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','g', 'MarkerSize',8);
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
%,'+','LineWidth',3, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
%When the first node dies
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
%Election of Cluster Heads
if(temp_rand<= (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;
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'k*');
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
%Calculation of Energy dissipated
distance;
if (distance>do)
%0000
S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Emp*40000000*( distance*distance*distance*distance ));
E(cluster)=S(i).E;
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Efs*40000000*( distance * distance ));
E(cluster)=S(i).E;
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
Energy_disp(r+1) = S(i).E;
end
end
end
end
STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;
end
for i=1:1:r
r
if(r==2)
CIa1=0;
clusterData = cell(size(X,2),1);
for k=1:size(clusterData)
clusterData{k}.clusterHead = [X(k) Y(k) E(k)];
%CH=clusterData{k}.clusterHead;
clusterData{k}.clusterNodes = [];
end
for k=1:size(S,2)
x = S(k).xd;
y = S(k).yd;
energy = S(k).ENERGY;
minDist = Inf;
at = 0;
for u=1:size(X,2)
dist = sqrt((x-X(u))^2+(y-Y(u))^2);
if dist<minDist
minDist = dist;
at = u;
end
end
if at>0
clusterData{at}.clusterNodes = [ clusterData{at}.clusterNodes ; [x y energy] ];
disp('cluster head')
clusterData{at}
disp ('cluster head member node')
clusterData{at}.clusterNodes
end
end
end
end
hold on;
warning('OFF');
[VX,VY]=voronoi(X,Y);
plot(X,Y,'k+',VX,VY,'r-');
axis([0 xm 0 ym]);
Geoff Hayes
2014-10-17
Yes, you are right. If the code that you have written is executed, then those negative values appear. One problem is that you are incrementing cluster too early - note how you increment it and then set the E value a few lines later. You must do the following instead
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
%Calculation of Energy dissipated
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Emp*40000000*( ...
distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Efs*40000000*( ...
distance * distance ));
end
E(cluster)=S(i).E;
Energy_disp(r+1) = S(i).E;
cluster=cluster+1;
So that should fix the problem with a zero appearing as the first value in your E matrix.
As for negative values, where did these equations come from? Does the 40000000 correspond to 40 MHz from a WLAN 802.11n 40 MHz channel? Why have you chose EDA, ETX, and Emp to be the values that they are?
Much of you code seems to be copied from the Akshay Gore's FEX submission Comparison of LEACH EAMMH SEP TEEN Protocols SEP.m file. If this the case? Based on the block of code for the calculation of energy dissipation, the author does
%Calculation of Energy dissipated
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( ...
distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*(...
distance * distance ));
end
I'm guessing, based on the documentation included in the FEX submission, that 4000 corresponds to the 4000 bit packet size. So what does your 40000000 correspond to?
As for the error message vertcat CAT arguments dimensions are not consistent, this is because (for whatever reason) the energy local variable is empty, which causes the line
[ clusterData{at}.clusterNodes ; [x y energy] ]
to generate the error. This seems to occur only for the last element, the sink, which never gets the energy set, as only the first n elements of the n+1 struct array S gets updated properly.
Matlab111
2014-10-26
yes your are correct. " So what does your 40000000 correspond to? " sir that's wrong value(40000000) but 4000 is correct value after correcting that value i'm getting the same error problem
- ??? Error using ==> vertcat CAT arguments dimensions are not consistent.*
so what shall i do for this ?
Geoff Hayes
2014-10-26
My previous comment indicates what the problem is: the error only seems to occur for the last element, the sink, which never gets its energy set as only the first n elements of the n+1 struct array S get their energy initialized. Either exclude n+1 from the final loop, or set its energy.
Geoff Hayes
2014-10-29
Arul - the above will just overwrite the original sink values that were initialized at the beginning of the script. Find the code where the energy is set for all other nodes and make the change there. It isn't difficult but may require that you step through the code.
Matlab111
2014-10-29
sir, after modifying the value i'm facing the same problem. In my code E0=1 , this is the initial energy after modifying E0=10, also getting same error.
Geoff Hayes
2014-10-29
Arul - randomly changing values of local variables is not going to solve the problem. Why do you think that changing Eo is going to determine the energy for the sink? You must initialize the energy in the n+1 node (sink) or not use this node in the final loop.
Geoff Hayes
2014-10-29
Arul - we seem to be going in circles! You already mentioned this in a previous comment, and my response was that you will just overwrite the original sink values. Here is the code that you are overwriting (or not, as I don't know where you have added these lines)
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
Again, as in my previous comment, why do that changing the xd and yd for the sink, at S(n+1), will change anything? You keep avoiding the problem which is why the energy is not set for the sink or why bother with the energy for the sink.
The sink is the (n+1)th element in S, but most of your (or rather, Akshay Gore's) loops only consider the first n elements...that is why the energy is never set.
Or, in your last for loop, just ignore the sink. Iterate as
for k=1:size(S,2)-1
instead.
Matlab111
2014-10-30
1. sir, now i'm getting value for member nodes but that value is not correct. it's showing like this..
"cluster head
ans =
clusterHead: [11.6533 92.5642 0.9940]
clusterNodes: [9x3 double]
cluster head member node
ans =
11.6533 92.5642 0.9940
37.9830 122.7349 0.9961
6.7117 114.9017 0.9937
34.8620 46.0027 0.9949
16.9343 76.2849 0.9948
24.8237 56.9441 0.9948
29.7975 90.6634 0.9958
8.7175 33.2884 0.9893
0.8790 137.4425 0.9912"
Cluster head should have the higher residual energy compere than cluster head member nodes, but it's showing wrong value.
2. And one more thing, the same 'cluster head node' is displaying under 'cluster head member node', so it should not show the "cluster head node" in "cluster head member node".
Geoff Hayes
2014-10-30
As your question has been answered, any further questions relating to the code you have taken from the File Exchange should be directed to the author.
Matlab111
2014-10-30
k sir thank you , but according to author which ever has the higher residual energy that's going to be the cluster head and even in basic LEACH algorithm also saying the same thing. so please sir try this one please.
Don't ignore this code please tell the mistake in my code please...
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)