Skip to content

SimEnv

Python reference for nagasim.rl.SimEnv.

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:

Name Type Description Default
layout dict or str

The model, or the path of its file.

required
policy str

The policy of the Policy-mode DECIDE this environment decides for.

required
reward str

A name from BUILTIN_REWARDS or an expression.

"neg_wip"
horizon float

The end of an episode, in model time units.

required
observation_extra list of str

Expressions appended to the DECIDE's observation.

None
others dict

Controllers for the model's other Policy DECIDEs.

None
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)

reset(seed=None, options=None)

Start a fresh episode at its first decision.

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

Parameters:

Name Type Description Default
seed int

The episode's seed; without one the previous seed is kept.

None
options dict

Unused, for the gymnasium signature.

None

Returns:

Type Description
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})

step(action)

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

Parameters:

Name Type Description Default
action int

The index of the outgoing link the DECIDE takes.

required

Returns:

Type Description
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)

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:

Type Description
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()
>>> obs, info = env.reset(seed=0)