Advertisement
user_137

PS10 numpy.array

May 27th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. import numpy as np
  2.     import sys, time, string
  3.  
  4.  
  5.     def timeWrap(func):
  6.         def wrapper(*args, **kwargs):
  7.             t0 = time.time()
  8.             result = func(*args, **kwargs)
  9.             print "It took:", time.time() - t0, "seconds"
  10.             return result
  11.         return wrapper
  12.  
  13.  
  14.     def load_map(mapFilename):
  15.         maxRC = 0
  16.         atemp = []
  17.         with open(mapFilename, 'r') as F:
  18.             for line in F.readlines():
  19.                 ltemp = string.split(line, " ")
  20.                 ltemp[3] = ltemp[3][:-1]
  21.                 ltemp = [int(i) for i in ltemp]
  22.                 atemp.append(ltemp)
  23.                 maxRC = max(maxRC, ltemp[0], ltemp[1])
  24.             maxRC += 1
  25.             mitKey = np.zeros((maxRC), dtype=np.int32)
  26.             mitMap = np.zeros((maxRC, maxRC, 2), dtype=np.int32)
  27.             for i in range(len(atemp)):
  28.                 mitKey[atemp[i][0]] += 1
  29.                 tot = mitMap[atemp[i][0], atemp[i][1], 0]
  30.                 out = mitMap[atemp[i][0], atemp[i][1], 1]
  31.                 if tot == 0 or atemp[i][2] < tot or (atemp[i][2] == tot and atemp[i][3]):
  32.                     mitMap[atemp[i][0], atemp[i][1], 0] = atemp[i][2]
  33.                     mitMap[atemp[i][0], atemp[i][1], 1] = atemp[i][3]
  34.         return mitKey, mitMap
  35.  
  36.  
  37.     @timeWrap
  38.     def bruteForceSearch(digraph, dikey, start, end, maxTotalDist, maxDistOutdoors):
  39.  
  40.         def bFS(digraph, dikey, start, end, maxTotalDist, maxDistOutdoors, path = None, shortest = None):
  41.  
  42.             def lenPath(path):
  43.                 tlen = olen = 0
  44.                 for i in range(len(path)-1):
  45.                     td, od = digraph[path[i], path[i+1]]
  46.                     tlen += td
  47.                     olen += od
  48.                 return (tlen, olen)
  49.  
  50.             path = (path or []) + [start]
  51.             size = len(dikey)
  52.  
  53.             if start == end:
  54.                 td, od = lenPath(path)
  55.                 if td > maxTotalDist or od > maxDistOutdoors:
  56.                     return
  57.                 # print td, '.',
  58.                 if shortest is None or td < lenPath(shortest)[0]:
  59.                     if shortest is not None: print path, td, lenPath(shortest)[0], '_:_'
  60.                     return path
  61.                 return
  62.  
  63.             def childrenOf(digraph, s):
  64.                 d = 0
  65.                 childrenList = []
  66.                 for d in range(size):
  67.                     if digraph[s, d, 0] != 0:
  68.                         childrenList.append(d)
  69.                 return childrenList
  70.  
  71.  
  72.             for node in childrenOf(digraph, start):
  73.                 if node not in path: #avoid cycles
  74.                     newPath = bFS(digraph, dikey, node, end, maxTotalDist, maxDistOutdoors, path, shortest)
  75.                     if newPath is not None:
  76.                         td, od = lenPath(path)
  77.                         if shortest is not None:
  78.                             st, od = lenPath(shortest)
  79.                             if (td <= st):
  80.                                 shortest = newPath
  81.                         else:
  82.                             shortest = newPath
  83.             return shortest
  84.  
  85.  
  86.         shortestpath = bFS(digraph, dikey, start, end, maxTotalDist, maxDistOutdoors)
  87.         if shortestpath is None:
  88.             pass
  89.             # raise ValueError('No such path!')
  90.         return shortestpath
  91.  
  92.  
  93.     if __name__ == '__main__':
  94.         LARGE_DIST = 1000000
  95.         mitKey, mitMap = load_map("mit_map.txt")
  96.         brutePath1 = bruteForceSearch(mitMap, mitKey, 32, 56, LARGE_DIST, LARGE_DIST)
  97.         print "Brute-force: ", brutePath1
  98.  
  99.     # SDSize = maxSourceOrDestNumber+1
  100.     # mitKey = np.array SDSize 1 dimensional
  101.     # mitMap = np.array (SDSize, SDSize, 2)
  102.     # path = [[s,d,t,o], .... ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement