def act(self, game): all_locs = {(x, y) for x in xrange(19) for y in xrange(19)} spawn = {loc for loc in all_locs if 'spawn' in rg.loc_types(loc)} obstacle = {loc for loc in all_locs if 'obstacle' in rg.loc_types(loc)} team = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} enemy = set(game.robots) - team adjacent = set(rg.locs_around(self.location)) - obstacle adjacent_enemy = adjacent & enemy adjacent_enemy2 = {loc for loc in adjacent if (set(rg.locs_around(loc)) & enemy)} - team safe = adjacent - adjacent_enemy - adjacent_enemy2 - spawn - team def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) if enemy: closest_enemy = mindist(enemy, self.location) else closest_enemy = rg.CENTER_POINT move = ['guard'] if self.location in spawn: if safe: move = ['move', mindist(safe, rg.CENTER_POINT)] elif adjacent_enemy: if 9 * len(adjacent_enemy) >= self.hp: if safe: move = ['move', mindist(safe, rg.CENTER_POINT)] else: move = ['attack', adjacent_enemy.pop()] elif adjacent_enemy2: move = ['attack', adjacent_enemy2.pop()] elif safe: move = ['move', mindist(safe, closest_enemy)]
def locs_around_55(loc): xc,yc = loc for x in xrange(xc-2,xc+3): for y in xrange(yc-2,yc+3): typ = rg.loc_types((x,y)) if "invalid" not in typ and "obstacle" not in typ: yield (x,y)
def sense_environment(self, game): xc, yc = self.location features = np.zeros((Robot.inputs,1)) i = 0 for x in xrange(xc - Robot.halfwidth, xc + Robot.halfwidth + 1): for y in xrange(yc - Robot.halfwidth, yc + Robot.halfwidth + 1): bot = game.robots.get((x,y)) if bot == None: features[(i, 0)] = 0.0 features[(i+1, 0)] = 0.0 else: features[(i, 0)] = bot['hp'] / float(rg.settings.robot_hp) if bot['player_id'] == self.player_id else 0.0 features[(i+1, 0)] = bot['hp'] / float(rg.settings.robot_hp) if bot['player_id'] != self.player_id else 0.0 features[(i+2, 0)] = 1.0 if 'spawn' in rg.loc_types((x,y)) else 0.0 features[(i+3, 0)] = 1.0 if 'obstacle' in rg.loc_types((x,y)) else 0.0 i += 4 return features
def genSafePlace(self): #, r=True): ######################################################################### #Metoda losuje pozycję do 'obrony', do której roboty będą się poruszały; ######################################################################### while True: values.safeSpace = (randint(X['min'], X['max']), randint(Y['min'], Y['max'])) if 'normal' in rg.loc_types(values.safeSpace): break
def __init__(self): while True: x = random.randint(*self.RALLY_RANGE) y = random.randint(*self.RALLY_RANGE) locations = rg.loc_types((x, y)) target = set(['normal']) if locations != target: continue else: self.RALLY_POINT = x, y break
def _spawn_test(location): """test input location is at spawn place or not Args: location (tuple): location of the map Returns: bool: True if the place is a spawnplace False if not """ spot = rg.loc_types(location) if ("spawn") in spot: return True else: return False
def test_types_spawn(self): self.assertEqual(rg.loc_types((3, 4)), set(['normal', 'spawn']))
def test_types_invalid(self): self.assertEqual(rg.loc_types((-1, 9)), set(['invalid']))
def evaluate(self, game, robot, ally_fut, attack_fut): """Recursive evaluation of this Node and it's children given game state.""" result = False dir_result = self.direction # First handle this node's expression if self.type == "SPAWN": if self.comp_op == "LT" and self.turns_since_spawn > ( game['turn'] % 10): result = True elif self.comp_op == "GT" and self.turns_since_spawn < ( game['turn'] % 10): result = True elif self.type == "HP": if self.comp_op == "LT" and robot.hp < self.hp: result = True elif self.comp_op == "GT" and robot.hp > self.hp: result = True elif self.type == "RLOC": true_loc = (robot.location[0] + self.rloc[0], robot.location[1] + self.rloc[1]) bots = game.get('robots') check_hp = False hp_to_check = 0 if self.rloc_type == "ENEMY": if true_loc in bots.keys(): bot = bots[true_loc] if bot.player_id != robot.player_id: check_hp = True hp_to_check = bot.hp elif self.rloc_type == "SPAWN": if 'spawn' in rg.loc_types(true_loc): result = True elif self.rloc_type == "INVALID": if any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): result = True elif self.rloc_type == "EMPTY": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): if true_loc not in bots.keys(): result = True elif self.rloc_type == "ALLY": if true_loc in bots.keys(): bot = bots[true_loc] if bot.player_id == robot.player_id: check_hp = True hp_to_check = bot.hp elif self.rloc_type == "ALLY_FUT": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): hp_to_check = ally_fut[true_loc[0]][true_loc[1]] if hp_to_check > 0: check_hp = True elif self.rloc_type == "ATT_FUT": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): result = (attack_fut[true_loc[0]][true_loc[1]] > 0) if check_hp: if self.comp_op == "LT" and hp_to_check < self.hp: result = True elif self.comp_op == "GT" and hp_to_check > self.hp: result = True if self.not_op: result = not result # Now check children Nodes # Precedence of True evalutations: # True OR child > True AND children + True self > No AND children + True self # Direction is set by the "lowest in the tree" True node of the highest # precedence condition. For example, a True node with all True AND # children and two True OR children will use the direction of the first True # OR child. Assuming non-contradictory Node expressions, this should allow # every Node to determine the direction in at least one game state. and_still_possible = True for child in self.children: ch_result = child.evaluate(game, robot, ally_fut, attack_fut) if result and (child.op == "AND"): and_still_possible = (and_still_possible and ch_result[0]) dir_result = ch_result[1] if child.op == "OR" and ch_result[0]: return ch_result return ((result and and_still_possible), dir_result)
def evaluate(self, game, robot, ally_fut, attack_fut): """Recursive evaluation of this Node and it's children given game state.""" result = False dir_result = self.direction # First handle this node's expression if self.type == "SPAWN": if self.comp_op == "LT" and self.turns_since_spawn > (game['turn'] % 10): result = True elif self.comp_op == "GT" and self.turns_since_spawn < (game['turn'] % 10): result = True elif self.type == "HP": if self.comp_op == "LT" and robot.hp < self.hp: result = True elif self.comp_op == "GT" and robot.hp > self.hp: result = True elif self.type == "RLOC": true_loc = (robot.location[0] + self.rloc[0], robot.location[1] + self.rloc[1]) bots = game.get('robots') check_hp = False hp_to_check = 0 if self.rloc_type == "ENEMY": if true_loc in bots.keys(): bot = bots[true_loc] if bot.player_id != robot.player_id: check_hp = True hp_to_check = bot.hp elif self.rloc_type == "SPAWN": if 'spawn' in rg.loc_types(true_loc): result = True elif self.rloc_type == "INVALID": if any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): result = True elif self.rloc_type == "EMPTY": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): if true_loc not in bots.keys(): result = True elif self.rloc_type == "ALLY": if true_loc in bots.keys(): bot = bots[true_loc] if bot.player_id == robot.player_id: check_hp = True hp_to_check = bot.hp elif self.rloc_type == "ALLY_FUT": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): hp_to_check = ally_fut[true_loc[0]][true_loc[1]] if hp_to_check > 0: check_hp = True elif self.rloc_type == "ATT_FUT": if not any(x in rg.loc_types(true_loc) for x in ['obstacle', 'invalid']): result = (attack_fut[true_loc[0]][true_loc[1]] > 0) if check_hp: if self.comp_op == "LT" and hp_to_check < self.hp: result = True elif self.comp_op == "GT" and hp_to_check > self.hp: result = True if self.not_op: result = not result # Now check children Nodes # Precedence of True evalutations: # True OR child > True AND children + True self > No AND children + True self # Direction is set by the "lowest in the tree" True node of the highest # precedence condition. For example, a True node with all True AND # children and two True OR children will use the direction of the first True # OR child. Assuming non-contradictory Node expressions, this should allow # every Node to determine the direction in at least one game state. and_still_possible = True for child in self.children: ch_result = child.evaluate(game, robot, ally_fut, attack_fut) if result and (child.op == "AND"): and_still_possible = (and_still_possible and ch_result[0]) dir_result = ch_result[1] if child.op == "OR" and ch_result[0]: return ch_result return ((result and and_still_possible), dir_result)
def test_types_normal(self): self.assertEqual(rg.loc_types((7, 2)), set(['normal']))
def test_types_obstacle(self): self.assertEqual(rg.loc_types((16, 4)), set(['normal', 'obstacle']))
def action_is_valid(act): if len(act) > 1 and ("invalid" in rg.loc_types(act[1]) or "obstacle" in rg.loc_types(act[1])): return False return True