def train_model(model, n_battles=10000, mode='vs_self', eps=0.5, decay=0.9998): scores = [] for i in range(n_battles): actions = [] op_actions = [] state = np.zeros((1, 6 * n_heroes)) op_state = np.zeros((1, 6 * n_heroes)) eps *= decay if i % 100 == 0: print("Battle {} of {}".format(i + 1, n_battles)) for turn in range(6): a = get_next_hero(model, eps, state, actions) op_a = get_next_hero(model, eps, op_state, op_actions) actions.append(a) op_actions.append(op_a) if turn < 5: new_state = copy.copy(state) new_state[0][n_heroes * turn + a] = 1 new_op_state = copy.copy(op_state) new_op_state[0][n_heroes * turn + op_a] = 1 target = np.max(np.delete(model.predict(new_state)[0], actions)) op_target = np.max(np.delete(model.predict(new_op_state)[0], op_actions)) else: team = Team([heroes[a]() for a in actions]) if mode == 'vs_self': op_team = Team([heroes[op_a]() for op_a in op_actions]) elif mode == 'vs_random': op_team = get_random_team() else: raise Warning('Unknown training mode') game = Game(team, op_team) game.process() target = (game.winner + 1) / 2 op_target = 1 - target target_vec = model.predict(state)[0] target_vec[actions[-1]] = target model.fit(state, target_vec.reshape((1, n_heroes)), epochs=1, verbose=0) if mode == 'vs_self': op_target_vec = model.predict(op_state)[0] op_target_vec[op_actions[-1]] = op_target model.fit(op_state, op_target_vec.reshape((1, n_heroes)), epochs=1, verbose=0) state = new_state op_state = new_op_state scores.append(sum([ranks[a] for a in actions + op_actions])) return scores
def gauntlet_from_sample(sample, length, from_hero=False, hero=None, pos=None, rune=None, artifact=None, player=True, totg_max=2): gauntlet = [] for sub in sample[:length]: totg_left = totg_max heroes_left = len(sub) team_heroes = [] if from_hero: if artifact is None: artifact = None if hero.name.value in no_totg else Artifact.tears_of_the_goddess.O6 for h in sub[:pos - 1]: new_h, totg_left, heroes_left = get_new_hero( h, totg_left=totg_left, heroes_left=heroes_left) team_heroes.append(new_h) if rune is None and artifact is None: team_heroes.append(hero()) elif rune is None: team_heroes.append(hero(artifact=artifact)) elif artifact is None: team_heroes.append(hero(rune=rune)) else: team_heroes.append(hero(rune=rune, artifact=artifact)) for h in sub[pos - 1:]: new_h, totg_left, heroes_left = get_new_hero( h, totg_left=totg_left, heroes_left=heroes_left) team_heroes.append(new_h) else: for h in sub: new_h, totg_left, heroes_left = get_new_hero( h, totg_left=totg_left, heroes_left=heroes_left) team_heroes.append(new_h) if player: team = Team(team_heroes, cancel_aura=True) else: team = Team(team_heroes, pet=Familiar.empty(), cancel_aura=True) gauntlet.append(team) return gauntlet
def get_prediction(model): actions = [] state = np.zeros((1, 6 * n_heroes)) for turn in range(6): scores = model.predict(state)[0] scores[actions] = 0 a = np.argmax(scores) actions.append(a) state[0][n_heroes * turn + a] = 1 return Team([heroes[a]() for a in actions])
async def on_message(message): # we do not want the bot to reply to itself if message.author == client.user: return if message.content.startswith('!sym'): #Breaks the chat message into an array (with !sym as first value, that's easily solvable so i'll sort later) teamArray = message.content.split() print(teamArray) #Builds array for battle, needs to pull values from chat message team_1 = Team([ getattr(Hero, teamArray[1]), Hero.valkyrie(), Hero.lindberg(), Hero.shudde_m_ell(), Hero.drow(), Hero.freya() ]) team_2 = Team([ Hero.ultima(), Hero.valkyrie(), Hero.lindberg(), Hero.shudde_m_ell(), Hero.drow(), Hero.freya() ]) sim = GameSim(team_1, team_2, n_sim=100) #Runs simulation sim.process() #Sends results in the chat where request was made await client.send_message(message.channel, '**TEAM #1**') await client.send_message(message.channel, team_1.comp()) await client.send_message(message.channel, '**TEAM #2**') await client.send_message(message.channel, team_2.comp()) await client.send_message(message.channel, '==================') await sim.print_winrate(client, message) await client.send_message(message.channel, '==================')
async def on_message(message): rawHeroes = ['abyss_lord', 'aden', 'blood_tooth', 'centaur', 'chessia', 'dettlaff', 'drow', 'dziewona', 'freya', 'gerald', 'grand', 'hester', 'lexar', 'lindberg', 'luna', 'mars', 'martin', 'medusa', 'megaw', 'minotaur', 'monkey_king', 'mulan', 'nameless_king', 'phoenix', 'orphee', 'reaper', 'ripper', 'rlyeh', 'samurai', 'saw_machine', 'scarlet', 'skuld', 'wolnir', 'xexanoth'] # we do not want the bot to reply to itself if message.author == client.user: return if message.content.startswith('!sim heroes'): heroes = '\n'.join('{}.{}'.format(index, hero) for index, hero in enumerate(rawHeroes)) await client.send_message(message.channel, '```'+heroes+'```') if message.content.startswith('!sim battle'): #Breaks the chat message into an array (with !sym as first value, that's easily solvable so i'll sort later) teams = message.content.replace("!sim battle", "").replace(',', ' ').replace(', ', '').split('][') print(teams) if teams[0] != '': rawTeam_1 = teams[0].replace('[','').replace(']','').split() #Builds array for battle, needs to pull values from chat message team_1 = Team([getattr(Hero, rawHeroes[int(hero_string)])() for hero_string in rawTeam_1]) else: #Fallback to default team if no parameters are specified team_1 = Team([Hero.wolnir(), Hero.valkyrie(), Hero.lindberg(artifact=Artifact.tears_of_the_goddess.O6), Hero.skuld(artifact=Artifact.tears_of_the_goddess.O6), Hero.mars(artifact=Artifact.tears_of_the_goddess.O6), Hero.skuld()]) try: rawTeam_2 = teams[1].replace('[','').replace(']','').split() #Builds array for battle, needs to pull values from chat message team_2 = Team([getattr(Hero, rawHeroes[int(hero_string)])() for hero_string in rawTeam_2]) except IndexError: #Fallback to default team if no parameters are specified team_2 = Team([Hero.phoenix(), Hero.skuld(artifact=Artifact.tears_of_the_goddess.O6), Hero.skuld(), Hero.drow(artifact=Artifact.tears_of_the_goddess.O6), Hero.monkey_king(artifact=Artifact.tears_of_the_goddess.O6), Hero.shudde_m_ell()]) try: simNumber=int(teams[2].replace('[','').replace(']','')) if simNumber >= 1000: simNumber = 1000 except IndexError: simNumber=500 print(team_1) print(team_2) print(simNumber) sim = GameSim(team_1, team_2, n_sim=1000) #Runs simulation sim.process() team1Comp = team_1.comp() team2Comp = team_2.comp() #Sends results in the chat where request was made await client.send_message(message.channel, '**TEAM #1**'+team1Comp+'\n\n**TEAM #2**'+team2Comp+'\n') await client.send_message(message.channel, '==================') await sim.print_winrate(client, message) await client.send_message(message.channel, '==================')
def get_random_team(): return Team([rd.choice(heroes)() for _ in range(6)])
from sim.heroes import Team, DummyTeam, Hero from sim.models import Armor, Helmet, Weapon, Pendant, Rune, Artifact, Familiar from sim.processing import GameSim, GauntletAttackSim, GauntletDefenseSim, GauntletSim, Game from sim.tests import friend_boss_test, main_friend_boss_test, master_friend_boss_test, guild_boss_test, \ main_guild_boss_test, master_guild_boss_test, trial_test, main_trial_test, pvp_test, sim_setup # List heroes heroes = [Hero.__dict__[key] for key in Hero.__dict__ if '__' not in key and 'empty' not in key] # Create teams team_1 = Team([Hero.wolnir(), Hero.valkyrie(), Hero.lindberg(), Hero.skuld(artifact=Artifact.tears_of_the_goddess.O6), Hero.mars(artifact=Artifact.tears_of_the_goddess.O6), Hero.skuld()]) print('This is team_1 :', team_1.comp()) team_2 = Team([Hero.phoenix(), Hero.skuld(), Hero.skuld(), Hero.drow(artifact=Artifact.tears_of_the_goddess.O6), Hero.monkey_king(artifact=Artifact.tears_of_the_goddess.O6), Hero.shudde_m_ell()]) print('\nThis is team_2 :', team_2.comp()) # Do simulations game = Game(team_1, team_2, verbose_full=True) game.process() print(game.log.text)
def battle_simulate_post(simulate_request=None): # noqa: E501 """Simulates a given number of battles # noqa: E501 :param count: number of battles to be simulated :type count: dict | bytes :rtype: None """ if connexion.request.is_json: simulate_request = SimulateRequest.from_dict( connexion.request.get_json()) # noqa: E501 #attacker attack_player = simulate_request.attacker.player attack_heroes = [ get_hero_from_request(simulate_request.attacker.hero_front1, attack_player), get_hero_from_request(simulate_request.attacker.hero_front2, attack_player), get_hero_from_request(simulate_request.attacker.hero_front3, attack_player), get_hero_from_request(simulate_request.attacker.hero_rear1, attack_player), get_hero_from_request(simulate_request.attacker.hero_rear2, attack_player), get_hero_from_request(simulate_request.attacker.hero_rear3, attack_player) ] attack_familiar_request = simulate_request.attacker.player.active_pet attack_familiar = familiar_from_request[attack_familiar_request.id]( attack_familiar_request.level, attack_familiar_request.skill1level, attack_familiar_request.skill2level, attack_familiar_request.skill3level, attack_familiar_request.skill4level) attack_team = Team(attack_heroes, attack_familiar) #defender defense_player = simulate_request.defender.player defense_heroes = [ get_hero_from_request(simulate_request.defender.hero_front1, defense_player), get_hero_from_request(simulate_request.defender.hero_front2, defense_player), get_hero_from_request(simulate_request.defender.hero_front3, defense_player), get_hero_from_request(simulate_request.defender.hero_rear1, defense_player), get_hero_from_request(simulate_request.defender.hero_rear2, defense_player), get_hero_from_request(simulate_request.defender.hero_rear3, defense_player) ] defense_familiar_request = simulate_request.defender.player.active_pet defense_familiar = familiar_from_request[defense_familiar_request.id]( defense_familiar_request.level, defense_familiar_request.skill1level, defense_familiar_request.skill2level, defense_familiar_request.skill3level, defense_familiar_request.skill4level) defense_team = Team(defense_heroes, defense_familiar) game = Game(attack_team, defense_team) game.process() return game.log.text return 'empty request'