-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from gtri/13-write-docs-about-data-gathering
Simulation data gathering
- Loading branch information
Showing
26 changed files
with
1,453 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
|
||
.. _SimPy: https://simpy.readthedocs.io/ | ||
|
||
.. |Actor| replace:: :py:class:`~upstage.actor.Actor` | ||
.. |State| replace:: :py:class:`~upstage.states.State` | ||
.. |Task| replace:: :py:class:`~upstage.task.Task` | ||
.. |TaskNetwork| replace:: :py:class:`~upstage.task_network.TaskNetwork` | ||
.. |TaskNetworks| replace:: :py:class:`TaskNetworks <upstage.task_network.TaskNetwork>` | ||
.. |EnvironmentContext| replace:: :py:class:`~upstage.base.EnvironmentContext` | ||
.. |UpstageBase| replace:: :py:class:`~upstage.base.UpstageBase` | ||
.. |NamedEntity| replace:: :py:class:`~upstage.base.NamedUpstageEntity` | ||
.. |LinearChangingState| replace:: :py:class:`~upstage.states.LinearChangingState` | ||
.. |CartesianLocation| replace:: :py:class:`~upstage.data_types.CartesianLocation` | ||
.. |GeodeticLocationChangingState| replace:: :py:class:`~upstage.states.GeodeticLocationChangingState` | ||
.. |ResourceState| replace:: :py:class:`~upstage.states.ResourceState` | ||
.. |SelfMonitoringStore| replace:: :py:class:`~upstage.stores.SelfMonitoringStore` | ||
.. |DecisionTask| replace:: :py:class:`~upstage.task.DecisionTask` | ||
.. |Actor| replace:: :py:class:`~upstage_des.actor.Actor` | ||
.. |State| replace:: :py:class:`~upstage_des.states.State` | ||
.. |Task| replace:: :py:class:`~upstage_des.task.Task` | ||
.. |TaskNetwork| replace:: :py:class:`~upstage_des.task_network.TaskNetwork` | ||
.. |TaskNetworks| replace:: :py:class:`TaskNetworks <upstage_des.task_network.TaskNetwork>` | ||
.. |EnvironmentContext| replace:: :py:class:`~upstage_des.base.EnvironmentContext` | ||
.. |UpstageBase| replace:: :py:class:`~upstage_des.base.UpstageBase` | ||
.. |NamedEntity| replace:: :py:class:`~upstage_des.base.NamedUpstageEntity` | ||
.. |LinearChangingState| replace:: :py:class:`~upstage_des.states.LinearChangingState` | ||
.. |CartesianLocation| replace:: :py:class:`~upstage_des.data_types.CartesianLocation` | ||
.. |GeodeticLocationChangingState| replace:: :py:class:`~upstage_des.states.GeodeticLocationChangingState` | ||
.. |ResourceState| replace:: :py:class:`~upstage_des.states.ResourceState` | ||
.. |SelfMonitoringStore| replace:: :py:class:`~upstage_des.stores.SelfMonitoringStore` | ||
.. |DecisionTask| replace:: :py:class:`~upstage_des.task.DecisionTask` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
=================== | ||
Environment Context | ||
=================== | ||
|
||
UPSTAGE uses Python's [context variable](https://docs.python.org/3/library/contextvars.html) | ||
capabilities to safely manage "global" state information while not polluting the module | ||
level data with run-specific information. | ||
|
||
The context manager accepts three arguments: | ||
|
||
1. Simulation start time (passes through to ``simpy.Environment``) | ||
2. A random seed for ``random.Random`` | ||
3. A random number generator object, if different than ``random.Random`` | ||
|
||
For more about the random numbers, see :doc:`Random Numbers </user_guide/how_tos/random_numbers>`. | ||
|
||
.. note:: | ||
|
||
If you get a warning or error about not finding an environment, you have likely | ||
tried to instantiate an actor, task, or other UPSTAGE object outside of an | ||
environment context. | ||
|
||
|
||
Creating Contexts | ||
================= | ||
|
||
Use the ``EnvironmentContext`` context manager: | ||
|
||
.. code:: python | ||
impoprt upstage_des.api as UP | ||
with UP.EnvironmentContext() as env: | ||
... | ||
# everything in here can find that environment | ||
... | ||
env.run() | ||
Or, create a context at the current scope: | ||
|
||
.. code:: python | ||
from upstage_des.base import create_top_context, clear_top_context | ||
ctx = create_top_context() | ||
env = ctx.env_ctx.get() | ||
... | ||
env.run() | ||
clear_top_context(ctx) | ||
This way is friendlier to Jupyter notebooks, where you might run a simulation and want to | ||
explore the data without needing to remain in the context manager. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
============== | ||
Resource Types | ||
============== | ||
|
||
UPSTAGE comes with new resource types in addition to the SimPy resources: | ||
|
||
1. :py:class:`~upstage_des.resources.container.ContinuousContainer` | ||
2. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringStore` | ||
3. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringFilterStore` | ||
4. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringContainer` | ||
5. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringContinuousContainer` | ||
6. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringSortedFilterStore` | ||
7. :py:class:`~upstage_des.resources.monitoring.SelfMonitoringReserveContainer` | ||
8. :py:class:`~upstage_des.resources.reserve.ReserveContainer` | ||
9. :py:class:`~upstage_des.resources.sorted.SortedFilterStore` | ||
|
||
The self-monitoring stores are discussed in :doc:`the simulation data section </user_guide/tutorials/data>`. | ||
They enable recording of data within the store or container over time, with an optional input for a function to | ||
evaluate when recording on the items in the stores. | ||
|
||
ContinuousContainer | ||
=================== | ||
|
||
This container accepts gets and puts that act continuously, requiring both a rate and time to get or pull at that rate: | ||
|
||
.. code:: python | ||
tank = UP.ContinuousContainer(env, capacity=10, init=0) | ||
tank.put(rate=3.0, time=2.5) | ||
env.run(until=3.0) | ||
print(tank.level) | ||
>>> 7.5 | ||
The gets and puts can be done simultaneously, and the container will determine the current level when asked for it. The | ||
container will also, by default, raise errors when it has reach capacity or when it is empty. | ||
|
||
SortedFilterStore | ||
================= | ||
|
||
This store behaves similar to the SimPy ``FilterStore``, except that it also accepts a function that prioritizes the | ||
items in the store. | ||
|
||
.. code:: python | ||
with UP.EnvironmentContext() as env: | ||
shelf = UP.SortedFilterStore(env) | ||
# pre-load items | ||
shelf.items.extend([(1, "a"), (2, "b"), (1, "b"), (1, "B")]) | ||
def _proc() -> tuple[float, str]: | ||
ans = yield shelf.get( | ||
filter=lambda x: x[1] == x[1].lower(), | ||
sorter=lambda x: (x[1], -x[0]), | ||
reverse=True, | ||
) | ||
return ans | ||
p = env.process(_proc()) | ||
env.run() | ||
print(p.value) | ||
>>> (1, 'b') | ||
In the above, we filter items to have lower-case letters. Then we sort by ascending alphabetical and | ||
descending numerical. Note the use of ``reverse=True`` and the ``-x[0]`` to do this. That gives us the | ||
tie-breaker between ``(1, "a")`` and ``(1, "b")`` that ignores ``(1, "B")``. | ||
|
||
ReserveContainer | ||
================ | ||
|
||
The reserve container is not a true Container, in that it doesn't hold on queues. It is used to hold first-come | ||
reservations for something numeric. Those requests can be timed out, and then checked on later by the | ||
requestor. This is useful if you want to reserve access to a limited resource, but don't want or need to | ||
hold in a line to do so. | ||
|
||
The public methods on the ``ReserveContainer`` are: | ||
|
||
1. ``reserve(requestor, quantity, expiration=None)``: Hold an amount | ||
2. ``cancel_request(requestor)``: Cancel a hold | ||
3. ``take(requestor)``: Get the amount held - or fail if request expired | ||
4. ``put(amount, capacity_increase=False)``: Put something in the container, optionally increasing capacity. | ||
|
||
The workflow with this resource is to resever, take, then put back when done - if the resource represented isn't | ||
consumable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.