当前位置: 首页 > 工具软件 > PySC2 > 使用案例 >

PySC2 环境的基本使用

姬俊能
2023-12-01

PySC2环境在Win10系统上的部署与安装,请参考:https://www.jianshu.com/p/e4d2313fa341

  1. 如何使用自定义的地图、agent来启动、运行、测试环境:
python -m pysc2.bin.agent --map <Map> --agent <Agent>

其中<Agent>参数是一个指向你自定义类的python路径,比如,你的项目目录为[PATH],你的agent的类名为 MyAgentClassName,它被定义在了MyAgentFile.py的文件中,这种情况下,假设你在[PATH]目录下运行上述命令,需要这样的指定<Agent>参数:

python -m pysc2.bin.agent --map <Map> --agent MyAgentFile.MyAgentClassName
  1. 如何设计、编写自己的agent

任何自定义的agent类都需要继承自BaseAgent基类(在 pysc2.agents.base_agent 中,需要引入),并且重写其 step(self, obs) 函数,该函数中也需要调用其父类方法,该函数的一个参数是obs

一个基本的agent类应该拥有如下的结构:

# to be compatible with python 2.x
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy

# import the BaseAgent class which we should derive from
from pysc2.agents import base_agent
# import actions
from pysc2.lib import actions
# import features
from pysc2.lib import features

# define our own agent
class MyAgent (base_agent.BaseAgent):
    
    def step(self, obs):
        super(MyAgent, self).step(obs)
        
        #-------------------#
        # RL algorithm here #
        #-------------------#
        
        # return the actions
        return action
  1. 获取环境信息(state/observation)与奖赏(reward)

有关环境的信息都包含在了step函数的obs参数中,从中可以获取所有相关的能够从环境中获取到的信息,比如:

  • state、不同的feature: obs.observation["screen"][feature_map_name]
  • 该状态下的有效action:obs.observation["available_actions"]
  • 回报/奖赏(rewards): obs.reward
  1. 如何使用action与环境进行交互

同样在 step 函数中,使用一定的算法获取要执行的动作,然后由 step 函数返回即可,即 step 函数必须返回一个action

 类似资料: