Random 为NS2上给定任意数量的节点创建随机有线拓扑

Random 为NS2上给定任意数量的节点创建随机有线拓扑,random,tcl,ns2,Random,Tcl,Ns2,我想使用NS2创建和模拟有线拓扑。尝试编写tcl并使用rand()定位节点和链接。 我的解决办法是: ### Create a simulator object set ns [new Simulator] set num_node 10 set num_flow 5 set x_dim 150 set y_dim 150 ### Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red

我想使用NS2创建和模拟有线拓扑。尝试编写tcl并使用rand()定位节点和链接。 我的解决办法是:

### Create a simulator object
set ns [new Simulator]
set num_node 10
set num_flow 5
set x_dim 150
set y_dim 150

### Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

### Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
set tracefd [open out.tr w]
$ns trace-all $tracefd

### set up topography object
set topo [new Topography];            # This is needed for wireless
$topo load_flatgrid $x_dim $y_dim;    # Setting a 2D space for the nodes

### Define a 'finish' procedure
proc finish {} {
    global ns nf tracefd
    $ns flush-trace
    ### Close the NAM trace file
    close $nf
    close $tracefd
    ### Execute NAM on the trace file
    exec nam out.nam &
    exit 0
}

#Create four nodes
for {set i 0} {$i < [expr $num_node]} {incr i} {
    set n($i) [$ns node]
    puts "Created node $i"
}

### Create links between the nodes
for {set i 0} {$i < [expr $num_node + 1]} {incr i} {
    set s_node [expr int($num_node*rand())];     # src node
    set d_node $s_node
    while {$d_node==$s_node} { ;                 # while the random pair are same node
        set d_node [expr int($num_node*rand())]; # dest node
    }
    $ns duplex-link $n($s_node) $n($d_node) 2Mb 10ms DropTail
    $ns queue-limit $n($s_node) $n($d_node) 50
    puts "Linking $s_node and $d_node"
}

### Give node position (for NAM)
set i 0
while {$i < $num_node } {
    ### Set random position for nodes
    set x_pos [expr int($x_dim*rand())];   # random settings
    set y_pos [expr int($y_dim*rand())];   # random settings

    $n($i) set X_ $x_pos
    $n($i) set Y_ $y_pos
    $n($i) set Z_ 0.0
    puts "Put $i to ($x_pos , $y_pos)"

    #puts -nonewline $topofile "$i x: [$node_($i) set X_] y: [$node_($i) set Y_] \n"
    incr i;
}; 

### Setup UDP connections
for {set i 0} {$i < [expr $num_flow]} {incr i} {
    set s_node [expr int($num_node*rand())];     # src node
    set d_node $s_node
    while {$d_node==$s_node} {;                  # while the random pair are same node
        set d_node [expr int($num_node*rand())]; # dest node
    }
    set udp($i) [new Agent/UDP]
    $udp($i) set class_ $i
    $ns attach-agent $n($s_node) $udp($i)
    set null($i) [new Agent/Null]
    $ns attach-agent $n($d_node) $null($i)
    $ns connect $udp($i) $null($i)
    $udp($i) set fid_ $i
    puts "Flow $s_node - $d_node"
}

### Setup a CBR over UDP connections
for {set i 0} {$i < [expr $num_flow]} {incr i} {
    set cbr($i) [new Application/Traffic/CBR]
    $cbr($i) attach-agent $udp($i)
    $cbr($i) set type_ CBR
    $cbr($i) set packet_size_ 1000
    $cbr($i) set rate_ 1mb
    $cbr($i) set random_ false
    puts "setting cbr for $i"
}

### Schedule events for the CBR and FTP agents
for {set i 0} {$i < [expr $num_flow]} {incr i} {
    $ns at 0.1 "$cbr($i) start"
}

for {set i 0} {$i < [expr $num_flow]} {incr i} {
    $ns at 4.5 "$cbr($i) stop"
}

for {set i 0} {$i < [expr $num_node]  } { incr i} {
    $ns initial_node_pos $n($i) 4
}

### Run the simulation
$ns run
###创建一个模拟器对象
设置ns[新模拟器]
设置num_节点10
设置num_流5
设置x_尺寸150
设置y_尺寸150
###为数据流定义不同的颜色(用于NAM)
$ns颜色1蓝色
$ns颜色2红色
###打开NAM跟踪文件
设置nf[open out.nam w]
$ns namtrace all$nf
设置tracefd[open out.tr w]
$ns跟踪所有$tracefd
###设置地形对象
设置地形[新地形];#这是无线通信所需要的
$topo load_flatgrid$x_dim$y_dim;#为节点设置二维空间
###定义“完成”过程
过程完成{}{
全局ns-nf-tracefd
$ns刷新跟踪
###关闭NAM跟踪文件
收盘价$nf
关闭$tracefd
###在跟踪文件上执行NAM
执行长nam out.nam&
出口0
}
#创建四个节点
对于{set i 0}{$i<[expr$num_node]}{incr i}{
设置n($i)[$ns节点]
放置“已创建节点$i”
}
###在节点之间创建链接
对于{set i 0}{$i<[expr$num_node+1]}{incr i}{
设置s_节点[expr int($num_节点*rand())];#src节点
设置d_节点$s_节点
而{$d_node==$s_node}{;#而随机对是相同的节点
设置数据节点[expr int($num_node*rand())];#dest node
}
$ns双工链路$n($s_节点)$n($d_节点)2Mb 10ms DropTail
$ns队列限制$n($s_节点)$n($d_节点)50
放置“链接$s_节点和$d_节点”
}
###给出节点位置(对于NAM)
设置i 0
而{$i<$num_node}{
###为节点设置随机位置
设置x_pos[expr int($x_dim*rand())];#随机设置
设置y_pos[expr int($y_dim*rand())];#随机设置
$n($i)套X_uu$X_u位置
$n($i)套Y_$Y_位置
$n($i)集Z_0.0
放入“将$i放入($x\u pos,$y\u pos)”
#puts-nonewline$topofile“$IX:[$node\$i)集合x\$y:[$node\$i)集合y\\ n”
增量i;
}; 
###设置UDP连接
对于{set i 0}{$i<[expr$num_flow]}{incr i}{
设置s_节点[expr int($num_节点*rand())];#src节点
设置d_节点$s_节点
而{$d_node==$s_node}{;#而随机对是相同的节点
设置数据节点[expr int($num_node*rand())];#dest node
}
设置udp($i)[新代理/udp]
$udp($i)设置类\u$i
$ns附加代理$n($s_节点)$udp($i)
设置null($i)[新代理/null]
$ns附加代理$n($d_节点)$null($i)
$ns connect$udp($i)$null($i)
$udp($i)设置fid_u$i
将“流$s_节点-$d_节点”
}
###通过UDP连接设置CBR
对于{set i 0}{$i<[expr$num_flow]}{incr i}{
设置cbr($i)[新应用程序/流量/cbr]
$cbr($i)附加代理$udp($i)
$cbr($i)集合类型\uCBR
$cbr($i)设置数据包大小\u 1000
$cbr($i)设定费率\u1MB
$cbr($i)设置为随机\uuFalse
将“设置$i的cbr”
}
###为CBR和FTP代理安排事件
对于{set i 0}{$i<[expr$num_flow]}{incr i}{
0.1“$cbr($i)开始时为$ns”
}
对于{set i 0}{$i<[expr$num_flow]}{incr i}{
4.5“$cbr($i)停止时的$ns
}
对于{set i 0}{$i<[expr$num_node]}{incr i}{
$ns首字母\节点\位置$n($i)4
}
###运行模拟
$ns跑步
但随机化通常会产生错误的链接,从而在模拟和获得该错误时出现问题:

--- Classfier::no-slot{} default handler (tcl/lib/ns-lib.tcl) --- _o28: no target for slot 4294967295 _o28 type: Classifier/Hash/Dest content dump: classifier _o28 0 offset 0 shift 1073741823 mask 1 slots slot 5: _o268 (Classifier/Port) -1 default ---------- Finished standard no-slot{} default handler ---------- ---Classifier::没有插槽{}默认处理程序(tcl/lib/ns lib.tcl)--- _o28:插槽4294967295没有目标 _o28类型:分类器/哈希/目的地 内容转储: 分类器o28 0偏移量 0班 1073741823面罩 1个插槽 插槽5:_o268(分类器/端口) -1违约 ----------已完成标准无插槽{}默认处理程序---------- 但这也是随机的,并不总是发生。如果没有出现,nam文件将显示节点定义重复。 有人能给我一些关于如何创建带有随机有效链接的随机有线拓扑的指导吗?

您的文件“random wired.tcl”在这里工作正常。。。PCLinuxOS 2017.04-x86_64

$ ns235-64-orig random-wired.tcl
Created node 0
Created node 1
Created node 2
Created node 3
Created node 4
Created node 5
Created node 6
Created node 7
Created node 8
Created node 9
Linking 9 and 0
Linking 9 and 8
Linking 5 and 8
Linking 1 and 6
Linking 9 and 6
Linking 8 and 0
Linking 1 and 4
Linking 3 and 7
Linking 8 and 7
Linking 1 and 2
Linking 9 and 0
Put 0 to (139 , 71)
Put 1 to (107 , 146)
Put 2 to (14 , 9)
Put 3 to (16 , 23)
Put 4 to (89 , 30)
Put 5 to (26 , 65)
Put 6 to (46 , 76)
Put 7 to (87 , 31)
Put 8 to (12 , 105)
Put 9 to (89 , 56)
Flow 6 - 4
Flow 0 - 1
Flow 2 - 8
Flow 5 - 2
Flow 2 - 3
setting cbr for 0
setting cbr for 1
setting cbr for 2
setting cbr for 3
setting cbr for 4
创建文件out.nam 3.3MB和out.tr 1.4MB。和nam:一些节点显示活动

---Classifier::没有插槽{}默认处理程序(tcl/lib/ns lib.tcl)---

。。。是某些模拟/某些Linux操作系统的已知错误。另一个ns2友好的操作系统是CentOS 7-64位:您的文件运行正常。不太好的消息是:Ubuntu 16.04-64和Ubuntu 17.04-64失败。实际上Ubuntu并不是ns2的首选。ns2不太友好,因为它的“不同”补丁,例如libc6

示例,
rand()
aodv18.tcl、aodv_802_15_4.tcl、aodv-Jenova.tcl、aodv-Soumia.tcl、aodv testcode rand.tcl


编辑,2017年5月26日:

---Classifier::没有插槽{}默认处理程序

我认为找到了解决办法(post#14@newthink)→ 添加
$ns多播

set ns [ new Simulator ]
$ns multicast

与有问题的Antnet模拟完美配合。

$ns多播
有时对我不起作用。我发现,当生成的图形断开连接时,会发生此错误

因此,我检查了一下,确保图形从未断开连接,并且每个节点至少有一个链接

#nodeFlag_ keeps of which nodes has at least one link
for {set i 0} {$i < $num_node} {incr i} {
        set nodeFlag_($i) 0
    }
    ### Create links between the nodes
    for {set i 0} {$i < [expr $num_node + 1]} {incr i} {
        set s_node [expr int($num_node*rand())];     # src node
        set d_node $s_node
        while {$d_node==$s_node} { ;                 # while the random pair are same node
            set d_node [expr int($num_node*rand())]; # dest node
        }
        $ns duplex-link $node_($s_node) $node_($d_node) 2Mb 10ms DropTail
        set nodeFlag_($s_node) 1
        set nodeFlag_($d_node) 1
        $ns queue-limit $node_($s_node) $node_($d_node) 50
        puts "Linking $s_node and $d_node"
    }
    for {set i 0} {$i < $num_node} {incr i} {
            #see here
            if {$nodeFlag_($i) == 0} {
            set random [expr int($num_node*rand())]
            $ns duplex-link $node_($i) $node_($random) 2Mb 10ms DropTail
            puts "left link $i and $random"
        }
        puts "$nodeFlag_($i)"
    }
#nodeFlag#保留至少有一个链接的节点
对于{set i 0}{$i<$num_node}{incr i}{
将nodeFlag_ui设置为0
}
###在节点之间创建链接
对于{set i 0}{$i<[expr$num_node+1]}{incr i}{
设置s_节点[expr int($num_节点*rand())];#src节点
设置d_节点$s_节点
而{$d_node==$s_node}{;#而随机对是相同的节点
设置数据节点[expr int($num_node*rand())];#dest node
}
$ns双工链路$node_($s_node)$node_($d_node)2Mb 10ms DropTail
将nodeFlag_uu($s_节点)设置为1
将nodeFlag_uu($d_节点)设置为1
$ns队列限制$node_u($s_节点)$node_u($d_节点)50
放置“链接$s_节点和$d_节点”
}
对于{set i 0}{$i