Skip to content

Commit 2ffa166

Browse files
committed
Resources class guide improved.
1 parent 3086c3d commit 2ffa166

File tree

10 files changed

+85
-8
lines changed

10 files changed

+85
-8
lines changed
2.61 KB
Binary file not shown.
72 Bytes
Binary file not shown.
592 Bytes
Binary file not shown.

docs/build/html/_sources/api/resource.rst.txt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
Resource
22
========
33

4-
The :class:`piperabm.resource.Resource` class provides a lightweight container
5-
for resource quantities (food, water, energy) with validation and arithmetic
4+
Resources may be provided as plain dictionaries (default) or as instances of the optional :class:`piperabm.resource.Resource` helper class.
5+
This class provides a lightweight container for resource quantities (food, water, energy) with validation and arithmetic
66
support. It is an optional convenience wrapper; internally, PiperABM stores
77
resources as plain dictionaries attached to NetworkX graph nodes.
88

9+
.. code-block:: python
10+
11+
from piperabm.resource import Resource
12+
13+
model.society.add_agent(resources=Resource(food=1, water=2, energy=3))
14+
# An alternative to:
15+
model.society.add_agent(resources={"food":1, "water":2, "energy":3})
16+
17+
.. code-block:: python
18+
19+
# Retrive resources as a Resource object
20+
resources = model.society.get_resources(id=0, object=True)
21+
# Retrive resources as a dictionary
22+
resources = model.society.get_resources(id=0)
23+
24+
Since the Resource class supports arithmetic operations, it can be used to easily manipulate resource quantities.
25+
26+
.. code-block:: python
27+
28+
# Retrive resources as a Resource object
29+
resources = model.society.get_resources(id=0, object=True)
30+
resources =* 2 # Multiply all resource quantities by 2
31+
resources =+ Resource(food=1) # Add 1 unit of food
32+
resources =- {"water": 1} # Subtract 1 unit of water
33+
934
.. autoclass:: piperabm.resource.Resource
1035
:members:
1136
:undoc-members:

docs/build/html/_sources/step-by-step.rst.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ To build the infrastructure, we can either manually add elements:
137137
**Figure 2:** An example of automatically generated infrastructure, after the baking process. The grid is created with some imperfections, and a market node is added to the center of the environment and the homes are randomly placed. The figure is borrowed from `Automatic Creation <https://github.com/cmudrc/pied-piper/blob/main/examples/automatic-creation>`_ example.
138138

139139
For further details on how to load infrastructure using satellite data and maps, refer to the :ref:`Working with Satellite Data <working-with-satellite-data>`.
140+
Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
140141
Before continuing to the next step, we need to "bake" the infrastructure. The process of baking finalizes the infrastructure setup that involves applying certain graph grammars to create a physically sensinble network. For more information, please visit :meth:`~piperabm.Model.bake`.
141142

142143
.. note::
@@ -274,6 +275,7 @@ The other method is to automatically generate the society. The generator method
274275
average_balance=1000,
275276
)
276277
278+
Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
277279
Agents use OODA loop, which stands for Observe, Orient, Decide, and Act as the decision-making framework. Agents observe themselves, others, and their environment, and then analyze that information using their values. The result of this decision-making a set of action, that, once executed, will impact the agents and their environment. This loop continues as long as the agetns are alive.
278280

279281
.. figure:: _static/step-by-step/ooda.png

docs/build/html/api/resource.html

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,31 @@
3535

3636
<section id="resource">
3737
<h1>Resource<a class="headerlink" href="#resource" title="Link to this heading"></a></h1>
38-
<p>The <a class="reference internal" href="#piperabm.resource.Resource" title="piperabm.resource.Resource"><code class="xref py py-class docutils literal notranslate"><span class="pre">piperabm.resource.Resource</span></code></a> class provides a lightweight container
39-
for resource quantities (food, water, energy) with validation and arithmetic
38+
<p>Resources may be provided as plain dictionaries (default) or as instances of the optional <a class="reference internal" href="#piperabm.resource.Resource" title="piperabm.resource.Resource"><code class="xref py py-class docutils literal notranslate"><span class="pre">piperabm.resource.Resource</span></code></a> helper class.
39+
This class provides a lightweight container for resource quantities (food, water, energy) with validation and arithmetic
4040
support. It is an optional convenience wrapper; internally, PiperABM stores
4141
resources as plain dictionaries attached to NetworkX graph nodes.</p>
42+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">piperabm.resource</span><span class="w"> </span><span class="kn">import</span> <span class="n">Resource</span>
43+
44+
<span class="n">model</span><span class="o">.</span><span class="n">society</span><span class="o">.</span><span class="n">add_agent</span><span class="p">(</span><span class="n">resources</span><span class="o">=</span><span class="n">Resource</span><span class="p">(</span><span class="n">food</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">water</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">energy</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>
45+
<span class="c1"># An alternative to:</span>
46+
<span class="n">model</span><span class="o">.</span><span class="n">society</span><span class="o">.</span><span class="n">add_agent</span><span class="p">(</span><span class="n">resources</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;food&quot;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;water&quot;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s2">&quot;energy&quot;</span><span class="p">:</span><span class="mi">3</span><span class="p">})</span>
47+
</pre></div>
48+
</div>
49+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Retrive resources as a Resource object</span>
50+
<span class="n">resources</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">society</span><span class="o">.</span><span class="n">get_resources</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">object</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
51+
<span class="c1"># Retrive resources as a dictionary</span>
52+
<span class="n">resources</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">society</span><span class="o">.</span><span class="n">get_resources</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
53+
</pre></div>
54+
</div>
55+
<p>Since the Resource class supports arithmetic operations, it can be used to easily manipulate resource quantities.</p>
56+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Retrive resources as a Resource object</span>
57+
<span class="n">resources</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">society</span><span class="o">.</span><span class="n">get_resources</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">object</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
58+
<span class="n">resources</span> <span class="o">=*</span> <span class="mi">2</span> <span class="c1"># Multiply all resource quantities by 2</span>
59+
<span class="n">resources</span> <span class="o">=+</span> <span class="n">Resource</span><span class="p">(</span><span class="n">food</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># Add 1 unit of food</span>
60+
<span class="n">resources</span> <span class="o">=-</span> <span class="p">{</span><span class="s2">&quot;water&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span> <span class="c1"># Subtract 1 unit of water</span>
61+
</pre></div>
62+
</div>
4263
<dl class="py class">
4364
<dt class="sig sig-object py" id="piperabm.resource.Resource">
4465
<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">piperabm.resource.</span></span><span class="sig-name descname"><span class="pre">Resource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">food</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">float</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">water</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">float</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">energy</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">float</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#piperabm.resource.Resource" title="Link to this definition"></a></dt>

docs/build/html/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/build/html/step-by-step.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
</figcaption>
155155
</figure>
156156
<p>For further details on how to load infrastructure using satellite data and maps, refer to the <a class="reference internal" href="satellite.html#working-with-satellite-data"><span class="std std-ref">Working with Satellite Data</span></a>.
157+
Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
157158
Before continuing to the next step, we need to “bake” the infrastructure. The process of baking finalizes the infrastructure setup that involves applying certain graph grammars to create a physically sensinble network. For more information, please visit <a class="reference internal" href="api/model.html#piperabm.Model.bake" title="piperabm.Model.bake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bake()</span></code></a>.</p>
158159
<div class="admonition note">
159160
<p class="admonition-title">Note</p>
@@ -272,7 +273,8 @@
272273
<span class="p">)</span>
273274
</pre></div>
274275
</div>
275-
<p>Agents use OODA loop, which stands for Observe, Orient, Decide, and Act as the decision-making framework. Agents observe themselves, others, and their environment, and then analyze that information using their values. The result of this decision-making a set of action, that, once executed, will impact the agents and their environment. This loop continues as long as the agetns are alive.</p>
276+
<p>Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
277+
Agents use OODA loop, which stands for Observe, Orient, Decide, and Act as the decision-making framework. Agents observe themselves, others, and their environment, and then analyze that information using their values. The result of this decision-making a set of action, that, once executed, will impact the agents and their environment. This loop continues as long as the agetns are alive.</p>
276278
<figure class="align-center" id="id7">
277279
<img alt="OODA loop as the core decision-making framework." src="_images/ooda.png" />
278280
<figcaption>

docs/source/api/resource.rst

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
Resource
22
========
33

4-
The :class:`piperabm.resource.Resource` class provides a lightweight container
5-
for resource quantities (food, water, energy) with validation and arithmetic
4+
Resources may be provided as plain dictionaries (default) or as instances of the optional :class:`piperabm.resource.Resource` helper class.
5+
This class provides a lightweight container for resource quantities (food, water, energy) with validation and arithmetic
66
support. It is an optional convenience wrapper; internally, PiperABM stores
77
resources as plain dictionaries attached to NetworkX graph nodes.
88

9+
.. code-block:: python
10+
11+
from piperabm.resource import Resource
12+
13+
model.society.add_agent(resources=Resource(food=1, water=2, energy=3))
14+
# An alternative to:
15+
model.society.add_agent(resources={"food":1, "water":2, "energy":3})
16+
17+
.. code-block:: python
18+
19+
# Retrive resources as a Resource object
20+
resources = model.society.get_resources(id=0, object=True)
21+
# Retrive resources as a dictionary
22+
resources = model.society.get_resources(id=0)
23+
24+
Since the Resource class supports arithmetic operations, it can be used to easily manipulate resource quantities.
25+
26+
.. code-block:: python
27+
28+
# Retrive resources as a Resource object
29+
resources = model.society.get_resources(id=0, object=True)
30+
resources =* 2 # Multiply all resource quantities by 2
31+
resources =+ Resource(food=1) # Add 1 unit of food
32+
resources =- {"water": 1} # Subtract 1 unit of water
33+
934
.. autoclass:: piperabm.resource.Resource
1035
:members:
1136
:undoc-members:

docs/source/step-by-step.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ To build the infrastructure, we can either manually add elements:
137137
**Figure 2:** An example of automatically generated infrastructure, after the baking process. The grid is created with some imperfections, and a market node is added to the center of the environment and the homes are randomly placed. The figure is borrowed from `Automatic Creation <https://github.com/cmudrc/pied-piper/blob/main/examples/automatic-creation>`_ example.
138138

139139
For further details on how to load infrastructure using satellite data and maps, refer to the :ref:`Working with Satellite Data <working-with-satellite-data>`.
140+
Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
140141
Before continuing to the next step, we need to "bake" the infrastructure. The process of baking finalizes the infrastructure setup that involves applying certain graph grammars to create a physically sensinble network. For more information, please visit :meth:`~piperabm.Model.bake`.
141142

142143
.. note::
@@ -274,6 +275,7 @@ The other method is to automatically generate the society. The generator method
274275
average_balance=1000,
275276
)
276277
278+
Resources may be provided as plain dictionaries (default) or as instances of the optional Resource helper class; see the API reference for details.
277279
Agents use OODA loop, which stands for Observe, Orient, Decide, and Act as the decision-making framework. Agents observe themselves, others, and their environment, and then analyze that information using their values. The result of this decision-making a set of action, that, once executed, will impact the agents and their environment. This loop continues as long as the agetns are alive.
278280

279281
.. figure:: _static/step-by-step/ooda.png

0 commit comments

Comments
 (0)