本文主要是介绍改进版的 setdest,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ns自带setdest函数只能针对所有节点设置移动速度。如果我想让其中的10个节点移动速度较快,而另外40个节点较慢,自带的setdest就无能为力了。
为了克服这个问题,自己写了个setdest。贴上来大家一起讨论。
# ======================================================================
# default value
# ======================================================================
set opt(nn) 50 ;# number of normal nodes
set opt(fn) 0 ;# number of fast nodes
set opt(ns1) 1.0 ;# min speed of normal nodes
set opt(ns2) 3.0 ;# max speed of normal nodes
set opt(fs1) 20.0 ;# min speed of fast nodes
set opt(fs2) 30.0 ;# max speed of fast nodes
set opt(t) 300.0 ;# simulation time
set opt(x) 1000.0 ;# x dimension of space
set opt(y) 1000.0 ;# y dimension of space
set opt(seed) 1.0 ;# seed for random
# ======================================================================
proc getopt {argc argv} { ;# get cmd line opt
global opt
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string range $arg 0 0] != "-"} continue
set name [string range $arg 1 end]
set opt($name) [lindex $argv [expr $i+1]]
}
}
proc distance {x1 y1 x2 y2} {
global d
set x1 [expr $x1 - $x2 ]
set x1 [expr pow($x1, 2) ]
set y1 [expr $y1 - $y2 ]
set y1 [expr pow($y1, 2) ]
set x1 [expr $x1 + $x2 ]
set x1 [expr pow($x1, 0.5) ]
set d $x1
}
# ======================================================================
getopt $argc $argv
puts "#/n# normal nodes: $opt(nn), fast nodes: $opt(fn), speed: $opt(ns1)~$opt(ns2), fast speed: $opt(fs1)~$opt(fs2), time: $opt(t), x: $opt(x) y: $opt(y), seed:$opt(seed) /n#"
set rng [new RNG]
$rng seed $opt(seed)
set u [new RandomVariable/Uniform]
# 产生普通节点
for {set i 0} {$i < $opt(nn) } {incr i} {
$u set min_ 0.000001
$u set max_ $opt(x)
$u use-rng $rng
set x [$u value] ;# x coordinate of node
$u set min_ 0.000001
$u set max_ $opt(y)
$u use-rng $rng
set y [$u value] ;# y coordinate of node
puts [format "/$node_(%d) set X_ %f" $i $x ]
puts [format "/$node_(%d) set Y_ %f" $i $y ]
puts [format "/$node_(%d) set Z_ 0.000000" $i ]
set t 0.0
while { $t < $opt(t) } {
$u set min_ 0.000001
$u set max_ $opt(x)
$u use-rng $rng
set xd [$u value] ;# x coordinate of node
$u set min_ 0.000001
$u set max_ $opt(y)
$u use-rng $rng
set yd [$u value] ;# y coordinate of node
$u set min_ [expr $opt(ns1) + 0.000001 ]
$u set max_ $opt(ns2)
$u use-rng $rng
set s [$u value] ;# speed of node
puts [format "/$ns_ at %f /"/$node_(%d) setdest %f %f %f/"" $t $i $xd $yd $s ]
distance $x $y $xd $yd
global d
set t [expr $t + $d/$s]
}
}
# 产生快速节点
for {set i $opt(nn)} {$i < [ expr $opt(nn) + $opt(fn) ] } {incr i} {
$u set min_ 0.000001
$u set max_ $opt(x)
$u use-rng $rng
set x [$u value] ;# x coordinate of node
$u set min_ 0.000001
$u set max_ $opt(y)
$u use-rng $rng
set y [$u value] ;# y coordinate of node
puts [format "/$node_(%d) set X_ %f" $i $x ]
puts [format "/$node_(%d) set Y_ %f" $i $y ]
puts [format "/$node_(%d) set Z_ 0.000000" $i ]
puts [format "/$node_(%d) set nodeType_ 1" $i ]
set t 0.0
while { $t < $opt(t) } {
$u set min_ 0.000001
$u set max_ $opt(x)
$u use-rng $rng
set xd [$u value] ;# x coordinate of node
$u set min_ 0.000001
$u set max_ $opt(y)
$u use-rng $rng
set yd [$u value] ;# y coordinate of node
$u set min_ [expr $opt(fs1) + 0.000001 ]
$u set max_ $opt(fs2)
$u use-rng $rng
set s [$u value] ;# speed of node
puts [format "/$ns_ at %f /"/$node_(%d) setdest %f %f %f/"" $t $i $xd $yd $s ]
distance $x $y $xd $yd
global d
set t [expr $t + $d/$s]
}
}
下面是我习惯使用的批量产生场景的脚本:
#!/bin/sh
#====================================================================================
i=0;
while [ $i -le 15 ]
do
j=`expr 50 - $i`
echo generating secen $i/15
ns my-setdest.tcl -nn $j -fn $i -ns1 1 -ns2 3 -fs1 20 -fs2 30 -t 300 -x 1000 -y 1000 > scene-${j}n-${i}f-300t-1000-1000
i=`expr $i + 1`
done
转自:http://blog.baisi.net/?110511/viewspace-1261
这篇关于改进版的 setdest的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!