-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_random_agent.py
53 lines (40 loc) · 1.55 KB
/
run_random_agent.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
"""Try Craft environment with random agent."""
from __future__ import division
from __future__ import print_function
import numpy as np
import time
import env_factory
def run_loop(env, n_steps, visualise=False):
possible_actions = env.action_specs()
observations = env.reset()
for t in xrange(n_steps):
# Random action
action = np.random.choice(possible_actions.values())
# Step (this will plot if visualise is True)
reward, done, observations = env.step(action)
if visualise:
env.render_matplotlib(frame=observations['image'])
else:
print("[{}] reward={} done={} \n observations: {}".format(
t, reward, done, observations))
if reward:
rewarding_frame = observations['image'].copy()
rewarding_frame[:40] *= np.array([0, 1, 0])
env.render_matplotlib(frame=rewarding_frame, delta_time=0.7)
print("[{}] Got a rewaaaard! {:.1f}".format(t, reward))
elif done:
env.render_matplotlib(
frame=np.zeros_like(observations['image']), delta_time=0.3)
print("[{}] Finished with nothing... Reset".format(t))
def main():
visualise = True
recipes_path = "resources/recipes.yaml"
hints_path = "resources/hints.yaml"
env_sampler = env_factory.EnvironmentFactory(
recipes_path, hints_path, max_steps=100, reuse_environments=False,
visualise=visualise)
env = env_sampler.sample_environment(task_name='get[rock]')
print("Environment: task {}: {}".format(env.task_name, env.task))
run_loop(env, 100 * 3, visualise=visualise)
if __name__ == '__main__':
main()