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

【强化学习】pysc2 星际争霸环境测试

戚侯林
2023-12-01

如何安装就不说了

Game.py

from pysc2.env import sc2_env

class Game_StarCraftII(object):
    def __init__(self, config, agents):
        self.game_history = {
            'observations': [],
            'actions': [],
            'rewards': None
        }
        players = []
        if config.player_one_type == 'Agent':
            player_one = sc2_env.Agent(
                race=sc2_env.Race[config.player_one_race]
            )
        elif config.player_one_type == 'Bot':
            player_one = sc2_env.Bot(
                race=sc2_env.Race[config.player_one_race],
                difficulty=sc2_env.Difficulty[config.difficulty]
            )
        else:
            raise NotImplementedError(
                'Player type only can choise "Agent" or "Bot"'
            )
        players.append(player_one)
        if config.player_two_type == 'Agent':
            player_two = sc2_env.Agent(
                race=sc2_env.Race[config.player_two_race]
            )
        elif config.player_two_type == 'Bot':
            player_two = sc2_env.Bot(
                race=sc2_env.Race[config.player_two_race],
                difficulty=sc2_env.Difficulty[config.difficulty]
            )

        else:
            raise NotImplementedError(
                'Player type only can choise "Agent" or "Bot"'
            )
        players.append(player_two)

        env = sc2_env.SC2Env(
            map_name=config.map_name,
            battle_net_map=config.battle_net_map,
            players=players,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=config.feature_screen_size,
                feature_minimap=config.feature_minimap_size,
                rgb_screen=config.rgb_screen_size,
                rgb_minimap=config.rgb_minimap_size,
                action_space=config.action_space,
                use_feature_units=config.use_feature_units,
                use_raw_units=config.use_raw_units),
            step_mul=config.step_mul,
            game_steps_per_episode=config.game_steps_per_episode,
            disable_fog=config.disable_fog,
            realtime=config.realtime,
            visualize=config.visualize
        )
        for agent, obs_spec, act_spec in zip(agents, env.observation_spec(), env.action_spec()):
            agent.setup(obs_spec, act_spec)

        timesteps = env.reset()
        for agent in agents:
            agent.reset()

        while not timesteps[0].last():
            step_actions = [
                agent.step(timestep)
                for agent, timestep in zip(agents, timesteps)
            ]
            self.game_history['observations'].append(timesteps)
            self.game_history['actions'].append(step_actions)
            timesteps = env.step(actions=step_actions)

        self.game_history['rewards'] = [obs.reward for obs in timesteps]

        print(self.game_history['rewards'])

        env.close()

    def load_game_data(self):
        return

test.py

from pysc2.agents.base_agent import BaseAgent
from environment.Game import Game_StarCraftII
from absl import flags
import sys

class Config:
    def __init__(self):
        self.player_one_type = 'Agent'
        self.player_one_race = 'protoss'
        self.player_two_type = 'Bot'
        self.player_two_race = 'protoss'
        self.difficulty = 'very_hard'
        self.map_name = 'Simple64'
        self.battle_net_map = False
        self.feature_screen_size = 128
        self.feature_minimap_size = 64
        self.rgb_screen_size = None
        self.rgb_minimap_size = None
        self.action_space = None
        self.use_feature_units = True
        self.use_raw_units = True
        self.step_mul = None
        self.game_steps_per_episode = None
        self.disable_fog = False
        self.realtime = False
        self.visualize = True

if __name__ == '__main__':
    FLAGS = flags.FLAGS
    FLAGS(sys.argv)

    config = Config()
    agents = [BaseAgent()]
    game = Game_StarCraftII(config, agents)

 类似资料: