## Creating node objects..
for {set i 0} {$i < $val(nn) } { incr i } {
set node_($i) [$ns node]
}
##sensing range of nodes
for {set i 0} {$i < $val(nn) } { incr i } {
set CSThresh 4.80696e-07 ;#sensing range 20m
set RXThresh 4.80696e-07 ;#receiving range 20m
}
## Provide initial location of nodes..
for {set i 0} {$i < $val(nn) } { incr i } {
set xx [expr round(rand()*500)]
set yy [expr round(rand()*500)]
$node_($i) set X_ $xx
$node_($i) set Y_ $yy
}
# 25 defines the node size for nam
for {set i 0} {$i < $val(nn)} { incr i } {
$ns initial_node_pos $node_($i) 25
}
#UDP agent
for {set i 0} {$i < $val(nn) } { incr i } {
set udp [new Agent/UDP]
$ns attach-agent $node_($i) $udp
}
set null [new Agent/Null]
$ns attach-agent $node_(15) $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetsize_ 500
$cbr set rate_ 300kb
$cbr set maxpackets_ 500
$cbr attach-agent $udp
for {set i 0} {$i < $val(nn)-1 } { incr i } {
for {set j 0} {$j < $val(nn)-1 } { incr j } {
set dist [expr {sqrt(($i(X)-$j(X)) - ($i(Y)-$j(Y))} ]
}
}
for {set i 0} {$i < $val(nn)-1 } { incr i } {
if {$dist($i,$j)<=20}
{
set level "level 1"
}
elseif {20<$dist($i,$j)<=40}
{
set level "level 2"
}
首先设置源节点和目标节点。然后使用等式计算从源节点到网络中所有其他节点的距离
dist (i, j) = sqrt [ (Xi-Xj) - (Yi-Yj) ]
其中Xi和Yi是网络中节点的坐标值。
基于与源节点的距离,我们如何将网络划分为不同的层次。