How to find neighbor list in ns2

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I would like to find the neighbor nodes list in sensor network. It would be great help if someone provides me sample tcl script for that. Thanks

回答1:

# neighbor node calculation  set nbr [open Neighbor w]  puts $nbr "\t\t\t\t\tNeighbor Detail"  puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"  puts $nbr "\tNode\t\tNb node\t\tNode-Xpos\tNode-Ypos\tNb-Xpos\t\tNb-Ypos\t\tDistance(d)"  puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"  close $nbr  proc distance { n1 n2 nd1 nd2} {    global c n bnd src dst j0 j1     set a 0    set nbr [open Neighbor a]    set x1 [expr int([$n1 set X_])]    set y1 [expr int([$n1 set Y_])]   set x2 [expr int([$n2 set X_])]    set y2 [expr int([$n2 set Y_])]    set d [expr int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]    if {$d<300} {      if {$nd2!=$nd1} {        puts $nbr "\t$nd1\t\t$nd2\t\t$x1\t\t$y1\t\t$x2\t\t$y2\t\t$d"      }     }      close $nbr }    U can use above function to print neighbor node's of all nodes in ns2. 

call the function"distance" to print neighbor list for example,

for {set i 0} {$i <$val(nn)} {incr i} {       for {set j 0} {$j <$val(nn)} {incr j} {          $ns at 10.002 "distance $n($i) $n($j) $i $j"        }     }  here val(nn) denotes number of nodes. 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!