def getPathWithPathDistanceMap(start, end, mapData, finishWhenFoundPath = True): sq2 = math.sqrt(2) w = len(mapData) h = len(mapData[0]) distance =[ [(0) for y in range(h)] for x in range(w)] parent =[ [None for y in range(h)] for x in range(w)] queue = PriorityQueue() xstart = int(start.x) ystart = int(start.y) xend = int(end.x) yend = int(end.y) def heuristic(x0,y0, x1,y1): return math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)) def checkPos(d, x, y, dx ,dy, delta): x1 =x+dx y1 = y+dy result = False if (x1<0 or x1>=w or y1<0 or y1>=h): return False fullDistance = d+delta*(mapData[x][y]) if (mapData[x1][y1]!=-1) and (parent[x1][y1]==None or distance[x1][y1]>fullDistance): parent[x1][y1] = (x,y) distance[x1][y1] = fullDistance heursticD = fullDistance+heuristic(x,y,x1,y1) queue._put((heursticD, (x1,y1))) if ((x1==xend) and (y1==yend)): result = True return result pathFounded = False if checkPos(0, xstart,ystart, 0,0, 0) and finishWhenFoundPath: return [Vector2(x+0.5,y+0.5)], distance, parent while queue._qsize()>0: prior,pos = queue._get() x,y=pos d = distance[x][y] if (checkPos(d, x,y, 1,0, 1) or checkPos(d, x,y, -1,0, 1) or checkPos(d, x,y, 0,1, 1) or checkPos(d, x,y, 0,-1, 1) or checkPos(d, x,y, 1,1, sq2) or checkPos(d, x,y, -1,1, sq2) or checkPos(d, x,y, 1,-1, sq2) or checkPos(d, x,y, -1,-1, sq2)): pathFounded = True if (finishWhenFoundPath): break result = [] if pathFounded: x = xend y = yend while (parent[x][y]!=(x,y)): result.append(Vector2(x+0.5,y+0.5)) x,y = parent[x][y] result.reverse() savePath('path',result, mapData) return result, distance, parent
def getAdjustedTrees(tree): results = list() treeHashSet = set() ctr = 20 queue = PriorityQueue() #TODO :check if p queue is working properly H = dict() queue.put(tree) results.append(tree) H[tree.__hash__()] = tree tree.setEdit(0) #TODO addON is wrong treeWithON = tree.addON() queue.put(treeWithON) results.append(treeWithON) H[treeWithON.__hash__()] = treeWithON treeWithON.setEdit(0) while not queue.empty(): ctr -= 1 scoreList = [] editList = [] debug_size = queue._qsize() # print "\nqueue size = %d\n" %(debug_size) # print "Queue tree: " tempList = [] oriTree = queue.get() if (oriTree.getEdit() >= TreeAdjustor.MAX_EDIT): continue treeList = TreeAdjustor.adjust(oriTree) # print "treeAdjuster :219 " numInvalidNodes = SyntacticEvaluator().numberOfInvalidNodes( oriTree) for i in range(0, len(treeList)): currentTree = treeList[i] hashValue = currentTree.__hash__() if not (H.has_key(hashValue)): H[hashValue] = currentTree currentTree.setEdit(oriTree.getEdit() + 1) # print "treeAdjuster :229 " if SyntacticEvaluator().numberOfInvalidNodes( currentTree) <= numInvalidNodes: queue.put(currentTree) results.append(currentTree) return results
def getAdjustedTrees( tree): results = list() treeHashSet = set() ctr = 20 queue = PriorityQueue() #TODO :check if p queue is working properly H = dict() queue.put(tree) # if tree not in treeHashSet: # treeHashSet.add(tree) results.append(tree); H[tree.__hash__()] = tree tree.setEdit(0); #TODO addON is wrong treeWithON = tree.addON() queue.put(treeWithON); # if treeWithON not in treeHashSet: # treeHashSet.add(treeWithON) results.append(treeWithON); H[treeWithON.__hash__()] = treeWithON treeWithON.setEdit(0) while not queue.empty() : ctr -= 1 scoreList = [] editList = [] debug_size = queue._qsize() # print "\nqueue size = %d\n" %(debug_size) # print "Queue tree: " tempList =[] # for i in range(0, queue._qsize()): # tr = queue.get() # #print tr.getSentence() # # print "%s --> %d"%(tr.getSentence(), tr.getScore()) # scoreList.append(tr.getScore()) # editList.append(tr.getEdit()) # tempList.append(tr) # # print "scoreList: " # print scoreList # print "editList: " # print editList # for tr in tempList: # queue.put(tr) # if ctr == 10: # oriTree = queue.get() # # secTree = queue.get() # queue.put(oriTree) # # oriTree =secTree # else: oriTree = queue.get() # print "Currently Adjusting tree: %s:%d" % (oriTree.getSentence(), oriTree.getScore()) # print if (oriTree.getEdit() >= TreeAdjustor.MAX_EDIT): continue treeList = TreeAdjustor.adjust(oriTree) # print "treeAdjuster :219 " numInvalidNodes = SyntacticEvaluator().numberOfInvalidNodes(oriTree) for i in range(0,len(treeList)): currentTree = treeList[i] hashValue = currentTree.__hash__() if not( H.has_key(hashValue) ): H[hashValue] = currentTree currentTree.setEdit(oriTree.getEdit() + 1); # print "treeAdjuster :229 " if SyntacticEvaluator().numberOfInvalidNodes(currentTree) <= numInvalidNodes: queue.put(currentTree) results.append(currentTree) # for tr in results: # print "Result: %s %d" % (tr.getSentence(), tr.getScore()) return results