def compute_food_score(self, distance_decay=2.5): """ Compute: distance to every pill, first step for every pill. Out: dict of step options weighed by distance """ # initialise a dict of step options: {next_cell: weight_count} # loop through the list of available pills distances = np.zeros(len(self.player.enemy_food)) i = 0 for p in self.player.enemy_food: # compute the path to the next one path_to_pill = self.player.adjacency.a_star(self.player.current_pos, p) first_step = diff_pos(self.player.current_pos, path_to_pill[-1]) # compute the length for scaling distance = len(path_to_pill) distances[i] = distance weight = np.exp(-distance / distance_decay) # populate the step options dict self.step_options[first_step] += weight i += 1 if self.loop_counter > 3: # print("Stuck in a loop for %d steps" % self.loop_counter) distances_idx = np.argsort(distances) for i in range(min(int((self.loop_counter - 10) / 3), len(distances_idx))): path_to_pill = self.player.adjacency.a_star( self.player.current_pos, self.player.enemy_food[distances_idx[i]] ) first_step = diff_pos(self.player.current_pos, path_to_pill[-1]) # compute the length for scaling distance = len(path_to_pill) weight = np.exp(-distance / distance_decay) # populate the step options dict self.step_options[first_step] -= weight
def get_move(self): # from SmartRandom dangerous_enemy_pos = [bot.current_pos for bot in self.enemy_bots if (bot.is_destroyer and not bot.noisy)] killable_enemy_pos = [bot.current_pos for bot in self.enemy_bots if (bot.is_harvester and not bot.noisy)] # easy kill (kind of tested) for killable in killable_enemy_pos: if killable in self.legal_moves.values(): self.say("Easy kill!") print("Easy kill!") move = diff_pos(self.current_pos, killable) return move # don't die forbidden_moves = [] for dangerous in dangerous_enemy_pos: relative_pos = diff_pos(self.current_pos, dangerous) # check if the destroyer is nearby if relative_pos in ( (0,1), (1,0), (-1,0), (0,-1)): self.say("Enemy nearby!") forbidden_moves.append(relative_pos) if relative_pos in ( (0,2), (2,0), (-2,0), (0,-2)): self.say("Enemy in sight!") rpx, rpy = relative_pos forbidden_moves.append( (rpx//2, rpy//2) ) if relative_pos in ( (1,1), (1,-1), (-1,1), (-1,-1)): self.say("Enemy on diagonal!") rpx, rpy = relative_pos forbidden_moves.append( (0, rpy) ) forbidden_moves.append( (rpx, 0) ) forbidden_absolute_positions = [self.abs_pos(fm) for fm in forbidden_moves] # forbidden_absolute_positions WAS tested (kind of) # it doesn't account for walls (relevant in the second case) # check, if food is still present # if the nearest is not suitable, choose one at random! if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.get_efficient_eater_move() try: move = self.get_efficient_eater_move() # if it's not allowed, take a random move if move in forbidden_moves: # but we are not checking if it's forbidden again! next_pos = self.rnd.choice(list(self.legal_moves.values())) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def get_move(self, player): ''' ''' self.player = player self.player.say("DID %d" % self.player.me.index) if (self.next_food is None) or (self.next_food not in self.player.enemy_food): if not self.player.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop score_diff = self.player.team.score - self.player.enemy_team.score sit_criterion = score_diff > len(self.player.team_food) - 1 if not sit_criterion: index_enemy_to_block = self.get_enemy_to_block() path_en_to_all_food = self.get_closest_food_to_enemy(index_enemy_to_block) intercept = self.get_point_to_intercept(path_en_to_all_food) if len(self.player.adjacency.a_star(self.player.enemy_bots[index_enemy_to_block].current_pos, self.player.current_pos)) < 3 and self.player.me.is_destroyer and not self.player.enemy_bots[index_enemy_to_block].is_destroyer: intercept = self.get_slay_enemy(index_enemy_to_block) else: intercept = self.get_sit_on_food() self.player.memory.store((self.player._index, 'sit'), True) try: next_pos = self.goto_pos(intercept) move = diff_pos(self.player.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def attack_move(self): self.adjacency = AdjacencyList(self.current_uni.free_positions()) attackpath = [] if self.tracking_idx is not None: # if the enemy is no longer in our zone if not self.team.in_zone(self.tracking_target.current_pos): self.tracking_idx = None return self.go_for_food() # otherwise update the path to the target else: attackpath = self.path_to_target if self.tracking_idx is None: # check the enemy positions possible_targets = [enemy for enemy in self.enemy_bots if self.team.in_zone(enemy.current_pos)] if possible_targets: # get the path to the closest one try: possible_paths = [(enemy, self.adjacency.a_star(self.current_pos, enemy.current_pos)) for enemy in possible_targets] except NoPathException: return None else: return None if possible_paths: closest_enemy, path = min(possible_paths, key=lambda enemy_path: len(enemy_path[1])) self.tracking_idx = closest_enemy.index if len(attackpath)==0: return self.random_move() if len(attackpath)>0 and self.round_index%20==0: return self.random_move() return diff_pos(self.current_pos, attackpath.pop())
def get_move(self): border_path = self.find_path(self.team_border) self.say("Border!!!!") if len(border_path)==0: return stop if border_path==None: return stop return diff_pos(self.current_pos, border_path.pop())
def get_move(self): # check, if food is still present, otherwise go somewhere else if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop self.say("I am hungry.") return stop # all the food is in self.enemy_food # we just pick one to go to # (of course, there may be a smarter choice than just going random) self.next_food = self.rnd.choice(self.enemy_food) shortest_len = 9999 choosen_pos = None for pos in self.enemy_food: path_len = len(self.path_to(pos)) if (path_len < shortest_len and self.enemies_around(pos) is False) : shortest_len = path_len choosen_pos = pos self.next_food = choosen_pos try: # figure out the path to take shortest_path = self.path_to(self.next_food) # our next position is the last element in the path next_pos = shortest_path[-1] # we are a little exited about eating # (this does not account for any food we additionally eat on our way # to the food we have picked.) if len(shortest_path) == 1: self.say("Yay. Food NEXT.") else: self.say("Eating in {0} STEPS.".format(len(shortest_path))) # should we check for the enemy at this position? # self.enemy_bots ? # Naah – we risk it :) # the difference between here and there # is the direction we need to go to move = diff_pos(self.current_pos, next_pos) return move except NoPathException: # whoops, there is no path possible # we better wait return stop
def check_easykill(self): """Returns a move if right next to a killable enemy.""" killable_enemy_pos = [bot.current_pos for bot in self.enemy_bots if (bot.is_harvester and not bot.noisy)] # easy kill (kind of tested) for killable in killable_enemy_pos: if killable in self.legal_moves.values(): move = graph.diff_pos(self.current_pos, killable) return move
def go_for_food(self): food_path = self.find_path(self.enemy_food) mandist = manhattan_dist(self.me.current_pos, self.partner.me.current_pos) if mandist <=3 and self.round_index<100: distlst = [x for x in self.enemy_food if manhattan_dist(x, self.partner.me.current_pos)>5] food_path = self.find_path(distlst) if food_path==None: return self.random_move() if len(food_path)==0: return self.random_move() return diff_pos(self.current_pos, food_path.pop())
def get_move(self): # from SmartRandom dangerous_enemy_pos = [bot.current_pos for bot in self.enemy_bots if bot.is_destroyer] killable_enemy_pos = [bot.current_pos for bot in self.enemy_bots if bot.is_harvester] # easy kill (please test) for killable in killable_enemy_pos: if killable in self.legal_moves.items(): move = diff_pos(self.current_pos, killable) self.say("Easy kill!") return move #pdb.set_trace() # panic # for dangerous in dangerous_enemy_pos: # if killable in self.legal_moves.items(): # move = diff_pos(self.current_pos, killable) # self.say("Easy kill!") # return move # check, if food is still present # if the nearest is not suitable, choose one at random! if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop # SUBOPTIMAL (chooses at random) self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def compute_food_score(self, distance_decay=2.5): """ Compute: distance to every pill, first step for every pill. Out: dict of step options weighed by distance """ #initialise a dict of step options: {next_cell: weight_count} # loop through the list of available pills distances = np.zeros(len(self.player.enemy_food)) i = 0 for p in self.player.enemy_food: # compute the path to the next one path_to_pill = self.player.adjacency.a_star( self.player.current_pos, p) first_step = diff_pos(self.player.current_pos, path_to_pill[-1]) # compute the length for scaling distance = len(path_to_pill) distances[i] = distance weight = np.exp(-distance / distance_decay) # populate the step options dict self.step_options[first_step] += weight i += 1 if self.loop_counter > 3: #print("Stuck in a loop for %d steps" % self.loop_counter) distances_idx = np.argsort(distances) for i in range( min(int((self.loop_counter - 10) / 3), len(distances_idx))): path_to_pill = \ self.player.adjacency.a_star(self.player.current_pos, self.player.enemy_food[distances_idx[i]]) first_step = diff_pos(self.player.current_pos, path_to_pill[-1]) # compute the length for scaling distance = len(path_to_pill) weight = np.exp(-distance / distance_decay) # populate the step options dict self.step_options[first_step] -= weight
def get_move(self): # check, if food is still present if self.next_food is None or self.next_food not in self.enemy_food: if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def go_for_border(self): if (self.me.index==0 or self.me.index==1) and self.border_mode: bor_u = [x for x in self.team_border if x[1]>x[0]//2 ] border_path = self.find_path(bor_u) elif (self.me.index==2 or self.me.index==3) and self.border_mode: bor_d = [x for x in self.team_border if x[1]<=x[0]//2] border_path = self.find_path(bor_d) else: border_path = self.find_path(self.team_border) if len(border_path)==0: return stop if border_path==None: return stop return diff_pos(self.current_pos, border_path.pop())
def get_move(self): # check, if food is still present, otherwise go somewhere else if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop self.say("I am hungry.") return stop # all the food is in self.enemy_food # we just pick one to go to # (of course, there may be a smarter choice than just going random) min_dist = 30. food_pos = self.enemy_food[0] for food in self.enemy_food: dist = sqrt((food[0]-self.current_pos[0])**2+(food[1]-self.current_pos[1])**2) dist_bot1 = sqrt((food[0]-self.enemy_bots[0].current_pos[0])**2+(food[1]-self.enemy_bots[0].current_pos[1])**2) dist_bot2 = sqrt((food[0]-self.enemy_bots[1].current_pos[0])**2+(food[1]-self.enemy_bots[1].current_pos[1])**2) if dist < min_dist and dist_bot1>2 and dist_bot2>2: min_dist = dist food_pos = food self.next_food = food_pos try: # figure out the path to take shortest_path = self.path_to(self.next_food) # our next position is the last element in the path next_pos = shortest_path[-1] # we are a little exited about eating # (this does not account for any food we additionally eat on our way # to the food we have picked.) if len(shortest_path) == 1: self.say("Yay. Food next.") else: self.say("Eating in {0} steps.".format(len(shortest_path))) # should we check for the enemy at this position? # self.enemy_bots ? # Naah – we risk it :) # the difference between here and there # is the direction we need to go to move = diff_pos(self.current_pos, next_pos) return move except NoPathException: # whoops, there is no path possible # we better wait return stop
def compute_food_score(self, distance_decay=2.5): """ Compute: distance to every pill, first step for every pill. Out: dict of step options weighed by distance """ #initialise a dict of step options: {next_cell: weight_count} # loop through the list of available pills for p in self.enemy_food: # compute the path to the next one path_to_pill = self.adjacency.a_star(self.current_pos, p) first_step = diff_pos(self.current_pos, path_to_pill[-1]) # compute the length for scaling weight = np.exp(-len(path_to_pill)/distance_decay) # populate the step options dict self.step_options[first_step]+=weight
def compute_food_score(self, distance_decay=2.5): """ Compute: distance to every pill, first step for every pill. Out: dict of step options weighed by distance """ # initialise a dict of step options: {next_cell: weight_count} # loop through the list of available pills for p in self.enemy_food: # compute the path to the next one path_to_pill = self.adjacency.a_star(self.current_pos, p) first_step = diff_pos(self.current_pos, path_to_pill[-1]) # compute the length for scaling weight = np.exp(-len(path_to_pill) / distance_decay) # populate the step options dict self.step_options[first_step] += weight
def get_move(self): # check if food we wanted to pick is still present if self.next_food is None or self.next_food not in self.enemy_food: if not self.enemy_food: # all food has been eaten? ok, game is over and i’ll stop return datamodel.stop # Then make a choice and stick to it self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: # This is case something unexpected happens return datamodel.stop
def get_move(self): # check, if food is still present, otherwise go somewhere else if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop self.say("I am hungry.") return stop # all the food is in self.enemy_food # we just pick one to go to # (of course, there may be a smarter choice than just going random) self.next_food = self.rnd.choice(self.enemy_food) try: # figure out the path to take shortest_path = self.path_to(self.next_food) # our next position is the last element in the path next_pos = shortest_path[-1] # we are a little exited about eating # (this does not account for any food we additionally eat on our way # to the food we have picked.) if len(shortest_path) == 1: self.say("Yay. Food next.") else: self.say("Eating in {0} steps.".format(len(shortest_path))) # should we check for the enemy at this position? # self.enemy_bots ? # Naah – we risk it :) # the difference between here and there # is the direction we need to go to move = diff_pos(self.current_pos, next_pos) return move except NoPathException: # whoops, there is no path possible # we better wait return stop
def get_move(self): # check, if food is still present self.say("Go Away!!") if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.get_enemy_to_block() self.get_closest_food() self.get_food_to_protect() self.get_enemy_path() self.get_point_to_intercept() try: next_pos = self.goto_pos(self.p_intercept) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: print("Help!") return datamodel.stop
def get_move(self, player): ''' ''' self.player = player self.player.say("DID %d" % self.player.me.index) if (self.next_food is None) or (self.next_food not in self.player.enemy_food): if not self.player.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop score_diff = self.player.team.score - self.player.enemy_team.score sit_criterion = score_diff > len(self.player.team_food) - 1 if not sit_criterion: index_enemy_to_block = self.get_enemy_to_block() path_en_to_all_food = self.get_closest_food_to_enemy( index_enemy_to_block) intercept = self.get_point_to_intercept(path_en_to_all_food) if len( self.player.adjacency.a_star( self.player.enemy_bots[index_enemy_to_block]. current_pos, self.player.current_pos) ) < 3 and self.player.me.is_destroyer and not self.player.enemy_bots[ index_enemy_to_block].is_destroyer: intercept = self.get_slay_enemy(index_enemy_to_block) else: intercept = self.get_sit_on_food() self.player.memory.store((self.player._index, 'sit'), True) try: next_pos = self.goto_pos(intercept) move = diff_pos(self.player.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: dangerous_enemy_pos = [ bot.current_pos for bot in self.enemy_bots if bot.is_destroyer ] next_pos = self.goto_pos(self.next_food) # check, if the next_pos has an enemy on it if next_pos in dangerous_enemy_pos: # whoops, better wait this round and take another food next time self.next_food = None return datamodel.stop move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: dangerous_enemy_pos = [bot.current_pos for bot in self.enemy_bots if bot.is_destroyer] next_pos = self.goto_pos(self.next_food) # check, if the next_pos has an enemy on it if next_pos in dangerous_enemy_pos: # whoops, better wait this round and take another food next time self.next_food = None return datamodel.stop move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) # !!! to improve: not random from all enemy_food by from closest food # determine enemy positions dangerous & killable dangerous_enemy_pos = [bot.current_pos for bot in self.enemy_bots if bot.is_destroyer] non_noisy_dangerous_enemy_pos = [bot.current_pos for bot in self.enemy_bots if (bot.is_destroyer and not bot.noisy)] # killable_enemy_pos = [bot.current_pos for bot in self.enemy_bots if bot.is_harvester] try: next_pos = self.goto_pos(self.next_food) # next_pos = self.rnd.choice([(0,1),(0,-1),(1,0),(-1,0)]) move = diff_pos(self.current_pos, next_pos) my_adjecent_pos = self.adjacency.pos_within(self.current_pos,5) legal_moves = self.legal_moves # check if the next position is dangerous # list of dangerous enemy adecent positions # dangerous_enemy_adj_pos = [] acceptable_adjecent_pos = list(my_adjecent_pos) # for position in dangerous_enemy_pos: for position in non_noisy_dangerous_enemy_pos: dangerous_enemy_adj_pos = self.adjacency.pos_within(position,3) for enemy_adj_pos in dangerous_enemy_adj_pos: if enemy_adj_pos in acceptable_adjecent_pos: acceptable_adjecent_pos.remove(enemy_adj_pos) # TODO: improve to -> escape to the direction oposite from the enemy if len(acceptable_adjecent_pos) == 0: return self.rnd.choice(list(legal_moves.keys())) if next_pos not in my_adjecent_pos: next_pos = self.rnd.choice(list(my_adjecent_pos)) move = diff_pos(self.current_pos, next_pos) # Remove stop # try: # del legal_moves[datamodel.stop] # except KeyError: # pass # # now remove the move that would lead to the enemy # # unless there is no where else to go. # if len(legal_moves) > 1: # for (k,v) in legal_moves.items(): # if v in dangerous_enemy_pos: # break # del legal_moves[k] # # just in case, there is really no way to go to: # if not legal_moves: # return datamodel.stop # # and select a move at random # return self.rnd.choice(list(legal_moves.keys())) # selecting one of the moves # while next_pos in dangerous_enemy_pos: # move = self.rnd.choice(possible_moves) # next_pos = (self.current_pos[0] + move[0],self.current_pos[1] + move[1]) self.say("bla bla!") return move except NoPathException: return datamodel.stop
def test_diff_pos(self): assert north == diff_pos((1, 1), (1, 0)) assert south == diff_pos((1, 1), (1, 2)) assert east == diff_pos((1, 1), (2, 1)) assert west == diff_pos((1, 1), (0, 1)) assert stop == diff_pos((1, 1), (1, 1))
def test_diff_pos(self): self.assertEqual(north, diff_pos((1, 1), (1, 0))) self.assertEqual(south, diff_pos((1, 1), (1, 2))) self.assertEqual(east, diff_pos((1, 1), (2, 1))) self.assertEqual(west, diff_pos((1, 1), (0, 1))) self.assertEqual(stop, diff_pos((1, 1), (1, 1)))
def test_diff_pos_arbitrary(self): vectors = [(0, 0), (0, 1), (-1, 1), (-2, 3)] orig = (1, 1) for vec in vectors: new = move_pos(orig, vec) self.assertEqual(vec, diff_pos(orig, new))
def test_diff_pos_arbitrary(self): vectors = [(0, 0), (0, 1), (-1, 1), (-2, 3)] orig = (1, 1) for vec in vectors: new = new_pos(orig, vec) self.assertEqual(vec, diff_pos(orig, new))
def test_diff_pos_arbitrary(self): vectors = [(0, 0), (0, 1), (-1, 1), (-2, 3)] orig = (1, 1) for vec in vectors: new = move_pos(orig, vec) assert vec == diff_pos(orig, new)