I wrote a scenario in ns2 software that has 5 fixed nodes and 1 mobile sink. The mobile sink should collect the nodes' information. I want to calculate the mean avg (energy_level ,number of nodes & traffic's data on nodes) and store it in the mobile sink. The my code that I wrote does not any error but it does not print out. My code & trace file are in attachment I have to calculate this avg with proc in tcl script code not with awk code.Please help me.
proc create_common_app {destination_id disseminating_type disseminating_interval energy_level } {
global val
#puts "executing create_common_app\n"
if { $val(commonApp) == "Application/SensorBaseApp/CommonNodeApp" }
{
set app_ [new $val(commonApp)]
$app_ set destination_id_ $destination_id
}
$app_ set disseminating_type_ $disseminating_type
$app_ set disseminating_interval_ $disseminating_interval
$app_ set energy_level _ $energy_level
return $app_
}
proc create_mobile_sink_app { source _id disseminating_type disseminating_interval energy_level outside_network} {
global val
set app_ [new $val(sinkApp)]
$app_ set source _id _ $source _id
$app_ set disseminating_type_ $disseminating_type
$app_ set disseminating_interval_ $disseminating_interval
$app_ set energy_level _ $energy_level
$app_ set outside_network_ $outside_network
#$app_ set avg_ $avg
return $app_
}
#########################################################################
#Me
####################################################################
proc avg { energy_level nn disseminating_interval } {
set avgen 0
for {set i 0} {$i < 5} {incr i} {
set $avgen [expr $energy_level(i) + $avgen ]
}
set $avgen [expr $avgen / $nn]
return $avgen
set xl [list $avgen $nn $disseminating_interval]
}
proc ArithmeticMean { $xl } {
set length [llength $xl]
if {$length == 0} {
return 0.0
}
set sum [::tcl::mathop::+ {*}$xl]
return [expr {double($sum) / $length}]
set avgt [expr {double($sum) / $length}]
puts "average is $avgt_"
}
#####################################################################
M 0.00000 0 (9.00, 5.00, 0.00), (0.00, 0.00), 0.00
s 5.000000000 _1_ AGT --- 0 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [0] 0 0
r 5.000000000 _1_ RTR --- 0 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [0] 0 0
s 5.000000000 _1_ AGT --- 1 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [1] 0 0
r 5.000000000 _1_ RTR --- 1 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [1] 0 0
s 5.000000000 _1_ AGT --- 2 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [2] 0 0
r 5.000000000 _1_ RTR --- 2 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [2] 0 0
s 5.000000000 _1_ AGT --- 3 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [3] 0 0
r 5.000000000 _1_ RTR --- 3 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [3] 0 0
s 5.000000000 _1_ AGT --- 4 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [4] 0 0
r 5.000000000 _1_ RTR --- 4 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [4] 0 0
s 5.000000000 _1_ AGT --- 5 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [5] 0 0
r 5.000000000 _1_ RTR --- 5 cbr 150 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [5] 0 0
s 5.000000000 _1_ AGT --- 6 cbr 100 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [6] 0 0
r 5.000000000 _1_ RTR --- 6 cbr 100 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:0 2:0 32 0] [6] 0 0
s 5.000000000 _1_ RTR --- 0 AODV 48 [0 0 0 0] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
s 5.000535000 _1_ MAC --- 0 AODV 106 [0 ffffffff 2 800] [energy 0.050000 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
N -t 5.000535 -n 0 -e 0.049980
N -t 5.000535 -n 4 -e 0.049980
N -t 5.000535 -n 2 -e 0.049980
N -t 5.000535 -n 3 -e 0.049980
r 5.001383005 _0_ MAC --- 0 AODV 48 [0 ffffffff 2 800] [energy 0.049980 ei 0.000 es 0.000 et 0.000 er 0.000] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST)
r 5.001383013 _4_ MAC --- 0 AODV 48 [0 ffffffff 2 800] [energy 0.049980 ei 0.000 es 0.000 et 0.000
.
.
.....
proc avg { energy_level nn disseminating_interval } {
set avgen 0
for {set i 0} {$i < 5} {incr i} {
set $avgen [expr $energy_level(i) + $avgen ]
}
set $avgen [expr $avgen / $nn]
return $avgen
set xl [list $avgen $nn $disseminating_interval]
}
Notes:
upvar
to connect the name to the actual array in the caller's contextexpr
expressions.$i
in the array expansion.$
in the first argument to set
$nn
in the for
loop, or the hard-coded value 5
?Corrected code
proc avg { energy_level_var nn disseminating_interval } {
upvar 1 $energy_level_var energy_level
if {$nn == 0} {
error "denominator cannot be zero"
}
set sum 0
for {set i 0} {$i < $nn} {incr i} {
set sum [expr {$energy_level($i) + $sum}]
}
return [expr {$sum / $nn}]
}
And call like this
array set energy_level {0 1.0 1 1.1 2 1.2 3 1.3 4 1.4 5 1.5}
puts "average is: [avg energy_level 6 "?"]" ;# average is: 1.25
Also, don't use $
in the declaration of the ArithmeticMean proc:
proc ArithmeticMean { xl } {