Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

# A 10-node example for ad-hoc simulation with DSR

# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation
model
set val(netif) Phy/WirelessPhy ;# network interface
type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) CMUPriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) DSR ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set val(stop) 50 ;# time of simulation end

set ns [new Simulator]


set tracefd [open simple-dsdv.tr w]
set windowVsTime2 [open win.tr w]
set namtrace [open simwrls.nam w]

$ns trace-all $tracefd


$ns use-newtrace
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# set up topography object


set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)

#
# Create nn mobilenodes [$val(nn)] and attach them to the channel.
#

# configure the nodes


$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

for {set i 0} {$i < $val(nn) } { incr i } {


set node_($i) [$ns node]
}
for {set i 0} {$i < $val(nn)} {incr i} {
$node_($i) set X_ [expr rand()*500]
$node_($i) set Y_ [expr rand()*400]
$node_($i) set Z_ 0

# Generation of movements
#$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
#$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
#$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"

# Set a TCP connection between node_(0) and node_(1)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(9) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 2.0 "$ftp start"

for {set i 0} {$i<$val(nn)} {incr i} {


$ns initial_node_pos $node_($i) 30
}

# Telling nodes when the simulation ends


for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$node_($i) reset";
}

# ending nam and the simulation


$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150.01 "puts \"end simulation\" ; $ns halt"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam simwrls.nam &
}

$ns run
MATLAB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% This is the LEACH [1] code we have used. %
% The same code can be used for FAIR if m=1 %
% %
% [1] W.R.Heinzelman, A.P.Chandrakasan and H.Balakrishnan, %
% "An application-specific protocol architecture for wireless %
% microsensor networks" %
% IEEE Transactions on Wireless Communications, 1(4):660-670,2002 %
% Mohammad Hossein Homaei %
% Homaei@wsnlab.ir % Homaei@wsnlab.org %
% www.wsnlab.ir & www.wsnlab.ir %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% LEACH Protocol %
% %
% By: Mohammad Hossein Homaei %
% homaei@wsnlab.ir %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Field Dimensions - x and y maximum (in meters)/ Tarife Size mohite shabake(m)
xm=100;
ym=100;
%x and y Coordinates of the Sink /
sink.x=0.5*xm;
sink.y=0.5*ym;
%Number of Nodes in the field / Tedade Node haye shabake
n=100
%Optimal Election Probability of a node to become cluster head/ Ehtemale Entekhab Node
be onvane Cluster Head
p=0.1;
%Energy Model (all values in Joules)/ Energy ha bar hasbe Joule
%Initial Energy / Energy Avaliye
Eo=0.5;
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types / Ghodrate Ersal
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy/ Energy Masrafi Tajmie Dade
EDA=5*0.000000001;
%Values for Hetereogeneity
%Percentage of nodes than are advanced
m=0.1;
%\alpha
a=1;
%maximum number of rounds/ Max tedade round ha
rmax=200
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do/
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network/ Tolide Randome shabake
figure(1);
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/ Dar ebteda hich Cluster Head i
mojud nist
S(i).type='N';

temp_rnd0=i;
%Random Election of Normal Nodes/ Entekhabe Tasadofi Node ha
if (temp_rnd0>=m*n+1)
S(i).E=Eo;
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
%Random Election of Advanced Nodes/ Entekhab Tasadofie CH ha
if (temp_rnd0<m*n+1)
S(i).E=Eo*(1+a)
S(i).ENERGY=1;
plot(S(i).xd,S(i).yd,'+');
hold on;
end
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x');

%First Iteration
figure(1);
%counter for CHs/ Tedade Cluster Head ha
countCHs=0;
%counter for CHs per round/ Tedade CH haye har Round
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
for r=0:1:rmax
r
%Operation for epoch/ Formule entekhabe CH
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/ Tedade Node haye morde dar kol
dead=0;
%Number of dead Advanced Nodes/ Tedade Node haye CH morde
dead_a=0;
%Number of dead Normal Nodes/ Tedade Node haye morde mamuli
dead_n=0;
%counter for bit transmitted to Bases Station and to Cluster Heads/ Tedade packet haye
ersali be BS
packets_TO_BS=0;
packets_TO_CH=0;
%counter for bit transmitted to Bases Station and to Cluster Heads /Tedade packet haye
Ersali be BS dar har round
%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/ Check kardane zende budane Node ha
if (S(i).E<=0)
plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
if(S(i).ENERGY==1)
dead_a=dead_a+1;
end
if(S(i).ENERGY==0)
dead_n=dead_n+1;
end
hold on;
end
if S(i).E>0
S(i).type='N';
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
DEAD_N(r+1)=dead_n;
DEAD_A(r+1)=dead_a;
%When the first node dies/ Pas az marge avalin Node
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/ Entekhabe CH
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/ Mohasebe energy masrafi
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
end

end
end
end
STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;
%Election of Associated Cluster Head for Normal Nodes/ Entekhabe CH marbute baraye
Node haye mamuli
for i=1:1:n
if ( S(i).type=='N' && S(i).E>0 )
if(cluster-1>=1)
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
min_dis_cluster=1;
for c=1:1:cluster-1
temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
if ( temp<min_dis )
min_dis=temp;
min_dis_cluster=c;
end
end

%Energy dissipated by associated Cluster Head/ Mohasebe energy masrafi CH ha


min_dis;
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis *
min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
%Energy dissipated/ Masrafe energy kol
if(min_dis>0)
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000
);
PACKETS_TO_CH(r+1)=n-dead-cluster+1;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;

end
end
end
hold on;
countCHs;
rcountCHs=rcountCHs+countCHs;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STATISTICS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% DEAD : a rmax x 1 array of number of dead nodes/round
%
% DEAD_A : a rmax x 1 array of number of dead Advanced nodes/round
%
% DEAD_N : a rmax x 1 array of number of dead Normal nodes/round
%
% CLUSTERHS : a rmax x 1 array of number of Cluster Heads/round
%
% PACKETS_TO_BS : a rmax x 1 array of number packets send to Base Station/round
%
% PACKETS_TO_CH : a rmax x 1 array of number of packets send to ClusterHeads/round
%
% first_dead: the round where the first node died
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

Sample Ns2 code for Spin Protocol


Int RCAgent::command(int argc, const char*const* argv)
{

TclObject *obj;
Tcl& tcl = Tcl::instance();

if (argc == 3) {

if(strcmp(argv[1], "log-target") == 0) {

log_target = (Trace*) TclObject::lookup(argv[2]);


if(log_target == 0)
return TCL_ERROR;
return TCL_OK;

} else if(strcmp(argv[1], "log") == 0) {

log(argv[2]);
return TCL_OK;

} else if (argc == 4){

if (strcasecmp(argv[1], "add-ll") == 0) {

if( (obj = TclObject::lookup(argv[2])) == 0) {

fprintf(stderr, "RCAgent: %s lookup of %s failed\n", argv[1],


argv[2]);
return TCL_ERROR;

}
ll = (NsObject*) obj;
if( (obj = TclObject::lookup(argv[3])) == 0) {

fprintf(stderr, "RCAgent: %s lookup of %s failed\n", argv[1],


argv[2]);
return TCL_ERROR;

}
mac = (Mac*) obj;
return TCL_OK;

} else if (argc == 5) {

if (strcmp(argv[1], "sendmsg") == 0) {

int destination;
if (Tcl_GetInt(tcl.interp(),(char *)argv[4], &destination) != TCL_OK) {

fprintf(stderr, "RCAgent: could not convert %s to int", argv[4]);


return TCL_ERROR;

}
RCAgent::sendmsg(atoi(argv[2]), argv[3], destination, -1);
return (TCL_OK);

} else if (argc == 6) {

if (strcmp(argv[1], "sendmsg") == 0) {

int mac_dst;
if (Tcl_GetInt(tcl.interp(),(char *)argv[4], &mac_dst) != TCL_OK) {

fprintf(stderr, "RCAgent: could not convert %s to int", argv[4]);


return TCL_ERROR;

}
int link_dst;
if (Tcl_GetInt(tcl.interp(),(char *)argv[5], &link_dst) != TCL_OK) {

fprintf(stderr, "RCAgent: could not convert %s to int", argv[5]);


return TCL_ERROR;

}
RCAgent::sendmsg(atoi(argv[2]), argv[3], mac_dst, link_dst);
return (TCL_OK);

}
return Agent::command(argc, argv);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy