首页 > 编程语言 >PokéLLMon 源码解析(六)

PokéLLMon 源码解析(六)

时间:2024-03-07 15:12:10浏览次数:25  
标签:__ self 30 源码 Pok str import LLMon 31

.\PokeLLMon\poke_env\teambuilder\teambuilder_pokemon.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

# 初始化 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

"""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

# 导入必要的库
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

# 导入必要的库
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

# 导入必要的库
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())

标签:__,self,30,源码,Pok,str,import,LLMon,31
From: https://www.cnblogs.com/apachecn/p/18058941

相关文章

  • PokéLLMon 源码解析(一)
    .\PokeLLMon\poke_env\concurrency.py#导入必要的模块importasyncioimportatexitimportsysfromloggingimportCRITICAL,disablefromthreadingimportThreadfromtypingimportAny,List#在新线程中运行事件循环def__run_loop(loop:asyncio.AbstractEventLo......
  • P6390 [COCI2007-2008#4] POKLON 题解
    感谢@\(\color{#AEF}{\texttt{CelestialCyan}}\)大神对我的骚扰帮助。分析一眼DP。对于求最大满足条件区间数,我们定义状态函数\(\mathit{f}_{i}\)表示在第\(1\)到\(i\)个区间中选择,且必选第\(i\)个区间能够得到的最大长度。有转移方程:\(\mathit{f}_{i}=\max\{f[j]|......
  • opencv+opencv_contrib源码安装及卸载
    源码安装opencv3.4.16+opencv_contrib-3.4.16及卸载1.下载opencv-3.4.16和opencv_contrib-3.4.16opencv官网下载链接:opencv点击Sources下载压缩包在GitHub中下载opencv_contrib-3.4.16:opencv_contrib-3.4.16选择与opencv相同版本的opencv_contrib2.解压opencv-3.4.1......
  • 速存,详细罗列香橙派AIpro外设接口样例大全(附源码)
    本文分享自华为云社区《香橙派AIpro外设接口样例大全(附源码)》,作者:昇腾CANN。OrangePiAIPro开发板是香橙派联合华为精心打造的高性能AI开发板,其搭载了昇腾AI处理器,可提供8TOPSINT8的计算能力,内存提供了8GB和16GB两种版本。可以实现图像、视频等多种数据分析与推理计......
  • drf源码剖析----request
    点击查看代码源码解析:#请求函数内的request,*args,**kwargs传给父类APIView内的dispatch()函数defdispatch(self,request,*args,**kwargs):self.args=argsself.kwargs=kwargs#request,*args,**kwargs传给self.initialize_request(request,......
  • drf源码剖析----as_view()
    点击查看代码path('example/',views.ExampleView.as_view())#as_view()作为入口函数,先去类ExampleView()中找as_view()函数点击查看代码#类ExampleView()中没有找到,去父类APIView()中找as_view()函数classExampleView(APIView):defget(self,request):......
  • Centos5 源码升级sshd
      1.下载源码包#openssh下载地址http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.4p1.tar.gz#openssl下载地址https://www.openssl.org/source/old/0.9.x/openssl-0.9.8t.tar.gz2.编译安装openssl#需要修改Makefile在CFLAG项中加入-fPIC......
  • Java 源码,反码和补码
    计算机在对数据进行运算的原理?3-2=13+(-2)=1先将3这个十进制,变成二进制的原码形式,然后变成反码形式,最后变成补码形式先将-2这个十进制,变成二进制的原码形式,然后变成反码形式,最后变成补码形式将这两个数二......
  • Pokemon Go自動走路 iOS/Android 在家玩寶可夢不用出門 不用移動
    有時您可能想知道如何在不動的情況下玩PokemonGo。好消息是,我們將為您介紹PokemonGo自動步行,以偽造GPS位置。PokemonGo欺騙器將作為位置變換器引入。閱讀更多有關如何在PokemonGo中無需步行即可移動的方法。第1部分.是否可以不動地玩PokemonGO 在家玩寶可夢......
  • 记一次部署vika源码以及安装Falsk框架到linux服务器
    前言接受到需求,需要把python项目vika的源码修改部分内容后,部署到服务器,然后再安装一个python的快速开发框架Falsk。部署vika1.什么是vika?Vika PythonSDK是对维格表FusionAPI的官方封装,提供类似DjangoORM风格的API。2.下载源码前往源码地址(https://github.com/vikadat......