def get_next_action(self): build_game_tree(self) if CONF['debug_level'] >= 1: print('Current State: ' + str(self)) for c in self.children: print('Team1 Move: %s,\nTeam2 Move: %s,\nNew State Value: %f,\nNew State {\n%s\n}' % ( Action.to_str(c[0]), Action.to_str(c[1]), c[2].worst_child_val, str(c[2]))) # choose an action: get the worst value from each possible action of the opponent and group # it by move. Then, choose the move with the highest value. worst_actions_group_by_a = [] for a in Action.get_all(): l = [] for c in self.children: if c[0] == a: l.append((c[2].state.value(), c[2].state)) worst_actions_group_by_a.append( min([c[0] for c in l]) if l else -1.0 ) best_action = 0 max_val = worst_actions_group_by_a[0] for i in range(1, len(worst_actions_group_by_a)): if max_val < worst_actions_group_by_a[i]: best_action = i max_val = worst_actions_group_by_a[i] return best_action
def get_next_action(self): build_game_tree(self) if CONF['debug_level'] >= 2: print('Current State: ' + str(self)) for c in self.children: print( 'Team1 Move: %s,\nTeam2 Move: %s,\nNew State Value: %f,\nNew State {\n%s\n}' % (Action.to_str(c[0]), Action.to_str( c[1]), c[2].worst_child_val, str(c[2]))) # choose an action: get the worst value from each possible action of the opponent and group # it by move. Then, choose the move with the highest value. worst_actions_group_by_a = [] for a in Action.get_all(): l = [] for c in self.children: if c[0] == a: l.append((c[2].state.value(), c[2].state)) worst_actions_group_by_a.append( min([c[0] for c in l]) if l else -1.0) best_action = 0 max_val = worst_actions_group_by_a[0] for i in range(1, len(worst_actions_group_by_a)): if max_val < worst_actions_group_by_a[i]: best_action = i max_val = worst_actions_group_by_a[i] return best_action
def get_sucessors(self): # TODO: consider accuracy, crits, status, move effects, etc. when choosing sucessors # TODO: rearrange child order for optimization sucessors = [] #import pdb; pdb.set_trace() for a in Action.get_all(): for b in Action.get_all(): if self.are_actions_valid(a, b): sucessors.append((a, b, self.mutate(a, b))) return sucessors
def random_get_enemy_action(game_tree): valid_actions = [] for a in Action.get_all(): if game_tree.state.is_action_valid(a, game_tree.state.team2): valid_actions.append(a) return random.choice(valid_actions)
get_enemy_action = ai_get_enemy_action elif CONF['enemy_config'] == 'random': get_enemy_action = random_get_enemy_action else: # This is supposed to be a "battle aid" and not a simulator, # so user-control should be the default get_enemy_action = uc_get_enemy_action else: get_enemy_action = uc_get_enemy_action # main loop game_state = PkmnState(ally_team, enemy_team, True) while True: print("Current State: \n%s" % str(game_state)) game_tree = SearchNode(game_state) if game_tree.is_goal() == 1: print('The enemy team wins!') break elif game_tree.is_goal() == -1: print('The ally team wins!') break a = game_tree.get_next_action() print('Best action for Team 1: %s' % Action.to_str(a)) b = get_enemy_action(game_tree) game_state = game_state.mutate(a, b) if CONF['debug_level'] >= 1: print('yay!')