-
Notifications
You must be signed in to change notification settings - Fork 42
/
example.py
executable file
·192 lines (148 loc) · 6.7 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
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
"""This file contains multiple examples of how you might use Holodeck."""
import numpy as np
import holodeck
from holodeck import agents
from holodeck.environments import *
from holodeck.sensors import Sensors
def uav_example():
"""A basic example of how to use the UAV agent."""
env = holodeck.make("UrbanCity")
# This changes the control scheme for the uav
env.set_control_scheme("uav0", ControlSchemes.UAV_ROLL_PITCH_YAW_RATE_ALT)
for i in range(10):
env.reset()
# This command tells the UAV to not roll or pitch, but to constantly yaw left at 10m altitude.
command = np.array([0, 0, 2, 10])
for _ in range(1000):
state, reward, terminal, _ = env.step(command)
# To access specific sensor data:
pixels = state[Sensors.PIXEL_CAMERA]
velocity = state[Sensors.VELOCITY_SENSOR]
# For a full list of sensors the UAV has, view the README
# It is useful to know that you can control the AgentFollower camera(what you see) by pressing V to toggle spectator
# mode. This detaches the camera and allows you to move freely about the world.
# You can also press C to snap to the location of the pixel camera to see the world from the perspective of the
# agent. See the Controls section of the ReadMe for more details.
def sphere_example():
"""A basic example of how to use the sphere agent."""
env = holodeck.make("MazeWorld")
# This command is to constantly rotate to the right
command = 2
for i in range(10):
env.reset()
for _ in range(1000):
state, reward, terminal, _ = env.step(command)
# To access specific sensor data:
pixels = state[Sensors.PIXEL_CAMERA]
orientation = state[Sensors.ORIENTATION_SENSOR]
# For a full list of sensors the sphere robot has, view the README
def android_example():
"""A basic example of how to use the android agent."""
env = holodeck.make("AndroidPlayground")
# The Android's command is a 94 length vector representing torques to be applied at each of his joints
command = np.ones(94) * 10
for i in range(10):
env.reset()
for j in range(1000):
if j % 50 == 0:
command *= -1
state, reward, terminal, _ = env.step(command)
# To access specific sensor data:
pixels = state[Sensors.PIXEL_CAMERA]
orientation = state[Sensors.ORIENTATION_SENSOR]
# For a full list of sensors the android has, view the README
def multi_agent_example():
"""A basic example of using multiple agents"""
env = holodeck.make("UrbanCity")
cmd0 = np.array([0, 0, -2, 10])
cmd1 = np.array([0, 0, 5, 10])
for i in range(10):
env.reset()
# This will queue up a new agent to spawn into the environment, given that the coordinates are not blocked.
sensors = [Sensors.PIXEL_CAMERA, Sensors.LOCATION_SENSOR, Sensors.VELOCITY_SENSOR]
agent = AgentDefinition("uav1", agents.UavAgent, sensors)
env.spawn_agent(agent, [1, 1, 5])
env.set_control_scheme("uav0", ControlSchemes.UAV_ROLL_PITCH_YAW_RATE_ALT)
env.set_control_scheme("uav1", ControlSchemes.UAV_ROLL_PITCH_YAW_RATE_ALT)
env.tick() # Tick the environment once so the second agent spawns before we try to interact with it.
env.act("uav0", cmd0)
env.act("uav1", cmd1)
for _ in range(1000):
states = env.tick()
uav0_terminal = states["uav0"][Sensors.TERMINAL]
uav1_reward = states["uav1"][Sensors.REWARD]
def world_command_examples():
"""A few examples to showcase commands for manipulating the worlds."""
env = holodeck.make("MazeWorld")
# This is the unaltered MazeWorld
for _ in range(300):
_ = env.tick()
env.reset()
# The set_day_time_command sets the hour between 0 and 23 (military time). This example sets it to 6 AM.
env.set_day_time(6)
for _ in range(300):
_ = env.tick()
env.reset() # reset() undoes all alterations to the world
# The start_day_cycle command starts rotating the sun to emulate day cycles.
# The parameter sets the day length in minutes.
env.start_day_cycle(5)
for _ in range(1500):
_ = env.tick()
env.reset()
# The set_fog_density changes the density of the fog in the world. 1 is the maximum density.
env.set_fog_density(.25)
for _ in range(300):
_ = env.tick()
env.reset()
# The set_weather_command changes the weather in the world. The two available options are "rain" and "cloudy".
# The rainfall particle system is attached to the agent, so the rain particles will only be found around each agent.
# Every world is clear by default.
env.set_weather("rain")
for _ in range(500):
_ = env.tick()
env.reset()
env.set_weather("cloudy")
for _ in range(500):
_ = env.tick()
env.reset()
env.teleport_camera([1000, 1000, 1000], [0, 0, 0])
for _ in range(500):
_ = env.tick()
env.reset()
def editor_example():
"""This editor example shows how to interact with holodeck worlds while they are being built
in the Unreal Engine. Most people that use holodeck will not need this.
"""
sensors = [Sensors.PIXEL_CAMERA, Sensors.LOCATION_SENSOR, Sensors.VELOCITY_SENSOR]
agent = AgentDefinition("uav0", agents.UavAgent, sensors)
env = HolodeckEnvironment(agent, start_world=False)
env.agents["uav0"].set_control_scheme(1)
command = [0, 0, 10, 50]
for i in range(10):
env.reset()
for _ in range(1000):
state, reward, terminal, _ = env.step(command)
def editor_multi_agent_example():
"""This editor example shows how to interact with holodeck worlds that have multiple agents.
This is specifically for when working with UE4 directly and not a prebuilt binary.
"""
agent_definitions = [
AgentDefinition("uav0", agents.UavAgent, [Sensors.PIXEL_CAMERA, Sensors.LOCATION_SENSOR]),
AgentDefinition("uav1", agents.UavAgent, [Sensors.LOCATION_SENSOR, Sensors.VELOCITY_SENSOR])
]
env = HolodeckEnvironment(agent_definitions, start_world=False)
cmd0 = np.array([0, 0, -2, 10])
cmd1 = np.array([0, 0, 5, 10])
for i in range(10):
env.reset()
env.act("uav0", cmd0)
env.act("uav1", cmd1)
for _ in range(1000):
states = env.tick()
uav0_terminal = states["uav0"][Sensors.TERMINAL]
uav1_reward = states["uav1"][Sensors.REWARD]
if __name__ == "__main__":
if 'DefaultWorlds' not in holodeck.installed_packages():
holodeck.install("DefaultWorlds")
print(holodeck.package_info("DefaultWorlds"))
uav_example()