def iterateEggPoly(self, egg, type): # Create a node for each quad if isinstance(egg, EggPolygon): node = GridNode(self.nodeCount, egg.getVertex(0), egg.getVertex(1), egg.getVertex(2), egg.getVertex(3)) if type == "Full": self.oldList.append(node) # Find the correct vertex number to use if (self.lowestVertex == -1): self.analyzeVertex(node) # Store the bottom left node if (self.firstNode == None): self.firstNode = node else: if node.vertex[self.lowestVertex].getX( ) < self.firstNode.vertex[self.lowestVertex].getX( ) and node.vertex[self.lowestVertex].getZ( ) < self.firstNode.vertex[self.lowestVertex].getZ(): self.firstNode = node self.nodeCount = self.nodeCount + 1 else: self.oldCollList.append(node) self.collNodeCount = self.collNodeCount + 1 if isinstance(egg, EggGroupNode): child = egg.getFirstChild() while child != None: self.iterateEggPoly(child, type) child = egg.getNextChild()
def makergrid(gridf): grid = open(gridf) rgrid = [[None for x in range(101)]for y in range(101)] for i in list(range(101)): #converts text grid to more easily used array form line = grid.readline() for s in list(range(101)): rgrid[i][s] = GridNode.node(i, s, math.inf, None, 0, line[s*2:s*2+2].rstrip() == "1") #(x, y, costToGo, parent, search, blocked) return rgrid
def main(): if DEBUGFLAG: print("blah") #import pdb;pdb.set_trace() else: #print("blah") sys.stdout = open('outputA.txt', 'w') #sys.stdout = open("output.txt",'w') start_time = time() expanded = 0 counter = 1 #set iteration counter text = sys.argv[1] grid = open(text) #grid = open(sys.argv[1]) #grid = open("C:\\Users\\epywa\\OneDrive\\Documents\\vscode\\AIProjects-master\\arrs\\randGrid\\00.txt") print(text) lstart = lgoal = start = goal = None rgrid = [[None for x in range(101)] for y in range(101)] for i in list( range(101)): #converts text grid to more easily used array form line = grid.readline() for s in list(range(101)): rgrid[i][s] = GridNode.node(i, s, math.inf, None, 0, line[s * 2:s * 2 + 2].rstrip() == "1") #(x, y, costToGo, parent, search, blocked) while (lstart is None) or (lgoal is None) or lstart.blocked or lgoal.blocked: #goal = (random.randint(0,100),random.randint(0,100)) # tuple(column, row) #start = (random.randint(0,100),random.randint(0,100)) # tuple(column, row) #if False: start = ( int(sys.argv[2]), int(sys.argv[3]) ) #(random.randint(0,100),random.randint(0,100)) # tuple(column, row) goal = ( int(sys.argv[4]), int(sys.argv[5]) ) #(random.randint(0,100),random.randint(0,100)) # tuple(column, row) lstart = rgrid[start[0]][start[1]] lgoal = rgrid[goal[0]][goal[1]] #using random gen for the moment #initialize start and goal nodes #lstart = rgrid[start[0]][start[1]] #lgoal = rgrid[goal[0]][goal[1]] print("start: (", start[0], ',', start[1], ")") print("goal: (", goal[0], ',', goal[1], ")") lstart.setCostToCome(goal[0], goal[1]) lstart.costToGo = 0 openlist = BreakLargeHeap.BLHeap() #outlog = [0,0] sgoal = 0 #diff = 0 while lstart != lgoal: lgoal.costToGo = math.inf #increment counter to keep track of nodes over iterations #initialize lists openlist.wipe() closedlist = [ ] #make array for now. #TODO make closed list consistent over code openlist.insert(lstart) #run A* ComputePath(rgrid, lgoal, openlist, closedlist, counter, sgoal) expanded += len(closedlist) if openlist.size == 0: print("Cannot reach target") return path = [] #TODO move along path and implement action-cost adjustments #need to have ability to track changes over the path and iterate until action changes node = lgoal while node != lstart: path.append(node) node = node.parent #why #if node is lstart: #break path.append(lstart) nstart = None flagnode = None #diff += sgoal-len(path) #add difference between path lengths each run, keeping a running tally of overall difference #outlog.append(diff) sgoal = len(path) for i in range(sgoal, 0, -1): #TODO implement later. check = path[i - 1] print(str(counter), check.x, check.y, str(check.blocked)) if check == goal: nstart = check break if check.blocked: flagnode = check break #i -= 1 nstart = check #check = path[len(path)-1-i] lstart = nstart if flagnode is not None: flagnode.action_cost = math.inf counter += 1 print("target reached") print("num-expanded:" + str(expanded)) print("counter:" + str(counter)) print("path_length:" + str(len(path))) print("start: (", start[0], ',', start[1], ")") print("goal: (", goal[0], ',', goal[1], ")") print("execution_time:", str(time() - start_time)) return
CYAN = (0, 255, 255) # Init the game pygame.init() screen = pygame.display.set_mode((screenWidth, screenHeight)) done = False angle = 1 clock = pygame.time.Clock() # Create the list of gridCells (nodes) nodes = [] breadthGraph = Graph() for i in range(0, numberRows): nodeRow = [] for j in range(0, numberCols): node = GridNode(CYAN, BLACK, Vector(i * gridCellSize, j * gridCellSize), gridCellSize, i, j) nodeRow.append(node) breadthGraph.addNode(node) nodes.append(nodeRow) # Create the edges between neighbors for i in range(0, numberRows): for j in range(0, numberCols): for m in range(i - 1, i + 2): for n in range(j - 1, j + 2): if m >= 0 and n >= 0 and m < numberRows and n < numberCols \ and (i != m or j != n): edge = GridEdge(nodes[i][j], nodes[m][n], Distance(nodes[i][j], nodes[m][n]), RED, WHITE) nodes[i][j].addNeighborEdge(edge)
def main(): if DEBUGFLAG: import pdb;pdb.set_trace() else: sys.stdout = open('outputB.txt','w') start_time = time() expanded = 0 counter = 0 #set iteration counter text = sys.argv[1] grid = open(text) print(text) rgrid = [[None for x in range(101)]for y in range(101)] for i in list(range(101)): #converts text grid to more easily used array form line = grid.readline() for s in list(range(101)): rgrid[i][s] = GridNode.node(i, s, math.inf, None, 0, line[s*2:s*2+2].rstrip() == "1") #(x, y, costToGo, parent, search, blocked) lstart = lgoal = start = goal = None while (lstart is None) or (lgoal is None) or lstart.blocked or lgoal.blocked: goal= (int(sys.argv[2]),int(sys.argv[3]))#(random.randint(0,100),random.randint(0,100)) # tuple(column, row) start = (int(sys.argv[4]),int(sys.argv[5]))#(random.randint(0,100),random.randint(0,100)) # tuple(column, row) #using random gen for the moment #initialize start and goal nodes lstart = rgrid[start[0]][start[1]] lgoal = rgrid[goal[0]][goal[1]] print("start: (", start[0], ',', start[1],")") print("goal: (", goal[0], ',', goal[1],")") lstart.setCostToCome(goal[0], goal[1]) lstart.costToGo = 0 openlist = BreakLargeHeap.BLHeap() while lstart != lgoal: lgoal.costToGo = math.inf #increment counter to keep track of nodes over iterations counter = counter+1 #initialize lists openlist.wipe() closedlist = [] #make array for now. #TODO make closed list consistent over code openlist.insert(lstart) #run A* ComputePath(rgrid, lgoal, openlist, closedlist, counter) expanded += len(closedlist) if openlist.size == 0: print("Cannot reach target") return path = [] #TODO move along path and implement action-cost adjustments #need to have ability to track changes over the path and iterate until action changes node = lgoal while node != lstart: path.append(node) node = node.parent #why #if node is lstart: #break path.append(lstart) nstart = None flagnode = None for i in range(len(path),0,-1): #TODO implement later. check = path[i-1] print(str(counter),check.x,check.y,str(check.blocked)) if check == goal: nstart = check break if check.blocked: flagnode = check break #i -= 1 nstart = check #check = path[len(path)-1-i] lstart = nstart if flagnode is not None: flagnode.action_cost = math.inf print("target reached") print("num-expanded:" + str(expanded)) print("counter:" + str(counter)) print("path_length:" + str(len(path))) print("start: (", start[0], ',', start[1],")") print("goal: (", goal[0], ',', goal[1],")") print("execution_time:",str(time()-start_time)) return