def solve(): """ Solve the puzzle by getting the required parameters from the request args. """ algorithm = request.args['algorithm'] arr = list(map(int, request.args['input[]'][1:-1].split(','))) print(algorithm) agent = None if algorithm == "BFS": agent = BFS() elif algorithm == "DFS": agent = DFS() elif algorithm == "A start (Euclidean)": agent = AStar(Euclidean) elif algorithm == "A start (Manhatten)": agent = AStar(Manhatten) else: return arr start = timeit.default_timer() res = agent.search(arr) end = timeit.default_timer() res['time'] = end - start ret = jsonify(res) return ret
def __init__(self, number, initState, goalState, wallStates, game): """ :param number: int number of the player in the players list :param initState: list the initial position of the player :param goalState: list the goal position of the player :param wallStates: list of lists the walls position :param game: game object the game (the map with all informations like the walls...) """ self.initState = initState self.goalState = goalState self.number = number self.game = game self.index = 1 self.astar = AStar(initState, goalState, wallStates, game.spriteBuilder.rowsize, game.spriteBuilder.colsize) self.path = (self.astar.run()).trace() self.move = True self.walls = wallStates self.astarForTrueDist = AStar(goalState, initState, wallStates, game.spriteBuilder.rowsize, game.spriteBuilder.colsize)
def main(): row, column = list(map(int, input().split())) table = Table(row, column) table.parseInput() # for test: # for i in range(row): # for j in range(column): # print(table.matrix[i][j], end=" ") # print() # print(table.xyRobot) # print(table.xyButters) # print(table.xyPersons) # print(table.matrix) # bbfs bbfs = BidirectionalBFS(table) startT = time.time() bbfs.calcPathAndCost() endT = time.time() if not bbfs.path: print("Can't pass the butter") else: print("path: ", bbfs.path) print("cost: ", bbfs.cost) print("depthForward: ", bbfs.forwardLastNode.depth) print("depthBackward: ", len(bbfs.path2)) print("sum of depth: ", len(bbfs.path)) print("Execution time in seconds: ", endT - startT) print("Expanded Nodes: ", bbfs.numVisitedNodes) print("Generated Nodes: ", bbfs.numAllNodes) # #Draw # draw = DrawBoard(table, bbfs.path) # draw.draw() # aStar aStar = AStar(table) startT = time.time() aStar.calcPathAndCost() endT = time.time() if not aStar.path: print("Can't pass the butter") else: print("path: ", aStar.path) print("cost: ", aStar.cost) print("depth: ", aStar.lastNode.depth) print("Execution time in seconds: ", endT - startT) print("Expanded Nodes: ", aStar.numVisitedNodes) print("Generated Nodes: ", aStar.numAllNodes)
def findPath(self, start, goal): """ start : (float,float), goal : (float,float,float) or (float,float) uses the AStar class to find the shortest path between 'start' and 'goal' then simplifies the path to obtain straight lines as long as possible """ if len(goal) == 2: goal = goal + (0, ) # default value of 'goalRadius' is 0 a = AStar(start, goal, self.thresholdMap) print "Résultat de A*: " + str(a.aStar()) p = a.buildPath() if p == None: self.path == None else: l = len(p) current = l - 1 self.path = [p[current]] while current > 0: i = 0 while i + 1 < current and not self.isLineClear( p[current], p[i]): i += 1 current = i self.path.insert(0, p[current])
def test_AStar_Manhatten(self): agent = AStar(Manhatten) steps = agent.search([1, 2, 5, 3, 4, 0, 6, 7, 8]) print("Solved in {} steps".format(len(steps))) print(steps)