OrmaosDocs

Guides

RL environment

Record the episodes of a reinforcement-learning environment and read how many steps each episode took, what step returned, and the lines a step ran.

View as Markdown

The example is rl-env: a grid world in pure Python and a loop of 20 episodes with a seeded policy. An environment makes many small calls, so this guide also uses --function and --path.

Record the episodes#

bash
ormaos run -- python run.py
rl-env
$ ormaos run -- python run.py
20 episodes, mean return -10.55
ormaos: r1 exit 0 in …: 1 Python process, 1969 calls, 0 exceptions
ormaos: saved in .ormaos/r1, next: ormaos tree r1:c1

How many calls did each episode make?#

rl-env
$ ormaos tree r1 --function run_episode --limit 5
r1: 20 of the 1969 calls match --function run_episode (20 run_episode; 0 raised)
r1:c6 run_episode(env: env.GridEnv, rng: random.Random, returns: list[0]) -> NoneType  run.py:28  …  calls below: 36
r1:c97 run_episode(env: env.GridEnv, rng: random.Random, returns: list[1 float]) -> NoneType  run.py:28  …  calls below: 32
r1:c176 run_episode(env: env.GridEnv, rng: random.Random, returns: list[2 float]) -> NoneType  run.py:28  …  calls below: 36
r1:c267 run_episode(env: env.GridEnv, rng: random.Random, returns: list[3 float]) -> NoneType  run.py:28  …  calls below: 34
r1:c356 run_episode(env: env.GridEnv, rng: random.Random, returns: list[4 float]) -> NoneType  run.py:28  …  calls below: 54
... 15 more: --offset 5
next: ormaos tree r1:c6 | ormaos tree r1:c97 | ormaos tree r1 --offset 5 --function run_episode | ormaos tree r1 | ormaos exceptions r1

returns: list[0], list[1 float], ... is the list of returns as each episode received it. calls below counts the calls inside each episode.

What happened inside one episode?#

rl-env
$ ormaos tree r1:c6 --depth 1 --limit 6
inside: r1:c1 <module> > r1:c4 main
r1:c6 run_episode(env: env.GridEnv, rng: random.Random, returns: list[0]) -> NoneType  run.py:28  …  calls below: 36
  r1:c7 GridEnv.reset(self: env.GridEnv) -> tuple[2 int]  env.py:20  …
  r1:c8 choose(rng: random.Random, position: tuple[2 int], goal: tuple[2 int], explore: float) -> str[4]  run.py:10  …
  r1:c9 GridEnv.step(self: env.GridEnv, action: str[4]) -> tuple[3]  env.py:25  …  calls below: 2
  r1:c14 choose(rng: random.Random, position: tuple[2 int], goal: tuple[2 int], explore: float) -> str[4]  run.py:10  …
  r1:c15 GridEnv.step(self: env.GridEnv, action: str[4]) -> tuple[3]  env.py:25  …  calls below: 2
... 31 of the 36 calls directly below r1:c6 are not shown (15 choose, 15 GridEnv.step, 1 ended_at_goal): --offset 5
next: ormaos tree r1:c6 --offset 5 | ormaos tree r1:c9 | ormaos tree r1:c4 | ormaos exceptions r1:c6

What did step return, and how often did it run?#

rl-env
$ ormaos tree r1 --function GridEnv.step --limit 5
r1: 363 of the 1969 calls match --function GridEnv.step (363 GridEnv.step; 0 raised)
r1:c9 GridEnv.step(self: env.GridEnv, action: str[4]) -> tuple[3]  env.py:25  …  calls below: 2
r1:c15 GridEnv.step(self: env.GridEnv, action: str[4]) -> tuple[3]  env.py:25  …  calls below: 2
r1:c20 GridEnv.step(self: env.GridEnv, action: str[5]) -> tuple[3]  env.py:25  …  calls below: 2
r1:c25 GridEnv.step(self: env.GridEnv, action: str[2]) -> tuple[3]  env.py:25  …  calls below: 2
r1:c30 GridEnv.step(self: env.GridEnv, action: str[4]) -> tuple[3]  env.py:25  …  calls below: 2
... 358 more: --offset 5
next: ormaos tree r1:c9 | ormaos tree r1:c15 | ormaos tree r1 --offset 5 --function GridEnv.step | ormaos tree r1 | ormaos exceptions r1

step ran 363 times. Its result is tuple[3]: observation, reward, done. With --values, the result itself is recorded, cut and masked.

Which lines did step run?#

Record again with a --path scope, then read the lines of one call:

bash
ormaos run --path env.py:step -- python run.py
ormaos path r2:c9
rl-env
$ ormaos path r2
r2: lines recorded in 1 function
  GridEnv.step env.py:25: 363 calls, 2541 steps  [r2:c9 r2:c15 r2:c20]
next: ormaos path r2:c9
rl-env
$ ormaos path r2:c9 --limit 15
r2:c9 GridEnv.step env.py:25: 7 steps, showing 1-7
  1    26 |         reward = self._reward()
  2        call GridEnv._reward [r2:c10] returned
  3    27 |         self._move(action)
  4        call GridEnv._move [r2:c12] returned
  5    28 |         self.steps += 1
  6    29 |         done = self.position == self.goal or self.steps >= self.limit
  7    30 |         return self.position, reward, done
next: ormaos tree r2:c9

Notes for environments#

  • Lines are recorded only inside the scope; path has the lines of step, and the calls it makes appear as calls.
  • A long training run makes very many calls: record with --only context,exceptions,io, or record a few episodes with with ormaos.record(): around them.
  • Random generators are values of your program; rng: random.Random is their shape.