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

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="c1"># if the value which has to be affected to the variable is</span>
149             <span class="c1"># already a statevar, retrieve its value from the agent</span>
150             <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span> <span class="ow">in</span> <span class="n">agent</span><span class="o">.</span><span class="n">statevars</span><span class="p">:</span>
151                 <span class="n">value</span> <span class="o">=</span> <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">parameter</span><span class="p">]</span>
152             <span class="c1"># otherwise get its value through the model</span>
153             <span class="k">else</span><span class="p">:</span>
154                 <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>
155             <span class="c1"># agent.set_information(self.statevar_name, value)</span>
156             <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>
158     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
159         <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>
160                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
161     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
164 <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>
165     <span class="sd">&quot;&quot;&quot;A RateChangeAction is aimed at increasing or decreasing a</span>
166 <span class="sd">    specific state variable or attribute, according to a specific rate</span>
167 <span class="sd">    (i.e. the actual increase or decrease is the product of the</span>
168 <span class="sd">    `parameter` attribute and a population size).</span>
170 <span class="sd">    &quot;&quot;&quot;</span>
171     <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>
172         <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>
173         <span class="bp">self</span><span class="o">.</span><span class="n">sign</span> <span class="o">=</span> <span class="n">sign</span>
175 <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>
176         <span class="sd">&quot;&quot;&quot;Execute the action on the specified unit, with the</span>
177 <span class="sd">        specified population size.</span>
179 <span class="sd">        &quot;&quot;&quot;</span>
180         <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>
181         <span class="k">if</span> <span class="n">population</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
182             <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>
183         <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>
184         <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>
185         <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>
186         <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>
187         <span class="c1"># print(&#39;Executing&#39;, self.__class__.__name__, &#39;for&#39;, unit,</span>
188         <span class="c1">#       self.statevar_name, current_val, &#39;-&gt;&#39;, new_val,</span>
189         <span class="c1">#       self.sign, rate, population)</span>
190         <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>
192     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
193         <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>
194                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
195     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
198 <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>
199     <span class="sd">&quot;&quot;&quot;A RateDecreaseAction is aimed at decreasing a specific state</span>
200 <span class="sd">    variable or attribute, according to a specific rate (i.e. the</span>
201 <span class="sd">    actual decrease is the product of the `parameter` attribute and a</span>
202 <span class="sd">    population size).</span>
204 <span class="sd">    &quot;&quot;&quot;</span>
205     <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>
206         <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>
208 <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>
209     <span class="sd">&quot;&quot;&quot;A RateIncreaseAction is aimed at increasing a specific state</span>
210 <span class="sd">    variable or attribute, according to a specific rate (i.e. the</span>
211 <span class="sd">    actual increase is the product of the `parameter` attribute and a</span>
212 <span class="sd">    population size).</span>
214 <span class="sd">    &quot;&quot;&quot;</span>
215     <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>
216         <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>
218 <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>
219     <span class="sd">&quot;&quot;&quot;A StochAdditiveAction is aimed at increasing or decreasing a</span>
220 <span class="sd">    specific state variable or attribute, according to a specific</span>
221 <span class="sd">    rate, using a *binomial sampling*.</span>
223 <span class="sd">    &quot;&quot;&quot;</span>
224     <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>
225         <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>
226         <span class="bp">self</span><span class="o">.</span><span class="n">sign</span> <span class="o">=</span> <span class="n">sign</span>
228 <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>
229         <span class="sd">&quot;&quot;&quot;Execute the action on the specified unit, with the</span>
230 <span class="sd">        specified population size.</span>
232 <span class="sd">        &quot;&quot;&quot;</span>
233         <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>
234         <span class="k">if</span> <span class="n">population</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
235             <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>
236         <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>
237         <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>
238         <span class="c1"># convert rate into a probability</span>
239         <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>
240         <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>
241         <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>
242         <span class="c1"># print(&#39;Executing&#39;, self.__class__.__name__, &#39;for&#39;, unit,</span>
243         <span class="c1">#       self.statevar_name, current_val, &#39;-&gt;&#39;, new_val,</span>
244         <span class="c1">#       self.sign, rate, population)</span>
245         <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>
247     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
248         <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>
249                                                       <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span><span class="p">)</span>
250     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
253 <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>
254     <span class="sd">&quot;&quot;&quot;A StochDecreaseAction is aimed at decreasing a specific state</span>
255 <span class="sd">    variable or attribute, according to a specific rate, using a</span>
256 <span class="sd">    *binomial sampling*.</span>
258 <span class="sd">    &quot;&quot;&quot;</span>
259     <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>
260         <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>
262 <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>
263     <span class="sd">&quot;&quot;&quot;A StochIncreaseAction is aimed at increasing a specific state</span>
264 <span class="sd">    variable or attribute, according to a specific rate, using a</span>
265 <span class="sd">    *binomial sampling*.</span>
267 <span class="sd">    &quot;&quot;&quot;</span>
268     <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>
269         <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>
271 <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>
272     <span class="sd">&quot;&quot;&quot;A StringAction is based on the specification of a string</span>
273 <span class="sd">    parameter.</span>
275 <span class="sd">    &quot;&quot;&quot;</span>
276     <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>
277         <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>
278         <span class="bp">self</span><span class="o">.</span><span class="n">parameter</span> <span class="o">=</span> <span class="n">parameter</span>
279         <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>
280         <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>
282     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
283         <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>
284                                                             <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">,</span>
285                                                             <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="p">)</span>
286     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span></div>
289 <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">AbstractAction</span><span class="p">):</span>
290     <span class="sd">&quot;&quot;&quot;A BecomeAction is aimed at making an agent change its state</span>
291 <span class="sd">    according to one ore more specified prototypes.</span>
293 <span class="sd">    &quot;&quot;&quot;</span>
294     <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">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>
295         <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>
296         <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>\
297                                <span class="k">else</span> <span class="p">[</span><span class="n">prototypes</span><span class="p">]</span>
298         <span class="k">if</span> <span class="n">probas</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
299             <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>
300         <span class="k">else</span><span class="p">:</span>
301             <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>
302         <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>
303         <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>
305     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
306         <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">)&#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> <span class="bp">self</span><span class="o">.</span><span class="n">probas</span><span class="p">)</span>
307     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>
309 <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>
310         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
311 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
312 <span class="sd">        the action. If changes of state variables in relation to a</span>
313 <span class="sd">        state machine occur, the corresponding actions (if any) are</span>
314 <span class="sd">        executed: on_exit from the current state, and on_enter for the</span>
315 <span class="sd">        new state.</span>
317 <span class="sd">        &quot;&quot;&quot;</span>
318         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
319             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
320         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
321             <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>
322             <span class="c1"># compute actual values for probabilities</span>
323             <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>
324             <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>
325             <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>
326             <span class="k">if</span> <span class="n">total</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
327                 <span class="c1"># add complement</span>
328                 <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>
329                 <span class="c1"># if N-1 probabilities were given for N prototypes, no</span>
330                 <span class="c1"># problem: the last prototype is getting the 1-total</span>
331                 <span class="c1"># value. Otherwise, this means that there is a</span>
332                 <span class="c1"># possibility that no individuals are produced =&gt; None</span>
333                 <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>
334                     <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>
335             <span class="n">prototype</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">choice</span><span class="p">(</span><span class="n">protos</span><span class="p">,</span> <span class="n">p</span><span class="o">=</span><span class="n">proba_values</span><span class="p">)</span>
336             <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>
337                 <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="n">prototype</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>
339 <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>
340     <span class="sd">&quot;&quot;&quot;A CloneAction produces several copies of the agent with a given</span>
341 <span class="sd">    prototype.</span>
343 <span class="sd">    &quot;&quot;&quot;</span>
344     <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>
345         <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>
346         <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>\
347                                <span class="k">else</span> <span class="p">[</span><span class="n">prototypes</span><span class="p">]</span>
348         <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>
349         <span class="k">if</span> <span class="n">probas</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
350             <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>
351         <span class="k">else</span><span class="p">:</span>
352             <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>
353         <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>
354         <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>
355         <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>
357     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
358         <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>
359                                                             <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>
360     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>
362 <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>
363         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
364 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
365 <span class="sd">        the action. If changes of state variables in relation to a</span>
366 <span class="sd">        state machine occur, the corresponding actions (if any) are</span>
367 <span class="sd">        executed: on_exit from the current state, and on_enter for the</span>
368 <span class="sd">        new state.</span>
370 <span class="sd">        &quot;&quot;&quot;</span>
371         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
372             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
373         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
374             <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>
375             <span class="c1"># compute actual values for probabilities</span>
376             <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>
377             <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>
378             <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>
379             <span class="k">if</span> <span class="n">total</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
380                 <span class="c1"># add complement</span>
381                 <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>
382                 <span class="c1"># if N-1 probabilities were given for N prototypes, no</span>
383                 <span class="c1"># problem: the last prototype is getting the 1-total</span>
384                 <span class="c1"># value. Otherwise, this means that there is a</span>
385                 <span class="c1"># possibility that no individuals are produced =&gt; None</span>
386                 <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>
387                     <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>
388             <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>
389                                                <span class="n">proba_values</span><span class="p">)</span>
390             <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>
391                 <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>
392                     <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>
393                                 <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>
394                     <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>
397 <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>
398     <span class="sd">&quot;&quot;&quot;A MessageAction is aimed at making an agent print a given</span>
399 <span class="sd">    string. It requires a string message. This string can contain one</span>
400 <span class="sd">    reference to a variable or method of the agent, using Python&#39;s</span>
401 <span class="sd">    formatting syntax.</span>
403 <span class="sd">    For instance, &#39;My state is {.statevars.health_state}&#39; will print</span>
404 <span class="sd">    the current health state of the agent.</span>
406 <span class="sd">    Output is formatted in three comma-separated fields: the time step</span>
407 <span class="sd">    when the message was produced, the agent speaking, and the message</span>
408 <span class="sd">    itself.</span>
410 <span class="sd">    &quot;&quot;&quot;</span>
411 <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>
412         <span class="sd">&quot;&quot;&quot;Execute the action in the specified unit. If the `agents` parameter</span>
413 <span class="sd">        is specified (as a list), each agent of this list will execute</span>
414 <span class="sd">        the action.</span>
416 <span class="sd">        &quot;&quot;&quot;</span>
417         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
418             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
419         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
420             <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>
421             <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>
424 <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>
425     <span class="sd">&quot;&quot;&quot;A MethodAction is aimed at making an agent perform an action on</span>
426 <span class="sd">    a specific population. It requires a method name, and optionnally</span>
427 <span class="sd">    a list and a dictionary of parameters.</span>
429 <span class="sd">    &quot;&quot;&quot;</span>
430     <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>
431         <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>
432         <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">method</span>
433         <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>
434         <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>
436     <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
437         <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>
438                                                             <span class="bp">self</span><span class="o">.</span><span class="n">l_params</span><span class="p">,</span>
439                                                             <span class="bp">self</span><span class="o">.</span><span class="n">d_params</span><span class="p">)</span>
440     <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>
442 <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>
443         <span class="sd">&quot;&quot;&quot;Execute the action using the specified unit. If the</span>
444 <span class="sd">        `agents` parameter is a list of units, each unit of this list</span>
445 <span class="sd">        will execute the action.</span>
447 <span class="sd">        &quot;&quot;&quot;</span>
448         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
449             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
450         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
451             <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>
452             <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>
453                         <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>
454             <span class="c1">### introduced to pass internal information such as population</span>
455             <span class="n">d_params</span> <span class="o">=</span> <span class="n">others</span>
456             <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>
457                              <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>
458             <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>
460 <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>
461     <span class="sd">&quot;&quot;&quot;A FunctionAction is aimed at making an agent perform an action</span>
462 <span class="sd">    on a specific population. It requires a function, and optionnally</span>
463 <span class="sd">    a list and a dictionary of parameters. A FunctionAction runs</span>
464 <span class="sd">    faster than a MethodAction since it does not require to retrieve</span>
465 <span class="sd">    the method in each agent.</span>
467 <span class="sd">    &quot;&quot;&quot;</span>
468     <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>
469         <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>
470         <span class="bp">self</span><span class="o">.</span><span class="n">function</span> <span class="o">=</span> <span class="n">function</span>
471         <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>
473 <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>
474         <span class="sd">&quot;&quot;&quot;Execute the action using the specified unit. If the</span>
475 <span class="sd">        `agents` parameter is a list of units, each unit of this list</span>
476 <span class="sd">        will execute the action.</span>
478 <span class="sd">        &quot;&quot;&quot;</span>
479         <span class="k">if</span> <span class="n">agents</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
480             <span class="n">agents</span> <span class="o">=</span> <span class="p">[</span><span class="n">unit</span><span class="p">]</span>
481         <span class="k">for</span> <span class="n">agent</span> <span class="ow">in</span> <span class="n">agents</span><span class="p">:</span>
482             <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>
483                                        <span class="n">agent</span><span class="p">)</span>
484                         <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>
485             <span class="c1">### introduced to pass internal information such as population</span>
486             <span class="n">d_params</span> <span class="o">=</span> <span class="n">others</span>
487             <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>\
488                              <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>
489                                             <span class="n">agent</span><span class="p">)</span>
490                              <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>
491             <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>
494 <span class="n">ACTION_DICT</span> <span class="o">=</span> <span class="p">{</span>
495     <span class="s1">&#39;increase&#39;</span><span class="p">:</span> <span class="n">RateIncreaseAction</span><span class="p">,</span>
496     <span class="s1">&#39;decrease&#39;</span><span class="p">:</span> <span class="n">RateDecreaseAction</span><span class="p">,</span>
497     <span class="s1">&#39;increase_stoch&#39;</span><span class="p">:</span> <span class="n">StochIncreaseAction</span><span class="p">,</span>
498     <span class="s1">&#39;decrease_stoch&#39;</span><span class="p">:</span> <span class="n">StochDecreaseAction</span><span class="p">,</span>
499     <span class="s1">&#39;message&#39;</span><span class="p">:</span> <span class="n">MessageAction</span><span class="p">,</span>
500     <span class="s1">&#39;become&#39;</span><span class="p">:</span> <span class="n">BecomeAction</span><span class="p">,</span>
501     <span class="s1">&#39;clone&#39;</span><span class="p">:</span> <span class="n">CloneAction</span><span class="p">,</span>
502     <span class="s1">&#39;produce_offspring&#39;</span><span class="p">:</span> <span class="n">CloneAction</span><span class="p">,</span>
503     <span class="s1">&#39;action&#39;</span><span class="p">:</span> <span class="n">MethodAction</span><span class="p">,</span>
504     <span class="s1">&#39;duration&#39;</span><span class="p">:</span> <span class="n">FunctionAction</span><span class="p">,</span>
505     <span class="s1">&#39;set_var&#39;</span><span class="p">:</span> <span class="n">SetVarAction</span>
506 <span class="p">}</span>
507 </pre></div>
509           </div>
510         </div>
511       </div>
512       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
513         <div class="sphinxsidebarwrapper">
514 <h1 class="logo"><a href="../../../index.html">EMULSION</a></h1>
518 <p class="blurb">Epidemiological Multi-Level Simulation Framework</p>
525 <h3>Navigation</h3>
526 <ul>
527 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Install.html">Installation</a></li>
528 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Getting_started.html">Getting started with EMULSION</a></li>
529 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_principles.html">Modelling principles</a></li>
530 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_basics.html">Modelling language (basics)</a></li>
531 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Modelling_language_advanced.html">Modelling language (advanced)</a></li>
532 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Feature_examples.html">Feature examples</a></li>
533 <li class="toctree-l1"><a class="reference internal" href="../../../pages/Information.html">Information</a></li>
534 <li class="toctree-l1"><a class="reference internal" href="../../../pages/License.html">License</a></li>
535 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion_for_modellers.html">High-level functions for model designers</a></li>
536 <li class="toctree-l1"><a class="reference internal" href="../../../emulsion.html">emulsion package</a></li>
537 </ul>
539 <div class="relations">
540 <h3>Related Topics</h3>
541 <ul>
542   <li><a href="../../../index.html">Documentation overview</a><ul>
543   <li><a href="../../index.html">Module code</a><ul>
544   </ul></li>
545   </ul></li>
546 </ul>
547 </div>
548 <div id="searchbox" style="display: none" role="search">
549   <h3>Quick search</h3>
550     <div class="searchformwrapper">
551     <form class="search" action="../../../search.html" method="get">
552       <input type="text" name="q" />
553       <input type="submit" value="Go" />
554       <input type="hidden" name="check_keywords" value="yes" />
555       <input type="hidden" name="area" value="default" />
556     </form>
557     </div>
558 </div>
559 <script type="text/javascript">$('#searchbox').show(0);</script>
560         </div>
561       </div>
562       <div class="clearer"></div>
563     </div>
564     <div class="footer">
565       &copy;2016, INRA and Univ. Lille.
566       
567       |
568       Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.5</a>
569       &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
570       
571     </div>
573     
575     
576   </body>
577 </html>