File size: 37,048 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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 | #include <iostream>
#include <algorithm>
#include <limits>
#include <madrona/mw_gpu_entry.hpp>
#include <madrona/physics.hpp>
#include "level_gen.hpp"
#include "obb.hpp"
#include "sim.hpp"
#include "utils.hpp"
#include "rasterizer.hpp"
#include "knn.hpp"
#include "dynamics.hpp"
using namespace madrona;
using namespace madrona::math;
using namespace madrona::phys;
namespace RenderingSystem = madrona::render::RenderingSystem;
namespace madrona_gpudrive {
CountT getCurrentStep(const StepsRemaining &stepsRemaining) {
return consts::episodeLen - stepsRemaining.t;
}
// Register all the ECS components and archetypes that will be
// used in the simulation
void Sim::registerTypes(ECSRegistry ®istry, const Config &cfg)
{
base::registerTypes(registry);
phys::PhysicsSystem::registerTypes(registry);
RenderingSystem::registerTypes(registry, cfg.renderBridge);
registry.registerComponent<Action>();
registry.registerComponent<SelfObservation>();
registry.registerComponent<MapObservation>();
registry.registerComponent<AgentMapObservations>();
registry.registerComponent<Reward>();
registry.registerComponent<Done>();
registry.registerComponent<Progress>();
registry.registerComponent<OtherAgents>();
registry.registerComponent<PartnerObservations>();
registry.registerComponent<Lidar>();
registry.registerComponent<BevObservations>();
registry.registerComponent<StepsRemaining>();
registry.registerComponent<EntityType>();
registry.registerComponent<VehicleSize>();
registry.registerComponent<Goal>();
registry.registerComponent<Trajectory>();
registry.registerComponent<ControlledState>();
registry.registerComponent<CollisionDetectionEvent>();
registry.registerComponent<AbsoluteSelfObservation>();
registry.registerComponent<Info>();
registry.registerComponent<AgentInterfaceEntity>();
registry.registerComponent<RoadInterfaceEntity>();
registry.registerComponent<AgentID>();
registry.registerComponent<RoadMapId>();
registry.registerComponent<MapType>();
registry.registerComponent<MetaData>();
registry.registerSingleton<WorldReset>();
registry.registerSingleton<Shape>();
registry.registerSingleton<Map>();
registry.registerSingleton<ResetMap>();
registry.registerSingleton<WorldMeans>();
registry.registerSingleton<DeletedAgents>();
registry.registerSingleton<MapName>();
registry.registerSingleton<ScenarioId>();
registry.registerArchetype<Agent>();
registry.registerArchetype<PhysicsEntity>();
registry.registerArchetype<CameraAgent>();
registry.registerArchetype<AgentInterface>();
registry.registerArchetype<RoadInterface>();
registry.exportSingleton<WorldReset>((uint32_t)ExportID::Reset);
registry.exportSingleton<Shape>((uint32_t)ExportID::Shape);
registry.exportSingleton<Map>((uint32_t)ExportID::Map);
registry.exportSingleton<ResetMap>((uint32_t)ExportID::ResetMap);
registry.exportSingleton<WorldMeans>((uint32_t)ExportID::WorldMeans);
registry.exportSingleton<DeletedAgents>((uint32_t)ExportID::DeletedAgents);
registry.exportSingleton<MapName>((uint32_t)ExportID::MapName);
registry.exportSingleton<ScenarioId>((uint32_t)ExportID::ScenarioId);
registry.exportColumn<AgentInterface, Action>(
(uint32_t)ExportID::Action);
registry.exportColumn<AgentInterface, SelfObservation>(
(uint32_t)ExportID::SelfObservation);
registry.exportColumn<AgentInterface, AgentMapObservations>(
(uint32_t)ExportID::AgentMapObservations);
registry.exportColumn<RoadInterface, MapObservation>(
(uint32_t)ExportID::MapObservation);
registry.exportColumn<AgentInterface, PartnerObservations>(
(uint32_t)ExportID::PartnerObservations);
registry.exportColumn<AgentInterface, Lidar>(
(uint32_t)ExportID::Lidar);
registry.exportColumn<AgentInterface, BevObservations>(
(uint32_t)ExportID::BevObservations);
registry.exportColumn<AgentInterface, StepsRemaining>(
(uint32_t)ExportID::StepsRemaining);
registry.exportColumn<AgentInterface, Reward>(
(uint32_t)ExportID::Reward);
registry.exportColumn<AgentInterface, Done>(
(uint32_t)ExportID::Done);
registry.exportColumn<AgentInterface, ControlledState>(
(uint32_t) ExportID::ControlledState);
registry.exportColumn<AgentInterface, AbsoluteSelfObservation>(
(uint32_t)ExportID::AbsoluteSelfObservation);
registry.exportColumn<AgentInterface, Info>(
(uint32_t)ExportID::Info);
registry.exportColumn<AgentInterface, ResponseType>(
(uint32_t)ExportID::ResponseType);
registry.exportColumn<AgentInterface, Trajectory>(
(uint32_t)ExportID::Trajectory);
registry.exportColumn<AgentInterface, MetaData>(
(uint32_t)ExportID::MetaData);
}
static inline void cleanupWorld(Engine &ctx) {
destroyWorld(ctx);
}
static inline void initWorld(Engine &ctx)
{
phys::PhysicsSystem::reset(ctx);
// Assign a new episode ID
EpisodeManager &episode_mgr = *ctx.data().episodeMgr;
int32_t episode_idx = episode_mgr.curEpisode.fetch_add<sync::relaxed>(1);
ctx.data().rng = RNG::make(episode_idx);
ctx.data().curEpisodeIdx = episode_idx;
if(ctx.singleton<ResetMap>().reset == 1)
{
createPersistentEntities(ctx);
ctx.singleton<ResetMap>().reset = 0;
phys::PhysicsSystem::reset(ctx);
}
// Defined in src/level_gen.hpp / src/level_gen.cpp
resetWorld(ctx);
}
// This system runs in TaskGraphID::Reset and checks if the code external to the
// application has forced a reset by writing to the WorldReset singleton. If a
// reset is needed, cleanup the existing world and generate a new one.
inline void resetSystem(Engine &ctx, WorldReset &reset)
{
if (reset.reset == 0)
{
return;
}
reset.reset = 0;
auto resetMap = ctx.singleton<ResetMap>();
if (resetMap.reset == 1)
{
cleanupWorld(ctx);
}
initWorld(ctx);
}
inline void collectSelfObsSystem(Engine &ctx,
const VehicleSize &size,
const Position &pos,
const Rotation &rot,
const Velocity &vel,
const Goal &goal,
const CollisionDetectionEvent& collisionEvent,
const AgentInterfaceEntity &agent_iface)
{
auto &self_obs = ctx.get<SelfObservation>(agent_iface.e);
self_obs.speed = vel.linear.length();
self_obs.vehicle_size = size;
auto goalPos = goal.position - pos.xy();
self_obs.goal.position = rot.inv().rotateVec({goalPos.x, goalPos.y, 0}).xy();
auto hasCollided = collisionEvent.hasCollided.load_relaxed();
self_obs.collisionState = hasCollided ? 1.f : 0.f;
self_obs.id = ctx.get<AgentID>(agent_iface.e).id;
}
inline void collectPartnerObsSystem(Engine &ctx,
const Position &pos,
const Rotation &rot,
const OtherAgents &other_agents,
const AgentInterfaceEntity &agent_iface)
{
if(ctx.data().params.disableClassicalObs)
return;
auto &partner_obs = ctx.get<PartnerObservations>(agent_iface.e);
CountT arrIndex = 0; CountT agentIdx = 0;
while(agentIdx < ctx.data().numAgents - 1)
{
Entity other = other_agents.e[agentIdx++];
const Position &other_position = ctx.get<Position>(other);
const Velocity &other_velocity = ctx.get<Velocity>(other);
const Rotation &other_rot = ctx.get<Rotation>(other);
const VehicleSize &other_size = ctx.get<VehicleSize>(other);
Vector2 relative_pos = (other_position - pos).xy();
relative_pos = rot.inv().rotateVec({relative_pos.x, relative_pos.y, 0}).xy();
float relative_speed = other_velocity.linear.length(); // Design decision: return the speed of the other agent directly
Rotation relative_orientation = rot.inv() * other_rot;
float relative_heading = utils::quatToYaw(relative_orientation);
if(relative_pos.length() > ctx.data().params.observationRadius)
{
continue;
}
partner_obs.obs[arrIndex++] = {
.speed = relative_speed,
.position = relative_pos,
.heading = relative_heading,
.vehicle_size = other_size,
.type = (float)ctx.get<EntityType>(other),
.id = (float)ctx.get<AgentID>(ctx.get<AgentInterfaceEntity>(other).e).id
};
}
while(arrIndex < consts::kMaxAgentCount - 1) {
partner_obs.obs[arrIndex++] = PartnerObservation::zero();
}
}
inline void collectMapObservationsSystem(Engine &ctx,
const Position &pos,
const Rotation &rot,
const AgentInterfaceEntity &agent_iface)
{
if(ctx.data().params.disableClassicalObs)
return;
auto &map_obs = ctx.get<AgentMapObservations>(agent_iface.e);
const auto alg = ctx.data().params.roadObservationAlgorithm;
if (alg == FindRoadObservationsWith::KNearestEntitiesWithRadiusFiltering) {
selectKNearestRoadEntities<consts::kMaxAgentMapObservationsCount>(
ctx, rot, pos.xy(), map_obs.obs);
return;
}
assert(alg == FindRoadObservationsWith::AllEntitiesWithRadiusFiltering);
utils::ReferenceFrame referenceFrame(pos.xy(), rot);
CountT arrIndex = 0; CountT roadIdx = 0;
while(roadIdx < ctx.data().numRoads && arrIndex < consts::kMaxAgentMapObservationsCount) {
Entity road = ctx.data().roads[roadIdx++];
auto roadPos = ctx.get<Position>(road);
auto roadRot = ctx.get<Rotation>(road);
auto dist = referenceFrame.distanceTo(roadPos);
if (dist > ctx.data().params.observationRadius) {
continue;
}
map_obs.obs[arrIndex] = referenceFrame.observationOf(
roadPos, roadRot, ctx.get<Scale>(road), ctx.get<EntityType>(road), static_cast<float>(ctx.get<RoadMapId>(road).id), ctx.get<MapType>(road));
arrIndex++;
}
while (arrIndex < consts::kMaxAgentMapObservationsCount) {
map_obs.obs[arrIndex++] = MapObservation::zero();
}
}
// Make the agents easier to control by zeroing out their velocity
// after each step.
inline void agentZeroVelSystem(Engine &,
Velocity &vel)
{
vel.linear.x = 0;
vel.linear.y = 0;
vel.linear.z = 0;
vel.angular = Vector3::zero();
}
inline void movementSystem(Engine &e,
const AgentInterfaceEntity &agent_iface,
VehicleSize &size,
Rotation &rotation,
Position &position,
Velocity &velocity,
CollisionDetectionEvent& collisionEvent,
const ResponseType &responseType) {
if (collisionEvent.hasCollided.load_relaxed()) {
switch (e.data().params.collisionBehaviour) {
case CollisionBehaviour::AgentStop:
e.get<Done>(agent_iface.e).v = 1;
agentZeroVelSystem(e, velocity);
break;
case CollisionBehaviour::AgentRemoved:
e.get<Done>(agent_iface.e).v = 1;
position = consts::kPaddingPosition;
agentZeroVelSystem(e, velocity);
break;
case CollisionBehaviour::Ignore:
// Reset collision state at the start of each timestep.
// This ensures the collision state is only true if the agent collided in the current timestep.
collisionEvent.hasCollided.store_relaxed(0); // Reset the collision state.
Info& info = e.get<Info>(agent_iface.e);
info.collidedWithRoad = info.collidedWithVehicle = info.collidedWithNonVehicle = 0;
break;
}
}
const auto &controlledState = e.get<ControlledState>(agent_iface.e);
if (responseType == ResponseType::Static) {
// Do nothing. The agent is static.
// Agent can only be static if isStaticAgentControlled is set to true.
return;
}
if (e.get<Done>(agent_iface.e).v && responseType != ResponseType::Static) {
// Case: Agent has not collided but is done.
// This can only happen if the agent has reached goal or the episode has ended.
// In that case we teleport the agent. The agent will not collide with anything.
position = consts::kPaddingPosition;
velocity.linear.x = 0;
velocity.linear.y = 0;
velocity.linear.z = 0;
velocity.angular = Vector3::zero();
return;
}
if (controlledState.controlled) {
Action &action = e.get<Action>(agent_iface.e);
switch (e.data().params.dynamicsModel) {
case DynamicsModel::InvertibleBicycle:
{
forwardBicycleModel(action, rotation, position, velocity);
break;
}
case DynamicsModel::DeltaLocal:
{
forwardDeltaModel(action, rotation, position, velocity);
break;
}
case DynamicsModel::Classic:
{
forwardKinematics(action, size, rotation, position, velocity);
break;
}
case DynamicsModel::State:
{
forwardStateModel(action, rotation, position, velocity);
break;
}
}
} else {
// Follow expert trajectory
const Trajectory &trajectory = e.get<Trajectory>(agent_iface.e);
CountT curStepIdx = getCurrentStep(e.get<StepsRemaining>(agent_iface.e));
position.x = trajectory.positions[curStepIdx].x;
position.y = trajectory.positions[curStepIdx].y;
position.z = 1;
velocity.linear.x = trajectory.velocities[curStepIdx].x;
velocity.linear.y = trajectory.velocities[curStepIdx].y;
velocity.linear.z = 0;
velocity.angular = Vector3::zero();
rotation = Quat::angleAxis(trajectory.headings[curStepIdx], madrona::math::up);
}
}
static inline float encodeType(EntityType type)
{
return (float)type;
}
// Launches consts::numLidarSamples per agent.
// This system is specially optimized in the GPU version:
// a warp of threads is dispatched for each invocation of the system
// and each thread in the warp traces one lidar ray for the agent.
inline void lidarSystem(Engine &ctx, Entity e, const AgentInterfaceEntity &agent_iface,
EntityType &entityType) {
Lidar &lidar = ctx.get<Lidar>(agent_iface.e);
const Action &action = ctx.get<Action>(agent_iface.e);
Vector3 pos = ctx.get<Position>(e);
Quat rot = ctx.get<Rotation>(e);
auto &bvh = ctx.singleton<broadphase::BVH>();
Vector3 agent_fwd = rot.rotateVec(math::fwd);
Vector3 right = rot.rotateVec(math::right);
auto traceRay = [&](int32_t idx, float offset, LidarSample *samples) {
// float theta = 2.f * math::pi * (
// float(idx) / float(consts::numLidarSamples));
float head_angle = ctx.get<ControlledState>(agent_iface.e).controlled ? action.classic.headAngle : 0.f;
float theta = consts::lidarAngle * (2 * float(idx) / float(consts::numLidarSamples) - 1) + head_angle;
float x = cosf(theta);
float y = sinf(theta);
Vector3 ray_dir = (x * right + y * agent_fwd).normalize();
float hit_t;
Vector3 hit_normal;
Entity hit_entity =
bvh.traceRay(pos + offset * math::up, ray_dir, &hit_t,
&hit_normal, consts::lidarDistance);
if (hit_entity == Entity::none()) {
samples[idx] = {
.depth = 0.f,
.encodedType = encodeType(EntityType::None),
.position = {0.f, 0.f},
};
} else {
EntityType entity_type = ctx.get<EntityType>(hit_entity);
samples[idx] = {
.depth = hit_t,
.encodedType = encodeType(entity_type),
.position = {hit_t * x,
hit_t * y},
};
}
};
// MADRONA_GPU_MODE guards GPU specific logic
#ifdef MADRONA_GPU_MODE
// Can use standard cuda variables like threadIdx for
// warp level programming
int32_t idx = threadIdx.x % 32;
while (idx < consts::numLidarSamples) {
traceRay(idx, consts::lidarCarOffset, lidar.samplesCars);
traceRay(idx, consts::lidarRoadEdgeOffset, lidar.samplesRoadEdges);
traceRay(idx, consts::lidarRoadLineOffset, lidar.samplesRoadLines);
idx += 32;
}
#else
for (CountT i = 0; i < consts::numLidarSamples; i++) {
traceRay(i, consts::lidarCarOffset, lidar.samplesCars);
traceRay(i, consts::lidarRoadEdgeOffset, lidar.samplesRoadEdges);
traceRay(i, consts::lidarRoadLineOffset, lidar.samplesRoadLines);
}
#endif
}
inline void collectBevObservationsSystem(Engine &ctx,
const Position &pos,
const Rotation &rot,
const OtherAgents &other_agents,
const AgentInterfaceEntity &agent_iface)
{
if(ctx.data().params.disableClassicalObs)
return;
auto &bev_obs = ctx.get<BevObservations>(agent_iface.e);
for (size_t i = 0; i < consts::bev_rasterization_resolution; i++)
{
for (size_t j = 0; j < consts::bev_rasterization_resolution; j++)
{
bev_obs.obs[i][j].type = 0;
}
}
utils::ReferenceFrame referenceFrame(pos.xy(), rot);
// Roads
CountT roadIdx = 0;
CountT arrIndex = 0;
while (roadIdx < ctx.data().numRoads && arrIndex < consts::kMaxAgentMapObservationsCount)
{
Entity road = ctx.data().roads[roadIdx++];
auto roadPos = ctx.get<Position>(road);
auto roadRot = ctx.get<Rotation>(road);
const MapObservation &map_obs = referenceFrame.observationOf(
roadPos,
roadRot,
ctx.get<Scale>(road),
ctx.get<EntityType>(road),
static_cast<float>(ctx.get<RoadMapId>(road).id),
ctx.get<MapType>(road)
);
auto dist = referenceFrame.distanceTo(roadPos);
if (dist > ctx.data().params.observationRadius)
continue;
madrona::math::Vector2 rel_pos = map_obs.position;
float rel_yaw = map_obs.heading;
auto new_scale = map_obs.scale;
new_scale.d1 = std::max( //Ensure minimum segment width
map_obs.scale.d1,
(2 * ctx.data().params.observationRadius / consts::bev_rasterization_resolution)
);
rasterizer::rasterizeRotatedRectangle(
bev_obs,
rel_pos,
rel_yaw,
new_scale.d0,
new_scale.d1,
map_obs.type,
ctx.data().params.observationRadius,
consts::bev_rasterization_resolution
);
arrIndex++;
}
// Other agents
CountT agentIdx = 0;
while (agentIdx < ctx.data().numAgents - 1)
{
Entity other = other_agents.e[agentIdx++];
const Position &other_position = ctx.get<Position>(other);
const Rotation &other_rot = ctx.get<Rotation>(other);
const VehicleSize &other_size = ctx.get<VehicleSize>(other);
const auto type = static_cast<size_t>(ctx.get<EntityType>(other));
Vector2 relative_pos = (other_position - pos).xy();
relative_pos = rot.inv().rotateVec({relative_pos.x, relative_pos.y, 0}).xy();
Rotation relative_orientation = rot.inv() * other_rot;
float relative_heading = utils::quatToYaw(relative_orientation);
if(relative_pos.length() > ctx.data().params.observationRadius)
continue;
rasterizer::rasterizeRotatedRectangle(
bev_obs,
relative_pos,
relative_heading,
other_size.length,
other_size.width,
type,
ctx.data().params.observationRadius,
consts::bev_rasterization_resolution
);
}
}
// Computes reward for each agent and keeps track of the max distance achieved
// so far through the challenge. Continuous reward is provided for any new
// distance achieved.
inline void rewardSystem(Engine &ctx,
const Position &position,
const Goal &goal,
const AgentInterfaceEntity &agent_iface)
{
Reward &out_reward = ctx.get<Reward>(agent_iface.e);
const auto &rewardType = ctx.data().params.rewardParams.rewardType;
if(rewardType == RewardType::DistanceBased)
{
float dist = (position.xy() - goal.position).length();
float reward = -dist;
out_reward.v = reward;
}
else if(rewardType == RewardType::OnGoalAchieved)
{
float dist = (position.xy() - goal.position).length();
float reward = (dist < ctx.data().params.rewardParams.distanceToGoalThreshold) ? 1.f : 0.f;
out_reward.v = reward;
}
else if(rewardType == RewardType::Dense)
{
// TODO: Implement full trajectory reward
assert(false);
}
// Just in case agents do something crazy, clamp total reward
// out_reward.v = fmaxf(fminf(out_reward.v, 1.f), 0.f);
}
inline void stepTrackerSystem(Engine &ctx, const AgentInterfaceEntity &agent_iface) {
StepsRemaining &stepsRemaining = ctx.get<StepsRemaining>(agent_iface.e);
--stepsRemaining.t;
}
// Keep track of the number of steps remaining in the episode and
// notify training that an episode has completed by
// setting done = 1 on the final step of the episode
inline void doneSystem(Engine &ctx,
const Position &position,
const Goal &goal,
AgentInterfaceEntity &agent_iface)
{
StepsRemaining &steps_remaining = ctx.get<StepsRemaining>(agent_iface.e);
Done &done = ctx.get<Done>(agent_iface.e);
Info &info = ctx.get<Info>(agent_iface.e);
int32_t num_remaining = steps_remaining.t;
if (num_remaining == consts::episodeLen && done.v != 1)
{ // Make sure to not reset an agent's done flag
done.v = 0;
return;
}
else if (num_remaining == 0)
{
done.v = 1;
}
// An agent can be done early if it reaches the goal
if (done.v != 1 || info.reachedGoal != 1)
{
float dist = (position.xy() - goal.position).length();
if (dist < ctx.data().params.rewardParams.distanceToGoalThreshold)
{
done.v = 1;
info.reachedGoal = 1;
}
}
}
void collisionDetectionSystem(Engine &ctx,
const CandidateCollision &candidateCollision) {
auto isInvalidExpertOrDone = [&](const Loc &candidate) -> bool
{
auto agent_iface = ctx.getCheck<AgentInterfaceEntity>(candidate);
if (agent_iface.valid())
{
auto controlledState = ctx.get<ControlledState>(agent_iface.value().e).controlled;
// Case: If an expert agent is in an invalid state, we need to ignore the collision detection for it.
if (controlledState == false)
{
auto currStep = getCurrentStep(ctx.get<StepsRemaining>(agent_iface.value().e));
auto &validState = ctx.get<Trajectory>(agent_iface.value().e).valids[currStep];
if (!validState)
{
return true;
}
}
else
{
// Case: If a controlled agent gets done, we teleport it to the padding position
// Hence we need to ignore the collision detection for it.
// The agent can also be done because it collided.
// In that case, we dont want to ignore collision. Especially if AgentStop is set.
auto &done = ctx.get<Done>(agent_iface.value().e);
auto &collisionEvent = ctx.get<CollisionDetectionEvent>(candidate);
if (done.v && !collisionEvent.hasCollided.load_relaxed())
{
return true;
}
}
}
return false;
};
if (isInvalidExpertOrDone(candidateCollision.a) ||
isInvalidExpertOrDone(candidateCollision.b)) {
return;
}
const CountT PositionColumn{2};
const CountT RotationColumn{3};
const CountT ScaleColumn{4};
const Loc locationA{candidateCollision.a};
const Position positionA{
ctx.getDirect<Position>(PositionColumn, locationA)};
const Rotation rotationA{
ctx.getDirect<Rotation>(RotationColumn, locationA)};
const Scale scaleA{ctx.getDirect<Scale>(ScaleColumn, locationA)};
const Loc locationB{candidateCollision.b};
const Position positionB{
ctx.getDirect<Position>(PositionColumn, locationB)};
const Rotation rotationB{
ctx.getDirect<Rotation>(RotationColumn, locationB)};
const Scale scaleB{ctx.getDirect<Scale>(ScaleColumn, locationB)};
auto obbA = OrientedBoundingBox2D::from(positionA, rotationA, scaleA);
auto obbB = OrientedBoundingBox2D::from(positionB, rotationB, scaleB);
bool hasCollided = OrientedBoundingBox2D::hasCollided(obbA, obbB);
if (not hasCollided) {
return;
}
EntityType aEntityType = ctx.get<EntityType>(candidateCollision.a);
EntityType bEntityType = ctx.get<EntityType>(candidateCollision.b);
for(auto &pair : ctx.data().collisionPairs)
{
if((pair.first == aEntityType && pair.second == bEntityType) ||
(pair.first == bEntityType && pair.second == aEntityType))
{
return;
}
}
auto maybeCollisionDetectionEventA =
ctx.getCheck<CollisionDetectionEvent>(candidateCollision.a);
if (maybeCollisionDetectionEventA.valid()) {
maybeCollisionDetectionEventA.value().hasCollided.store_relaxed(1);
auto agent_iface = ctx.get<AgentInterfaceEntity>(candidateCollision.a).e;
if(bEntityType > EntityType::None && bEntityType <= EntityType::StopSign)
{
ctx.get<Info>(agent_iface).collidedWithRoad = 1;
}
else if(bEntityType == EntityType::Vehicle)
{
ctx.get<Info>(agent_iface).collidedWithVehicle = 1;
}
else if(bEntityType <= EntityType::Cyclist)
{
ctx.get<Info>(agent_iface).collidedWithNonVehicle = 1;
}
}
auto maybeCollisionDetectionEventB =
ctx.getCheck<CollisionDetectionEvent>(candidateCollision.b);
if (maybeCollisionDetectionEventB.valid()) {
maybeCollisionDetectionEventB.value().hasCollided.store_relaxed(1);
auto agent_iface = ctx.get<AgentInterfaceEntity>(candidateCollision.b).e;
if(aEntityType > EntityType::None && aEntityType <= EntityType::StopSign)
{
ctx.get<Info>(agent_iface).collidedWithRoad = 1;
}
else if(aEntityType == EntityType::Vehicle)
{
ctx.get<Info>(agent_iface).collidedWithVehicle = 1;
}
else if(aEntityType <= EntityType::Cyclist)
{
ctx.get<Info>(agent_iface).collidedWithNonVehicle = 1;
}
}
}
// Helper function for sorting nodes in the taskgraph.
// Sorting is only supported / required on the GPU backend,
// since the CPU backend currently keeps separate tables for each world.
// This will likely change in the future with sorting required for both
// environments
#ifdef MADRONA_GPU_MODE
template <typename ArchetypeT>
TaskGraph::NodeID queueSortByWorld(TaskGraph::Builder &builder,
Span<const TaskGraph::NodeID> deps)
{
auto sort_sys =
builder.addToGraph<SortArchetypeNode<ArchetypeT, WorldID>>(
deps);
auto post_sort_reset_tmp =
builder.addToGraph<ResetTmpAllocNode>({sort_sys});
return post_sort_reset_tmp;
}
#endif
inline void collectAbsoluteObservationsSystem(Engine &ctx,
const Position &position,
const Rotation &rotation,
const Goal &goal,
const VehicleSize &vehicleSize,
AgentInterfaceEntity &agent_iface) {
auto &out = ctx.get<AbsoluteSelfObservation>(agent_iface.e);
out.position = position;
out.rotation.rotationAsQuat = rotation;
out.rotation.rotationFromAxis = utils::quatToYaw(rotation);
out.goal = goal;
out.vehicle_size = vehicleSize;
out.id = ctx.get<AgentID>(agent_iface.e).id;
}
void setupRestOfTasks(TaskGraphBuilder &builder, const Sim::Config &cfg,
Span<const TaskGraphNodeID> dependencies,
bool decrementStep) {
// setupBroadphaseTasks consists of the following sub-tasks:
// 1. updateLeafPositionsEntry
// 2. broadphase::updateBVHEntry
// 3. broadphase::refitEntry
auto broadphase_setup_sys =
phys::PhysicsSystem::setupBroadphaseTasks(builder, dependencies);
auto findOverlappingEntities =
phys::PhysicsSystem::setupStandaloneBroadphaseOverlapTasks(
builder, {broadphase_setup_sys});
auto detectCollisions = builder.addToGraph<
ParallelForNode<Engine, collisionDetectionSystem, CandidateCollision>>(
{findOverlappingEntities});
// Finalize physics subsystem work
auto phys_done = phys::PhysicsSystem::setupStandaloneBroadphaseCleanupTasks(
builder, {detectCollisions});
phys_done = phys::PhysicsSystem::setupCleanupTasks(
builder, {detectCollisions});
auto reward_sys = builder.addToGraph<ParallelForNode<Engine,
rewardSystem,
Position,
Goal,
AgentInterfaceEntity
>>({phys_done});
auto previousSystem = reward_sys;
if (decrementStep) {
previousSystem = builder.addToGraph<
ParallelForNode<Engine, stepTrackerSystem, AgentInterfaceEntity>>(
{reward_sys});
}
// Check if the episode is over
auto done_sys =
builder.addToGraph<ParallelForNode<Engine, doneSystem, Position, Goal,
AgentInterfaceEntity>>(
{previousSystem});
auto clear_tmp = builder.addToGraph<ResetTmpAllocNode>({done_sys});
(void)clear_tmp;
#ifdef MADRONA_GPU_MODE
// RecycleEntitiesNode is required on the GPU backend in order to reclaim
// deleted entity IDs.
auto recycle_sys = builder.addToGraph<RecycleEntitiesNode>({done_sys});
(void)recycle_sys;
#endif
// Finally, collect observations for the next step.
// auto collect_obs = builder.addToGraph<ParallelForNode<Engine,
// collectObservationsSystem,
// VehicleSize,
// Position,
// Rotation,
// Velocity,
// Goal,
// Progress,
// OtherAgents,
// EntityType,
// CollisionDetectionEvent,
// AgentInterfaceEntity
// >>({clear_tmp});
auto collect_self_obs = builder.addToGraph<ParallelForNode<Engine,
collectSelfObsSystem,
VehicleSize,
Position,
Rotation,
Velocity,
Goal,
CollisionDetectionEvent,
AgentInterfaceEntity>>({clear_tmp});
auto collect_partner_obs = builder.addToGraph<ParallelForNode<Engine,
collectPartnerObsSystem,
Position,
Rotation,
OtherAgents,
AgentInterfaceEntity>>({clear_tmp});
auto collect_map_obs = builder.addToGraph<ParallelForNode<Engine,
collectMapObservationsSystem,
Position,
Rotation,
AgentInterfaceEntity>>({clear_tmp});
auto collect_bev_obs = builder.addToGraph<ParallelForNode<Engine,
collectBevObservationsSystem,
Position,
Rotation,
OtherAgents,
AgentInterfaceEntity>>({clear_tmp});
auto collectAbsoluteSelfObservations = builder.addToGraph<
ParallelForNode<Engine, collectAbsoluteObservationsSystem, Position,
Rotation, Goal, VehicleSize, AgentInterfaceEntity>>(
{clear_tmp});
if (cfg.renderBridge) {
RenderingSystem::setupTasks(builder, dependencies);
}
TaskGraphNodeID lidar;
if(cfg.enableLidar) {
// The lidar system
#ifdef MADRONA_GPU_MODE
// Note the use of CustomParallelForNode to create a taskgraph node
// that launches a warp of threads (32) for each invocation (1).
// The 32, 1 parameters could be changed to 32, 32 to create a system
// that cooperatively processes 32 entities within a warp.
lidar = builder.addToGraph<CustomParallelForNode<Engine,
lidarSystem, 32, 1,
#else
lidar = builder.addToGraph<ParallelForNode<Engine,
lidarSystem,
#endif
Entity,
AgentInterfaceEntity,
EntityType
>>({clear_tmp});
}
#ifdef MADRONA_GPU_MODE
TaskGraphNodeID sort_agents;
if(cfg.enableLidar)
{
sort_agents = queueSortByWorld<Agent>(builder, {lidar, collect_self_obs, collect_partner_obs, collect_map_obs, collectAbsoluteSelfObservations});
} else {
sort_agents = queueSortByWorld<Agent>(builder, {collect_self_obs, collect_partner_obs, collect_map_obs, collectAbsoluteSelfObservations});
}
// Sort entities, this could be conditional on reset like the second
// BVH build above.
auto sort_phys_objects = queueSortByWorld<PhysicsEntity>(
builder, {sort_agents});
auto sort_agent_ifaces = queueSortByWorld<AgentInterface>(
builder, {sort_phys_objects});
auto sort_road_ifaces = queueSortByWorld<RoadInterface>(
builder, {sort_agent_ifaces});
(void)sort_road_ifaces;
#else
(void)lidar;
(void)collect_self_obs;
(void)collect_partner_obs;
(void)collect_map_obs;
(void)collectAbsoluteSelfObservations;
(void)collect_bev_obs;
#endif
}
static void setupStepTasks(TaskGraphBuilder &builder, const Sim::Config &cfg) {
auto moveSystem = builder.addToGraph<ParallelForNode<Engine,
movementSystem,
AgentInterfaceEntity,
VehicleSize,
Rotation,
Position,
Velocity,
CollisionDetectionEvent,
ResponseType
>>({});
setupRestOfTasks(builder, cfg, {moveSystem}, true);
}
static void setupResetTasks(TaskGraphBuilder &builder, const Sim::Config &cfg) {
auto reset =
builder.addToGraph<ParallelForNode<Engine, resetSystem, WorldReset>>(
{});
setupRestOfTasks(builder, cfg, {reset}, false);
}
void Sim::setupTasks(TaskGraphManager &taskgraph_mgr, const Config &cfg) {
setupResetTasks(taskgraph_mgr.init(TaskGraphID::Reset), cfg);
setupStepTasks(taskgraph_mgr.init(TaskGraphID::Step), cfg);
}
Sim::Sim(Engine &ctx,
const Config &cfg,
const WorldInit &init)
: WorldBase(ctx),
episodeMgr(init.episodeMgr),
params(*init.params)
{
// Below check is used to ensure that the map is not empty due to incorrect WorldInit copy to GPU
assert(init.map->numObjects);
assert(init.map->numRoadSegments <= consts::kMaxRoadEntityCount);
// Currently the physics system needs an upper bound on the number of
// entities that will be stored in the BVH. We plan to fix this in
// a future release.
// auto max_total_entities = init.map->numObjects + init.map->numRoadSegments;
auto max_total_entities = consts::kMaxAgentCount + consts::kMaxRoadEntityCount;
phys::PhysicsSystem::init(ctx, init.rigidBodyObjMgr,
consts::deltaT, consts::numPhysicsSubsteps, -9.8f * math::up,
max_total_entities);
enableRender = cfg.renderBridge != nullptr;
if (enableRender) {
RenderingSystem::init(ctx, cfg.renderBridge);
}
auto& map = ctx.singleton<Map>();
map = *(init.map);
auto& deletedAgents = ctx.singleton<DeletedAgents>();
for (auto i = 0; i < consts::kMaxAgentCount; i++) {
deletedAgents.deletedAgents[i] = -1;
}
// Creates agents, walls, etc.
createPersistentEntities(ctx);
// Generate initial world state
initWorld(ctx);
}
// This declaration is needed for the GPU backend in order to generate the
// CUDA kernel for world initialization, which needs to be specialized to the
// application's world data type (Sim) and config and initialization types.
// On the CPU it is a no-op.
MADRONA_BUILD_MWGPU_ENTRY(Engine, Sim, Sim::Config, WorldInit);
} |