# SimEnv

Python reference for `nagasim.rl.SimEnv`.

`nagasim.rl.SimEnv`

```python
SimEnv(layout, policy: str, reward: str = 'neg_wip', horizon: float, observation_extra: list[str] | None = None, others: dict | None = None)
```

One RL episode = one replication of *layout*, paused at every decision
of *policy*. Gymnasium-shaped but does not require gymnasium installed.

*reward* is one of `BUILTIN_REWARDS` or an expression string (see
`core/expressions.py`) evaluated with no `entity` in scope -- so it may
read model variables and NQ/NR/MR/NS, but not entity attributes or the
random-sampling functions (there is no rng bound to a "reward" -- only to
a block). *observation_extra*, when given, is a list of such expressions,
evaluated the same way and appended after the DECIDE block's own
`observation` values.

*others* names a controller for the model's other Policy DECIDEs, so one
policy trains at a time: `{key: controller}`, the key a policy name or a
Policy DECIDE's block name, the controller anything `evaluate_policy`
accepts. A Policy DECIDE not named runs the shortest queue (reading its
own `observation`), not its fallback.

Parameters
----------
layout : dict or str
    The model, or the path of its file.
policy : str
    The policy of the Policy-mode DECIDE this environment decides for.
reward : str, default "neg_wip"
    A name from `BUILTIN_REWARDS` or an expression.
horizon : float
    The end of an episode, in model time units.
observation_extra : list of str, optional
    Expressions appended to the DECIDE's observation.
others : dict, optional
    Controllers for the model's other Policy DECIDEs.

See Also
--------
nagasim.rl.evaluate_policy : Scores a controller over several episodes.
nagasim.core.session.SimSession : The same stepping for a person instead of a policy.

Examples
--------
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.rl import SimEnv
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> env = SimEnv(str(path), policy="routing", reward="throughput", horizon=50.0)
>>> env.action_space
Discrete(2)
>>> obs, info = env.reset(seed=0)
>>> obs, reward, terminated, truncated, info = env.step(0)
>>> reward, terminated, truncated
(1.0, False, False)

### `nagasim.rl.SimEnv.reset`

```python
reset(seed: int | None = None, options = None)
```

Start a fresh episode at its first decision.

There is no reward yet: nothing happened before the first decision.

Parameters
----------
seed : int, optional
    The episode's seed; without one the previous seed is kept.
options : dict, optional
    Unused, for the gymnasium signature.

Returns
-------
tuple
    ``(observation, info)``, the observation as a float array and the info as
    ``{"t", "completions"}``.

See Also
--------
nagasim.rl.SimEnv.step : Answers the decision and runs to the next.

Examples
--------
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.rl import SimEnv
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> env = SimEnv(str(path), policy="routing", horizon=50.0)
>>> obs, info = env.reset(seed=0)
>>> obs.shape, info
((4,), {'t': 0, 'completions': 0})

### `nagasim.rl.SimEnv.step`

```python
step(action)
```

Resolve the pending decision with an action and run until the next one.

Parameters
----------
action : int
    The index of the outgoing link the DECIDE takes.

Returns
-------
tuple
    ``(observation, reward, terminated, truncated, info)``. ``terminated`` is
    true when the model drained, ``truncated`` when the horizon came first.

See Also
--------
nagasim.rl.SimEnv.reset : Starts an episode.

Examples
--------
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.rl import SimEnv
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> env = SimEnv(str(path), policy="routing", reward="throughput", horizon=50.0)
>>> _ = env.reset(seed=0)
>>> obs, reward, terminated, truncated, info = env.step(0)
>>> reward, terminated, truncated
(1.0, False, False)

### `nagasim.rl.SimEnv.as_gymnasium_env`

```python
as_gymnasium_env()
```

This environment as a real `gymnasium.Env` (passes gymnasium's `check_env`).

Needs gymnasium (`pip install "nagasim[ml]"`); it is imported here only.

Returns
-------
gymnasium.Env
    An environment over this one, to hand to a trainer.

See Also
--------
nagasim.rl.SimEnv.reset : The step-wise calls it wraps.

Examples
--------
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.rl import SimEnv
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> env = SimEnv(str(path), policy="routing", horizon=50.0).as_gymnasium_env()  # doctest: +SKIP
>>> obs, info = env.reset(seed=0)  # doctest: +SKIP
