SimSession¶
Python reference for nagasim.core.session.SimSession.
One replication of a model, driven from outside in chunks.
The session builds replication 0 as a normal run does, then lets the caller advance
it, read the dashboard, make the edits that are safe mid-run and answer Policy-mode
decisions. One thread drives a session. An illegal call raises ValueError. The
module docstring has the rules of pausing, logging and replay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
dict or str
|
The model, or the path of its file. |
required |
seed
|
int
|
The seed of the replication. |
0
|
trace
|
callable
|
A recorder subscribed from the first event on (the editor animates with it). |
None
|
subscribers
|
iterable of callable
|
Further listeners from the first event on. |
()
|
See Also
nagasim.api.run_model : A whole run in one call. nagasim.rl.SimEnv : The same stepping as a reinforcement-learning environment.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100)
>>> m.add_resource(name="Server", capacity=2)
'Server'
>>> arrive = m.create(distribution=nb.Exponential(rate=1.0))
>>> _ = arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.0)) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> session.advance(to=10)
'time'
>>> session.now
10.0
>>> session.finish()["system"].shape[0] > 0
True
advance(to=None, until=None)
¶
Run the replication forward.
Without to: run until a decision is pending, the horizon or the model drains.
With to (capped at the horizon): process every event strictly before
it and move the clock there (simpy's run(until=...)). A pending
decision stops it early; a drain leaves the clock at the drain time,
as a normal run's KPIs do.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to
|
float
|
The model time to move the clock to. |
None
|
until
|
callable
|
Called with no arguments before each event while no decision is pending. True stops the run there: that event is not processed and the clock stays where the last one left it. With to as well the first to hold wins, and "until" when both hold before the same event. It only decides where this call returns: the events and their order are a plain run's. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
|
See Also
nagasim.core.session.SimSession.finish : Runs to the end.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100)
>>> m.add_resource(name="Server", capacity=2)
'Server'
>>> arrive = m.create(distribution=nb.Exponential(rate=1.0))
>>> _ = arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.0)) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> session.advance(to=40)
'time'
>>> session.advance(to=500)
'horizon'
>>> session.now
100.0
pending()
¶
The decisions waiting for decide(), oldest first.
Returns:
| Type | Description |
|---|---|
list of dict
|
One |
See Also
nagasim.core.session.SimSession.decide : Answers a request.
Examples:
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.core.session import SimSession
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> session = SimSession(str(path), seed=0)
>>> session.advance()
'decision'
>>> request = session.pending()[0]
>>> request["policy"], request["options"]
('routing', ['process_a', 'process_b'])
snapshot()
¶
The dashboard now, with the sampler's own evaluation (no rng, a failed read is NaN).
Read-only: the levels are peeked, never integrated, so any number of snapshots leaves the run bit-identical to a plain one.
Returns:
| Type | Description |
|---|---|
dict
|
|
See Also
nagasim.core.session.SimSession.advance : Moves the clock.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100, dashboard={"widgets": [
... {"name": "clock", "kind": "kpi", "expr": "TNOW"}]})
>>> _ = m.create(distribution=nb.Constant(value=1.0)) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=10)
>>> session.snapshot()
{'t': 10.0, 'widgets': {'clock': 10.0}}
set_variable(name, value)
¶
Set a model variable now, as an ASSIGN block would.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A variable the model declares. |
required |
value
|
float or str or bool
|
The new value. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.set_constant : Replaces a system dynamics auxiliary.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100)
>>> m.add_variable(name="boost", initial=1)
'boost'
>>> _ = m.create(max_arrivals=1) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=10)
>>> session.set_variable("boost", 3)
>>> session.game_log[-1]["action"], session.game_log[-1]["value"]
('set_variable', 3)
set_constant(name, value)
¶
Replace a system dynamics auxiliary with a constant; AUX() reads it on every read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
An auxiliary the model declares. |
required |
value
|
float
|
The constant; it must be finite. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.set_variable : Sets a model variable.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=10, dashboard={"widgets": [
... {"name": "stock", "kind": "kpi", "expr": 'LEVEL("Stock")'}]})
>>> m.add_level(name="Stock", initial=0)
'Stock'
>>> m.add_flow(name="in", to="Stock", rate='AUX("r")')
'in'
>>> m.add_auxiliary(name="r", expr="1")
'r'
>>> _ = m.create(max_arrivals=1) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=4)
>>> session.snapshot()["widgets"]["stock"]
4.0
>>> session.set_constant("r", 3)
>>> _ = session.advance(to=6)
>>> round(session.snapshot()["widgets"]["stock"], 6)
10.0
set_capacity(resource, units)
¶
Cap a resource's capacity, from 0 to the capacity it declares.
The capacity in force is the least of the schedule and the cap, less what
failures hold, which the resource's CapacityArbiter decides: lowering waits
for a unit to free, and a raise during a failure waits for the repair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
str
|
A resource the model declares. |
required |
units
|
int
|
The cap, a whole number from 0 to the declared capacity. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.apply_control : Sets a cap through a dashboard control.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100)
>>> m.add_resource(name="Server", capacity=2)
'Server'
>>> arrive = m.create(distribution=nb.Exponential(rate=1.0))
>>> _ = arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.0)) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=10)
>>> session.set_capacity("Server", 1)
>>> try:
... session.set_capacity("Server", 5)
... except ValueError as error:
... print(error)
resource 'Server': capacity 5 outside 0..2
set_probs(block, probs)
¶
Set a Chance-mode DECIDE's branch probabilities, checked like the block's own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
str
|
The name of a DECIDE block in Chance mode. |
required |
probs
|
sequence of float
|
One probability per branch, or one fewer than the links when the last link is the ELSE branch; they must be finite, not negative and sum to at most 1. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.decide : Answers a Policy-mode DECIDE instead.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=8)
>>> gate = m.decide("gate", mode="Chance", probs=[1.0])
>>> arrive = m.create(max_arrivals=6, distribution=nb.Constant(value=1.0))
>>> _ = arrive >> gate
>>> _ = gate >> m.dispose("kept")
>>> _ = gate >> m.dispose("rest")
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=2.5)
>>> session.set_probs("gate", [0.0])
>>> session.game_log[-1]["action"], session.game_log[-1]["value"]
('set_probs', [0.0])
decide(policy, option, request_id=None)
¶
Answer a pending Policy-mode decision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
str
|
The policy the DECIDE block names. |
required |
option
|
int
|
The index of the outgoing link to take. |
required |
request_id
|
int
|
The request to answer; by default the policy's oldest pending one. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.pending : Lists the requests and their options.
Examples:
>>> from pathlib import Path
>>> import nagasim
>>> from nagasim.core.session import SimSession
>>> path = Path(nagasim.__file__).parent / "models" / "parallel_routing_policy.json"
>>> session = SimSession(str(path), seed=0)
>>> session.advance()
'decision'
>>> session.decide("routing", 0)
>>> [entry["action"] for entry in session.game_log]
['decide']
apply_control(name, value)
¶
Use a dashboard control: a slider sets its target, a decision control answers its policy.
A value outside the slider's [min, max] raises. The call is logged as the
underlying call, so replay needs nothing new.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A control of the model's |
required |
value
|
float
|
The slider's new value, or the option a decision control answers with. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
See Also
nagasim.core.session.SimSession.set_variable : What a variable slider calls. nagasim.core.session.SimSession.set_capacity : What a capacity slider calls.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100, dashboard={
... "widgets": [{"name": "clock", "kind": "kpi", "expr": "TNOW"}],
... "controls": [{"name": "boost", "kind": "slider", "target": {"variable": "boost"},
... "min": 0, "max": 10, "step": 1}]})
>>> m.add_variable(name="boost", initial=1)
'boost'
>>> _ = m.create(max_arrivals=1) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> session.apply_control("boost", 4)
>>> session.game_log[-1]["action"], session.game_log[-1]["value"]
('set_variable', 4)
finish()
¶
Run to the horizon (or drain) and return the run's frames.
Finishing ends the player's part: every decision still pending or met on the
way takes the DECIDE's own fallback, as with no controller attached. Those
are not logged, because a replay's finish() makes the same ones from the
same state.
Returns:
| Type | Description |
|---|---|
dict
|
The frames of a normal run, plus |
See Also
nagasim.core.session.SimSession.advance : Moves the clock part of the way.
Examples:
>>> from nagasim import build as nb
>>> from nagasim.core.session import SimSession
>>> m = nb.Model(time_limit=100)
>>> m.add_resource(name="Server", capacity=2)
'Server'
>>> arrive = m.create(distribution=nb.Exponential(rate=1.0))
>>> _ = arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.0)) >> m.dispose()
>>> session = SimSession(m.to_dict(), seed=0)
>>> _ = session.advance(to=10)
>>> session.set_capacity("Server", 1)
>>> results = session.finish()
>>> list(results["game_log"]["Action"])
['set_capacity']