._site

(Arne Babenhauserheide)
2012-07-25: use a proper wraparound metric (dist())

use a proper wraparound metric (dist())

diff --git a/sim.py b/sim.py
--- a/sim.py
+++ b/sim.py
@@ -8,6 +8,10 @@ import pylab as pl
 import bisect
 from copy import deepcopy
 
+def dist(loc0, loc1):
+    """return the wraparound distance of 2 nodes in the [0,1) space."""
+    return min((loc0-loc1)%1, (loc1-loc0)%1)
+
 def randomnodes(num=1000):
     """Generate num nodes with locations between 0 and 1."""
     nodes = set()
@@ -99,12 +103,12 @@ def closest(target, sortedlist):
     """return the node which is closest to the target."""
     if not sortedlist:
         raise ValueError("Cannot find the closest node in an emtpy list.")
-    dist = 1.1
+    dis = 1.1
     for n in sortedlist:
-        d = abs(n - target)
-        if d < dist:
+        d = dist(n,target)
+        if d < dis:
             best = n
-            dist = d
+            dis = d
         else: 
             return best
     return best
@@ -179,7 +183,7 @@ def linklengths(net):
     lengths = []
     for node, targets in net.items():
         for t in targets:
-            lengths.append(abs(t-node))
+            lengths.append(dist(t, node))
     return lengths
 
 if __name__ == "__main__":