[or-cvs] r20931: {projects} Add comments and fix some bugs in hillclimbing code (projects/performance/node-selection)

sjm217 at seul.org sjm217 at seul.org
Mon Nov 9 11:22:23 UTC 2009


Author: sjm217
Date: 2009-11-09 06:22:23 -0500 (Mon, 09 Nov 2009)
New Revision: 20931

Modified:
   projects/performance/node-selection/hillclimbing.py
Log:
Add comments and fix some bugs in hillclimbing code

Modified: projects/performance/node-selection/hillclimbing.py
===================================================================
--- projects/performance/node-selection/hillclimbing.py	2009-11-09 11:20:52 UTC (rev 20930)
+++ projects/performance/node-selection/hillclimbing.py	2009-11-09 11:22:23 UTC (rev 20931)
@@ -13,15 +13,19 @@
 
 import threading
 
+## Number of iterations before narrowing adjustment
 ILIMIT = 1000
+NUM=8000
 
 def wait(x, p, L, isbroken):
+    '''Calculate waiting time at each node'''
     z = p*L*x
     a = z*x
     b = 2.0*(1.0-z)
     return x + a/b
 
 def calc_waittime(prob, xs, totalusage, debug=False):
+    '''Function to optimize'''
     #print "info", prob[0], nodebw[0], totalusage
     ## Check that probabilities really add up to 1
     assert abs(1.0 - prob.sum()) < 1e-6
@@ -112,10 +116,10 @@
         prob = sprob.copy()
         
         ## Find elements to change
-        a = random.randint(0, l-1)
-        b = random.randint(0, l-1)
+        a = random.randint(0, l)
+        b = random.randint(0, l)
 
-        ## Find amount to modify by
+        ## Clip amount to modify by
         if prob[a] < amount:
             damount = prob[a]
         else:
@@ -128,12 +132,15 @@
         ## Calculate the function to optimize
         wwait = calc_waittime(prob, xs, totalusage, True)
         if wwait is None:
+            ## We reached a bad state, give up
             continue
+
+        ## If this is an improvement, return new probabilities
         s = wwait.sum()
-
         if s < ss:
             return i, prob, wwait, s
         if i > ILIMIT:
+            ## We reached the maximum limit, return the original parameters
             wwait = calc_waittime(sprob, xs, totalusage, True)
             return i, sprob, wwait, wwait.sum()
 
@@ -169,10 +176,10 @@
         amount = 1.0/2
 
         i = 0
-        while True:
+        while i<NUM:
             i += 1
             cnt, self.prob, wwait, s = optimize(self.prob, s, self.xs, self.totalusage, amount)
-            print i, cnt, s
+            print "%6d %4d %f"%(i, cnt, s)
             if cnt > ILIMIT:
                 amount /= 2.0
                 print "Narrowing... to", amount
@@ -181,9 +188,19 @@
             line.set_ydata(cumsum(wwait))
             self.fig.canvas.draw()
             #time.sleep(1)
+        save(self.anodebw, self.prob, self.xs, self.totalusage, i)
+        sys.exit()
 
 def main():
     totalusage, anodebw, x, y = test()
+
+    #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)
     
     pt = anodebw/anodebw.sum()
     



More information about the tor-commits mailing list