-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathglulx_logger.py
More file actions
265 lines (204 loc) · 6.57 KB
/
Copy pathglulx_logger.py
File metadata and controls
265 lines (204 loc) · 6.57 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
import json
from typing import Tuple, List, Optional, Iterable, Union, Sized, Any, Mapping
from textworld.core import GameState, Wrapper
from textworld.envs.glulx.git_glulx_ml import GitGlulxMLEnvironment, GlulxGameState
class GameLog:
"""
GameLog object. Allows you to load and save previous game logs.
"""
def __init__(self):
self._logs = [[]]
self._filename = ''
def __getitem__(self, idx: int) -> list:
"""
Gets a particular game log at index idx.
Args:
idx: index to retrieve
Returns: list of logs at idx
"""
assert idx <= len(self._logs)
return self._logs[idx]
def __len__(self) -> int:
return len(self._logs)
@property
def current_game(self) -> list:
"""
Gets current game we're logging.
Returns: list of logs from current game.
"""
return self._logs[-1]
@property
def logs(self) -> list:
"""
Get all logs from all games.
Returns: All logs from all games.
"""
return self._logs
def new_game(self) -> list:
"""
Start logs for a new game.
Returns: log object for current game.
"""
if len(self.current_game) > 0:
self._logs.append([])
return self.current_game
def set(self, key: Any, value: Any) -> None:
"""
Sets value for latest game
Args:
key: Key to set
value: Value to set
"""
current = self.current_game[-1]
current[key] = value
def append_optional(self, value: Any) -> None:
"""
Appends optional information to current game
Args:
value: Value to append. Must be JSON serializable.
"""
current = self.current_game[-1]
if 'optional' not in current:
current['optional'] = []
current['optional'].append(value)
def add_log(self, log: Mapping):
"""
Adds a new log to our logs
Args:
log: Mapping of a log
"""
self.current_game.append(log)
def save(self, filename: str) -> None:
"""
Save current logs to specified file name
Args:
filename: File path to save to (should have JSON extension)
"""
self._filename = filename
try:
with open(filename, 'w') as outfile:
json.dump(self._logs, outfile)
except TypeError as e:
raise TypeError('Log not serializable')
def load(self, filename: str) -> None:
"""
Loads a JSON object as logs
Args:
filename: file path to load.
"""
self._filename = filename
with open(filename) as f:
self._logs = json.load(f)
class GlulxLogger(Wrapper):
"""
Wrapper around a TextWorld GitGlulxML environment to provide logging capabilities.
Args:
env: The GitGlulxML environment to wrap.
"""
def __init__(self, env: GitGlulxMLEnvironment) -> None:
super().__init__(env)
self.activate_state_tracking()
self.serialized_game = env.game.serialize()
self._gamefile = env.gamefile
self._game_log = GameLog()
def step(self, command: str) -> Tuple[GlulxGameState, float, bool]:
"""
Take a step in the environment.
Args:
command: input string for taking an action
Returns:
GlulxGameState, score and done.
"""
new_log = {}
new_log['optional'] = []
new_log['command'] = command
game_state, score, done = super().step(command)
new_log['feedback'] = game_state.feedback
new_log['score'] = score
new_log['done'] = done
new_log['description'] = game_state.description
new_log['inventory'] = game_state.inventory
new_log['state'] = game_state.state.serialize()
self._game_log.add_log(new_log)
return game_state, score, done
def reset(self) -> GameState:
"""
Reset the environment.
Adds a new game into the logs.
Returns:
GameState
"""
new_log = {}
self._game_log.new_game()
game_state = super().reset()
new_log['optional'] = []
new_log['done'] = False
new_log['description'] = game_state.description
new_log['inventory'] = game_state.inventory
new_log['state'] = game_state.state.serialize()
self._game_log.add_log(new_log)
return game_state
def add_commands(self, commands: List[str], scores: Optional[Iterable[float]]=None) -> None:
"""
Add custom commands to the logger. Optionally add scores for each command.
Args:
commands: A list of commands.
scores: scores for each command. Must be same size as commands if provided.
"""
if scores is not None:
self._game_log.set('command_scores', scores)
self._game_log.set('commands', commands)
def add(self, info: Any) -> None:
"""
Add any additional information you want to log.
Args:
info: Additional information to log for the current game state.
"""
self._game_log.append_optional(info)
@property
def current(self) -> Mapping:
"""
Returns:
Current game state logs.
"""
return self._game_log.current_game[-1]
@property
def logs(self) -> List[Mapping]:
"""
Returns: List of all logs from this game.
"""
return self._game_log.current_game
@property
def all_logs(self) -> GameLog:
"""
Returns: GameLog object containing all logs.
"""
return self._game_log
@property
def gamefile(self) -> str:
"""
Returns:
Game file currently loaded
"""
return self._gamefile
def __getitem__(self, index: int) -> Mapping:
"""
Get a certain log at a given index.
Args:
index: index of log to get.
Returns:
log at index
"""
assert index <= len(self._game_log)
return self._game_log.current_game[index]
def __str__(self) -> str:
return str(self._game_log.current_game)
def serialize(self) -> List[Mapping]:
"""
Get serialized mappings of logs.
Returns:
List of serialized mappings.
"""
return self._game_log.logs