.\PokeLLMon\poke_env\teambuilder\teambuilder_pokemon.py
py
复制代码
"""This module defines the TeambuilderPokemon class, which is used as an intermediate
format to specify pokemon builds in teambuilders custom classes.
"""
# 导入必要的模块
from typing import List, Optional
from poke_env.data import to_id_str
# 定义 TeambuilderPokemon 类
class TeambuilderPokemon:
# 不同属性对应的 IV 值
HP_TO_IVS = {
"bug": [31, 31, 31, 30, 31, 30],
"dark": [31, 31, 31, 31, 31, 31],
"dragon": [30, 31, 31, 31, 31, 31],
"electric": [31, 31, 31, 31, 30, 31],
"fighting": [31, 31, 30, 30, 30, 30],
"fire": [31, 30, 31, 30, 31, 30],
"flying": [31, 31, 31, 30, 30, 30],
"ghost": [31, 30, 31, 31, 31, 30],
"grass": [30, 31, 31, 31, 30, 31],
"ground": [31, 31, 31, 31, 30, 30],
"ice": [31, 30, 30, 31, 31, 31],
"poison": [31, 31, 30, 31, 30, 30],
"psychic": [30, 31, 31, 30, 31, 31],
"rock": [31, 31, 30, 30, 31, 30],
"steel": [31, 31, 31, 31, 31, 30],
"water": [31, 31, 31, 30, 30, 31],
}
# 初始化属性
evs: List[int]
ivs: List[int]
moves: List[str]
# 初始化方法
def __init__(
self,
nickname: Optional[str] = None,
species: Optional[str] = None,
item: Optional[str] = None,
ability: Optional[str] = None,
moves: Optional[List[str]] = None,
nature: Optional[str] = None,
evs: Optional[List[int]] = None,
gender: Optional[str] = None,
ivs: Optional[List[int]] = None,
shiny: Optional[bool] = None,
level: Optional[int] = None,
happiness: Optional[int] = None,
hiddenpowertype: Optional[str] = None,
gmax: Optional[bool] = None,
tera_type: Optional[str] = None,
# 定义 Pokemon 类,包含各种属性和方法
):
# 初始化 Pokemon 对象的属性
self.nickname = nickname
self.species = species
self.item = item
self.ability = ability
self.nature = nature
self.gender = gender
self.shiny = shiny
self.level = level
self.happiness = happiness
self.hiddenpowertype = hiddenpowertype
self.gmax = gmax
self.tera_type = tera_type
# 如果 EVs 为 None,则初始化为全 0
self.evs = evs if evs is not None else [0] * 6
# 如果 IVs 为 None,则初始化为全 31
self.ivs = ivs if ivs is not None else [31] * 6
# 如果 moves 为 None,则初始化为空列表
if moves is None:
self.moves = []
else:
self.moves = moves
# 定义 __repr__ 方法,返回格式化后的字符串表示
def __repr__(self) -> str:
return self.formatted
# 定义 __str__ 方法,返回格式化后的字符串表示
def __str__(self) -> str:
return self.formatted
# 定义 formatted_evs 属性,返回格式化后的 EVs 字符串
@property
def formatted_evs(self) -> str:
f_evs = ",".join([str(el) if el != 0 else "" for el in self.evs])
if f_evs == "," * 5:
return ""
return f_evs
# 定义 formatted_ivs 属性,返回格式化后的 IVs 字符串
@property
def formatted_ivs(self) -> str:
f_ivs = ",".join([str(el) if el != 31 else "" for el in self.ivs])
if f_ivs == "," * 5:
return ""
return f_ivs
# 定义 formatted_moves 属性,返回格式化后的 moves 字符串
@property
def formatted_moves(self) -> str:
return ",".join([to_id_str(move) for move in self.moves])
# 定义 formatted_endstring 属性,返回格式化后的结束字符串
@property
def formatted_endstring(self) -> str:
f_str = f",{self.hiddenpowertype or ''},"
# 根据条件返回不同的字符串
if self.gmax:
return f_str + ",G"
elif self.tera_type:
return f_str + f",,,{self.tera_type}"
if self.hiddenpowertype:
return f_str
return ""
# 定义 formatted 属性
@property
# 返回格式化后的字符串表示
def formatted(self) -> str:
# 准备数据以便格式化
self._prepare_for_formatting()
# 格式化输出字符串
return "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s%s" % (
self.nickname or "",
to_id_str(self.species) if self.species else "",
to_id_str(self.item) if self.item else "",
to_id_str(self.ability) if self.ability else "",
self.formatted_moves or "",
self.nature or "",
self.formatted_evs or "",
self.gender or "",
self.formatted_ivs or "",
"S" if self.shiny else "",
self.level or "",
self.happiness or "",
self.formatted_endstring,
)
# 准备数据以便格式化
def _prepare_for_formatting(self):
# 遍历每个招式
for move in self.moves:
# 将招式转换为标识符形式
move = to_id_str(move)
# 如果招式以"hiddenpower"开头且长度大于11且所有IV值都为31
if (
move.startswith("hiddenpower")
and len(move) > 11
and all([iv == 31 for iv in self.ivs])
):
# 将IV值替换为对应的隐藏力类型的IV值
self.ivs = list(self.HP_TO_IVS[move[11:]])
.\PokeLLMon\poke_env\teambuilder\__init__.py
py
复制代码
# 初始化 poke_env.teambuilder 模块
"""
# 导入 constant_teambuilder 和 teambuilder 模块
from poke_env.teambuilder import constant_teambuilder, teambuilder
# 导入 ConstantTeambuilder 类
from poke_env.teambuilder.constant_teambuilder import ConstantTeambuilder
# 导入 Teambuilder 类
from poke_env.teambuilder.teambuilder import Teambuilder
# 导入 TeambuilderPokemon 类
from poke_env.teambuilder.teambuilder_pokemon import TeambuilderPokemon
# 定义 __all__ 列表,包含需要导出的模块和类
__all__ = [
"ConstantTeambuilder",
"Teambuilder",
"TeambuilderPokemon",
"constant_teambuilder",
"teambuilder",
]
.\PokeLLMon\poke_env\__init__.py
py
复制代码
"""poke_env module init.
"""
# 导入日志模块
import logging
# 导入环境模块
import poke_env.environment as environment
# 导入异常模块
import poke_env.exceptions as exceptions
# 导入玩家模块
import poke_env.player as player
# 导入 PS 客户端模块
import poke_env.ps_client as ps_client
# 导入统计模块
import poke_env.stats as stats
# 导入队伍构建模块
import poke_env.teambuilder as teambuilder
# 导入生成数据和转换为 ID 字符串的函数
from poke_env.data import gen_data, to_id_str
# 导入 Showdown 异常
from poke_env.exceptions import ShowdownException
# 导入账户配置
from poke_env.ps_client import AccountConfiguration
# 导入服务器配置
from poke_env.ps_client.server_configuration import (
LocalhostServerConfiguration,
ServerConfiguration,
ShowdownServerConfiguration,
)
# 导入计算原始统计的函数
from poke_env.stats import compute_raw_stats
# 获取 logger 对象
__logger = logging.getLogger("poke-env")
# 创建流处理器
__stream_handler = logging.StreamHandler()
# 创建格式化器
__formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# 设置格式化器
__stream_handler.setFormatter(__formatter)
# 将流处理器添加到 logger
__logger.addHandler(__stream_handler)
# 添加日志级别名称
logging.addLevelName(25, "PS_ERROR")
# 导出的模块列表
__all__ = [
"AccountConfiguration",
"LocalhostServerConfiguration",
"ServerConfiguration",
"ShowdownException",
"ShowdownServerConfiguration",
"compute_raw_stats",
"environment",
"exceptions",
"gen_data",
"player",
"ps_client",
"stats",
"teambuilder",
"to_id_str",
]
.\PokeLLMon\vs_bot.py
py
复制代码
# 导入必要的库
import asyncio
import time
from tqdm import tqdm
import numpy as np
from poke_env import AccountConfiguration, ShowdownServerConfiguration
import os
import pickle as pkl
import argparse
# 从 poke_env.player 模块中导入 LLMPlayer 和 SimpleHeuristicsPlayer 类
from poke_env.player import LLMPlayer, SimpleHeuristicsPlayer
# 创建命令行参数解析器
parser = argparse.ArgumentParser()
# 添加命令行参数
parser.add_argument("--backend", type=str, default="gpt-4-0125-preview", choices=["gpt-3.5-turbo-0125", "gpt-4-1106-preview", "gpt-4-0125-preview"])
parser.add_argument("--temperature", type=float, default=0.8)
parser.add_argument("--prompt_algo", default="io", choices=["io", "sc", "cot", "tot"])
parser.add_argument("--log_dir", type=str, default="./battle_log/pokellmon_vs_bot")
# 解析命令行参数
args = parser.parse_args()
# 异步函数,主要逻辑在其中实现
async def main():
# 创建 SimpleHeuristicsPlayer 对象
heuristic_player = SimpleHeuristicsPlayer(battle_format="gen8randombattle")
# 确保日志目录存在
os.makedirs(args.log_dir, exist_ok=True)
# 创建 LLMPlayer 对象
llm_player = LLMPlayer(battle_format="gen8randombattle",
api_key="Your_openai_api_key",
backend=args.backend,
temperature=args.temperature,
prompt_algo=args.prompt_algo,
log_dir=args.log_dir,
account_configuration=AccountConfiguration("Your_account", "Your_password"),
save_replays=args.log_dir
)
# 禁用动态最大化功能
heuristic_player._dynamax_disable = True
llm_player._dynamax_disable = True
# 进行五场对战
for i in tqdm(range(5)):
# 随机选择对手
x = np.random.randint(0, 100)
if x > 50:
# 与 LLMPlayer 对战
await heuristic_player.battle_against(llm_player, n_battles=1)
else:
# 与 SimpleHeuristicsPlayer 对战
await llm_player.battle_against(heuristic_player, n_battles=1)
# 保存对战记录
for battle_id, battle in llm_player.battles.items():
with open(f"{args.log_dir}/{battle_id}.pkl", "wb") as f:
pkl.dump(battle, f)
if __name__ == "__main__":
# 获取当前事件循环并运行直到完成 main() 函数的执行
asyncio.get_event_loop().run_until_complete(main())
.\PokeLLMon\vs_ladder_player.py
py
复制代码
# 导入必要的库
import asyncio
from poke_env import AccountConfiguration, ShowdownServerConfiguration
from poke_env.player import LLMPlayer
import pickle as pkl
from tqdm import tqdm
import argparse
import os
# 创建命令行参数解析器
parser = argparse.ArgumentParser()
parser.add_argument("--backend", type=str, default="gpt-4-0125-preview", choices=["gpt-3.5-turbo-0125", "gpt-4-1106-preview", "gpt-4-0125-preview"])
parser.add_argument("--temperature", type=float, default=0.8)
parser.add_argument("--prompt_algo", default="io", choices=["io", "sc", "cot", "tot"])
parser.add_argument("--log_dir", type=str, default="./battle_log/pokellmon_vs_ladder_player")
args = parser.parse_args()
# 异步函数,用于执行主要逻辑
async def main():
# 确保日志目录存在
os.makedirs(args.log_dir, exist_ok=True)
# 创建 LLMPlayer 实例
llm_player = LLMPlayer(battle_format="gen8randombattle",
api_key="Your_openai_api_key",
backend=args.backend,
temperature=args.temperature,
prompt_algo=args.prompt_algo,
log_dir=args.log_dir,
account_configuration=AccountConfiguration("Your_account", "Your_password"),
server_configuration=ShowdownServerConfiguration,
save_replays=args.log_dir
)
# 在 ladder 上进行 5 场比赛
for i in tqdm(range(1)):
try:
# 在 ladder 上进行比赛
await llm_player.ladder(1)
# 保存每场比赛的数据
for battle_id, battle in llm_player.battles.items():
with open(f"{args.log_dir}/{battle_id}.pkl", "wb") as f:
pkl.dump(battle, f)
except:
continue
# 如果作为独立脚本运行,则执行主函数
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
.\PokeLLMon\vs_local_player.py
py
复制代码
# 导入必要的库
import asyncio
from poke_env import AccountConfiguration, ShowdownServerConfiguration
from poke_env.player import LLMPlayer
import pickle as pkl
from tqdm import tqdm
import argparse
import os
# 创建命令行参数解析器
parser = argparse.ArgumentParser()
parser.add_argument("--backend", type=str, default="gpt-4-0125-preview", choices=["gpt-3.5-turbo-0125", "gpt-4-1106-preview", "gpt-4-0125-preview"])
parser.add_argument("--temperature", type=float, default=0.8)
parser.add_argument("--prompt_algo", default="io", choices=["io", "sc", "cot", "tot"])
parser.add_argument("--log_dir", type=str, default="./battle_log/pokellmon_vs_invited_player")
args = parser.parse_args()
# 异步函数,用于执行主要逻辑
async def main():
# 确保日志目录存在
os.makedirs(args.log_dir, exist_ok=True)
# 创建 LLMPlayer 实例
llm_player = LLMPlayer(battle_format="gen8randombattle",
api_key="Your_openai_api_key",
backend=args.backend,
temperature=args.temperature,
prompt_algo=args.prompt_algo,
log_dir=args.log_dir,
account_configuration=AccountConfiguration("Your account", "Your_password"),
save_replays=args.log_dir
)
# 禁用 Dynamax 功能以确保公平性
llm_player._dynamax_disable = True
# 在本地进行5场比赛
for i in tqdm(range(5)):
try:
# 在排行榜上进行1场比赛
await llm_player.ladder(1)
# 保存每场比赛的数据
for battle_id, battle in llm_player.battles.items():
with open(f"{args.log_dir}/{battle_id}.pkl", "wb") as f:
pkl.dump(battle, f)
except:
continue
# 程序入口
if __name__ == "__main__":
# 运行主函数
asyncio.get_event_loop().run_until_complete(main())