-
Notifications
You must be signed in to change notification settings - Fork 7
/
types.hpp
297 lines (255 loc) · 7.66 KB
/
types.hpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#pragma once
#include <madrona/components.hpp>
#include <madrona/math.hpp>
#include <madrona/rand.hpp>
#include <madrona/physics.hpp>
#include <madrona/render/ecs.hpp>
#include "consts.hpp"
namespace madEscape {
// Include several madrona types into the simulator namespace for convenience
using madrona::Entity;
using madrona::RandKey;
using madrona::CountT;
using madrona::base::Position;
using madrona::base::Rotation;
using madrona::base::Scale;
using madrona::base::ObjectID;
using madrona::phys::Velocity;
using madrona::phys::ResponseType;
using madrona::phys::ExternalForce;
using madrona::phys::ExternalTorque;
// WorldReset is a per-world singleton component that causes the current
// episode to be terminated and the world regenerated
// (Singleton components like WorldReset can be accessed via Context::singleton
// (eg ctx.singleton<WorldReset>().reset = 1)
struct WorldReset {
int32_t reset;
};
// Discrete action component. Ranges are defined by consts::numMoveBuckets (5),
// repeated here for clarity
struct Action {
int32_t moveAmount; // [0, 3]
int32_t moveAngle; // [0, 7]
int32_t rotate; // [-2, 2]
int32_t grab; // 0 = do nothing, 1 = grab / release
};
// Per-agent reward
// Exported as an [N * A, 1] float tensor to training code
struct Reward {
float v;
};
// Per-agent component that indicates that the agent's episode is finished
// This is exported per-agent for simplicity in the training code
struct Done {
// Currently bool components are not supported due to
// padding issues, so Done is an int32_t
int32_t v;
};
// Observation state for the current agent.
// Positions are rescaled to the bounds of the play area to assist training.
struct SelfObservation {
float roomX;
float roomY;
float globalX;
float globalY;
float globalZ;
float maxY;
float theta;
float isGrabbing;
};
// The state of the world is passed to each agent in terms of egocentric
// polar coordinates. theta is degrees off agent forward.
struct PolarObservation {
float r;
float theta;
};
struct PartnerObservation {
PolarObservation polar;
float isGrabbing;
};
// Egocentric observations of other agents
struct PartnerObservations {
PartnerObservation obs[consts::numAgents - 1];
};
// PartnerObservations is exported as a
// [N, A, consts::numAgents - 1, 3] // tensor to pytorch
static_assert(sizeof(PartnerObservations) == sizeof(float) *
(consts::numAgents - 1) * 3);
// Per-agent egocentric observations for the interactable entities
// in the current room.
struct EntityObservation {
PolarObservation polar;
float encodedType;
};
struct RoomEntityObservations {
EntityObservation obs[consts::maxEntitiesPerRoom];
};
// RoomEntityObservations is exported as a
// [N, A, maxEntitiesPerRoom, 3] tensor to pytorch
static_assert(sizeof(RoomEntityObservations) == sizeof(float) *
consts::maxEntitiesPerRoom * 3);
// Observation of the current room's door. It's relative position and
// whether or not it is ope
struct DoorObservation {
PolarObservation polar;
float isOpen; // 1.0 when open, 0.0 when closed.
};
struct LidarSample {
float depth;
float encodedType;
};
// Linear depth values and entity type in a circle around the agent
struct Lidar {
LidarSample samples[consts::numLidarSamples];
};
// Number of steps remaining in the episode. Allows non-recurrent policies
// to track the progression of time.
struct StepsRemaining {
uint32_t t;
};
// Tracks progress the agent has made through the challenge, used to add
// reward when more progress has been made
struct Progress {
float maxY;
};
// Per-agent component storing Entity IDs of the other agents. Used to
// build the egocentric observations of their state.
struct OtherAgents {
madrona::Entity e[consts::numAgents - 1];
};
// Tracks if an agent is currently grabbing another entity
struct GrabState {
Entity constraintEntity;
};
// This enum is used to track the type of each entity for the purposes of
// classifying the objects hit by each lidar sample.
enum class EntityType : uint32_t {
None,
Button,
Cube,
Wall,
Agent,
Door,
NumTypes,
};
// A per-door component that tracks whether or not the door should be open.
struct OpenState {
bool isOpen;
};
// Linked buttons that control the door opening and whether or not the door
// should remain open after the buttons are pressed once.
struct DoorProperties {
Entity buttons[consts::maxEntitiesPerRoom];
int32_t numButtons;
bool isPersistent;
};
// Similar to OpenState, true during frames where a button is pressed
struct ButtonState {
bool isPressed;
};
// Room itself is not a component but is used by the singleton
// component "LevelState" (below) to represent the state of the full level
struct Room {
// These are entities the agent will interact with
Entity entities[consts::maxEntitiesPerRoom];
// The walls that separate this room from the next
Entity walls[2];
// The door the agents need to figure out how to lower
Entity door;
};
// A singleton component storing the state of all the rooms in the current
// randomly generated level
struct LevelState {
Room rooms[consts::numRooms];
};
/* ECS Archetypes for the game */
// There are 2 Agents in the environment trying to get to the destination
struct Agent : public madrona::Archetype<
// Basic components required for physics. Note that the current physics
// implementation requires archetypes to have these components first
// in this exact order.
Position,
Rotation,
Scale,
Velocity,
ObjectID,
ResponseType,
madrona::phys::solver::SubstepPrevState,
madrona::phys::solver::PreSolvePositional,
madrona::phys::solver::PreSolveVelocity,
ExternalForce,
ExternalTorque,
madrona::phys::broadphase::LeafID,
// Internal logic state.
GrabState,
Progress,
OtherAgents,
EntityType,
// Input
Action,
// Observations
SelfObservation,
PartnerObservations,
RoomEntityObservations,
DoorObservation,
Lidar,
StepsRemaining,
// Reward, episode termination
Reward,
Done,
// Visualization: In addition to the fly camera, src/viewer.cpp can
// view the scene from the perspective of entities with this component
madrona::render::RenderCamera,
// All entities with the Renderable component will be drawn by the
// viewer and batch renderer
madrona::render::Renderable
> {};
// Archetype for the doors blocking the end of each challenge room
struct DoorEntity : public madrona::Archetype<
Position,
Rotation,
Scale,
Velocity,
ObjectID,
ResponseType,
madrona::phys::solver::SubstepPrevState,
madrona::phys::solver::PreSolvePositional,
madrona::phys::solver::PreSolveVelocity,
ExternalForce,
ExternalTorque,
madrona::phys::broadphase::LeafID,
OpenState,
DoorProperties,
EntityType,
madrona::render::Renderable
> {};
// Archetype for the button objects that open the doors
// Buttons don't have collision but are rendered
struct ButtonEntity : public madrona::Archetype<
Position,
Rotation,
Scale,
ObjectID,
ButtonState,
EntityType,
madrona::render::Renderable
> {};
// Generic archetype for entities that need physics but don't have custom
// logic associated with them.
struct PhysicsEntity : public madrona::Archetype<
Position,
Rotation,
Scale,
Velocity,
ObjectID,
ResponseType,
madrona::phys::solver::SubstepPrevState,
madrona::phys::solver::PreSolvePositional,
madrona::phys::solver::PreSolveVelocity,
ExternalForce,
ExternalTorque,
madrona::phys::broadphase::LeafID,
EntityType,
madrona::render::Renderable
> {};
}