def get_direction(self, ant): '''Finds a direction for this ant to move in according to the food, enemy, exploration routine.''' # Get the list of directions towards food, enemy, and random rand_dirs = AIM.keys() random.shuffle(rand_dirs) dirs = (ant.toward(ant.closest_food()) + ant.toward(ant.closest_enemy()) + rand_dirs) # Get the first passable direction from that long list. d = ant.get_passable_direction(dirs) return d
def get_direction(self, state, ant): '''Returns the ant's least-visited adjacent location, prioritizing by food direction when multiple adjacent locations are equally explored.''' # Of the 4 possible squares to move to, determine which don't currently # contain an ant and are the least-visited. min_visits = float('Inf') min_visits_directions = [] for direction in AIM.keys(): test_position = ant.world.next_position(ant.location, direction) # Ignore water. if not ant.world.passable(test_position): continue # Don't move to a currently occupied location; # this helps somewhat mitigate collisions. if ant.world.ant_lookup[test_position] != -1: continue # Check to see how frequently this candidate location has been visited # in the past. num_visits = state[test_position] if state.has_key(test_position) else 1 if num_visits < min_visits: min_visits = num_visits min_visits_directions = [direction] elif num_visits == min_visits: min_visits_directions.append(direction) if not min_visits_directions: # Will only reach here if ant is boxed in by water on all sides. return None elif len(min_visits_directions) > 1: # Try to break ties by considering food direction. food_directions = ant.toward(ant.closest_food()) for fd in food_directions: if fd in min_visits_directions: return fd return min_visits_directions[0]
def extract(self, world, state, loc, action): """Extract the three simple features.""" if action is None: action = 'halt' food_loc = self.find_closest(world, loc, state.lookup_nearby_food(loc)) enemy_loc = self.find_closest(world, loc, state.lookup_nearby_enemy(loc)) friend_loc = self.find_closest(world, loc, state.lookup_nearby_friendly(loc)) # get closest hills own_hill_loc = self.find_closest(world, loc, world.my_hills()) if own_hill_loc is None: own_hill_loc = [] enemy_hill_loc = self.find_closest(world, loc, world.enemy_hills) if enemy_hill_loc is None: enemy_hill_loc = [] next_loc = world.next_position(loc, action) world.L.debug("loc: %s, food_loc: %s, enemy_loc: %s, friendly_loc: %s" % (str(loc), str(food_loc), str(enemy_loc), str(friend_loc))) # Feature vector f = list() # Moving towards enemy if enemy_loc is None: f.append(False) else: f.append(self.moving_towards(world, loc, next_loc, enemy_loc, state)); # Moving towards food if food_loc is None: f.append(False) else: f.append(self.moving_towards(world, loc, next_loc, food_loc, state)); # Moving towards friendly if friend_loc is None: f.append(False) else: f.append(self.moving_towards(world, loc, next_loc, friend_loc, state)); # Moving on aStar-path towards food # if food_loc is None: # f.append(False) # else: # f.append(self.moving_towards_on_astar(world, loc, next_loc, food_loc, state)) # Moving on aStar-path towards an enemy # This is a stupid feature. #if enemy_loc is None: # f.append(False) #else: # f.append(self.moving_towards_on_astar(world, loc, next_loc, state.paths, enemy_loc, state)) # Moving towards least visited, is true, if direction leads to a square, that is the least # visited of the all possible squares around the ant. other_dirs = AIM.keys() other_dirs.remove(action) if 'halt' in other_dirs: other_dirs.remove('halt') least_visited = True for direction in other_dirs: if state.get_next_visited(loc, action) > state.get_next_visited(loc, direction): least_visited = False f.append(least_visited) # Moving towards closest own hill if own_hill_loc is None: f.append(False) else: f.append(self.moving_towards(world, loc, next_loc, food_loc, state)) # Moving towards closest enemy hill if enemy_hill_loc is None: f.append(False) else: f.append(self.moving_towards(world, loc, next_loc, food_loc, state)) return f