The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings EMULSION public releases
Summary Activity Surveys SCM Listes Sympa

SCM Repository

df09b43b603bacce0c245c8fd6651fbe83fbe378
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
6   <head>
7     <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
8     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9     <title>emulsion.agent.action &#8212; EMULSION (Epidemiological Multi-Level Simulation framework)</title>
10     <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
11     <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
12     <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
13     <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
14     <script type="text/javascript" src="../../../_static/jquery.js"></script>
15     <script type="text/javascript" src="../../../_static/underscore.js"></script>
16     <script type="text/javascript" src="../../../_static/doctools.js"></script>
17     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
18     <link rel="shortcut icon" href="../../../_static/STEMAH-favicon.png"/>
19     <link rel="index" title="Index" href="../../../genindex.html" />
20     <link rel="search" title="Search" href="../../../search.html" />
21    
22   <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
23   
24   
25   <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
27   </head><body>
28   
30     <div class="document">
31       <div class="documentwrapper">
32         <div class="bodywrapper">
33           <div class="body" role="main">
34             
35   <h1>Source code for emulsion.agent.action</h1><div class="highlight"><pre>
36 <span></span><span class="sd">&quot;&quot;&quot;A Python implementation of the EMuLSion framework (Epidemiologic</span>
37 <span class="sd">MUlti-Level SImulatiONs).</span>
39 <span class="sd">Classes and functions for actions.</span>
40 <span class="sd">&quot;&quot;&quot;</span>
42 <span class="c1"># [HEADER]</span>
44 <span class="kn">from</span>   <span class="nn">abc</span>                 <span class="k">import</span> <span class="n">abstractmethod</span>
45 <span class="kn">import</span> <span class="nn">numpy</span>               <span class="k">as</span>     <span class="nn">np</span>
47 <span class="kn">from</span>   <span class="nn">emulsion.tools.misc</span> <span class="k">import</span> <span class="n">retrieve_value</span><span class="p">,</span> <span class="n">rates_to_probabilities</span>
50 <span class="c1">#  ______                    _   _</span>
51 <span class="c1"># |  ____|                  | | (_)</span>
52 <span class="c1"># | |__  __  _____ ___ _ __ | |_ _  ___  _ __  ___</span>
53 <span class="c1"># |  __| \ \/ / __/ _ \ &#39;_ \| __| |/ _ \| &#39;_ \/ __|</span>
54 <span class="c1"># | |____ &gt;  &lt; (_|  __/ |_) | |_| | (_) | | | \__ \</span>
55 <span class="c1"># |______/_/\_\___\___| .__/ \__|_|\___/|_| |_|___/</span>
56 <span class="c1">#                     | |</span>
57 <span class="c1">#                     |_|</span>
59 <div class="viewcode-block" id="InvalidActionException"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.InvalidActionException">[docs]</a><span class="k">class</span> <span class="nc">InvalidActionException</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
60     <span class="sd">&quot;&quot;&quot;Exception raised when a semantic error occurs in action definition.</span>
62 <span class="sd">    &quot;&quot;&quot;</span>
63     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
64         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
65         <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
67     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
68         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span></div>
71 <span class="c1">#   _____ _</span>
72 <span class="c1">#  / ____| |</span>
73 <span class="c1"># | |    | | __ _ ___ ___  ___  ___</span>
74 <span class="c1"># | |    | |/ _` / __/ __|/ _ \/ __|</span>
75 <span class="c1"># | |____| | (_| \__ \__ \  __/\__ \</span>
76 <span class="c1">#  \_____|_|\__,_|___/___/\___||___/</span>
79 <div class="viewcode-block" id="AbstractAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.AbstractAction">[docs]</a><span class="k">class</span> <span class="nc">AbstractAction</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
80     <span class="sd">&quot;&quot;&quot;AbstractActions are aimed at describing actions triggered by a</span>
81 <span class="sd">    state machine.</span>
83 <span class="sd">    &quot;&quot;&quot;</span>
84     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_machine</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">_</span><span class="p">):</span>
85         <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span> <span class="o">=</span> <span class="n">state_machine</span>
87 <div class="viewcode-block" id="AbstractAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.AbstractAction.execute_action">[docs]</a>    <span class="nd">@abstractmethod</span>
88     <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
89         <span class="sd">&quot;&quot;&quot;Execute the action on the specified unit.&quot;&quot;&quot;</span>
90 <span class="c1">#        print(self, &#39;executed by&#39;, unit)</span>
91         <span class="k">pass</span></div>
93 <div class="viewcode-block" id="AbstractAction.build_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.AbstractAction.build_action">[docs]</a>    <span class="nd">@classmethod</span>
94     <span class="k">def</span> <span class="nf">build_action</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">action_name</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
95         <span class="sd">&quot;&quot;&quot;Return an instance of the appropriate Action subclass,</span>
96 <span class="sd">        depending on its name. The appropriate parameters for this</span>
97 <span class="sd">        action should be passed as a dictionary.</span>
99 <span class="sd">        &quot;&quot;&quot;</span>
100         <span class="k">return</span> <span class="n">ACTION_DICT</span><span class="p">[</span><span class="n">action_name</span><span class="p">](</span><span class="o">**</span><span class="n">others</span><span class="p">)</span></div>
102     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
103         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span></div>
105 <div class="viewcode-block" id="ValueAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.ValueAction">[docs]</a><span class="k">class</span> <span class="nc">ValueAction</span><span class="p">(</span><span class="n">AbstractAction</span><span class="p">):</span>
106     <span class="sd">&quot;&quot;&quot;ValueActions represent modifications of state variables or</span>
107 <span class="sd">    attributes.</span>
109 <span class="sd">    &quot;&quot;&quot;</span>
110     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">statevar_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parameter</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">delta_t</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
111         <span class="sd">&quot;&quot;&quot;Create a ValueAction aimed at modifying the specified</span>
112 <span class="sd">        statevar according to the parameter.</span>
114 <span class="sd">        &quot;&quot;&quot;</span>
115         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
116         <span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span> <span class="o">=</span> <span class="n">statevar_name</span>
117         <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span> <span class="o">=</span> <span class="n">parameter</span>
118         <span class="bp">self</span><span class="o">.</span><span class="n">delta_t</span> <span class="o">=</span> <span class="n">delta_t</span></div>
120 <div class="viewcode-block" id="SetVarAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.SetVarAction">[docs]</a><span class="k">class</span> <span class="nc">SetVarAction</span><span class="p">(</span><span class="n">ValueAction</span><span class="p">):</span>
121     <span class="sd">&quot;&quot;&quot;SetVarAction allow to set the variable of the agent.</span>
123 <span class="sd">    &quot;&quot;&quot;</span>
124     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">statevar_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parameter</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
125         <span class="sd">&quot;&quot;&quot;Create a SetVarAction aimed at modifying the specified statevar</span>
126 <span class="sd">        according to the paramter.</span>
128 <span class="sd">        &quot;&quot;&quot;</span>
129         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
130         <span class="k">if</span> <span class="n">statevar_name</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">state_machines</span><span class="p">:</span>
131             <span class="k">raise</span> <span class="n">InvalidActionException</span><span class="p">(</span><span class="s2">&quot;Action set_var must not change values of state machines. Use action become instead.</span><span class="se">\n\t</span><span class="s2">set_var: </span><span class="si">{}</span><span class="s2"> value: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">statevar_name</span><span class="p">,</span> <span class="n">parameter</span><span class="p">))</span>
132         <span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span> <span class="o">=</span> <span class="n">statevar_name</span>
133         <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span> <span class="o">=</span> <span class="n">parameter</span>
134         <span class="c1"># print(self)</span>
136 <div class="viewcode-block" id="SetVarAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.SetVarAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
137         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
138 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
139 <span class="sd">        the action. If changes of state variables in relation to a</span>
140 <span class="sd">        state machine occur, the corresponding actions (if any) are</span>
141 <span class="sd">        executed: on_exit from the current state, and on_enter for the</span>
142 <span class="sd">        new state.</span>
144 <span class="sd">        &quot;&quot;&quot;</span>
145         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
146             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
147         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
148             <span class="n">value</span> <span class="o">=</span> <span class="n">agent</span><span class="o">.</span><span class="n">get_model_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
149             <span class="c1"># agent.set_information(self.statevar_name, value)</span>
150             <span class="n">agent</span><span class="o">.</span><span class="n">statevars</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span></div>
152     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
153         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; </span><span class="si">{}</span><span class="s1"> &lt;- </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">,</span>
154                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
155     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
158 <div class="viewcode-block" id="RateAdditiveAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.RateAdditiveAction">[docs]</a><span class="k">class</span> <span class="nc">RateAdditiveAction</span><span class="p">(</span><span class="n">ValueAction</span><span class="p">):</span>
159     <span class="sd">&quot;&quot;&quot;A RateChangeAction is aimed at increasing or decreasing a</span>
160 <span class="sd">    specific state variable or attribute, according to a specific rate</span>
161 <span class="sd">    (i.e. the actual increase or decrease is the product of the</span>
162 <span class="sd">    `parameter` attribute and a population size).</span>
164 <span class="sd">    &quot;&quot;&quot;</span>
165     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sign</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
166         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
167         <span class="bp">self</span><span class="o">.</span><span class="n">sign</span> <span class="o">=</span> <span class="n">sign</span>
169 <div class="viewcode-block" id="RateAdditiveAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.RateAdditiveAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">population</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
170         <span class="sd">&quot;&quot;&quot;Execute the action on the specified unit, with the</span>
171 <span class="sd">        specified population size.</span>
173 <span class="sd">        &quot;&quot;&quot;</span>
174         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">execute_action</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span>
175         <span class="k">if</span> <span class="n">population</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
176             <span class="n">population</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">agents</span><span class="p">)</span>
177         <span class="n">rate_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
178         <span class="n">rate</span> <span class="o">=</span> <span class="n">retrieve_value</span><span class="p">(</span><span class="n">rate_value</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span>
179         <span class="n">current_val</span> <span class="o">=</span> <span class="n">unit</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">)</span>
180         <span class="n">new_val</span> <span class="o">=</span> <span class="n">current_val</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sign</span><span class="o">*</span><span class="n">rate</span><span class="o">*</span><span class="n">population</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_t</span>
181         <span class="c1"># print(&#39;Executing&#39;, self.__class__.__name__, &#39;for&#39;, unit,</span>
182         <span class="c1">#       self.statevar_name, current_val, &#39;-&gt;&#39;, new_val,</span>
183         <span class="c1">#       self.sign, rate, population)</span>
184         <span class="n">unit</span><span class="o">.</span><span class="n">set_information</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">,</span> <span class="n">new_val</span><span class="p">)</span></div>
186     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
187         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; (</span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">,</span>
188                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
189     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
192 <div class="viewcode-block" id="RateDecreaseAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.RateDecreaseAction">[docs]</a><span class="k">class</span> <span class="nc">RateDecreaseAction</span><span class="p">(</span><span class="n">RateAdditiveAction</span><span class="p">):</span>
193     <span class="sd">&quot;&quot;&quot;A RateDecreaseAction is aimed at decreasing a specific state</span>
194 <span class="sd">    variable or attribute, according to a specific rate (i.e. the</span>
195 <span class="sd">    actual decrease is the product of the `parameter` attribute and a</span>
196 <span class="sd">    population size).</span>
198 <span class="sd">    &quot;&quot;&quot;</span>
199     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
200         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">sign</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">)</span></div>
202 <div class="viewcode-block" id="RateIncreaseAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.RateIncreaseAction">[docs]</a><span class="k">class</span> <span class="nc">RateIncreaseAction</span><span class="p">(</span><span class="n">RateAdditiveAction</span><span class="p">):</span>
203     <span class="sd">&quot;&quot;&quot;A RateIncreaseAction is aimed at increasing a specific state</span>
204 <span class="sd">    variable or attribute, according to a specific rate (i.e. the</span>
205 <span class="sd">    actual increase is the product of the `parameter` attribute and a</span>
206 <span class="sd">    population size).</span>
208 <span class="sd">    &quot;&quot;&quot;</span>
209     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
210         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">sign</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">)</span></div>
212 <div class="viewcode-block" id="StochAdditiveAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.StochAdditiveAction">[docs]</a><span class="k">class</span> <span class="nc">StochAdditiveAction</span><span class="p">(</span><span class="n">ValueAction</span><span class="p">):</span>
213     <span class="sd">&quot;&quot;&quot;A StochAdditiveAction is aimed at increasing or decreasing a</span>
214 <span class="sd">    specific state variable or attribute, according to a specific</span>
215 <span class="sd">    rate, using a *binomial sampling*.</span>
217 <span class="sd">    &quot;&quot;&quot;</span>
218     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sign</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
219         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
220         <span class="bp">self</span><span class="o">.</span><span class="n">sign</span> <span class="o">=</span> <span class="n">sign</span>
222 <div class="viewcode-block" id="StochAdditiveAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.StochAdditiveAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">population</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
223         <span class="sd">&quot;&quot;&quot;Execute the action on the specified unit, with the</span>
224 <span class="sd">        specified population size.</span>
226 <span class="sd">        &quot;&quot;&quot;</span>
227         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">execute_action</span><span class="p">(</span><span class="n">unit</span><span class="p">)</span>
228         <span class="k">if</span> <span class="n">population</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
229             <span class="n">population</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">agents</span><span class="p">)</span>
230         <span class="n">rate_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
231         <span class="n">rate</span> <span class="o">=</span> <span class="n">retrieve_value</span><span class="p">(</span><span class="n">rate_value</span><span class="p">,</span> <span class="n">unit</span><span class="p">)</span>
232         <span class="c1"># convert rate into a probability</span>
233         <span class="n">proba</span> <span class="o">=</span> <span class="n">rates_to_probabilities</span><span class="p">(</span><span class="n">rate</span><span class="p">,</span> <span class="p">[</span><span class="n">rate</span><span class="p">],</span> <span class="n">delta_t</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_t</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
234         <span class="n">current_val</span> <span class="o">=</span> <span class="n">unit</span><span class="o">.</span><span class="n">get_information</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">)</span>
235         <span class="n">new_val</span> <span class="o">=</span> <span class="n">current_val</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sign</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">binomial</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">proba</span><span class="p">)</span>
236         <span class="c1"># print(&#39;Executing&#39;, self.__class__.__name__, &#39;for&#39;, unit,</span>
237         <span class="c1">#       self.statevar_name, current_val, &#39;-&gt;&#39;, new_val,</span>
238         <span class="c1">#       self.sign, rate, population)</span>
239         <span class="n">unit</span><span class="o">.</span><span class="n">set_information</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">,</span> <span class="n">new_val</span><span class="p">)</span></div>
241     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
242         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; (</span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">statevar_name</span><span class="p">,</span>
243                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
244     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
247 <div class="viewcode-block" id="StochDecreaseAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.StochDecreaseAction">[docs]</a><span class="k">class</span> <span class="nc">StochDecreaseAction</span><span class="p">(</span><span class="n">StochAdditiveAction</span><span class="p">):</span>
248     <span class="sd">&quot;&quot;&quot;A StochDecreaseAction is aimed at decreasing a specific state</span>
249 <span class="sd">    variable or attribute, according to a specific rate, using a</span>
250 <span class="sd">    *binomial sampling*.</span>
252 <span class="sd">    &quot;&quot;&quot;</span>
253     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
254         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">sign</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">)</span></div>
256 <div class="viewcode-block" id="StochIncreaseAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.StochIncreaseAction">[docs]</a><span class="k">class</span> <span class="nc">StochIncreaseAction</span><span class="p">(</span><span class="n">StochAdditiveAction</span><span class="p">):</span>
257     <span class="sd">&quot;&quot;&quot;A StochIncreaseAction is aimed at increasing a specific state</span>
258 <span class="sd">    variable or attribute, according to a specific rate, using a</span>
259 <span class="sd">    *binomial sampling*.</span>
261 <span class="sd">    &quot;&quot;&quot;</span>
262     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
263         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">sign</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">)</span></div>
265 <div class="viewcode-block" id="StringAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.StringAction">[docs]</a><span class="k">class</span> <span class="nc">StringAction</span><span class="p">(</span><span class="n">AbstractAction</span><span class="p">):</span>
266     <span class="sd">&quot;&quot;&quot;A StringAction is based on the specification of a string</span>
267 <span class="sd">    parameter.</span>
269 <span class="sd">    &quot;&quot;&quot;</span>
270     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parameter</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">l_params</span><span class="o">=</span><span class="p">[],</span> <span class="n">d_params</span><span class="o">=</span><span class="p">{},</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
271         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
272         <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span> <span class="o">=</span> <span class="n">parameter</span>
273         <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span> <span class="o">=</span> <span class="n">l_params</span>
274         <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span> <span class="o">=</span> <span class="n">d_params</span>
276     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
277         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; (</span><span class="si">{!s}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">,</span>
278                                                             <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">,</span>
279                                                             <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="p">)</span>
280     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
283 <div class="viewcode-block" id="BecomeAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.BecomeAction">[docs]</a><span class="k">class</span> <span class="nc">BecomeAction</span><span class="p">(</span><span class="n">StringAction</span><span class="p">):</span>
284     <span class="sd">&quot;&quot;&quot;A BecomeAction is aimed at making an agent change its state</span>
285 <span class="sd">    according to a specified prototype an action. It requires a</span>
286 <span class="sd">    prototype name.</span>
288 <span class="sd">    &quot;&quot;&quot;</span>
289 <div class="viewcode-block" id="BecomeAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.BecomeAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
290         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
291 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
292 <span class="sd">        the action. If changes of state variables in relation to a</span>
293 <span class="sd">        state machine occur, the corresponding actions (if any) are</span>
294 <span class="sd">        executed: on_exit from the current state, and on_enter for the</span>
295 <span class="sd">        new state.</span>
297 <span class="sd">        &quot;&quot;&quot;</span>
298         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
299             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
300         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
301             <span class="n">agent</span><span class="o">.</span><span class="n">apply_prototype</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">,</span> <span class="n">execute_actions</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div></div>
303 <div class="viewcode-block" id="CloneAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.CloneAction">[docs]</a><span class="k">class</span> <span class="nc">CloneAction</span><span class="p">(</span><span class="n">AbstractAction</span><span class="p">):</span>
304     <span class="sd">&quot;&quot;&quot;A CloneAction produces several copies of the agent with a given</span>
305 <span class="sd">    prototype.</span>
307 <span class="sd">    &quot;&quot;&quot;</span>
308     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prototypes</span><span class="o">=</span><span class="p">[],</span> <span class="n">amount</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">probas</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
309         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
310         <span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span> <span class="o">=</span> <span class="n">prototypes</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">prototypes</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span>\
311                                <span class="k">else</span> <span class="p">[</span><span class="n">prototypes</span><span class="p">]</span>
312         <span class="n">amount</span><span class="o">=</span> <span class="n">amount</span> <span class="k">if</span> <span class="n">amount</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">1</span>
313         <span class="k">if</span> <span class="n">probas</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
314             <span class="n">probas</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span><span class="p">)]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span><span class="p">)</span>
315         <span class="k">else</span><span class="p">:</span>
316             <span class="n">probas</span> <span class="o">=</span> <span class="n">probas</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">probas</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="k">else</span> <span class="p">[</span><span class="n">probas</span><span class="p">]</span>
317         <span class="k">assert</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">probas</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span>
318         <span class="bp">self</span><span class="o">.</span><span class="n">amount</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">amount</span><span class="p">)</span>
319         <span class="bp">self</span><span class="o">.</span><span class="n">probas</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">add_expression</span><span class="p">(</span><span class="n">pr</span><span class="p">)</span> <span class="k">for</span> <span class="n">pr</span> <span class="ow">in</span> <span class="n">probas</span><span class="p">]</span>
321     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
322         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; (</span><span class="si">{!s}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span><span class="p">,</span>
323                                                             <span class="bp">self</span><span class="o">.</span><span class="n">amount</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">probas</span><span class="p">)</span>
324     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>
326 <div class="viewcode-block" id="CloneAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.CloneAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
327         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
328 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
329 <span class="sd">        the action. If changes of state variables in relation to a</span>
330 <span class="sd">        state machine occur, the corresponding actions (if any) are</span>
331 <span class="sd">        executed: on_exit from the current state, and on_enter for the</span>
332 <span class="sd">        new state.</span>
334 <span class="sd">        &quot;&quot;&quot;</span>
335         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
336             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
337         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
338             <span class="n">protos</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prototype_names</span><span class="p">)</span>
339             <span class="c1"># compute actual values for probabilities</span>
340             <span class="n">proba_values</span> <span class="o">=</span> <span class="p">[</span><span class="n">agent</span><span class="o">.</span><span class="n">get_model_value</span><span class="p">(</span><span class="n">prob</span><span class="p">)</span> <span class="k">for</span> <span class="n">prob</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">probas</span><span class="p">]</span>
341             <span class="n">total</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">proba_values</span><span class="p">)</span>
342             <span class="k">assert</span><span class="p">(</span><span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">total</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span>
343             <span class="k">if</span> <span class="n">total</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
344                 <span class="c1"># add complement</span>
345                 <span class="n">proba_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">total</span><span class="p">)</span>
346                 <span class="c1"># if N-1 probabilities were given for N prototypes, no</span>
347                 <span class="c1"># problem: the last prototype is getting the 1-total</span>
348                 <span class="c1"># value. Otherwise, this means that there is a</span>
349                 <span class="c1"># possibility that no individuals are produced =&gt; None</span>
350                 <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">proba_values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">protos</span><span class="p">):</span>
351                     <span class="n">protos</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
352             <span class="n">quantities</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">multinomial</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">agent</span><span class="o">.</span><span class="n">get_model_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">amount</span><span class="p">)),</span>
353                                                <span class="n">proba_values</span><span class="p">)</span>
354             <span class="k">for</span> <span class="n">prototype</span><span class="p">,</span> <span class="n">quantity</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">protos</span><span class="p">,</span> <span class="n">quantities</span><span class="p">):</span>
355                 <span class="k">if</span> <span class="n">prototype</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
356                     <span class="n">newborns</span> <span class="o">=</span> <span class="p">[</span><span class="n">agent</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">prototype</span> <span class="o">=</span> <span class="n">prototype</span><span class="p">)</span>
357                                 <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">quantity</span><span class="p">)]</span>
358                     <span class="n">agent</span><span class="o">.</span><span class="n">upper_level</span><span class="p">()</span><span class="o">.</span><span class="n">add_atoms</span><span class="p">(</span><span class="n">newborns</span><span class="p">)</span></div></div>
361 <div class="viewcode-block" id="MessageAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.MessageAction">[docs]</a><span class="k">class</span> <span class="nc">MessageAction</span><span class="p">(</span><span class="n">StringAction</span><span class="p">):</span>
362     <span class="sd">&quot;&quot;&quot;A MessageAction is aimed at making an agent print a given</span>
363 <span class="sd">    string. It requires a string message. This string can contain one</span>
364 <span class="sd">    reference to a variable or method of the agent, using Python&#39;s</span>
365 <span class="sd">    formatting syntax.</span>
367 <span class="sd">    For instance, &#39;My state is {.statevars.health_state}&#39; will print</span>
368 <span class="sd">    the current health state of the agent.</span>
370 <span class="sd">    Output is formatted in three comma-separated fields: the time step</span>
371 <span class="sd">    when the message was produced, the agent speaking, and the message</span>
372 <span class="sd">    itself.</span>
374 <span class="sd">    &quot;&quot;&quot;</span>
375 <div class="viewcode-block" id="MessageAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.MessageAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
376         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
377 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
378 <span class="sd">        the action.</span>
380 <span class="sd">        &quot;&quot;&quot;</span>
381         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
382             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
383         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
384             <span class="n">message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">agent</span><span class="p">)</span>
385             <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;@</span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">agent</span><span class="o">.</span><span class="n">statevars</span><span class="o">.</span><span class="n">step</span><span class="p">,</span> <span class="n">agent</span><span class="p">,</span> <span class="n">message</span><span class="p">))</span></div></div>
388 <div class="viewcode-block" id="MethodAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.MethodAction">[docs]</a><span class="k">class</span> <span class="nc">MethodAction</span><span class="p">(</span><span class="n">AbstractAction</span><span class="p">):</span>
389     <span class="sd">&quot;&quot;&quot;A MethodAction is aimed at making an agent perform an action on</span>
390 <span class="sd">    a specific population. It requires a method name, and optionnally</span>
391 <span class="sd">    a list and a dictionary of parameters.</span>
393 <span class="sd">    &quot;&quot;&quot;</span>
394     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">l_params</span><span class="o">=</span><span class="p">[],</span> <span class="n">d_params</span><span class="o">=</span><span class="p">{},</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
395         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
396         <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">method</span>
397         <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span> <span class="o">=</span> <span class="n">l_params</span>
398         <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span> <span class="o">=</span> <span class="n">d_params</span>
400     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
401         <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; (</span><span class="si">{!s}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="p">,</span>
402                                                             <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">,</span>
403                                                             <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="p">)</span>
404     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>
406 <div class="viewcode-block" id="MethodAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.MethodAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
407         <span class="sd">&quot;&quot;&quot;Execute the action using the specified unit. If the</span>
408 <span class="sd">        `agents` parameter is a list of units, each unit of this list</span>
409 <span class="sd">        will execute the action.</span>
411 <span class="sd">        &quot;&quot;&quot;</span>
412         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
413             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
414         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
415             <span class="n">action</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">agent</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
416             <span class="n">l_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">retrieve_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> <span class="n">agent</span><span class="p">)</span>
417                         <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">]</span>
418             <span class="c1">### introduced to pass internal information such as population</span>
419             <span class="n">d_params</span> <span class="o">=</span> <span class="n">others</span>
420             <span class="n">d_params</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">key</span><span class="p">:</span> <span class="n">retrieve_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> <span class="n">agent</span><span class="p">)</span>
421                              <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">expr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="o">.</span><span class="n">items</span><span class="p">()})</span>
422             <span class="n">action</span><span class="p">(</span><span class="o">*</span><span class="n">l_params</span><span class="p">,</span> <span class="o">**</span><span class="n">d_params</span><span class="p">)</span></div></div>
424 <div class="viewcode-block" id="FunctionAction"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.FunctionAction">[docs]</a><span class="k">class</span> <span class="nc">FunctionAction</span><span class="p">(</span><span class="n">MethodAction</span><span class="p">):</span>
425     <span class="sd">&quot;&quot;&quot;A FunctionAction is aimed at making an agent perform an action</span>
426 <span class="sd">    on a specific population. It requires a function, and optionnally</span>
427 <span class="sd">    a list and a dictionary of parameters. A FunctionAction runs</span>
428 <span class="sd">    faster than a MethodAction since it does not require to retrieve</span>
429 <span class="sd">    the method in each agent.</span>
431 <span class="sd">    &quot;&quot;&quot;</span>
432     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">function</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
433         <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">others</span><span class="p">)</span>
434         <span class="bp">self</span><span class="o">.</span><span class="n">function</span> <span class="o">=</span> <span class="n">function</span>
435         <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">function</span><span class="o">.</span><span class="vm">__name__</span>
437 <div class="viewcode-block" id="FunctionAction.execute_action"><a class="viewcode-back" href="../../../emulsion.agent.html#emulsion.agent.action.FunctionAction.execute_action">[docs]</a>    <span class="k">def</span> <span class="nf">execute_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">agents</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">others</span><span class="p">):</span>
438         <span class="sd">&quot;&quot;&quot;Execute the action using the specified unit. If the</span>
439 <span class="sd">        `agents` parameter is a list of units, each unit of this list</span>
440 <span class="sd">        will execute the action.</span>
442 <span class="sd">        &quot;&quot;&quot;</span>
443         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
444             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
445         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
446             <span class="n">l_params</span> <span class="o">=</span> <span class="p">[</span><span class="n">retrieve_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span>
447                                        <span class="n">agent</span><span class="p">)</span>
448                         <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">]</span>
449             <span class="c1">### introduced to pass internal information such as population</span>
450             <span class="n">d_params</span> <span class="o">=</span> <span class="n">others</span>
451             <span class="n">d_params</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">key</span><span class="p">:</span>\
452                              <span class="n">retrieve_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state_machine</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span>
453                                             <span class="n">agent</span><span class="p">)</span>
454                              <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">expr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="o">.</span><span class="n">items</span><span class="p">()})</span>
455             <span class="bp">self</span><span class="o">.</span><span class="n">function</span><span class="p">(</span><span class="n">agent</span><span class="p">,</span> <span class="o">*</span><span class="n">l_params</span><span class="p">,</span> <span class="o">**</span><span class="n">d_params</span><span class="p">)</span></div></div>
458 <span class="n">ACTION_DICT</span> <span class="o">=</span> <span class="p">{</span>
459     <span class="s1">&#39;increase&#39;</span><span class="p">:</span> <span class="n">RateIncreaseAction</span><span class="p">,</span>
460     <span class="s1">&#39;decrease&#39;</span><span class="p">:</span> <span class="n">RateDecreaseAction</span><span class="p">,</span>
461     <span class="s1">&#39;increase_stoch&#39;</span><span class="p">:</span> <span class="n">StochIncreaseAction</span><span class="p">,</span>
462     <span class="s1">&#39;decrease_stoch&#39;</span><span class="p">:</span> <span class="n">StochDecreaseAction</span><span class="p">,</span>
463     <span class="s1">&#39;message&#39;</span><span class="p">:</span> <span class="n">MessageAction</span><span class="p">,</span>
464     <span class="s1">&#39;become&#39;</span><span class="p">:</span> <span class="n">BecomeAction</span><span class="p">,</span>
465     <span class="s1">&#39;clone&#39;</span><span class="p">:</span> <span class="n">CloneAction</span><span class="p">,</span>
466     <span class="s1">&#39;produce_offspring&#39;</span><span class="p">:</span> <span class="n">CloneAction</span><span class="p">,</span>
467     <span class="s1">&#39;action&#39;</span><span class="p">:</span> <span class="n">MethodAction</span><span class="p">,</span>
468     <span class="s1">&#39;duration&#39;</span><span class="p">:</span> <span class="n">FunctionAction</span><span class="p">,</span>
469     <span class="s1">&#39;set_var&#39;</span><span class="p">:</span> <span class="n">SetVarAction</span>
470 <span class="p">}</span>
471 </pre></div>
473           </div>
474         </div>
475       </div>
476       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
477         <div class="sphinxsidebarwrapper">
478 <h1 class="logo"><a href="../../../index.html">EMULSION</a></h1>
482 <p class="blurb">Epidemiological Multi-Level Simulation Framework</p>
489 <h3>Navigation</h3>
490 <ul>
491 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Install.html">Installation</a></li>
492 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Getting_started.html">Getting started with EMULSION</a></li>
493 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_principles.html">Modelling principles</a></li>
494 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_basics.html">Modelling language (basics)</a></li>
495 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_advanced.html">Modelling language (advanced)</a></li>
496 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Feature_examples.html">Feature examples</a></li>
497 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Information.html">Information</a></li>
498 <li class="toctree-l1"><a class="reference internal" href="../../../pages/License.html">License</a></li>
499 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion_for_modellers.html">High-level functions for model designers</a></li>
500 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion.html">emulsion package</a></li>
501 </ul>
503 <div class="relations">
504 <h3>Related Topics</h3>
505 <ul>
506   <li><a href="../../../index.html">Documentation overview</a><ul>
507   <li><a href="../../index.html">Module code</a><ul>
508   </ul></li>
509   </ul></li>
510 </ul>
511 </div>
512 <div id="searchbox" style="display: none" role="search">
513   <h3>Quick search</h3>
514     <div class="searchformwrapper">
515     <form class="search" action="../../../search.html" method="get">
516       <input type="text" name="q" />
517       <input type="submit" value="Go" />
518       <input type="hidden" name="check_keywords" value="yes" />
519       <input type="hidden" name="area" value="default" />
520     </form>
521     </div>
522 </div>
523 <script type="text/javascript">$('#searchbox').show(0);</script>
524         </div>
525       </div>
526       <div class="clearer"></div>
527     </div>
528     <div class="footer">
529       &copy;2016, INRA and Univ. Lille.
530       
531       |
532       Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
533       &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
534       
535     </div>
537     
539     
540   </body>
541 </html>