class AStarAlgorithmWidget(AlgorithmWidget): def __init__(self, parent=None): self.heuristic = naive AlgorithmWidget.__init__(self, "A* Algorithm", parent) self.combo_box = QtGui.QComboBox(parent) self.combo_box.addItems(["naive", 'manhattan', 'crow']) self.combo_box.currentIndexChanged.connect(self.select_heuristic) self.buttons.append(self.combo_box) self.pack_buttons() def select_heuristic(self, index): if index == 0: self.heuristic = naive elif index == 1: self.heuristic = manhattan elif index == 2: self.heuristic = crow self.a_star.heuristic_cost_estimate = self.heuristic def setup_algorithm(self): """Sets up the algorithm""" self.costmap = Costmap2D(DEFAULT_WIDTH, DEFAULT_HEIGHT, resolution=DEFAULT_RESOLUTION) Obstacle(3,3,3,3).draw(self.costmap) Obstacle(9,5,3,3).draw(self.costmap) Obstacle(16,4,3,3).draw(self.costmap) self.costmap_widget = Costmap2DWidget(self.costmap, parent=self, show_goal=False, show_colorbar=True) self.costmap_widget.canvas.show_start = True self.costmap_widget.canvas.show_goal = True temp = self.costmap_widget.canvas.start_coord self.start_coord = (floor(temp[0]+0.5), floor(temp[1]+0.5)) temp = self.costmap_widget.canvas.goal_coord self.goal_coord = (floor(temp[0]+0.5), floor(temp[1]+0.5)) self.a_star = AStar(self.costmap, self.start_coord, self.goal_coord, self.heuristic) def step_solution(self): """Steps the solution""" # self.costmap_widget.canvas.freeze = True # count = 0 # while count < 25: # count += 1 # result = self.a_star.step_solution() # if result == False: # self.costmap_widget.canvas.freeze = False # self.costmap_widget.canvas.on_map_update() # return result # self.costmap_widget.canvas.freeze = False # self.costmap_widget.canvas.on_map_update() # return True return self.a_star.step_solution() def reset_algorithm(self): """Resets the algorithm""" self.costmap_widget.canvas.freeze = True self.costmap[:] = 0.0 Obstacle(3,3,3,3).draw(self.costmap) Obstacle(9,5,3,3).draw(self.costmap) Obstacle(16,4,3,3).draw(self.costmap) temp = self.costmap_widget.canvas.start_coord self.start_coord = (floor(temp[0]+0.5), floor(temp[1]+0.5)) temp = self.costmap_widget.canvas.goal_coord self.goal_coord = (floor(temp[0]+0.5), floor(temp[1]+0.5)) self.a_star = AStar(self.costmap, self.start_coord, self.goal_coord, self.heuristic) self.costmap_widget.canvas.freeze = False self.costmap_widget.canvas.on_map_update()
class AStarAlgorithmWidget(AlgorithmWidget): def __init__(self, parent=None): self.heuristic = naive AlgorithmWidget.__init__(self, "A* Algorithm", parent) self.combo_box = QtGui.QComboBox(parent) self.combo_box.addItems(["naive", 'manhattan', 'crow']) self.combo_box.currentIndexChanged.connect(self.select_heuristic) self.buttons.append(self.combo_box) self.pack_buttons() def select_heuristic(self, index): if index == 0: self.heuristic = naive elif index == 1: self.heuristic = manhattan elif index == 2: self.heuristic = crow self.a_star.heuristic_cost_estimate = self.heuristic def setup_algorithm(self): """Sets up the algorithm""" self.costmap = Costmap2D(DEFAULT_WIDTH, DEFAULT_HEIGHT, resolution=DEFAULT_RESOLUTION) Obstacle(3, 3, 3, 3).draw(self.costmap) Obstacle(9, 5, 3, 3).draw(self.costmap) Obstacle(16, 4, 3, 3).draw(self.costmap) self.costmap_widget = Costmap2DWidget(self.costmap, parent=self, show_goal=False, show_colorbar=True) self.costmap_widget.canvas.show_start = True self.costmap_widget.canvas.show_goal = True temp = self.costmap_widget.canvas.start_coord self.start_coord = (floor(temp[0] + 0.5), floor(temp[1] + 0.5)) temp = self.costmap_widget.canvas.goal_coord self.goal_coord = (floor(temp[0] + 0.5), floor(temp[1] + 0.5)) self.a_star = AStar(self.costmap, self.start_coord, self.goal_coord, self.heuristic) def step_solution(self): """Steps the solution""" # self.costmap_widget.canvas.freeze = True # count = 0 # while count < 25: # count += 1 # result = self.a_star.step_solution() # if result == False: # self.costmap_widget.canvas.freeze = False # self.costmap_widget.canvas.on_map_update() # return result # self.costmap_widget.canvas.freeze = False # self.costmap_widget.canvas.on_map_update() # return True return self.a_star.step_solution() def reset_algorithm(self): """Resets the algorithm""" self.costmap_widget.canvas.freeze = True self.costmap[:] = 0.0 Obstacle(3, 3, 3, 3).draw(self.costmap) Obstacle(9, 5, 3, 3).draw(self.costmap) Obstacle(16, 4, 3, 3).draw(self.costmap) temp = self.costmap_widget.canvas.start_coord self.start_coord = (floor(temp[0] + 0.5), floor(temp[1] + 0.5)) temp = self.costmap_widget.canvas.goal_coord self.goal_coord = (floor(temp[0] + 0.5), floor(temp[1] + 0.5)) self.a_star = AStar(self.costmap, self.start_coord, self.goal_coord, self.heuristic) self.costmap_widget.canvas.freeze = False self.costmap_widget.canvas.on_map_update()