[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r20933: {projects} Add comments (projects/performance/node-selection)
Author: sjm217
Date: 2009-11-09 06:51:33 -0500 (Mon, 09 Nov 2009)
New Revision: 20933
Modified:
projects/performance/node-selection/hillclimbing.py
Log:
Add comments
Modified: projects/performance/node-selection/hillclimbing.py
===================================================================
--- projects/performance/node-selection/hillclimbing.py 2009-11-09 11:39:11 UTC (rev 20932)
+++ projects/performance/node-selection/hillclimbing.py 2009-11-09 11:51:33 UTC (rev 20933)
@@ -15,10 +15,14 @@
## Number of iterations before narrowing adjustment
ILIMIT = 1000
+## Maximum number of iterations before exiting
NUM=8000
+## Fraction of network capacity which is being used (set to None to
+## use actual network data
+USAGE=0.5
def wait(x, p, L, isbroken):
- '''Calculate waiting time at each node'''
+ '''Calculate waiting time at each node (assuming M/D/1 queue)'''
z = p*L*x
a = z*x
b = 2.0*(1.0-z)
@@ -30,14 +34,7 @@
## Check that probabilities really add up to 1
assert abs(1.0 - prob.sum()) < 1e-6
- ## Calculate processing time of a node
- #xs = [1.0/t for t in nodebw]
- #xs = 1.0/nodebw
-
## Find overloaded nodes
- #loading_factor = [x*p*totalusage for p, x in zip(prob, xs)]
- #broken = [ld < 0.0 or ld > 1.0 for ld in loading_factor]
-
loading_factor = xs*prob*totalusage
broken = (loading_factor < 0.0) | (loading_factor > 1.0)
if broken.any():
@@ -46,30 +43,19 @@
#print "Broken", len([x for x in broken if x])
## Calculate weighted waiting time
- #wtime = [wait(x, p, totalusage, isbroken)
- # for x, p, isbroken in zip(xs, prob, broken)]
wtime = wait(xs, prob, totalusage, False)
wtime[broken] = -1.0
- #print wtime[0]
## Get maximimum waiting time for non-broken nodes
cap = wtime.max()
- #if debug:
- # print cap
- #print wtime
- #print "Cap", cap
- ## Calculated capped weighted waiting time
+ ## Calculate capped weighted waiting time
wtime[broken] = cap
wwtime = wtime * prob
-
- #wwtime = [wwait_cap(w, p, cap) for w, p in zip(wtime, prob)]
return wwtime
-def test(fn = None):
- if fn is None:
- fn = sys.argv[1]
+def load_data(fn):
fh = file(fn, "rt")
## Load in node bandwidths and total network usage
@@ -87,22 +73,18 @@
totalbw += bw
nodebw.append(bw)
- #print totalusage, totalbw
+ # Node selection probabilities
+ pu = array([1.0/len(nodebw)] * len(nodebw)) # uniform
+ pt = array([bw / totalbw for bw in nodebw]) # Tor
- pu = array([1.0/len(nodebw)] * len(nodebw))
- pt = array([bw / totalbw for bw in nodebw])
-
anodebw = array(nodebw)
xs = 1.0/anodebw
+
+ ## Weighted waiting time for Tor and uniform weighting
x = calc_waittime(pt, xs, totalusage, True)
y = calc_waittime(pu, xs, totalusage, True)
- #for i in range(10000):
- # _ = calc_waittime(pt, xs, totalusage)
-
- #print x
print "E(Tor)", sum(x)
- #print y
print "E(Uniform)", sum(y)
return totalusage, anodebw, x,y
@@ -175,32 +157,45 @@
line, = self.ax.plot(wwait.cumsum())
amount = 1.0/2
+ ## Main optimization loop
i = 0
- while i<NUM:
+ while i < NUM:
i += 1
cnt, self.prob, wwait, s = optimize(self.prob, s, self.xs, self.totalusage, amount)
print "%6d %4d %f"%(i, cnt, s)
if cnt > ILIMIT:
+ ## We tried for too long to optimize so reduce the
+ ## amount to modify probabilities
amount /= 2.0
print "Narrowing... to", amount
save(self.anodebw, self.prob, self.xs, self.totalusage, i)
line.set_ydata(cumsum(wwait))
self.fig.canvas.draw()
- #time.sleep(1)
+ ## Save the intermediate result
save(self.anodebw, self.prob, self.xs, self.totalusage, i)
sys.exit()
def main():
- totalusage, anodebw, x, y = test()
+ if len(sys.argv) != 2:
+ print "Usage: hillclimbing.py FILE"
+ sys.exit()
+ ## Load data file, where each line is:
+ ## BANDWIDTH USAGE
+ ## Where both are in kB/s
+ fn = sys.argv[1]
+ totalusage, anodebw, x, y = load_data(fn)
+
+ ## Try for different levels of bandwidth usage
#totalusage = anodebw.sum()*(1-1e-6)
#totalusage = anodebw.sum()*(1-1e-3)
#totalusage = anodebw.sum()*(1-1e-1)
#totalusage = anodebw.sum()*(0.75)
#totalusage = anodebw.sum()*(0.5)
#totalusage = anodebw.sum()*(0.25)
- totalusage = anodebw.sum()*(0.1)
+ if USAGE != None:
+ totalusage = anodebw.sum()*USAGE
pt = anodebw/anodebw.sum()
@@ -208,6 +203,7 @@
fig = plt.figure()
ax = fig.add_subplot(111)
+ ## Optimize selection probabilities
anim = Animator(pt, totalusage, anodebw, fig, ax)
win = fig.canvas.manager.window
@@ -218,5 +214,4 @@
if __name__=="__main__":
main()
-
-
+# vim: ai ts=4 sts=4 et sw=4