File size: 4,485 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from typing import Any, List, Union, Optional
from easydict import EasyDict
import time
import gym
import numpy as np
from ding.envs import BaseEnv, BaseEnvTimestep
from ding.envs.common.env_element import EnvElement, EnvElementInfo
from ding.torch_utils import to_ndarray, to_list
from ding.utils import ENV_REGISTRY, deep_merge_dicts


@ENV_REGISTRY.register('procgen')
class ProcgenEnv(BaseEnv):

    #If control_level is True, you can control the specific level of the generated environment by controlling start_level and num_level.
    config = dict(
        control_level=True,
        start_level=0,
        num_levels=0,
        env_id='coinrun',
    )

    def __init__(self, cfg: dict) -> None:
        cfg = deep_merge_dicts(EasyDict(self.config), cfg)
        self._cfg = cfg
        self._seed = 0
        self._init_flag = False
        self._observation_space = gym.spaces.Box(
            low=np.zeros(shape=(3, 64, 64)), high=np.ones(shape=(3, 64, 64)) * 255, shape=(3, 64, 64), dtype=np.float32
        )

        self._action_space = gym.spaces.Discrete(15)

        self._reward_space = gym.spaces.Box(low=float("-inf"), high=float("inf"), shape=(1, ), dtype=np.float32)
        self._control_level = self._cfg.control_level
        self._start_level = self._cfg.start_level
        self._num_levels = self._cfg.num_levels
        self._env_name = 'procgen:procgen-' + self._cfg.env_id + '-v0'
        # In procgen envs, we use seed to control level, and fix the numpy seed to 0
        np.random.seed(0)

    def reset(self) -> np.ndarray:
        if not self._init_flag:
            if self._control_level:
                self._env = gym.make(self._env_name, start_level=self._start_level, num_levels=self._num_levels)
            else:
                self._env = gym.make(self._env_name, start_level=0, num_levels=1)
            self._init_flag = True
        if hasattr(self, '_seed') and hasattr(self, '_dynamic_seed') and self._dynamic_seed:
            np_seed = 100 * np.random.randint(1, 1000)
            self._env.close()
            if self._control_level:
                self._env = gym.make(self._env_name, start_level=self._start_level, num_levels=self._num_levels)
            else:
                self._env = gym.make(self._env_name, start_level=self._seed + np_seed, num_levels=1)
        elif hasattr(self, '_seed'):
            self._env.close()
            if self._control_level:
                self._env = gym.make(self._env_name, start_level=self._start_level, num_levels=self._num_levels)
            else:
                self._env = gym.make(self._env_name, start_level=self._seed, num_levels=1)
        self._eval_episode_return = 0
        obs = self._env.reset()
        obs = to_ndarray(obs)
        obs = np.transpose(obs, (2, 0, 1))
        obs = obs.astype(np.float32)
        return obs

    def close(self) -> None:
        if self._init_flag:
            self._env.close()
        self._init_flag = False

    def seed(self, seed: int, dynamic_seed: bool = True) -> None:
        self._seed = seed
        self._dynamic_seed = dynamic_seed

    def step(self, action: np.ndarray) -> BaseEnvTimestep:
        assert isinstance(action, np.ndarray), type(action)
        if action.shape == (1, ):
            action = action.squeeze()  # 0-dim array
        obs, rew, done, info = self._env.step(action)
        self._eval_episode_return += rew
        if done:
            info['eval_episode_return'] = self._eval_episode_return
        obs = to_ndarray(obs)
        obs = np.transpose(obs, (2, 0, 1))
        obs = obs.astype(np.float32)
        rew = to_ndarray([rew])  # wrapped to be transfered to a array with shape (1,)
        rew = rew.astype(np.float32)
        return BaseEnvTimestep(obs, rew, bool(done), info)

    @property
    def observation_space(self) -> gym.spaces.Space:
        return self._observation_space

    @property
    def action_space(self) -> gym.spaces.Space:
        return self._action_space

    @property
    def reward_space(self) -> gym.spaces.Space:
        return self._reward_space

    def __repr__(self) -> str:
        return "DI-engine CoinRun Env"

    def enable_save_replay(self, replay_path: Optional[str] = None) -> None:
        if replay_path is None:
            replay_path = './video'
        self._replay_path = replay_path
        self._env = gym.wrappers.Monitor(
            self._env, self._replay_path, video_callable=lambda episode_id: True, force=True
        )