-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·122 lines (97 loc) · 3.32 KB
/
example.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
import numpy as np
import gym
from utils import *
MF = 0 # Move Forward
TL = 1 # Turn Left
TR = 2 # Turn Right
PK = 3 # Pickup Key
UD = 4 # Unlock Door
def example_use_of_gym_env():
'''
The Coordinate System:
(0,0): Top Left Corner
(x,y): x-th column and y-th row
'''
print('<========== Example Usages ===========> ')
env_path = './envs/example-8x8.env'
# env, info = load_env(env_path) # load an environment
env, info = load_env('./envs/doorkey-8x8-shortcut.env')
print('<Environment Info>\n')
print(info) # Map size
# agent initial position & direction,
# key position, door position, goal position
print('<================>\n')
# Visualize the environment
plot_env(env)
# Get the agent position
agent_pos = env.agent_pos
# Get the agent direction
agent_dir = env.dir_vec # or env.agent_dir
# Get the cell in front of the agent
front_cell = env.front_pos # == agent_pos + agent_dir
# Access the cell at coord: (2,3)
cell = env.grid.get(2, 3) # NoneType, Wall, Key, Goal
# Get the door status
door = env.grid.get(info['door_pos'][0], info['door_pos'][1])
is_open = door.is_open
is_locked = door.is_locked
print(is_locked)
# Determine whether agent is carrying a key
is_carrying = env.carrying is not None
# Take actions
#F
cost, done = step(env, MF) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Moving Forward Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#R
cost, done = step(env, TR) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Turning Right Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#K
cost, done = step(env, PK) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Picking Up Key Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#R
cost, done = step(env, TR) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Turning Right Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#F
cost, done = step(env, MF) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Moving Forward Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#R
cost, done = step(env, TR) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Turning Right Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#F
cost, done = step(env, MF) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Moving Forward Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#U
cost, done = step(env, UD) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Unlocking Door Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#F
cost, done = step(env, MF) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Moving Forward Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
#F
cost, done = step(env, MF) # MF=0, TL=1, TR=2, PK=3, UD=4
print('Moving Forward Costs: {}'.format(cost))
# Visualize the environment
plot_env(env)
# Determine whether we stepped into the goal
if done:
print("Reached Goal")
# The number of steps so far
print('Step Count: {}'.format(env.step_count))
example_use_of_gym_env()