<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pablo</title>
    <description>The latest articles on DEV Community by pablo (@pablo).</description>
    <link>https://dev.to/pablo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F27005%2Fe5a150a8-2e4c-4fdc-8b05-5c1364245745.jpg</url>
      <title>DEV Community: pablo</title>
      <link>https://dev.to/pablo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pablo"/>
    <language>en</language>
    <item>
      <title>Agent-Based Modelling. Part 1</title>
      <dc:creator>pablo</dc:creator>
      <pubDate>Wed, 07 Aug 2019 23:42:49 +0000</pubDate>
      <link>https://dev.to/pablo/agent-based-modelling-part-1-48d6</link>
      <guid>https://dev.to/pablo/agent-based-modelling-part-1-48d6</guid>
      <description>&lt;h1&gt;
  
  
  Agent-Based Modelling: An Introduction
&lt;/h1&gt;

&lt;p&gt;Agent-Based Modeling (I will refer to it as ABM from now on) is a simulating&lt;br&gt;
paradigm in which the programmer creates an entity that has specific&lt;br&gt;
properties and can perform a set of actions. Sounds simple, and they are&lt;br&gt;
more intuitive than mathematical or statistical models as they represent&lt;br&gt;
objects as we see them: individual elements in an environment. You might&lt;br&gt;
have already had a glimpse of what some ABM looks like if you have&lt;br&gt;
played The Sims or SimCity videogames.&lt;br&gt;
It’s possible to model almost anything: financial markets,&lt;br&gt;
transportation grids, social interactions, natural environments, etc. A&lt;br&gt;
simple example would be to model traffic. We can create three different&lt;br&gt;
templates: for a car, for a road, and for a pedestrian, and then we can&lt;br&gt;
simulate several vehicles, in a few streets interacting with some&lt;br&gt;
pedestrians. I told you it &lt;em&gt;sounded&lt;/em&gt; simple!&lt;/p&gt;

&lt;p&gt;The benefits from the agent-based models are:&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Capture the understanding of systems.&lt;/li&gt;
&lt;li&gt;Test that systems’ understanding of coherence and comprehensiveness.&lt;/li&gt;
&lt;li&gt;See if the individual-level behavior creates global patterns.&lt;/li&gt;
&lt;li&gt;Validate the theory against real data.&lt;/li&gt;
&lt;li&gt;Make predictions about the system.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;But how do we do this? In this series of posts, I am going to describe&lt;br&gt;
how to do this with Python and Ruby. I want to tell you that you don’t&lt;br&gt;
have to use those programming languages, there exist other options, such&lt;br&gt;
as any programming language with an Object-Oriented “mentality” (e.g.,&lt;br&gt;
C++, MATLAB or Java, among &lt;strong&gt;many&lt;/strong&gt; others) or specific tools like&lt;br&gt;
NetLogo or Dramatis.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;br&gt;
(&lt;em&gt;Disclaimer&lt;/em&gt;: I’ve been using Python for a few years now, but I became&lt;br&gt;
interested in Ruby a few months ago. Hence, this is more a way to&lt;br&gt;
finally force me to learn Ruby, and the way I use Ruby might not be the&lt;br&gt;
best you have ever seen.)&lt;/p&gt;

&lt;p&gt;Let’s get down to work!&lt;/p&gt;

&lt;p&gt;First things first: you need a functional version of Python and/or Ruby.&lt;br&gt;
It is possible to install both languages in your system, there are&lt;br&gt;
plenty of good tutorials on the Internet, and I highly recommend it to&lt;br&gt;
not depend on an Internet connection to run some scripts. In case you&lt;br&gt;
can’t do it, don’t want it, or think you’d fail miserably, there exists&lt;br&gt;
an excellent website—called &lt;a href="https://repl.it/"&gt;&lt;em&gt;Repl.it&lt;/em&gt;&lt;/a&gt;—that allows&lt;br&gt;
you to run scripts without any effort. This is convenient for learning&lt;br&gt;
but not scalable for more significant projects.&lt;/p&gt;

&lt;p&gt;Now, the &lt;em&gt;concepts:&lt;/em&gt; we are going to use &lt;strong&gt;objects&lt;/strong&gt;, the idea I will&lt;br&gt;
describe in this first post.&lt;/p&gt;

&lt;p&gt;An object, in a programming environment, is something that contains data&lt;br&gt;
(known as &lt;em&gt;attributes&lt;/em&gt; or &lt;em&gt;properties&lt;/em&gt;) and procedures (known as&lt;br&gt;
&lt;em&gt;methods&lt;/em&gt;). We can create an object, and then we create as many&lt;br&gt;
instances of that object as we want, all of them following the same&lt;br&gt;
pattern. Let’s see how this translates into Python and Ruby.&lt;br&gt;
Suppose we want to model the previous example of traffic modeling. We&lt;br&gt;
need cars, roads, and pedestrians. For simplicity, here the cars will&lt;br&gt;
only have the model and the color (attributes), and they will be able to&lt;br&gt;
move (method); the roads, a name and a defined number of lanes, and they&lt;br&gt;
can be congested or not; and the pedestrians, a speed, and they can be&lt;br&gt;
moving or still.&lt;/p&gt;

&lt;p&gt;The cars, in Python, will be like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The car is moving now"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The car stopped"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the reserved word “class” to define the car class. The&lt;br&gt;
__init__ method is particular to Python and is executed every time&lt;br&gt;
one instance of a car is created, i.e., initializes a vehicle with its&lt;br&gt;
properties. We also need a method that changes the state of the car:&lt;br&gt;
“change_state.”&lt;br&gt;
The roads, in Ruby, can be coded like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Road&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lanes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
        &lt;span class="vi"&gt;@lanes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lanes&lt;/span&gt;
        &lt;span class="vi"&gt;@state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_state&lt;/span&gt; 
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Is the road congested? &lt;/span&gt;&lt;span class="si"&gt;#@state&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax is similar to the one in Python but with a few more mandatory&lt;br&gt;
words and letters. We have an initializer, as before, and a method that&lt;br&gt;
gives us the state of the road. (Don’t worry now for the trifles of a&lt;br&gt;
real road and if it’s not very realistic. I want you to get the idea of&lt;br&gt;
how to model in Ruby.)&lt;br&gt;
The pedestrians, in Python and Ruby, can be modeled as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pedestrian&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;moving&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;moving&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The car is moving now"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The car
stopped"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pedestrian&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="vi"&gt;@speed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;
        &lt;span class="vi"&gt;@state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change_state&lt;/span&gt;
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"State was &lt;/span&gt;&lt;span class="si"&gt;#@state&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
        &lt;span class="vi"&gt;@state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="vi"&gt;@state&lt;/span&gt;
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"State is &lt;/span&gt;&lt;span class="si"&gt;#@state&lt;/span&gt;&lt;span class="s2"&gt; now"&lt;/span&gt;
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool. We know how to create the templates—classes—for our objects, and&lt;br&gt;
to create the copies—instances—of the objects, we proceed as follows (in&lt;br&gt;
Ruby and Python, respectively):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;ped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Pedestrian&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ped&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;change_state&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;car1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Buik'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;change_state&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the next post, I will go step by step on a dummy model to show what&lt;br&gt;
is my strategy to approach this kind of systems. It is worth noting that&lt;br&gt;
the critical bits come when you must associate the individuals’ behavior&lt;br&gt;
with the environment, with certain conditions, or with a set of observed&lt;br&gt;
actions.&lt;br&gt;
For this matter, there exist a few tools which help in the process. My&lt;br&gt;
background is in engineering, so I usually like diagrams, such as UML&lt;br&gt;
diagrams. UML stands for Unified Modeling Language and helps to&lt;br&gt;
visualize the architecture, design, and implementation of complex&lt;br&gt;
software systems. Among the benefits, UML is a standardized modeling&lt;br&gt;
language and, therefore, can be used across different programming&lt;br&gt;
languages. Software developers will understand it and might want to&lt;br&gt;
apply it to their work. There are many good tutorials on the Internet&lt;br&gt;
about UML,&lt;sup id="fnref3"&gt;3&lt;/sup&gt; and specialized tools to use with the languages you use&lt;br&gt;
to create the diagrams from your code.&lt;/p&gt;

&lt;p&gt;See you in the next post! :D&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Reference &lt;a href="http://www.geog.leeds.ac.uk/courses/other/crime/abm/general-modelling/index.html"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;See &lt;a href="https://es.wikipedia.org/wiki/Comparaci%C3%B3n_de_software_de_modelaje_basado_en_agentes"&gt;&lt;em&gt;this Wikipedia page&lt;/em&gt;&lt;/a&gt; for more information, and this article review of the state-of-the-art for more accurate information: Abar, S., Theodoropoulos, G. K., Lemarinier, P., &amp;amp; O’Hare, G. M. (2017). Agent Based Modelling and Simulation tools: A review of the state-of-art software. Computer Science Review, 24, 13-33. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://www.lucidchart.com/blog/types-of-UML-diagrams"&gt;&lt;em&gt;lucidchar website&lt;/em&gt;&lt;/a&gt;, and &lt;a href="https://tallyfy.com/uml-diagram/"&gt;&lt;em&gt;tallyfy website&lt;/em&gt;&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>abm</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
