-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbase.py
212 lines (157 loc) · 6.85 KB
/
base.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright (c) 2023, Haruka Kiyohara, Ren Kishimoto, HAKUHODO Technologies Inc., and Hanjuku-kaso Co., Ltd. All rights reserved.
# Licensed under the Apache 2.0 License.
"""Abstract base class for logged dataset."""
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from ..types import LoggedDataset
@dataclass
class BaseDataset(metaclass=ABCMeta):
"""Base class for logged dataset.
Imported as: :class:`scope_rl.dataset.BaseDataset`
"""
@abstractmethod
def obtain_episodes(self, n_trajectories: int) -> LoggedDataset:
"""Rollout behavior policy and obtain episodes.
Parameters
-------
n_trajectories: int, default=10000 (> 0)
Number of trajectories to generate by rolling out the behavior policy.
Returns
-------
logged_dataset(s): LoggedDataset or MultipleLoggedDataset
:class:`MultipleLoggedDataset` is an instance containing (multiple) logged datasets.
For API consistency, each logged dataset should contain the following.
.. code-block:: python
key: [
size,
n_trajectories,
step_per_trajectory,
action_type,
n_actions,
action_dim,
action_keys,
action_meaning,
state_dim,
state_keys,
state,
action,
reward,
done,
terminal,
info,
pscore,
]
size: int (> 0)
Number of steps the dataset records.
n_trajectories: int (> 0)
Number of trajectories the dataset records.
step_per_trajectory: int (> 0)
Number of timesteps in an trajectory.
action_type: str
Type of the action space.
Either "discrete" or "continuous".
n_actions: int (> 0)
Number of actions.
If action_type is "continuous", `None` is recorded.
action_dim: int (> 0)
Dimensions of the action space.
If action_type is "discrete", `None` is recorded.
action_keys: list of str
Name of each dimension in the action space.
If action_type is "discrete", `None` is recorded.
action_meaning: dict
Dictionary to map discrete action index to a specific action.
If action_type is "continuous", `None` is recorded.
state_dim: int (> 0)
Dimensions of the state space.
state_keys: list of str
Name of each dimension of the state space.
state: ndarray of shape (size, state_dim)
State observed under the behavior policy.
action: ndarray of shape (size, ) or (size, action_dim)
Action chosen by the behavior policy.
reward: ndarray of shape (size, )
Reward observed for each (state, action) pair.
done: ndarray of shape (size, )
Whether an episode ends or not.
terminal: ndarray of shape (size, )
Whether an episode reaches the pre-defined maximum steps.
info: dict
Additional feedbacks from the environment.
pscore: ndarray of shape (size, )
Propensity of the observed action being chosen under the behavior policy (pscore stands for propensity score).
"""
raise NotImplementedError
@abstractmethod
def obtain_steps(self, n_trajectories: int) -> LoggedDataset:
"""Rollout behavior policy and obtain steps.
Parameters
-------
n_trajectories: int, default=10000 (> 0)
Number of trajectories to generate by rolling out the behavior policy.
Returns
-------
logged_dataset(s): LoggedDataset or MultipleLoggedDataset
:class:`MultipleLoggedDataset` is an instance containing (multiple) logged datasets.
For API consistency, each logged dataset should contain the following.
.. code-block:: python
key: [
size,
n_trajectories,
step_per_trajectory,
action_type,
n_actions,
action_dim,
action_keys,
action_meaning,
state_dim,
state_keys,
state,
action,
reward,
done,
terminal,
info,
pscore,
]
size: int (> 0)
Number of steps the dataset records.
n_trajectories: int (> 0)
Number of trajectories the dataset records.
step_per_trajectory: int (> 0)
Number of timesteps in an trajectory.
action_type: str
Type of the action space.
Either "discrete" or "continuous".
n_actions: int (> 0)
Number of actions.
If action_type is "continuous", `None` is recorded.
action_dim: int (> 0)
Dimensions of the action space.
If action_type is "discrete", `None` is recorded.
action_keys: list of str
Name of each dimension of the action space.
If action_type is "discrete", `None` is recorded.
action_meaning: dict
Dictionary to map discrete action index to a specific action.
If action_type is "continuous", `None` is recorded.
state_dim: int (> 0)
Dimensions of the state space.
state_keys: list of str
Name of each dimension of the state space.
state: ndarray of shape (size, state_dim)
State observed under the behavior policy.
action: ndarray of shape (size, ) or (size, action_dim)
Action chosen by the behavior policy.
reward: ndarray of shape (size, )
Reward observed for each (state, action) pair.
done: ndarray of shape (size, )
Whether an episode ends or not.
terminal: ndarray of shape (size, )
Whether an episode reaches the pre-defined maximum steps.
info: dict
Additional feedbacks from the environment.
pscore: ndarray of shape (size, )
Propensity of the observed action being chosen under the behavior policy (pscore stands for propensity score).
"""
raise NotImplementedError