Skip to content

Ariadne L4: the gym (WorldGym, pycvc.WorldGym), and pycvc_gl.cars on it - #1170

Merged
transfix merged 40 commits into
masterfrom
feat/world-gym
Nov 17, 2026
Merged

transfix merged 40 commits into
masterfrom
feat/world-gym

Conversation

@transfix

@transfix transfix commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

This is L4 in ARIADNE_WORLDS.md (sections 14 and 15): the gym. It drives a world document headless with reset, step and observe, a reward and an episode log, from C++ or from Python, and pycvc_gl.cars' controllers run on it.

gym = pycvc.WorldGym("world_gym.ari", world="flat", count=4, max_steps=1200)
obs = gym.reset(seed=7)               # a dict of numpy arrays, one entry per agent
while True:
    steer, accel = my_policy(obs)     # (n,) each; NaN: nav@1 drives that agent
    obs, reward, terminated, truncated, info = gym.step(steer, accel)
    if terminated or truncated:
        break
gym.save_log("episode.csv")

cvc::ariadne::WorldGym (world_gym.h), in core libcvc with no GL:

  • One app per gym. Each gym loads its document on an app of its own: its own state tree, clock, physics world and vehicles. One process can run many gyms side by side. Its clock is paused, so only step() steps the world.
  • reset(seed) rebuilds the world as the document built it, with the clock back at tick 0. It seeds every draw (starts, goals, the spawn policy's random draw and round-robin turns) and spawns the agents: count of the first archetype with nav@1, or a list of specs. A seed and a list of commands give the same episode in any gym, which a test checks bit for bit.
  • step(steer, accel) runs one navigator tick (0.05 s, a whole number of physics steps). Each agent's command replaces its controller's for that tick, held to the agent's envelope. A NaN leaves the agent to its controller, so nav@1 is the baseline in the same loop. Goals are held for the episode, and set_goal moves one.
  • The observation, per agent, as flat arrays:
    • pose, velocity and steering;
    • the goal and the distance to it;
    • contacts over the tick: with the world's bodies other than its ground, and with another vehicle;
    • out: off the grid, or 5 m below its ground;
    • ray ranges on the world's grid;
    • the body and its envelope.
  • The end. An episode is terminated when every agent with a goal is at it, or when one agent is out. It is truncated at max_steps.
  • The reward is the user's, in C++ or in Python. A Python exception comes back out of step() as itself. The default reward is each agent's progress toward its goal, less 1 per tick it collides, plus 10 when it arrives.
  • The log has a row per agent per step, as CSV or numpy arrays. Keys under <prefix>.gym.* follow the episode.

What it needed in the runtime:

  • WorldVehicles:
    • command, applied at the navigator's tick through sim_world::override_command;
    • set_goal, set_wander and reseed;
    • after_step, which counts each physics step's contacts against the vehicles;
    • entity_info: velocity, steering, goal and reach, contacts.
  • WorldSpawns::reseed, and AppRuntime::pause_world.

pycvc: pycvc.WorldGym (pycvc_gym.i). It returns numpy observations and gymnasium's five-tuple from step. It also has set_reward(fn), log(), save_log, set_goal and grid(), and takes agents as dicts. The GIL is released while the world steps.

pycvc_gl.cars on the gym:

  • GymCars gives the template a CarsState and step/step_navigator/new_goals over a gym, so a cars controller runs unchanged in a world document.
  • world_from_gym is the gym's grid as a World, for rollouts and rasters.
  • --gym DOC runs the template on it, headless.

The template's own worlds and plants (kinematic, dynamic) stay on its sim_world and plant_host, because the gym runs Jolt vehicles only.

The example: examples/ariadne/python/world_gym.ari and world_gym.py.

  • The document holds world_lab's three worlds, vehicles and spawn points, and requires only scene.physics, so it runs headless and arigl draws the same document.
  • The script drives it with a simple policy of its own, or --navigator.
  • On the flat square the policy brings all 4 Humvees to their goals in 22 to 26 s. nav@1 gets 2 of 4 there in 60 s.

Fixed on the way: SceneWorld::restart now gives the vehicles the new ground bodies. A generated world's colliders come back from a restart with new body ids, and the vehicles kept the old list, so after a restart they counted the terrain as an obstacle. That affected nav_plant's contact counts as well as the gym's. The gym resets with a restart, which showed it: before the fix, nav@1 on the generated world logged 1,124 agent-ticks of contact in an episode, and after it 0.

Found, not fixed here: on world_gym.ari's generated world (seed 5, half 150), a vehicle placed at about (−51.7, 46.4) falls straight through the ground from its first step. The grid says the ground there is 8 to 10 m up, and the start is a few metres from a building, so the generator's terrain mesh seems to have a hole there. The gym's out flag ends such an episode (seed 1 of world_gym.py --world generated). Also, a generated world's buildings count as its ground, so touching one is not a contact.

Docs

  • ARIADNE.md: "The gym".
  • CARS.md: "In a world document".
  • ARIADNE_WORLDS.md: the L4 row (built), and §14's "As built (L4)".

Tests (Windows Release)

  • ariadne_scene_world_test, 6 new tests (AriadneWorldGym.*):
    • reset, step and observe: spawn points, rays, the tick on the paused clock, throttle and nav@1 in one step;
    • one seed and one list of commands give one world, in two gyms and again after a reset;
    • truncation, a reward of the user's, the log and its CSV;
    • a wall is a contact, and nav@1 makes way to a goal it was given;
    • an agent driven off the world's edge ends the episode;
    • a document without vehicles or without a world is refused.
  • pycvc_gym (new): the numpy observation, the five-tuple, scalar and wrong-sized commands, determinism across gyms, a Python reward and its exception, the log, agents as dicts, and refusals.
  • pycvc_gl_cars, section 8: GymCars with the template's controller, the navigator baseline, new_goals, a rollout on the gym's world, and --gym headless with its log.
  • AriadneComponents.ShippedExamplesDeclareWhatTheyNeed now accepts a scene that requires scene.physics (a world that runs headless) as well as gl.scene.
  • After a full build, 1,411 of 1,411 related ctest passed ([Aa]riadne|[Nn]av|pycvc_world|pycvc_ari|pycvc_gym|pycvc_gl_cars|cvcgl_ariadne|SceneWorld|[Pp]hysics|arigl|[Ww]orld[Rr]ules|room_rules|arisrv|[Jj]ob|plant).
  • Checked by hand: world_gym.py on the flat and generated worlds, with its own policy and with --navigator, and an arigl snapshot of world_gym.ari (the square, its spawn pads, the World and Physics panels).

Also in this PR

Two follow-ups were merged into this branch (#1202 was merged here, not into master), so this PR now carries them too.

The gym follow-up (4a81d3d)

It fixes three of the limits listed above: the gym ran Jolt vehicles only, a generated world's buildings counted as ground, and every episode had the same world.

  • Every plant in a scene world. WorldVehicles runs jolt, kinematic, dynamic and dynamic4w entities. The entities on each plant have a navigator of their own.
    • jolt is nav_plant in the scene's physics world, as before.
    • kinematic is nav's bicycle, with no body.
    • dynamic and dynamic4w are dyn_bike_plant and dyn_4w_plant. Each has its chassis in a physics world of its own that holds the world's colliders.
    • A spawn names its plant with entity_spec::plant or vehicles.spawn.plant; the L2 refusal of non-Jolt plants is gone.
    • Vehicles on different plants touch when their chassis boxes overlap. A vehicle with no chassis in the scene's world is drawn at its pose each frame.
    • GymCars and --gym take the cars template's plant names (kinematic, dynamic:roboracer).
  • Buildings are obstacles. Of a node's colliders, only its terrain (a height field) is ground (SceneWorldBody::ground). A vehicle touching a building is hit_obstacle.
  • A world per episode. With gym_options::regenerate (regenerate=True in pycvc), each reset makes the generated world again from its seed (SceneWorld::regenerate_world). Another seed gives another world; the same seed gives the same world back.
  • Not yet: vehicles on different plants don't steer around each other, because each plant's navigator sees only its own.
  • Tests:
    • AriadneWorldGym.EveryPlantDrivesInTheWorld, VehiclesOnOtherPlantsAndTheBicycleTouch and EachEpisodeCanHaveAWorldOfItsOwn.
    • The terrain as the one ground body.
    • A kinematic spawn and an unknown plant refused.
    • A kinematic vehicle's marker (cvcgl_ariadne_physics).
    • pycvc_gym on four plants with regenerate=True.
    • GymCars on kinematic, dynamic:humvee and dynamic4w.
    • world_gym.py on each plant: 4 of 4 agents reach their goals on the flat square.

Vehicles start on a slope, not inside it (5624aad, #1202)

There was no hole in the generated world's terrain. A Humvee facing uphill on a slope of about 0.75 started with its nose more than a metre below the ground and fell through. Now a start is tilted onto the plane under its chassis and lifted clear of the ground under it. Every plant reads the terrain's own heights, or interpolates the grid bilinearly where there are none. Details and tests are in #1202.

After the merges

The branch also carries master, including #1181, the sea-surface fix for the singleton scan. After a full build at da11281 (Windows Release), 1,382 of 1,382 related ctest passed ([Aa]riadne|[Nn]av|pycvc_world|pycvc_ari|pycvc_gym|pycvc_gl_cars|pycvc_plant|cvcgl_ariadne|SceneWorld|[Pp]hysics|[Oo]cean|[Pp]lant|[Dd]yn|arigl).

A world document driven headless by reset(seed), step(steer, accel) and
observe(), with a reward of the user's and an episode log: one app per gym,
its clock paused, every draw seeded. WorldVehicles gains per-tick commands,
held goals, reseeding and per-step contact counts; SceneWorld::restart hands
the vehicles the new ground bodies. GymCars and --gym run the cars template's
controllers in a world document; world_gym.ari/.py are the example.
…a world per episode

WorldVehicles runs jolt, kinematic, dynamic and dynamic4w entities, a navigator per plant (the
dynamic models' chassis in a world of their own holding the world's colliders); entity_spec and
vehicles.spawn.plant name it; across plants vehicles touch by their chassis boxes, and cvcGL draws a
vehicle with no chassis from its pose. A world's terrain alone is its ground
(SceneWorldBody::ground), so a building is an obstacle. WorldGym's regenerate makes a generated
world per episode from the reset's seed (SceneWorld::regenerate_world). GymCars and --gym take the
cars template's plant names; dyn_bike_plant and dyn_4w_plant gain add_desc.
A vehicle in world_gym.ari's generated world (seed 1, agent 3 at about
(-51.7, 46.4)) fell straight down from its first step. The terrain
collider has no hole there: a raycast at every free grid cell hits the
ground. The spot is a slope of about 0.75, and nav_plant placed every
start level at one ground sample (the nearest 2 m grid cell under the
rear axle) plus its resting height. Facing uphill, the Humvee's origin
started under the ground at its own centre and its nose more than a
metre deep; a body that starts below a height field falls through it.

nav_plant: with ground_height set, a start is tilted onto the plane
fitted to the ground under its chassis, its origin its resting height
above that plane, then lifted clear of the highest ground under the
chassis and the wheels at full reach. Level ground and a constant
ground_z keep the pose they had.

WorldVehicles: every plant's ground height (and height_at) is the world's
terrain where it has one, the physics collider's own heights, else the
grid bilinear between its samples, not the nearest sample.

Tests: a downward raycast at every free cell of the generated world finds
the ground at the grid's height; twelve Humvees placed on its slopes
(1 in 3 to 1 in 1, up and across) start above the ground and are on it
3 s later (two fell to z -27 before).
Vehicles start on a slope, not inside it
Each terrain cell took its material from one raster sample, its NE corner where the raster and
the mesh share samples. On a steep shore that corner can be dry or shallow while the cell is
metres deep, so the navigator and the physics took deep water for drivable ground: 151 cells
deeper than 0.5 m on world_gym.ari's generated world, the deepest at -1.91 m.

The materials stage now classes each cell's water by the mean of its corners against the sea,
with derived_class's depths (deep below sea - 0.5, shallow below sea, sand for a dry cell the
raster called water). A prop's footprint keeps its own material.

Test: WorldGenerated.ACellsWaterIsTheMeshsDepth checks every ungraded cell's class against its
mean height.
add_world_geometry adds four static boxes around the rectangle the collide height fields cover
(world_bodies::edges): 2 m thick, 1 cm outside the edge, from 20 m below the lowest height to
20 m above the highest. They are not items and not ground, so a vehicle touching one has hit an
obstacle. SceneWorld keeps them as the terrain node's bodies, so they go and come with it.

world_gym.ari's generated world, seed 1: from agent 1's start (-122.9, -72.6) toward its goal on
the west shore, the vehicle slid down the slippery underwater slope and off the west edge in 14
of 18 runs (3 masses x 3 top speeds x nav@1 and world_gym.py's policy). Now 0 of 18; seeds 1-20
under both controllers have no agent out.

Tests: PhysicsWorldGeometry.NothingLeavesTheTerrainOverItsEdge (a box sliding at 15 m/s down a
mu 0.1 slope stops at the wall; rays down on the edge still meet the terrain) and
AriadneWorldGym.AGeneratedWorldsEdgeHoldsAVehicleIn (Humvees flat out at each edge stay in). The
body counts of generated and data worlds include the 4 walls.
What is at a generated world's edge is now a choice (generated_world_params::edge, world_edge):

- wall (the default): the invisible walls of the previous commit.
- shore: a band at the rim (15 % of half, 40-250 m) becomes coast. From the band's inner end the
  ground eases down to a beach sloping 1 in 50 to the waterline, then under the sea to the island
  preset's shelf depth at the edge. Ground already lower keeps its bed. It wraps any terrain
  generator (fbm, dem, registered), lots stay off the shore, and the sea goes on past the world:
  outer_sea() is a ring at the waterline out 6 km, which cvcGL draws under <id>_water. No walls.
- none: the edge is open.

world_geometry::edge_walls() says whether add_world_geometry adds the walls; a generated world sets
it from its edge (only wall), and every other world_geometry keeps them on.

Ariadne: a generated_world node's `edge: wall | shore | none`. pycvc: generate_world's "edge"
param, and info()["edge"]. docs/ARIADNE.md describes it.

Tests: WorldGenerated.AShoreEdgeLinesTheWorldWithABeachAndTheSea (deep water all round the rim, a
sand beach inside it, the relief untouched past the band, the outer sea ring),
PhysicsWorldGeometry.TheEdgeWallsCanBeLeftOut, AriadneSceneWorld.AGeneratedWorldsEdgeCanBeAShore,
cvcgl_ariadne_realize (the drawn outer sea), test_pycvc_plant_host.py.
Rendered, the shore's blend from the relief to the beach was short enough (about 11 m on a 300 m
world) that high ground met the coast as a rock cliff, and the coastline ran dead straight. The
band is now 20 % of half (50-300 m), the blend takes its inner half (sea floor to waterline by
25 %, beach to 50 %), and the coastline lies up to 15 % of the band further in by a smooth wobble
the seed sets. The edge stays the sea floor and the relief past the band is untouched.
The shore's coast followed the world's square outline, corners and all. It now follows a rounded
square, |x|^4 + |y|^4 = half^4 (world_shore_roundness), so the corners curve away into the sea,
and its wobble is up to 35 % of the band on longer waves (2.5 and 0.8 bands), for bays and
headlands. The square's edge is still the sea floor everywhere and the relief past the band is
untouched; the test measures the band from the rounded edge.
…picker)

A generated_world node's edge was only its authored `edge:` key, so the terrain lab's World panel
could not change it. It is now a world.* key like the preset: world.edge (0 wall, 1 shore, 2 none),
seeded from the node, read on Regenerate, and part of the world's signature, so an edit marks the
world pending until Regenerate. world_controls.ari has an Edge combo (Wall, Shore, None) after
Water level. The walls follow the new world's colliders, and cvcGL draws or empties the outer sea
on the swap. pycvc_gl.cars logs it with the other world keys.

Tests: AriadneSceneWorld.TheWorldsEdgeIsALiveSetting (seeded 0; 1 waits, then Regenerate makes a
shore world with no walls; 0 brings them back) and the loader's world_controls check for the
Edge picker. Checked drawn too: an offscreen host switched a walled world to Shore and
regenerated it. 711/711 related ctest.
Terrain lab: an Edge picker for the generated world (world.edge)
Generated worlds: water by each cell's depth, and a choice of world edge (walls, shore, none)
Conflict in src/cvc/world/generated_world.cpp: master's #1203 made the
terrain generators each app's own (generators(app), terrainFrom), and
#1215 had put the shore edge (shore_terrain) in the old make_terrain.
shore_terrain now sits beside terrainFrom, which wraps any app's
generator in it, so make_terrain(app, p) and to_world_params(app, p)
both get the shore.

#1203 also broke code that only feat/world-gym has: its edge and gym
tests call generate_world, make_terrain and load_string without an app.
They make one first now.

cvc-cli's cmd_trace and three ariadne_runtime_test rich-text tests do
not build on master itself after #1203. This takes #1225's fix for them
line for line, so that #1170 builds; the two merge cleanly either way.
Master's #1233 (entity intents) conflicts with this branch's plants, where
each plant has its own group and navigator, not one sim. Intents now run per
group: the drive's external force is each group's, sampled by its
navigator's own agent indices. apply_intents overrides each entity's command
on its group's sim and sets or clears each group's ext_force. An external
entity's hold reads its speed from pose(), which every plant has.
before_step reads and applies the intents, then a gym's one-tick command
takes over (command.by "gym"), then each group steps.

entity_info keeps this branch's fields and adds master's command_steer,
command_accel and intent; the entity keeps both sides' members. ARIADNE.md
and the header say each plant's drive has its own force-steering switch.
…the 0.01 s clock, real time catching up 0.25 s a frame
Master's #1235 runs every plant in a scene world's one physics world (fleets per plant; dynamic
chassis boxes and kinematic boxes as bodies there; wheel poses). The gym's own plant groups (the
dynamic models in a world of their own, the bicycle with no body, the markers that drew it) give
way to it. Kept from L4 on master's fleets: a spawn's own plant (entity_spec::plant,
vehicles.spawn.plant), the gym's API (reseed, set_wander, command, set_goal, tick_seconds,
steps_per_tick, grid) and its per-tick command, the velocity, steer, goal and reach in
entity_info, contacts counted from the world's events (a kinematic box's obstacles by its grid
footprint, two kinematic boxes by overlap), the terrain's own heights under the vehicles, and
world_reset with the new ground bodies.
…r water class again

Master's #1271 lowers the terrain mesh's waterline slivers at the end of the grade stage, after
the materials stage classed each cell's water by its corners' mean height. A cell round a lowered
sample kept the class of the ground before it (sand, or shallow water over half a metre deep), so
WorldGenerated.ACellsWaterIsTheMeshsDepth failed. The materials stage's rule is now cell_class(),
and the cells touching a lowered sample are classed by it again from the lowered mesh.
…in-exports

# Conflicts:
#	src/cvc/physics/dyn_4w_plant.cpp
#	src/cvc/physics/dyn_bike_plant.cpp
#	src/cvc/tests/ariadne_scene_world_test.cpp
# Conflicts:
#	src/cvc/ariadne/world_vehicles.cpp
…ds (fields, plants)

Ariadne gym: what a differentiable twin of the world reads (fields, plants)
…le worlds on the 0.01 s clock, real time catching up 0.25 s

One simulation tick: 0.06 s by default everywhere, vehicle worlds on the 0.01 s clock, real time catching up 0.25 s
#1276 made server_link take the module object (self) so it finds the
link on its own host, and #1302 landed after it with five calls that
pass nothing: room, bridge_presence, bridge_absent, bridge_post and
listen_chat. Master's pycvc-linux build fails on them. Name self in
those five functions and pass it.
#1258 wrote the arms run's retarget at 3 s as tick 60, a 0.05 s tick.
#1275 made the sim tick 0.06 s, so the leg starts at tick 50. The test
takes the tick as round(3.0 / cars.DT) for the legs, the goals and the
alone run it compares against.
@transfix
transfix enabled auto-merge (squash) November 17, 2026 07:49
@transfix
transfix merged commit bbb47dd into master Nov 17, 2026
29 checks passed
@transfix
transfix deleted the feat/world-gym branch November 17, 2026 08:25
transfix added a commit that referenced this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant