def findPath2(): #Check for valid command-line arguments, otherwise set default if len(sys.argv) < 3 or sys.argv[1] == None or not str.isnumeric( sys.argv[1]): height = random.randint(10, 20) else: height = int(sys.argv[1]) if len(sys.argv) < 3 or sys.argv[2] == None or not str.isnumeric( sys.argv[2]): width = random.randint(10, 20) else: width = int(sys.argv[2]) size = (width, height) #Retrieving Grid data from function that creates the grid obstacles, start, goal, weightedCells = sim2.getGrid(size) prev = {} isFound = False prev[start] = None cost = {} cost[start] = 0 lead = PQ() lead.insert(start, 0) while not lead.isEmpty(): cell = lead.get() if cell == goal: isFound = True break for next in checkAdj( size, cell, obstacles): #Check for candidate cells from neighbors g_x = cost[cell] + getCost(next, weightedCells) #Get current cost if next not in cost or g_x < cost[ next]: #considering only new, low cost cells cost[next] = g_x (x1, y1) = goal (x2, y2) = next h_x = abs(x1 - x2) + abs( y1 - y2) #Calculate Admissable Heurstic Function f_x = g_x + h_x #total cost= current cost + cost to goal lead.insert(next, f_x) prev[next] = cell #update path list return prev, cost, start, goal, size, obstacles, weightedCells, isFound
def test_goalNotInObstacles(self): size = (100, 100) obstacles, start, goal, weightedCells = getGrid(size) self.assertNotIn(goal, obstacles) print("\nSuccess")
def test_startNotEqualGoal(self): size = (100, 100) obstacles, start, goal, weightedCells = getGrid(size) self.assertNotEqual(start, goal) print("\nSuccess")
def test_startWithinGrid(self): size = (100, 100) obstacles, start, goal, weightedCells = getGrid(size) self.assertLess(start, size) print("\nSuccess")