File size: 6,816 Bytes
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
266
267
268
269
270
271
272
#!/usr/bin/env python3
import argparse
from gym_minigrid.window import Window
from utils import *
import gym
import pickle
from datetime import datetime

episodes = []
record = [False]


def update_caption_with_recording_indicator():
    new_caption = f"Recoding {'ON' if record[0] else 'OFF'}\n------------------\n\n" + window.caption.get_text()
    window.set_caption(new_caption)

def redraw(img):
    if not args.agent_view:
        img = env.render('rgb_array', tile_size=args.tile_size, mask_unobserved=args.mask_unobserved)

    # adds the rocding
    update_caption_with_recording_indicator()

    window.show_img(img)

def start_recording():
    record[0] = True
    print("Recording started")

    episodes[-1][-1]["record"]=True

def reset():
    episodes.append([])
    obs, info = env.reset_with_info()
    record[0] = False
    redraw(obs)

    episodes[-1].append(
        {
            "action": None,
            "info": info,
            "obs": obs,
            "reward": None,
            "done": None,
            "record": record[0],
        }
    )


def step(action):
    if type(action) == np.ndarray:
        obs, reward, done, info = env.step(action)
    else:
        action = [int(action), np.nan, np.nan]
        obs, reward, done, info = env.step(action)

    episodes[-1].append(
        {
            "action": action,
            "info": info,
            "obs": obs,
            "reward": reward,
            "done": done,
            "record": record[0],
        }
    )
    redraw(obs)

    if done:
        print('done!')
        print('Reward=%.2f' % (reward))

        # reset and add initial state to episodes
        reset()

    else:
        print('\nStep=%s' % (env.step_count))


    # filter steps without recording
    episodes_to_save = [[s for s in ep if s["record"]] for ep in episodes]
    episodes_to_save = [ep for ep in episodes_to_save if len(ep) > 0]

    # set first recording step to be as if it was just reset (the real first step)
    for ep_to_save in episodes_to_save:
        ep_to_save[0]["action"]=None
        ep_to_save[0]["reward"]=None
        ep_to_save[0]["done"]=None


    # picle the episodes
    dump_pickle = Path(output_dir) / "episodes.pkl"
    print(f"Saving {len(episodes_to_save)} episodes ({[len(e) for e in episodes_to_save]}) to : {dump_pickle}")

    with open(dump_pickle, 'wb') as f:
        pickle.dump(episodes_to_save, f)


def key_handler(event):

    print('pressed', event.key)

    if event.key == 'r':
        start_recording()
        return

    if event.key == 'escape':
        window.close()
        return

    if event.key == 's':
        reset()
        return

    if event.key == 'tab':
        step(np.array([np.nan, np.nan, np.nan]))
        return

    if event.key == 'shift':
        step(np.array([np.nan, np.nan, np.nan]))
        return

    if event.key == 'left':
        step(env.actions.left)
        return
    if event.key == 'right':
        step(env.actions.right)
        return
    if event.key == 'up':
        step(env.actions.forward)
        return
    if event.key == 't':
        step(env.actions.speak)
        return

    if event.key == '1':
        step(np.array([np.nan, 0, 0]))
        return
    if event.key == '2':
        step(np.array([np.nan, 0, 1]))
        return
    if event.key == '3':
        step(np.array([np.nan, 1, 0]))
        return
    if event.key == '4':
        step(np.array([np.nan, 1, 1]))
        return
    if event.key == '5':
        step(np.array([np.nan, 2, 2]))
        return
    if event.key == '6':
        step(np.array([np.nan, 1, 2]))
        return
    if event.key == '7':
        step(np.array([np.nan, 2, 1]))
        return
    if event.key == '8':
        step(np.array([np.nan, 1, 3]))
        return
    if event.key == 'p':
        step(np.array([np.nan, 3, 3]))
        return

    # Spacebar
    if event.key == ' ':
        step(env.actions.toggle)
        return
    if event.key == '9':
        step(env.actions.pickup)
        return
    if event.key == '0':
        step(env.actions.drop)
        return

    if event.key == 'enter':
        step(env.actions.done)
        return


if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "--env",
        help="gym environment to load",
        # default="SocialAI-AsocialBoxInformationSeekingParamEnv-v1",
        # default="SocialAI-ColorBoxesLLMCSParamEnv-v1",
        default="SocialAI-ColorLLMCSParamEnv-v1",
    )
    parser.add_argument(
        "--seed",
        type=int,
        help="random seed to generate the environment with",
        default=-1
    )
    parser.add_argument(
        "--tile_size",
        type=int,
        help="size at which to render tiles",
        default=32
    )
    parser.add_argument(
        '--agent_view',
        default=False,
        help="draw the agent sees (partially observable view)",
        action='store_true'
    )
    parser.add_argument(
        '--mask-unobserved',
        default=False,
        help="mask cells that are not observed by the agent",
        action='store_true'
    )
    parser.add_argument(
        '--save-dir',
        default="./llm_data/in_context_examples/",
        help="file where to save episodes",
    )
    parser.add_argument(
        '--load',
        default=None,
        help="Load in context examples to append to",
    )
    parser.add_argument(
        '--name',
        default="in_context",
        help="additional name tag for the episodes",
    )
    parser.add_argument(
        '--draw-tree',
        action="store_true",
        help="Draw the sampling treee",
    )

    # Put all env related arguments after --env_args, e.g. --env_args nb_foo 1 is_bar True
    parser.add_argument("--env-args", nargs='*', default=None)

    args = parser.parse_args()

    env = gym.make(args.env, **env_args_str_to_dict(args.env_args))

    timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    output_dir = Path(args.save_dir) / f"{args.name}_{args.env}_{timestamp}"
    os.makedirs(output_dir, exist_ok=True)

    if args.load:
        with open(args.load, 'rb') as f:
            episodes = pickle.load(f)

    if args.draw_tree:
        # draw tree
        env.parameter_tree.draw_tree(
            filename=output_dir / f"/{args.env}_raw_tree",
            ignore_labels=["Num_of_colors"],
        )

    if args.seed >= 0:
        env.seed(args.seed)

    window = Window('gym_minigrid - ' + args.env, figsize=(6, 4))
    window.reg_key_handler(key_handler)
    env.window = window

    reset()
    # # a trick to make the first image appear right away
    # # this action is not saved
    # obs, _, _, _ = env.step(np.array([np.nan, np.nan, np.nan]))
    # redraw(obs)

    # Blocking event loop
    window.show(block=True)