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

Re: dynamically changing link cost



Salil, 

I've extended the tcl rtModel class (defined in tcl/rtglib/dynamics.tcl) to 
dynamically change the cost of a link. Since rtModel takes care of invoking 
route recalculation, the new routes will be recalculated. 

Below defines a toy tcl class rtModel/UniformCost that I used to test out the 
aforementioned mechanism. Once you include this script, you can invoke it like 
this
	$ns rtmodel UniformCost {.6 1 5} $n12 $n13

It means that every 0.6 second, the cost of the link($n12:$n13) will change to 
any integer between 1 and 5, with equal probability. 

Mingzhou Sun
RPI

#
# Uniform link cost, with deterministic inter-change interval
#

Class rtModel/UniformCost -superclass rtModel

rtModel/UniformCost instproc set-parms {interval min max} {
    $self instvar upInterval_ minCost_ maxCost_
    set upInterval_ $interval
    set minCost_ $min
    set maxCost_ $max 
}

rtModel/UniformCost instproc set-first-event {} {
    global rtglibRNG
    # puts "set-first-event"
    $self instvar startTime_ upInterval_
    $self set-event [expr $startTime_ +  $upInterval_] change-cost
}

rtModel/UniformCost instproc change-cost { } {
    global rtglibRNG
    $self instvar maxCost_ minCost_ links_ upInterval_
    set newCost [expr [ns-random] % [expr $maxCost_ - $minCost_ + 1] + 
$minCost_]
    foreach l [array names links_] {
	$links_($l) dynamic-cost $newCost
    }
    #puts "in change-cost"
    $self set-event $upInterval_ change-cost
}

Link instproc dynamic-cost newCost {
	$self instvar cost_ dynamics_ dynT_ fromNode_ toNode_
    # puts "in dynamic-cost"
	if ![info exists dynamics_] return
	;# $dynamics_ set status_ 1
    # puts "[[Simulator instance] now] cost-changed: from $cost_ to $newCost"
	if [info exists dynT_] {
		foreach tr $dynT_ {
			$tr format link([$fromNode_ id]:[$toNode_ id]) cost changed: from $cost_ to 
$newCost
			set ns [Simulator instance]
			$self instvar fromNode_ toNode_
			;# $tr ntrace "l -t [$ns now] -s [$fromNode_ id] -d [$toNode_ id] -S UP"
			$tr ntrace "v -t [$ns now] sim_annotation [$ns now] link([$fromNode_ 
id]:[$toNode_ id]) cost changed: from $cost_ to $newCost"
		}
	}
	set cost_ $newCost
}