Пример #1
0
 def targeted_attack(self, influence):
     'route my ants in high concentration area to some desired targets'
     # find if we have any high-concentration area (>0.5, maybe?)
     max_val = np.amax(influence)
     if max_val < 0.5:
         return
     max_loc = np.unravel_index(np.argmax(influence), influence.shape)
     
     # find the nearest desirable target (influece < CUTOFF, meaning is either un-explored, or we're fighting)
     min_loc = np.unravel_index(np.argmin(influence), influence.shape)
     # get a route
     route = path.astar(self.gamestate, max_loc, min_loc, length_limit = 20)
     # traverse the route from start, create a linear hill-descending influence map
     
     # back to our starting point
     # set this to attack_map
     # set its target location (task will be reset once tareget location reaches certain level, > 0.5, maybe?
Пример #2
0
 def create_strategic_movement(self, influence_map):
     'route my ants in high concentration area to some desired targets'
     # if our ant concentration is getting low, don't do any task
     max_val = np.amax(influence_map)
     if max_val < 0.5:
         self.strat_map = None
         self.strat_route = None
         return
     # if there is no target 
     #   or our target has been achieved, get new route/strat map
     # otherwise, continue to expand the current one
     get_new_route = True
     existing_route = []
     if self.strat_route is not None:
         existing_route, target_loc = self.strat_route
         if target_loc not in self.gamestate.my_ants():
             get_new_route = False                
     
     if get_new_route:
         debug_logger.debug('create_strategic_movement getting new route: %s' % str(self.gamestate.time_remaining())) 
         start_loc = np.unravel_index(influence_map.argmax(), influence_map.shape)
         initial_inf = -2
                 
         # try to attack enemy hill, if any
         enemy_hills = [hill_loc for hill_loc, owner in self.gamestate.enemy_hills()]
         if len(enemy_hills) > 0:
             target_loc = min(enemy_hills, key=lambda hill_loc: self.gamestate.manhattan_distance(start_loc, hill_loc))
         # TODO: instead, try to occupy closest un-occupied region
         # try to explore closest un-explored region
         else:
             start_region = (start_loc[0] / EXPLORE_GAP, start_loc[1] / EXPLORE_GAP)
             unexplored_regions = [(row, col) for row in range(self.gamestate.region_map.shape[0])
                                             for col in range(self.gamestate.region_map.shape[1])
                                             if self.gamestate.region_map[row,col] == 0]
             if len(unexplored_regions) > 0:
                 region_to_explore = min(unexplored_regions, key = lambda region: self.gamestate.manhattan_distance(start_region, region))
                 target_loc = (region_to_explore[0] * EXPLORE_GAP, region_to_explore[1] * EXPLORE_GAP)
     else:
         debug_logger.debug('create_strategic_movement extend current route: %s' % str(self.gamestate.time_remaining())) 
         start_loc = existing_route[-1]
         initial_inf = self.strat_map[start_loc]
     
     # don't do anything if we've already calculated the full route
     if start_loc == target_loc:
         return 
     
     # get a route
     route = path.astar(self.gamestate, start_loc, target_loc, length_limit = 100)
     # traverse the route from start, create a linear hill-descending influence map
     new_strat_map = np.zeros((self.gamestate.rows, self.gamestate.cols))
     for i in xrange(len(route)):
         cur_val = -i - 2
         new_strat_map[route[i]] = cur_val
         # set un-discovered neighbours to current + 1
         neighbour_not_in_route = [loc for loc in self.gamestate.neighbour_table[route[i]]
                                 if loc not in self.gamestate.water_list]
         for n_loc in neighbour_not_in_route:
             new_strat_map[n_loc] = min(new_strat_map[n_loc], cur_val + 1)
     
     self.strat_route = (route, target_loc)
     self.strat_map = new_strat_map 
     
     debug_logger.debug('turn %s, strategic move created, start=>target = %s => %s' % (str(self.gamestate.current_turn), str(start_loc), str(target_loc)))
     debug_logger.debug(self.strat_route)