改进版的 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值