1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/docs/_build/html/models.html
2017-04-27 13:15:56 +02:00

210 lines
15 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Developing new models &#8212; Soil 0.1 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="prev" title="Usage" href="usage.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="developing-new-models">
<h1>Developing new models<a class="headerlink" href="#developing-new-models" title="Permalink to this headline"></a></h1>
<p>This document describes how to develop a new analysis model.</p>
<div class="section" id="what-is-a-model">
<h2>What is a model?<a class="headerlink" href="#what-is-a-model" title="Permalink to this headline"></a></h2>
<p>A model defines the behaviour of the agents with a view to assessing their effects on the system as a whole.
In practice, a model consists of at least two parts:</p>
<ul class="simple">
<li>Python module: the actual code that describes the behaviour.</li>
<li>Setting up the variables in the Simulation Settings JSON file.</li>
</ul>
<p>This separation allows us to run the simulation with different agents.</p>
</div>
<div class="section" id="models-code">
<h2>Models Code<a class="headerlink" href="#models-code" title="Permalink to this headline"></a></h2>
<p>All the models are imported to the main file. The initialization look like this:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">settings</span>
<span class="n">networkStatus</span> <span class="o">=</span> <span class="p">{}</span> <span class="c1"># Dict that will contain the status of every agent in the network</span>
<span class="n">sentimentCorrelationNodeArray</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">number_of_nodes</span><span class="p">):</span>
<span class="n">sentimentCorrelationNodeArray</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="n">x</span><span class="p">})</span>
<span class="c1"># Initialize agent states. Let&#39;s assume everyone is normal.</span>
<span class="n">init_states</span> <span class="o">=</span> <span class="p">[{</span><span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="p">}</span> <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">settings</span><span class="o">.</span><span class="n">number_of_nodes</span><span class="p">)]</span>
<span class="c1"># add keys as as necessary, but &quot;id&quot; must always refer to that state category</span>
</pre></div>
</div>
<p>A new model have to inherit the BaseBehaviour class which is in the same module.
There are two basics methods:</p>
<ul class="simple">
<li>__init__</li>
<li>step: used to define the behaviour over time.</li>
</ul>
</div>
<div class="section" id="variable-initialization">
<h2>Variable Initialization<a class="headerlink" href="#variable-initialization" title="Permalink to this headline"></a></h2>
<p>The different parameters of the model have to be initialize in the Simulation Settings JSON file which will be
passed as a parameter to the simulation.</p>
<div class="code json highlight-default"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s2">&quot;agent&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;SISaModel&quot;</span><span class="p">,</span><span class="s2">&quot;ControlModelM2&quot;</span><span class="p">],</span>
<span class="s2">&quot;neutral_discontent_spon_prob&quot;</span><span class="p">:</span> <span class="mf">0.04</span><span class="p">,</span>
<span class="s2">&quot;neutral_discontent_infected_prob&quot;</span><span class="p">:</span> <span class="mf">0.04</span><span class="p">,</span>
<span class="s2">&quot;neutral_content_spon_prob&quot;</span><span class="p">:</span> <span class="mf">0.18</span><span class="p">,</span>
<span class="s2">&quot;neutral_content_infected_prob&quot;</span><span class="p">:</span> <span class="mf">0.02</span><span class="p">,</span>
<span class="s2">&quot;discontent_neutral&quot;</span><span class="p">:</span> <span class="mf">0.13</span><span class="p">,</span>
<span class="s2">&quot;discontent_content&quot;</span><span class="p">:</span> <span class="mf">0.07</span><span class="p">,</span>
<span class="s2">&quot;variance_d_c&quot;</span><span class="p">:</span> <span class="mf">0.02</span><span class="p">,</span>
<span class="s2">&quot;content_discontent&quot;</span><span class="p">:</span> <span class="mf">0.009</span><span class="p">,</span>
<span class="s2">&quot;variance_c_d&quot;</span><span class="p">:</span> <span class="mf">0.003</span><span class="p">,</span>
<span class="s2">&quot;content_neutral&quot;</span><span class="p">:</span> <span class="mf">0.088</span><span class="p">,</span>
<span class="s2">&quot;standard_variance&quot;</span><span class="p">:</span> <span class="mf">0.055</span><span class="p">,</span>
<span class="s2">&quot;prob_neutral_making_denier&quot;</span><span class="p">:</span> <span class="mf">0.035</span><span class="p">,</span>
<span class="s2">&quot;prob_infect&quot;</span><span class="p">:</span> <span class="mf">0.075</span><span class="p">,</span>
<span class="s2">&quot;prob_cured_healing_infected&quot;</span><span class="p">:</span> <span class="mf">0.035</span><span class="p">,</span>
<span class="s2">&quot;prob_cured_vaccinate_neutral&quot;</span><span class="p">:</span> <span class="mf">0.035</span><span class="p">,</span>
<span class="s2">&quot;prob_vaccinated_healing_infected&quot;</span><span class="p">:</span> <span class="mf">0.035</span><span class="p">,</span>
<span class="s2">&quot;prob_vaccinated_vaccinate_neutral&quot;</span><span class="p">:</span> <span class="mf">0.035</span><span class="p">,</span>
<span class="s2">&quot;prob_generate_anti_rumor&quot;</span><span class="p">:</span> <span class="mf">0.035</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In this file you will also define the models you are going to simulate. You can simulate as many models as you want.
The simulation returns one result for each model. For the usage, see <a class="reference internal" href="usage.html"><span class="doc">Usage</span></a>.</p>
</div>
<div class="section" id="example-model">
<h2>Example Model<a class="headerlink" href="#example-model" title="Permalink to this headline"></a></h2>
<p>In this section, we will implement a Sentiment Correlation Model.</p>
<p>The class would look like this:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">..BaseBehaviour</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">..</span> <span class="k">import</span> <span class="n">sentimentCorrelationNodeArray</span>
<span class="k">class</span> <span class="nc">SentimentCorrelationModel</span><span class="p">(</span><span class="n">BaseBehaviour</span><span class="p">):</span>
<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">environment</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">agent_id</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="p">()):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">environment</span><span class="o">=</span><span class="n">environment</span><span class="p">,</span> <span class="n">agent_id</span><span class="o">=</span><span class="n">agent_id</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="n">state</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">outside_effects_prob</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">environment_params</span><span class="p">[</span><span class="s1">&#39;outside_effects_prob&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">anger_prob</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">environment_params</span><span class="p">[</span><span class="s1">&#39;anger_prob&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">joy_prob</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">environment_params</span><span class="p">[</span><span class="s1">&#39;joy_prob&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sadness_prob</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">environment_params</span><span class="p">[</span><span class="s1">&#39;sadness_prob&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">disgust_prob</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">environment_params</span><span class="p">[</span><span class="s1">&#39;disgust_prob&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">time_awareness</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span> <span class="c1"># In this model we have 4 sentiments</span>
<span class="bp">self</span><span class="o">.</span><span class="n">time_awareness</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># 0-&gt; Anger, 1-&gt; joy, 2-&gt;sadness, 3 -&gt; disgust</span>
<span class="n">sentimentCorrelationNodeArray</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">now</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">now</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">behaviour</span><span class="p">()</span> <span class="c1"># Method which define the behaviour</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>
</pre></div>
</div>
<p>The variables will be modified by the user, so you have to include them in the Simulation Settings JSON file.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Developing new models</a><ul>
<li><a class="reference internal" href="#what-is-a-model">What is a model?</a></li>
<li><a class="reference internal" href="#models-code">Models Code</a></li>
<li><a class="reference internal" href="#variable-initialization">Variable Initialization</a></li>
<li><a class="reference internal" href="#example-model">Example Model</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="usage.html" title="previous chapter">Usage</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/models.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2017, GSI.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.5.1</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a>
|
<a href="_sources/models.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>