2008/07/11

處理 NS2 Scenarios Generator 產生的 tcl

NS2 Scenarios Generator
是個不錯的 ns2 scenario 產生工具

但是產生出來的 tcl 可能在命名上不是我所慣用的

舉例來說

他會產生以下這種表示 node 的方式

[code]$n0 set X_ 907
$n0 set Y_ 975
$n0 set Z_ 0.0[/code]

但我比較習慣寫成類似 array 的方式

例如

[code]$node(0) set X_ 907
$node(0) set Y_ 975
$node(0) set Z_ 0.0[/code]

這個時候問題來了

如果有 1000 個點

總不可能一個一個手動改吧 囧

這個時候 vim 的搜尋取代功能就非常好用了

利用以下寫法就能一口氣換過來喔

:%s/\$n\([0-9]\+\)/$node(\1)/g


想要瞭解更多 vim 的用法請參考這篇文章

以下介紹我覺得最常用到的

1. 單純置換字串

:%s/old string/new string/g
以上用法會把檔案內所有的 old string 變成 new string

2. 利用 regular expression 置換字串

:%s/[0-9]\+/number/g
以上用法會把數字(ex: 1, 12, 130, ... etc)變成字串 number

:%s/a.b/string/g
以上用法會把字串 aab, abb, a0b, a9b, a!b, ... 等,a開頭b結尾,三個字為一組的字串變成字串 string

3. 利用 regular expression 把比對出的字串重新組合出新的字串置換

:%s/\(abc\) \(def\)/This is \1 & \2/g
以上用法會把字串 abc def 變成字串 This is abc & def



vim 功能非常強大

只要用習慣了

就會覺得非常方便喔

2008/07/10

執行 nam 時出現 "Missing required flag" error

Reference: http://mailman.isi.edu/pipermail/ns-users/2003-April/031477.html

Problem: ns2 在執行 nam 在模擬結束時出現出現以下錯誤


Missing required flag -x in: W -t 5.0

Missing required flag -y in: W -t 5.0

Parsing error in event.



Solution:
發生的原因是因為找不到 nam 檔寫入
也就是說寫了關閉 nam 檔後
仍然有資料要寫入 nam 檔
因此要解決就是關閉 nam 檔前
tcl 要先執行到 $ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"

關閉模擬的 example:
[code]set opt(stop) 5.0 ;# time of simulation end

# Create a ns simulator
set ns [new Simulator]

# ...

# Open the NS trace file
$ns use-newtrace
set tracefile [open out.tr w]
$ns trace-all $tracefile

# Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $opt(x) $opt(y)

# ...

#===================================
# Termination
#===================================

# Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile opt
$ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"
$ns flush-trace
close $tracefile
close $namfile

puts "done"
$ns halt

puts "running nam..."
exec nam out.nam &
exit 0
}
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns at $opt(stop) "$node($i) reset"
}
$ns at $opt(stop) "finish"

puts "Starting Simulation..."
$ns run[/code]

[Solution] Warning: Tracefile events are not sorted by time

Reference: [patch] NAM Time precison and wireless packet coloring (cmu-trace.cc)

Problem: NS2 shows the "Warning: Tracefile events are not sorted by time" warning.

Solution: Modify file "ns-allinone-2.32/ns-2.32/trace/cmutrace.cc"

Any %.9f characters in the file change to "TIME_FORMAT" characters.

AWK

中文:
http://linux.tnc.edu.tw/techdoc/shell/x792.html
gawk 手冊 (mirror)
http://www.study-area.org/cyril/scripts/scripts/node61.html
AWK Tutorial Guide

English:
The GNU Awk User's Guide

2008/06/28

三種解 Recursive Function 的方法

1. The iteration method (疊代法, 或代入展開法)
2. 畫 Recursion Tree
3. Master Theorem

2008/06/27

[轉錄]讓男女都覺得窩心的事

讓男生感到窩心的事:
1. 在朋友面前很給他面子
2. 像小孩子般對他撒嬌
3. 對他很放心,不要一天到晚疑神疑鬼
4. 常稱讚他的優點,把他當成妳的英雄
5. 親手做一些點心或小禮物,讓他的朋友羡慕
6. 穿著他喜歡的衣服樣式,做他欣賞的打扮
7. 聽他說話時,會一副很崇拜他的樣子
8. 就算有別人追求或看見帥哥時,都會說他比那些人棒多了
做到這些,想變心都難,因為這樣的女孩不好找了!!~~~

讓女生感到窩心的事:
1. 了解她心中的感受,不要表現出無聊或不耐煩
2. 常說溫柔體貼的話
3. 記得她的服裝、髮型,也要知道她今天做了什麼改變
4. 到哪裡都能告訴她和誰在一起?何時回家?
5. 吵架時會讓她,先說對不起
6. 讓她有安全感
7. 不用讓她擔心你會出軌
8. 偶爾浪漫的送她花或小禮物如此一來會更恩愛喔!!~~~