File size: 15,345 Bytes
9897e20 | 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | #pragma once
#include <madrona/components.hpp>
#include <madrona/math.hpp>
#include <madrona/physics.hpp>
#include <madrona/render/ecs.hpp>
#include "consts.hpp"
namespace madrona_gpudrive
{
// Include several madrona types into the simulator namespace for convenience
using madrona::Entity;
using madrona::base::ObjectID;
using madrona::base::Position;
using madrona::base::Rotation;
using madrona::base::Scale;
using madrona::phys::ResponseType;
using madrona::phys::Velocity;
// This enum is used to track the type of each entity
// The order of the enum is important and should not be changed
// The order is {Road types that can be reduced, Road types that cannot be reduced, agent types, other types}
enum class EntityType : uint32_t
{
None,
RoadEdge,
RoadLine,
RoadLane,
CrossWalk,
SpeedBump,
StopSign,
Vehicle,
Pedestrian,
Cyclist,
Padding,
NumTypes,
};
enum class MapType : int32_t
{
LANE_UNDEFINED = 0,
LANE_FREEWAY = 1,
LANE_SURFACE_STREET = 2,
LANE_BIKE_LANE = 3,
// Original definition skips 4
ROAD_LINE_UNKNOWN = 5,
ROAD_LINE_BROKEN_SINGLE_WHITE = 6,
ROAD_LINE_SOLID_SINGLE_WHITE = 7,
ROAD_LINE_SOLID_DOUBLE_WHITE = 8,
ROAD_LINE_BROKEN_SINGLE_YELLOW = 9,
ROAD_LINE_BROKEN_DOUBLE_YELLOW = 10,
ROAD_LINE_SOLID_SINGLE_YELLOW = 11,
ROAD_LINE_SOLID_DOUBLE_YELLOW = 12,
ROAD_LINE_PASSING_DOUBLE_YELLOW = 13,
ROAD_EDGE_UNKNOWN = 14,
ROAD_EDGE_BOUNDARY = 15,
ROAD_EDGE_MEDIAN = 16,
STOP_SIGN = 17,
CROSSWALK = 18,
SPEED_BUMP = 19,
DRIVEWAY = 20, // New datatype in v1.2.0: Driveway entrances
UNKNOWN = -1,
NUM_TYPES = 21,
};
struct AgentID
{
int32_t id;
};
struct VehicleSize
{
float length;
float width;
float height;
};
struct Goal
{
madrona::math::Vector2 position;
};
// 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;
};
struct ResetMap {
int32_t reset;
};
struct DeletedAgents {
int32_t deletedAgents[consts::kMaxAgentCount];
};
struct WorldMeans {
madrona::math::Vector3 mean; // TODO: Z is 0 for now, but can be used for 3D in future
};
const size_t WorldMeansExportSize = 3;
static_assert(sizeof(WorldMeans) == sizeof(float) * WorldMeansExportSize);
struct ClassicAction
{
float acceleration;
float steering;
float headAngle;
};
struct DeltaAction
{
float dx;
float dy;
float dyaw;
};
struct StateAction
{
Position position; // 3 floats
float yaw; // 1 float
Velocity velocity; // 6 floats
};
union Action
{
ClassicAction classic;
DeltaAction delta;
StateAction state;
static inline Action zero()
{
return Action{
.classic = {.acceleration = 0, .steering = 0, .headAngle = 0}};
}
};
const size_t ActionExportSize = 3 + 1 + 6;
static_assert(sizeof(Action) == sizeof(float) * ActionExportSize);
// 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;
};
struct Info
{
int collidedWithRoad;
int collidedWithVehicle;
int collidedWithNonVehicle;
int reachedGoal;
int type;
static inline Info zero()
{
return Info{
.collidedWithRoad = 0,
.collidedWithVehicle = 0,
.collidedWithNonVehicle = 0,
.reachedGoal = 0,
.type = static_cast<int>(EntityType::Padding)};
}
};
const size_t InfoExportSize = 5;
static_assert(sizeof(Info) == sizeof(int) * InfoExportSize);
// Observation state for the current agent.
// Positions are rescaled to the bounds of the play area to assist training.
struct SelfObservation
{
float speed;
VehicleSize vehicle_size;
Goal goal;
float collisionState;
float id;
static inline SelfObservation zero()
{
return SelfObservation{
.speed = 0,
.vehicle_size = {0, 0, 0},
.goal = {.position = {0, 0}},
.collisionState = 0,
.id = -1};
}
};
const size_t SelfObservationExportSize = 8; // 1 + 3 + 2 + 1 + 1
static_assert(sizeof(SelfObservation) == sizeof(float) * SelfObservationExportSize);
struct MapObservation
{
madrona::math::Vector2 position;
Scale scale;
float heading;
float type;
float id;
float mapType;
static inline MapObservation zero()
{
return MapObservation{
.position = {0, 0},
.scale = madrona::math::Diag3x3{0, 0, 0},
.heading = 0,
.type = static_cast<float>(EntityType::None),
.id = -1,
.mapType = static_cast<float>(MapType::UNKNOWN)
};
}
};
const size_t MapObservationExportSize = 9; // 2 + 3 + 1 + 1 + 1 + 1
static_assert(sizeof(MapObservation) == sizeof(float) * MapObservationExportSize);
struct PartnerObservation
{
float speed;
madrona::math::Vector2 position;
float heading;
VehicleSize vehicle_size;
float type;
float id;
static inline PartnerObservation zero() {
return PartnerObservation{
.speed = 0,
.position = {0, 0},
.heading = 0,
.vehicle_size = {0, 0, 0},
.type = static_cast<float>(EntityType::None),
.id = -1};
}
};
// Egocentric observations of other agents
struct PartnerObservations
{
PartnerObservation obs[consts::kMaxAgentCount - 1];
};
const size_t PartnerObservationExportSize = 9; // 1 + 2 + 1 + 3 + 1 + 1
static_assert(sizeof(PartnerObservations) == sizeof(float) *
(consts::kMaxAgentCount - 1) * PartnerObservationExportSize);
struct RoadMapId{
int32_t id;
};
const size_t RoadMapIdExportSize = 1;
static_assert(sizeof(RoadMapId) == sizeof(int) * RoadMapIdExportSize);
struct AgentMapObservations
{
MapObservation obs[consts::kMaxAgentMapObservationsCount];
};
const size_t AgentMapObservationExportSize = MapObservationExportSize;
static_assert(sizeof(AgentMapObservations) ==
sizeof(float) * consts::kMaxAgentMapObservationsCount *
AgentMapObservationExportSize);
struct LidarSample
{
float depth;
float encodedType;
madrona::math::Vector2 position;
};
// Linear depth values and entity type in a circle around the agent
struct Lidar
{
LidarSample samplesCars[consts::numLidarSamples];
LidarSample samplesRoadEdges[consts::numLidarSamples];
LidarSample samplesRoadLines[consts::numLidarSamples];
};
const size_t LidarExportSize = 3 * consts::numLidarSamples * 4;
static_assert(sizeof(Lidar) == sizeof(float) * LidarExportSize);
struct BevObservation
{
float type;
};
struct BevObservations
{
BevObservation obs[consts::bev_rasterization_resolution][consts::bev_rasterization_resolution];
};
const size_t BevObservationExportSize = 1;
static_assert(sizeof(BevObservations) == BevObservationExportSize * sizeof(float) * consts::bev_rasterization_resolution * consts::bev_rasterization_resolution);
// Number of steps remaining in the episode. Allows non-recurrent policies
// to track the progression of time.
struct StepsRemaining
{
uint32_t t;
};
// Can be refactored for rewards
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::kMaxAgentCount - 1];
};
struct Trajectory
{
madrona::math::Vector2 positions[consts::kTrajectoryLength];
madrona::math::Vector2 velocities[consts::kTrajectoryLength];
float headings[consts::kTrajectoryLength];
float valids[consts::kTrajectoryLength];
Action inverseActions[consts::kTrajectoryLength];
static inline void zero(Trajectory& traj)
{
for (int i = 0; i < consts::kTrajectoryLength; i++)
{
traj.positions[i] = {0, 0};
traj.velocities[i] = {0, 0};
traj.headings[i] = 0;
traj.valids[i] = 0;
traj.inverseActions[i] = Action::zero();
}
}
};
const size_t TrajectoryExportSize = 2 * 2 * consts::kTrajectoryLength + 2 * consts::kTrajectoryLength + ActionExportSize * consts::kTrajectoryLength;
static_assert(sizeof(Trajectory) == sizeof(float) * TrajectoryExportSize);
struct Shape
{
int32_t agentEntityCount;
int32_t roadEntityCount;
};
struct ControlledState
{
int32_t controlled; // default: 1
};
struct CollisionDetectionEvent
{
madrona::AtomicI32 hasCollided{false};
};
struct AbsoluteRotation
{
Rotation rotationAsQuat; // x, y, z, w
float rotationFromAxis;
};
struct AbsoluteSelfObservation
{
Position position;
AbsoluteRotation rotation;
Goal goal;
VehicleSize vehicle_size;
float id;
};
const size_t AbsoluteSelfObservationExportSize = 14; // 3 + 5 + 2 + 3 + 1
static_assert(sizeof(AbsoluteSelfObservation) == sizeof(float) * AbsoluteSelfObservationExportSize);
struct MapName
{
char32_t mapName[32];
};
const size_t MapNameExportSize = 32;
static_assert(sizeof(MapName) == sizeof(char32_t) * MapNameExportSize);
struct ScenarioId
{
char32_t scenarioId[32];
};
const size_t ScenarioIdExportSize = 32;
static_assert(sizeof(ScenarioId) == sizeof(char32_t) * ScenarioIdExportSize);
//Metadata struct : using agent IDs.
struct MetaData
{
int32_t isSdc;
int32_t isObjectOfInterest;
int32_t isTrackToPredict;
int32_t difficulty;
static inline void zero(MetaData& metadata)
{
metadata.isSdc = -1;
metadata.isObjectOfInterest = -1;
metadata.isTrackToPredict = -1;
metadata.difficulty = -1;
}
};
const size_t MetaDataExportSize = 4;
static_assert(sizeof(MetaData) == sizeof(int32_t) * MetaDataExportSize);
struct AgentInterface : public madrona::Archetype<
Action,
Reward,
Done,
Info,
// Observations
SelfObservation,
AbsoluteSelfObservation,
PartnerObservations,
AgentMapObservations,
Lidar,
BevObservations,
StepsRemaining,
ResponseType,
Trajectory,
AgentID,
MetaData,
ControlledState // Drive Logic
>
{
};
struct AgentInterfaceEntity
{
madrona::Entity e;
};
// Needed so that the taskgraph doesnt run on InterfaceEntity from roads
struct RoadInterfaceEntity
{
madrona::Entity e;
};
/* ECS Archetypes for the game */
struct CameraAgent : public madrona::Archetype<
Position,
Rotation,
madrona::render::RenderCamera,
madrona::render::Renderable>
{
};
// 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,
ObjectID,
ResponseType,
madrona::phys::broadphase::LeafID,
Velocity,
CollisionDetectionEvent,
// Internal logic state.
Progress,
OtherAgents,
EntityType,
VehicleSize,
Goal,
// Interface
AgentInterfaceEntity,
// 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
>
{
};
struct RoadInterface : public madrona::Archetype<
MapObservation>
{
};
// Generic archetype for entities that need physics but don't have custom
// logic associated with them.
struct PhysicsEntity : public madrona::Archetype<
Position,
Rotation,
Scale,
ObjectID,
ResponseType,
madrona::phys::broadphase::LeafID,
Velocity,
RoadInterfaceEntity,
EntityType,
RoadMapId,
MapType,
madrona::render::Renderable>
{
};
}
|