스페이스¶
This module implements various spaces.
Spaces describe mathematical sets and are used in Gym to specify valid actions and observations.
Every Gym environment must have the attributes action_space
and observation_space
.
If, for instance, three possible actions (0,1,2) can be performed in your environment and observations
are vectors in the two-dimensional unit cube, the environment code may contain the following two lines:
self.action_space = spaces.Discrete(3)
self.observation_space = spaces.Box(0, 1, shape=(2,))
All spaces inherit from the Space
superclass.
- class gymnasium.spaces.Space(shape: Sequence[int] | None = None, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None, seed: int | Generator | None = None)[source]¶
Superclass that is used to define observation and action spaces.
Spaces are crucially used in Gym to define the format of valid actions and observations. They serve various purposes:
They clearly define how to interact with environments, i.e. they specify what actions need to look like and what observations will look like
They allow us to work with highly structured data (e.g. in the form of elements of
Dict
spaces) and painlessly transform them into flat arrays that can be used in learning codeThey provide a method to sample random elements. This is especially useful for exploration and debugging.
Different spaces can be combined hierarchically via container spaces (
Tuple
andDict
) to build a more expressive spaceWarning
Custom observation & action spaces can inherit from the
Space
class. However, most use-cases should be covered by the existing space classes (e.g.Box
,Discrete
, etc…), and container classes (Tuple
&Dict
). Note that parametrized probability distributions (through theSpace.sample()
method), and batching functions (ingym.vector.VectorEnv
), are only well-defined for instances of spaces provided in gym by default. Moreover, some implementations of Reinforcement Learning algorithms might not handle custom spaces properly. Use custom spaces with care.- Parameters:
shape (Optional[Sequence[int]]) – If elements of the space are numpy arrays, this should specify their shape.
dtype (Optional[Type | str]) – If elements of the space are numpy arrays, this should specify their dtype.
seed – Optionally, you can use this argument to seed the RNG that is used to sample from the space
속성¶
- property Space.shape: tuple[int, ...] | None¶
Return the shape of the space as an immutable property.
- property Space.dtype¶
이 스페이스의 데이터 유형을 반환합니다.
- property Space.is_np_flattenable: bool¶
Checks whether this space can be flattened to a
gymnasium.spaces.Box
.
메소드¶
각 스페이스는 다음 함수를 구현합니다:
- Space.sample(mask: Any | None = None, probability: Any | None = None) T_cov [source]¶
Randomly sample an element of this space.
Can be uniform or non-uniform sampling based on boundedness of space.
The binary mask and the probability mask can’t be used at the same time.
- Parameters:
mask – A mask used for random sampling, expected
dtype=np.int8
and see sample implementation for expected shape.probability – A probability mask used for sampling according to the given probability distribution, expected
dtype=np.float64
and see sample implementation for expected shape.
- Returns:
A sampled actions from the space
- Space.contains(x: Any) bool [source]¶
Return boolean specifying if x is a valid member of this space, equivalent to
sample in space
.
- Space.seed(seed: int | None = None) int | list[int] | dict[str, int] [source]¶
Seed the pseudorandom number generator (PRNG) of this space and, if applicable, the PRNGs of subspaces.
- Parameters:
seed – The seed value for the space. This is expanded for composite spaces to accept multiple values. For further details, please refer to the space’s documentation.
- Returns:
The seed values used for all the PRNGs, for composite spaces this can be a tuple or dictionary of values.
기본 스페이스¶
Gymnasium은 더 복잡한 스페이스를 위한 구성 요소로 사용되는 여러 기본 스페이스를 가지고 있습니다.
Box
- 연속형(및 이산형) 벡터 또는 행렬을 지원하며, 벡터 관찰, 이미지 등에 사용됩니다.Discrete
- 단일 이산형 값을 지원하며, 값의 시작점을 선택적으로 지정할 수 있습니다.MultiBinary
- 단일 또는 행렬 형태의 이진 값을 지원하며, 버튼을 누르거나 에이전트가 객체를 가지고 있는지 등에 사용됩니다.MultiDiscrete
- 여러 축에 걸쳐 다중 이산형 값을 지원하며, 컨트롤러 액션에 사용됩니다.Text
- 문자열을 지원하며, 에이전트 메시지 전달, 미션 세부 정보 등에 사용됩니다.
복합 스페이스¶
종종 환경 스페이스는 벡터화된 환경, 별도의 에이전트 또는 스페이스의 가독성을 위해 기본 스페이스를 함께 결합해야 합니다.
유틸리티 함수¶
Gymnasium은 스페이스를 평탄화하고 다시 평탄화하는 데 유용한 여러 유틸리티 함수를 포함하고 있습니다. 이는 신경망에 정보를 전달하는 데 중요할 수 있습니다.
utils.flatdim
- 평탄화된 스페이스가 포함할 차원의 수utils.flatten_space
-utils.flattened
스페이스 인스턴스가 포함될 수 있도록 스페이스를 평탄화합니다.utils.flatten
- 스페이스의 평탄화된 버전에 포함된 스페이스 인스턴스를 평탄화합니다.utils.unflatten
-utils.flatten_space
함수의 반대
벡터 유틸리티 함수¶
환경을 벡터화할 때, 새로운 배치 스페이스 크기에 맞게 관찰 및 액션 스페이스를 수정해야 합니다. 따라서 Gymnasium은 Vector 환경과 함께 스페이스를 사용할 때 사용되는 여러 추가 함수를 제공합니다.
vector.utils.batch_space
- 스페이스를 ``n``명의 사용자에 대한 동등한 스페이스로 변환합니다.vector.utils.concatenate
- 스페이스의 샘플을 미리 생성된 스페이스에 연결합니다.vector.utils.iterate
- 배치된 스페이스의 샘플을 순회합니다.vector.utils.create_empty_array
- 스페이스에 대한 빈 샘플을 생성합니다 (일반적으로 ``concatenate``와 함께 사용).vector.utils.create_shared_memory
- 비동기(멀티프로세싱) 환경을 위한 공유 메모리를 생성합니다.vector.utils.read_from_shared_memory
- 비동기(멀티프로세싱) 환경을 위한 공유 메모리를 읽습니다.vector.utils.write_to_shared_memory
- 비동기(멀티프로세싱) 환경을 위한 공유 메모리에 씁니다.