[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] Running multiple simulations with a single tcl script



On Tuesday 14 August 2001 14:28, Giuseppe Tringali wrote:
> Hi ns user,
>
> I'd like to run several simulations changing only a parameter (for
> example the length of the simulation or the delay of link).
>
> My question is this: is it possible with a single tcl script? An if
> so, could you give me a basic example of the script?
>
> I have seen in previous messages that expert user of NS use both
> perl script and shell script. But this solution is quite difficult
> for me.

Yes, it is possible.  Here is some basic code that uses command-line 
arguments.  You get different runs by passing in arguments.

#  Usage:  ns time1.tcl TIME
#	TIME is the simulation runtime in seconds
#
#  Example Usage:  ns time1.tcl 15
#	15 second simulation.

if {$argc  != 1} {
#  Must get a single argument or program fails.
#  If you need more arguments, increase the count.
 puts stderr "ERROR! ns called with wrong number of arguments!($argc)"
 exit 1
} else {
 set simTime [lindex $argv 0]
 #  simTime now holds the simulation runtime in seconds.
 #  Use it as you would any other variable.
}


Since you don't seem to like this solution, here is a slight 
variation that allows you to pass in multiple arguments, using them 
one at a time:

#  time2.tcl
#  Usage:  ns time2.tcl TIME1 TIME2 TIME3 ...
#	TIME1, TIME2, TIME3 (etc) are all runtimes in seconds
#
#  Example Usage:  ns time2.tcl 15 30 45 60
#	Run four sims of durations 15, 30, 45, and 60 seconds.

for {set i 1} {$i <= $argc} {incr i} {
  #  extract the next value from the list of command line args
  set time [lindex $argv $i]
  #  now use time for something
}


If you need to pass in multiple parameters, you can.  Let's say, for 
example, that you want to pass in the following parameters:  time, 
bandwidth, and delay.  There are two solutions that come to mind 
immediately, pass each one as a separate command line argument or 
quote the group to appear as a single command line argument.

Here is an example using each parameter as a separate command line 
arg:

#  time-bw-delay1.tcl
#  Usage:  ns time-bw-delay1.tcl TIME1 BW1 DELAY1 TIME2 BW2 DELAY2 ...
#	TIMEx is a time in seconds
#	BWx is a link bandwidth in Mbps
#	DELAYx is a link delay in ms
#
#  Example Usage:  ns time-bw-delay1.tcl 15 155 1.5 20 1.5 20

if { [expr $argc % 3] != 0 } {
 puts stderr "ERROR! ns called with wrong number of arguments!($argc)"
 exit 1
} else {
  for {set i 0} {$i < $argc} {incr i} {
	incr i
	set time [lindex $argv $i]
	incr i
	set bw [lindex $argv $i]
	incr i
	set delay [lindex $argv $i]

	#  use the variables for your sim
  }
}

Here is an alternative, quoting the params so that the group appears 
as a single command line arg:

#  time-bw-delay2.tcl
#  Usage:  ns time-bw-delay2.tcl "TIME1 BW1 DELAY1" "TIME2 BW2 DELAY2"
#	TIMEx is a time in seconds
#	BWx is a link bandwidth in Mbps
#	DELAYx is a link delay in ms
#	NOTE:  My mail program wanted to wrap the Usage line, so I
#		chopped it short.  You can keep adding more params
#		after the second set as needed.
#
#  Example Usage:  ns time-bw-delay2.tcl "15 155 1.5" "20 1.5 20"

for {set i 1} {$i <= $argc} {incr i} {
  set temp [lindex $argv $i]

  #  DANGER:  assuming that the param group is well formed.
  #           NO error checking!!  Use at own risk.
  set time [lindex $temp 1]
  set bw [lindex $temp 2]
  set delay [lindex $temp 3]

  #  use the variables for your sim
}


Anyone else have other suggestions?

-- 
Brian Lee Bowers	|	RADIANT Team (Summer Intern)
[email protected]	|	Los Alamos National Laboratory