google-deepmind / pysc2

StarCraft II Learning Environment
Apache License 2.0
7.96k stars 1.15k forks source link

absl.flags._exceptions.UnparsedFlagAccessError: Trying to access flag --sc2_run_config before flags were parsed #317

Closed MislavJuric closed 3 years ago

MislavJuric commented 3 years ago

I am writing a wrapper around PySC2 for TF-Agents.

I have encountered the following error while trying to construct a PySC2 environment via its constructor:

absl.flags._exceptions.UnparsedFlagAccessError: Trying to access flag --sc2_run_config before flags were parsed

Here is the code that I am using to test the environment:

from tf_agents.environments import utils
from rl_env.PySC2Environment import PySC2Environment

if __name__ == "__main__":
    environment = PySC2Environment()
    utils.validate_py_environment(environment, episodes=3)

And here is my PySC2 environment wrapper code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from tf_agents.environments import py_environment
from tf_agents.environments import wrappers
from tf_agents.specs import array_spec
from tf_agents.trajectories import time_step as ts
import pysc2

import importlib
import threading

from absl import app
from absl import flags
from future.builtins import range  # pylint: disable=redefined-builtin

from pysc2 import maps
from pysc2.env import available_actions_printer
from pysc2.env import run_loop
from pysc2.env import sc2_env
from pysc2.lib import point_flag
from pysc2.lib import stopwatch

# based on https://github.com/deepmind/pysc2/blob/master/pysc2/bin/agent.py and https://github.com/deepmind/pysc2/blob/master/pysc2/env/run_loop.py

class PySC2Environment(py_environment.PyEnvironment):

    def __init__(self):
        super().__init__()

        self.env = self.setup_env()

    def action_spec(self):
        return self.env.observation_spec()

    def observation_spec(self):
        return self.env.action_spec()

    def _reset(self):
        return self.env.reset()

    def _step(self, action):
        return self.env.step(action)

    def setup_env(self):
        """
        FLAGS = flags.FLAGS
        flags.DEFINE_bool("render", True, "Whether to render with pygame.")
        point_flag.DEFINE_point("feature_screen_size", "84",
                                "Resolution for screen feature layers.")
        point_flag.DEFINE_point("feature_minimap_size", "64",
                                "Resolution for minimap feature layers.")
        point_flag.DEFINE_point("rgb_screen_size", None,
                                "Resolution for rendered screen.")
        point_flag.DEFINE_point("rgb_minimap_size", None,
                                "Resolution for rendered minimap.")
        flags.DEFINE_enum("action_space", None, sc2_env.ActionSpace._member_names_,  # pylint: disable=protected-access
                          "Which action space to use. Needed if you take both feature "
                          "and rgb observations.")
        flags.DEFINE_bool("use_feature_units", False,
                          "Whether to include feature units.")
        flags.DEFINE_bool("use_raw_units", False,
                          "Whether to include raw units.")
        flags.DEFINE_bool("disable_fog", False, "Whether to disable Fog of War.")

        flags.DEFINE_integer("max_agent_steps", 0, "Total agent steps.")
        flags.DEFINE_integer("game_steps_per_episode", None, "Game steps per episode.")
        flags.DEFINE_integer("max_episodes", 0, "Total episodes.")
        flags.DEFINE_integer("step_mul", 8, "Game steps per agent step.")

        flags.DEFINE_string("agent", "pysc2.agents.random_agent.RandomAgent",
                    "Which agent to run, as a python path to an Agent class.")
        flags.DEFINE_string("agent_name", None,
                    "Name of the agent in replays. Defaults to the class name.")
        flags.DEFINE_enum("agent_race", "random", sc2_env.Race._member_names_,  # pylint: disable=protected-access
                  "Agent 1's race.")

        flags.DEFINE_string("agent2", "Bot", "Second agent, either Bot or agent class.")
        flags.DEFINE_string("agent2_name", None,
                    "Name of the agent in replays. Defaults to the class name.")
        flags.DEFINE_enum("agent2_race", "random", sc2_env.Race._member_names_,  # pylint: disable=protected-access
                  "Agent 2's race.")
        flags.DEFINE_enum("difficulty", "very_easy", sc2_env.Difficulty._member_names_,  # pylint: disable=protected-access
                  "If agent2 is a built-in Bot, it's strength.")
        flags.DEFINE_enum("bot_build", "random", sc2_env.BotBuild._member_names_,  # pylint: disable=protected-access
                  "Bot's build strategy.")

        flags.DEFINE_bool("profile", False, "Whether to turn on code profiling.")
        flags.DEFINE_bool("trace", False, "Whether to trace the code execution.")
        flags.DEFINE_integer("parallel", 1, "How many instances to run in parallel.")

        flags.DEFINE_bool("save_replay", True, "Whether to save a replay at the end.")

        flags.DEFINE_string("map", None, "Name of a map to use.")
        flags.DEFINE_bool("battle_net_map", False, "Use the battle.net map version.")
        flags.mark_flag_as_required("map")
        """
        players = []

        players.append(sc2_env.Agent(sc2_env.Race["random"],
                               None))

        players.append(sc2_env.Bot(sc2_env.Race["random"],
                                 sc2_env.Difficulty["very_easy"],
                                 sc2_env.BotBuild["random"]))

        return sc2_env.SC2Env(map_name="Simple64",
                              battle_net_map=False,
                              players=players,
                              agent_interface_format=sc2_env.parse_agent_interface_format(
                                  feature_screen=84,
                                  feature_minimap=64,
                                  rgb_screen=None,
                                  rgb_minimap=None,
                                  action_space=None,
                                  use_feature_units=False,
                                  use_raw_units=False),
                              step_mul=8,
                              game_steps_per_episode=None,
                              disable_fog=False,
                              visualize=False)

        """
        return sc2_env.SC2Env(map_name="Simple64",
                              battle_net_map=FLAGS.battle_net_map,
                              players=players,
                              agent_interface_format=sc2_env.parse_agent_interface_format(
                                  feature_screen=FLAGS.feature_screen_size,
                                  feature_minimap=FLAGS.feature_minimap_size,
                                  rgb_screen=FLAGS.rgb_screen_size,
                                  rgb_minimap=FLAGS.rgb_minimap_size,
                                  action_space=FLAGS.action_space,
                                  use_feature_units=FLAGS.use_feature_units,
                                  use_raw_units=FLAGS.use_raw_units),
                              step_mul=FLAGS.step_mul,
                              game_steps_per_episode=FLAGS.game_steps_per_episode,
                              disable_fog=FLAGS.disable_fog,
                              visualize=False)
        """

As you can see, I tried the variant with and without the flags (I commented out the flags in my most recent attempt). I still get the error.

Here is the full error log:

2020-09-19 11:09:13.518378: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-09-19 11:09:13.518403: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "PySC2Environment_test.py", line 10, in <module>
    environment = PySC2Environment()
  File "/home/mislav/Documents/AISC/Code/Preference_Extraction/agent/rl_env/PySC2Environment.py", line 33, in __init__
    self.env = self.setup_env()
  File "/home/mislav/Documents/AISC/Code/Preference_Extraction/agent/rl_env/PySC2Environment.py", line 123, in setup_env
    visualize=False)
  File "/home/mislav/anaconda3/envs/aisc/lib/python3.7/site-packages/pysc2/env/sc2_env.py", line 264, in __init__
    self._run_config = run_configs.get(version=version)
  File "/home/mislav/anaconda3/envs/aisc/lib/python3.7/site-packages/pysc2/run_configs/__init__.py", line 39, in get
    if FLAGS.sc2_run_config is None:  # Find the highest priority as default.
  File "/home/mislav/anaconda3/envs/aisc/lib/python3.7/site-packages/absl/flags/_flagvalues.py", line 491, in __getattr__
    raise _exceptions.UnparsedFlagAccessError(error_message)
absl.flags._exceptions.UnparsedFlagAccessError: Trying to access flag --sc2_run_config before flags were parsed.

What is going on here? How can I solve this?

MislavJuric commented 3 years ago

I fixed the error by having the code I use for testing the environment in the same file as my environment class (in a function called main) and by calling

app.run(main)

if the file is run. You can see the code that fixed the issue here.

ujnccs commented 2 years ago

cc

ujnccs commented 2 years ago

cc