def get_moves(poke, known_moves, graph, data, alpha=1.0): poke = correct_mega(poke) co = graph['cooccurences'] freq = graph['frequencies'] probs = {} if len(known_moves) == 0: probs = get_freqs(poke, freq) else: for move in known_moves: if move not in co[poke]: continue total = float(sum(co[poke][move].values())) for othermove in co[poke][move]: if othermove in MOVE_CORRECTIONS: probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] del probs[move] if othermove in known_moves: continue prob = co[poke][move][othermove] / total if othermove not in probs: probs[othermove] = 1 probs[othermove] *= prob if probs == {}: probs = get_freqs(poke, freq) return sorted(probs.items(), key=lambda x: -x[1])
def __init__(self, poke, pokedata): super(RandomMovePredictor, self).__init__(poke, pokedata) poke_moves = self.pokedata.poke_moves[correct_mega( correct_name(self.poke))] random.shuffle(poke_moves) prob = 1.0 / len(poke_moves) self.predictions = [(x, prob) for x in poke_moves]
def __init__(self, poke, pokedata): super(MoveCoPredictor, self).__init__(poke, pokedata) graph_move = self.pokedata.graph_move self.poke_moves = self.pokedata.poke_moves[correct_mega( correct_name(self.poke))] self.co = graph_move['cooccurences'] self.freq = graph_move['frequencies']
def get_freqs(self, freq): poke = correct_name(self.poke) poke = correct_mega(poke) probs = {} for move in freq[poke]: prob = freq[poke][move] probs[move] = prob return probs
def get_moves(self, known_moves): poke = correct_name(self.poke) poke = correct_mega(poke) probs = {} if len(known_moves) == 0: probs = self.get_freqs(self.freq) else: for move in self.co[poke]: if move in known_moves: continue prob = 1.0 for othermove in known_moves: if othermove not in self.co[poke][move]: prob *= 0 continue prob *= self.co[poke][move][othermove] if move in MOVE_CORRECTIONS: probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] del probs[move] prob *= self.freq[poke][move] probs[move] = prob #else: #for move in known_moves: #if move not in self.co[poke]: #continue #for othermove in self.co[poke][move]: #if othermove in MOVE_CORRECTIONS: #probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] #del probs[move] #if othermove in known_moves: #continue #prob = self.co[poke][move][othermove] #if othermove not in probs: #probs[othermove] = 1 #probs[othermove] *= prob if probs == {}: probs = self.get_freqs(poke, self.freq) self.predictions = sorted(probs.items(), key=lambda x: -x[1]) return self.predictions
def get_moves(self, known_moves): poke = correct_name(self.poke) poke = correct_mega(poke) probs = {} if len(known_moves) == 0: probs = self.get_freqs(self.freq) else: for move in self.co[poke]: if move in known_moves: continue prob = 1.0 for othermove in known_moves: if othermove not in self.co[poke][move]: prob *= 0 continue prob *= self.co[poke][move][othermove] if move in MOVE_CORRECTIONS: probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] del probs[move] prob *= self.freq[poke][move] probs[move] = prob #else: #for move in known_moves: #if move not in self.co[poke]: #continue #for othermove in self.co[poke][move]: #if othermove in MOVE_CORRECTIONS: #probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] #del probs[move] #if othermove in known_moves: #continue #prob = self.co[poke][move][othermove] #if othermove not in probs: #probs[othermove] = 1 #probs[othermove] *= prob if probs == {}: probs = self.get_freqs(self.freq) self.predictions = sorted(probs.items(), key=lambda x: -x[1]) return self.predictions
def get_moves_assumption_two(self, known_moves): poke = correct_name(self.poke) poke = correct_mega(self.poke) probs = {} if len(known_moves) == 0: probs = self.get_freqs(poke, self.freq) else: for move in known_moves: if move not in self.co[poke]: continue for othermove in self.co[poke][move]: if othermove in MOVE_CORRECTIONS: probs[MOVE_CORRECTIONS[othermove]] = probs[othermove] del probs[move] if othermove in known_moves: continue prob = self.co[poke][move][othermove] if othermove not in probs: probs[othermove] = 1 probs[othermove] *= prob if probs == {}: probs = self.get_freqs(poke, self.freq) self.predictions = sorted(probs.items(), key=lambda x: -x[1]) return self.predictions
def handle_event(self, gamestate, event): def get_pokemon(team, name): for poke in team: if poke.name == name: return poke def get_mega_item(name): for item, (premega, mega) in MEGA_ITEMS.items(): if mega == name: return item player = event.player type = event.type team = gamestate.get_team(player) poke = get_pokemon(team, event.poke) if poke == None: print(event.poke) opp_poke = gamestate.get_team(1 - player).primary() if type == "faint": poke.health = 0 poke.alive = False print("%s fainted." % (poke)) elif type == "mega_evolve": poke.item = get_mega_item(event.details['mega']) team = gamestate.get_team(player) team.poke_list[team.primary_poke] = poke.mega_evolve(self.pokedata) print("%s mega evolved!" % (poke)) elif type == "damage": hp = poke.final_stats['hp'] poke.damage(event.details['damage'] / 100 * hp) print("%s got damaged: %f" % (poke, event.details['damage'])) elif type == "move": print("%s used %s." % (poke, event.details['move'])) if event.details['move'] == "Relic Song": my_poke = gamestate.get_team(player).primary() my_poke.meloetta_evolve() if player == 1: move = event.details['move'] poke_name = correct_mega(poke.name) if move in MOVE_CORRECTIONS: move = MOVE_CORRECTIONS[move] if move == "Hidden Power": hidden_power = get_hidden_power(poke_name, self.smogon_data) if hidden_power: move = hidden_power else: return known_moves = poke.moveset.known_moves if move not in known_moves: self.total += 1 old_guess_moves = [ x[0] for x in poke.predict_moves(known_moves) ][:4 - len(known_moves)] if move in old_guess_moves: self.score += 1 known_moves.append(move) guess_moves = [ x[0] for x in poke.predict_moves(known_moves) if x[0] not in known_moves ][:4 - len(known_moves)] poke.moveset.moves = poke.moveset.known_moves + guess_moves if player == 0: my_poke = gamestate.get_team(player).primary() my_poke.last_move = event.details['move'] if poke.item in ["Choice Scarf", "Choice Specs", "Choice Band"]: moves = poke.moveset.moves try: moves.index(event.details['move']) poke.choiced = True poke.move_choice = event.details['move'] print("%s is choiced to %s" % (poke, event.details['move'])) except: pass elif type == "stat_change": stages = event.details['stages'] if stages > 0: poke.increase_stage(event.details['stat'], abs(stages)) print("%s increased its %s by %d stages" % (poke, event.details['stat'], stages)) else: poke.decrease_stage(event.details['stat'], abs(stages)) print("%s decreased its %s by %d stages" % (poke, event.details['stat'], stages)) elif type == "switch": if poke == "Meloetta": poke.meloetta_reset() team.set_primary(team.poke_list.index(poke)) print("Player %d switched in %s" % (player, poke)) elif type == "regain_health": poke.heal(0.5) print("%s regained health" % poke) elif type == "leftovers": poke.item = "Leftovers" poke.heal(1.0 / 16) print("%s regained health due to leftovers" % poke) elif type == "life_orb": poke.item = "Life Orb" poke.damage_percent(1.0 / 10) elif type == "leech_seed": damage = poke.damage_percent(1.0 / 8) opp_poke.heal(damage) print("%s was sapped and %s gained health due to leech seed." % (poke, opp_poke)) elif type == "rocks": gamestate.set_rocks(player, True) print("Player %u got rocked!" % player) elif type == "spikes": gamestate.spikes[player] += 1 print("Player %u now has %d spikes!" % (player, gamestate.spikes[player])) elif type == "rocks_gone": gamestate.set_rocks(player, False) print("Player %u's rocks disappeared!" % player) elif type == "burn": poke.set_status("burn") elif type == "paralyze": poke.set_status("paralyze") print("%s got burned!" % poke) elif type == "hurt_burn": poke.damage_percent(1.0 / 8) print("%s got hurt due to its burn!" % poke) elif type == "float_balloon": poke.item = "Air Balloon" print("%s has an air balloon!" % poke) elif type == "pop_balloon": poke.item = None print("%s's air balloon was popped!" % poke) elif type == "new_item": poke.item = event.details['item'] print("%s got a %s" % (poke, event.details['item'])) elif type == "lost_item": poke.item = None poke.choiced = False print("%s lost its item!" % poke) elif type == "belly_drum": poke.increase_stage('patk', 9999) elif type == "mold_breaker": poke.ability = "Mold Breaker" print("%s has mold breaker!" % poke) elif type == "disabled": move = event.details['move'] poke.disabled = move print("%s has mold breaker!" % poke) elif type == "taunt": poke.set_taunt(True) print("%s just got taunted!" % poke) elif type == "encore": poke.set_encore(True) print("%s just got encored!" % poke)
def handle_event(self, gamestate, event): def get_pokemon(team, name): for poke in team: if poke.name == name: return poke def get_mega_item(name): for item, (premega, mega) in MEGA_ITEMS.items(): if mega == name: return item player = event.player type = event.type team = gamestate.get_team(player) poke = get_pokemon(team, event.poke) opp_poke = gamestate.get_team(1 - player).primary() if type == "faint": poke.health = 0 poke.alive = False # print "%s fainted." % (poke) elif type == "mega_item": poke.item = event.details['item'] team = gamestate.get_team(player) team.poke_list[team.primary_poke] = poke.mega_evolve(self.pokedata) elif type == "damage": hp = poke.final_stats['hp'] poke.damage(event.details['damage'] / 100 * hp) # print "%s got damaged: %f" % (poke, event.details['damage']) elif type == "move": # print "%s used %s." % (poke, event.details['move']) poke.last_move = event.details['move'] if event.details['move'] == "Relic Song": my_poke = gamestate.get_team(player).primary() my_poke.meloetta_evolve() move = event.details['move'] poke_name = correct_mega(poke.name) if move in MOVE_CORRECTIONS: # print move move = MOVE_CORRECTIONS[move] # print move if move == "Hidden Power": hidden_power = get_hidden_power(poke_name, self.smogon_data) if hidden_power: move = hidden_power else: return if move != None and move not in poke.moveset.known_moves: #self.total += 1 #old_guess_moves = [x[0] for x in poke.predict_moves(poke.moveset.known_moves)][:4 - len(poke.moveset.known_moves)] #if move in old_guess_moves: # self.score += 1 poke.moveset.known_moves.append(move) #guess_moves = [x[0] for x in poke.predict_moves(poke.moveset.known_moves) if x[0] not in poke.moveset.known_moves][:4 - len(poke.moveset.known_moves)] # poke.moveset.moves = poke.moveset.known_moves + guess_moves if poke.item in ["Choice Scarf", "Choice Specs", "Choice Band"]: moves = poke.moveset.known_moves try: moves.index(event.details['move']) poke.choiced = True poke.move_choice = event.details['move'] # print "%s is choiced to %s" % (poke, event.details['move']) except: pass elif type == "stat_change": stages = event.details['stages'] if stages > 0: poke.increase_stage(event.details['stat'], abs(stages)) # print "%s increased its %s by %d stages" % (poke, event.details['stat'], stages) else: poke.decrease_stage(event.details['stat'], abs(stages)) # print "%s decreased its %s by %d stages" % (poke, event.details['stat'], stages) elif type == "switch": #if poke == "Meloetta": # poke.meloetta_reset() #team.set_primary(team.poke_list.index(poke)) gamestate.switch_pokemon(team.poke_list.index(poke), player, log=True) # print "Player %d switched in %s" % (player, poke) elif type == "regain_health": poke.heal(0.5) # print "%s regained health" % poke elif type == "leftovers": poke.item = "Leftovers" poke.heal(1.0 / 16) # print "%s regained health due to leftovers" % poke elif type == "life_orb": poke.item = "Life Orb" poke.damage_percent(1.0/10) elif type == "leech_seed": damage = poke.damage_percent(1.0 / 8) opp_poke.heal(damage) # print "%s was sapped and %s gained health due to leech seed." % (poke, opp_poke) elif type == "rocks": gamestate.set_rocks(player, True) # print "Player %u got rocked!" % player elif type == "spikes": gamestate.spikes[player] += 1 # print "Player %u now has %d spikes!" % (player, gamestate.spikes[player]) elif type == "rocks_gone": gamestate.set_rocks(player, False) # print "Player %u's rocks disappeared!" % player elif type == "burn": poke.set_status("burn") elif type == "paralyze": poke.set_status("paralyze") # print "%s got burned!" % poke elif type == "hurt_burn": poke.damage_percent(1.0 / 8) # print "%s got hurt due to its burn!" % poke elif type == "float_balloon": poke.item = "Air Balloon" # print "%s has an air balloon!" % poke elif type == "pop_balloon": poke.item = None # print "%s's air balloon was popped!" % poke elif type == "new_item": poke.item = event.details['item'] # print "%s got a %s" % (poke, event.details['item']) elif type == "lost_item": poke.item = None poke.choiced = False # print "%s lost its item!" % poke elif type == "belly_drum": poke.increase_stage('patk', 9999) elif type == "mold_breaker": poke.ability = "Mold Breaker" # print "%s has mold breaker!" % poke elif type == "disabled": move = event.details['move'] poke.disabled = move # print "%s has mold breaker!" % poke elif type == "taunt": poke.set_taunt(True) # print "%s just got taunted!" % poke elif type == "encore": poke.set_encore(True)
def __init__(self, poke, pokedata): super(MoveCoPredictor, self).__init__(poke, pokedata) graph_move = self.pokedata.graph_move self.poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))] self.co = graph_move['cooccurences'] self.freq = graph_move['frequencies']
def __init__(self, poke, pokedata): super(RandomMovePredictor, self).__init__(poke, pokedata) poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))] random.shuffle(poke_moves) prob = 1.0 / len(poke_moves) self.predictions = [(x, prob) for x in poke_moves]