def act(self, map_): laps_and_chopps = [] position = self._position(map_) for i_row, row in enumerate(map_): for i_colum, slot in enumerate(row): if slot in constants.SCORE_THINGS: laps_and_chopps.append((i_row, i_colum)) def distance(a, b): return sum([abs(a[i] - b[i]) for i in [0, 1]]) closest_objective = [] for objective in laps_and_chopps: objective_distance = distance(objective, position) if closest_objective: if objective_distance < closest_objective[1]: next_step = utils.a_star(map_, position, objective) if next_step != constants.DANCE: closest_objective = [ objective, objective_distance, next_step ] else: next_step = utils.a_star(map_, position, objective) closest_objective = [objective, objective_distance, next_step] if closest_objective: return closest_objective[2] else: return constants.DANCE
def button_callback(event): global pp path, cost = a_star(grid=grid, start=home_grid, goal=goal_grid) pp = np.array(path) # ax.imshow(grid, cmap='Greys') ax.plot(pp[:, 1], pp[:, 0], linewidth=3) plt.draw()
def act(self, map_): chopps = [] laps = [] position = self._position(map_) for i_row, row in enumerate(map_): for i_colum, slot in enumerate(row): if slot == constants.CHOPP: chopps.append((i_row, i_colum)) elif slot == constants.LAPTOP: laps.append((i_row, i_colum)) def distance(a, b): return sum([abs(a[i] - b[i]) for i in [0, 1]]) closest_lap = [] for lap in laps: lap_distance = distance(lap, position) if closest_lap: if lap_distance < closest_lap[1]: next_step = utils.a_star(map_, position, lap) if next_step != constants.DANCE: closest_lap = [lap, lap_distance, next_step] else: next_step = utils.a_star(map_, position, lap) closest_lap = [lap, lap_distance, next_step] if closest_lap: return closest_lap[2] closest_chopp = [] for chopp in chopps: chopp_distance = distance(chopp, position) if closest_chopp: if chopp_distance < closest_chopp[1]: next_step = utils.a_star(map_, position, chopp) if next_step != constants.DANCE: closest_chopp = [chopp, chopp_distance, next_step] else: next_step = utils.a_star(map_, position, chopp) closest_chopp = [chopp, chopp_distance, next_step] if closest_chopp: return closest_chopp[2] else: return constants.DANCE
def coolplace_act(map_, position, state): """Go to a place in the map where most things are accumulated. There is a timer to avoid changing the target place in each step. Because there is a chance that more than one place are equally interesting. """ coolplace = get_coolplace(map_) if coolplace_needs_update(state) else state['coolplace'] action = utils.a_star(map_, position, coolplace) return assoc_multi(state, { 'method': 'coolplace', 'action': action, 'coolplace': coolplace if action != constants.DANCE else None, 'coolplace_timer': tick_coolplace_timer(state), })
def act(self, map_): if self.id == constants.PLAYER_X: other = constants.PLAYER_Y else: other = constants.PLAYER_X my_position = utils.find_thing(map_, self.id) other_position = utils.find_thing(map_, other) def distance(a, b): return sum([abs(a[i] - b[i]) for i in [0, 1]]) if distance(my_position, other_position) == 1: next_step = constants.DANCE else: # remove to make that position reachable by astar map_[other_position[0]][other_position[1]] = constants.EMPTY next_step = utils.a_star(map_, my_position, other_position) return next_step
def quicky_act(map_, position, state): """If there is anything at hand, go grab it. There is a counter to avoid getting too much distracted with guickies, as grabbing one thing near after another can leave the bot far from the cool place. When the maximum of quickies is reached, a timer is set to go back to quicky mode. """ if state['quicky_timer'] <= 0: return assoc_multi(state, { 'method': 'quicky', 'quickies_targetted': set(), 'quicky_timer': QUICKY_TIMER, 'action': constants.DANCE, }) else: if len(state['quickies_targetted']) >= MAX_QUICKIES: return assoc_multi(state, { 'method': 'quicky', 'action': constants.DANCE, 'quicky_timer': state['quicky_timer'] - 1, }) else: quickything = get_quickything(map_, position) if quickything is None: return assoc_multi(state, { 'method': 'quicky', 'action': constants.DANCE, }) else: return assoc_multi(state, { 'method': 'quicky', 'action': utils.a_star(map_, position, quickything), 'quickies_targetted': set_add(state['quickies_targetted'], quickything), })
min_distance_to_others = utils.getMinDistance(start, others_start, gameMap) logging.debug("Found Minimum Distance") logging.debug(min_distance_to_others) map_attractiveness(myID, locslist, locsites, attractiveness_start) logging.debug("Mapped Attractiveness") utils.mapSmoothedAttractiveness(myID, gameMap, kernel=[1.5, 1.5, 1.5, 1.5]) logging.debug("Mapped Smoothed Attractiveness") target = utils.findLocalMaxSmootherAttr( start, myID, gameMap, regionRadius=min_distance_to_others / 2) logging.debug("Found Target") logging.debug(target) directions_dict, path = utils.a_star(target, start, gameMap, cost) logging.debug("Found Path") sendInit("SmartFrontierBot2") logging.debug("Init sent") decay = 0.1 momentumTerm = 1000. enemy_attr = 0.1 #0.5 works well too turn = 0 time_tracker = utils.TimeTracker(logging) game_dumper = utils.Dumper('gameMap', 'smartfrontier2', on=False) while True:
def main(): global FONT, MAIN_SCREEN, INTERNAL_MAP, H, WEIGHTS, W_INDEX, W_SELECTION pygame.init() MAIN_SCREEN = pygame.display.set_mode((SYSTEM_WIDTH, SYSTEM_HEIGHT), pygame.FULLSCREEN) icon = pygame.image.load('images/robot_icon.png') pygame.display.set_icon(icon) pygame.display.set_caption('CS440 Assignment 1') FONT = pygame.font.SysFont("monospace", FONT_SIZE) load_user_interface() internal_grid, map_visual_base = generate_a_map() start, goal, map_visual_full = draw_get_start_and_goal( internal_grid, map_visual_base) save_the_map(internal_grid, start, goal, HTT_COORDS) pygame.display.update() previous_highlighted_cell = normal_color = highlighted_cell = None info_ready = False while True: for e in pygame.event.get(): if e.type == pygame.QUIT: pygame.quit() quit() if e.type == pygame.KEYDOWN: solution = [] if e.key == pygame.K_c: # clear labels, keep map and start/goal MAIN_SCREEN.blit(map_visual_full, (0, 0)) load_user_interface() pygame.display.update() if e.key == pygame.K_n: # new map internal_grid, map_visual_base = generate_a_map() start, goal, map_visual_full = draw_get_start_and_goal( internal_grid, map_visual_base) pygame.display.update() if e.key == pygame.K_g: # new start/goal cells start, goal, map_visual_full = draw_get_start_and_goal( internal_grid, map_visual_base) pygame.display.update() if e.key == pygame.K_a: # run a* normal solution = a_star(start, goal, internal_grid, H) path_color = GREEN if e.key == pygame.K_u: # run UCS solution = uniform_cost_search(start, goal, internal_grid) path_color = PINK if e.key == pygame.K_w: # run weighted A* solution = weighted_a_star(start, goal, internal_grid, H, WEIGHTS[0]) path_color = PURPLE if e.key == pygame.K_s: # run sequential A* solution = sequential_a_star(start, goal, internal_grid, H_DEFS, WEIGHTS[1], WEIGHTS[2]) path_color = YELLOW if e.key == pygame.K_i: # run integrated A* solution = integrated_a_star(start, goal, internal_grid, H_DEFS, WEIGHTS[1], WEIGHTS[2]) path_color = RED if e.key == pygame.K_d: # save the map to file save_the_map(internal_grid, start, goal, HTT_COORDS) pygame.display.update() if e.key == pygame.K_1: H = C_dist load_user_interface() if e.key == pygame.K_2: H = M_dist load_user_interface() if e.key == pygame.K_3: H = E_dist load_user_interface() if e.key == pygame.K_4: H = In_dist load_user_interface() if e.key == pygame.K_5: H = A_dist load_user_interface() if e.key == pygame.K_DOWN: W_SELECTION[W_INDEX] = '' if W_INDEX == 2: W_INDEX = 0 else: W_INDEX += 1 W_SELECTION[W_INDEX] = '<<' load_user_interface() if e.key == pygame.K_UP: W_SELECTION[W_INDEX] = '' if W_INDEX == 0: W_INDEX = 2 else: W_INDEX -= 1 W_SELECTION[W_INDEX] = '<<' load_user_interface() if e.key == pygame.K_LEFT: WEIGHTS[W_INDEX] -= 0.25 load_user_interface() if e.key == pygame.K_RIGHT: WEIGHTS[W_INDEX] += 0.25 load_user_interface() if e.key == pygame.K_ESCAPE: pygame.quit() quit() if solution: info_ready = True for cell in solution: tmp_sq = pygame.Surface((SCALE - 1, SCALE - 1)) tmp_sq.fill(path_color) # tmp_sq.set_alpha(100) MAIN_SCREEN.blit(tmp_sq, (cell.pixel_x + 1, cell.pixel_y + 1)) pygame.display.update() if e.type == pygame.MOUSEBUTTONDOWN and e.button == 1: # if left mouse button clicked m_pos = pygame.mouse.get_pos() pix_x = m_pos[0] pix_y = m_pos[1] if pix_x < MAP_WIDTH and info_ready: # means clicked within the map and not the interface c_pix_x = RDTM(pix_x, SCALE) c_pix_y = RDTM(pix_y, SCALE) row = int(c_pix_y / SCALE) col = int(c_pix_x / SCALE) clicked_cell = internal_grid[row][col] display_info(clicked_cell) else: pass if e.type == pygame.MOUSEMOTION: m_pos = pygame.mouse.get_pos() pix_x = m_pos[0] pix_y = m_pos[1] if pix_x < MAP_WIDTH: # means hovering within the map and not the interface c_pix_x = RDTM(pix_x, SCALE) c_pix_y = RDTM(pix_y, SCALE) row = int(c_pix_y / SCALE) col = int(c_pix_x / SCALE) if highlighted_cell != internal_grid[row][col]: highlighted_cell = internal_grid[row][col] if previous_highlighted_cell: if previous_highlighted_cell is not highlighted_cell: draw_cell_rect( MAIN_SCREEN, normal_color, previous_highlighted_cell.pixel_x, previous_highlighted_cell.pixel_y, DEFAULT_BORDER_COLOR, 1) pygame.display.update() normal_color = MAIN_SCREEN.get_at( (highlighted_cell.pixel_x + 5, highlighted_cell.pixel_y + 5)) draw_cell_rect(MAIN_SCREEN, YELLOW, highlighted_cell.pixel_x, highlighted_cell.pixel_y, DEFAULT_BORDER_COLOR, 1) pygame.display.update() previous_highlighted_cell = internal_grid[row][col] else: pass return