<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul Petersik on Medium]]></title>
        <description><![CDATA[Stories by Paul Petersik on Medium]]></description>
        <link>https://medium.com/@paul.petersik?source=rss-254444637052------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6hn7pMKxBtYbbwZD7NEOUQ.jpeg</url>
            <title>Stories by Paul Petersik on Medium</title>
            <link>https://medium.com/@paul.petersik?source=rss-254444637052------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 04:28:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paul.petersik/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simulate a peloton in a bicycle race using the Julia language]]></title>
            <link>https://blog.devgenius.io/using-the-julia-language-to-simulate-a-peloton-in-a-bicycle-race-5f011ef1d678?source=rss-254444637052------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f011ef1d678</guid>
            <category><![CDATA[cycling]]></category>
            <category><![CDATA[julialang]]></category>
            <category><![CDATA[agent-based-modeling]]></category>
            <category><![CDATA[julia]]></category>
            <category><![CDATA[modeling]]></category>
            <dc:creator><![CDATA[Paul Petersik]]></dc:creator>
            <pubDate>Wed, 06 Apr 2022 12:34:41 GMT</pubDate>
            <atom:updated>2022-04-07T19:53:08.673Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Sbx5XKu3GyKYGzZI-Okpw.jpeg" /></figure><h3><strong>Introduction</strong></h3><p>In my <a href="https://medium.com/@paul.petersik/a-simple-cellular-automaton-of-plant-epidemics-in-julia-29ac62d87516">last article</a> on medium, I showed how one can model spatial disease dynamics of a plant disease using a cellular automaton. For my next little dive into the Julia language, I wanted to model something completely different. As I am a big fan of race cycling, my aim was to model a peloton.</p><p>In road bicycle races, the largest group of riders is the so-called peloton. It is a very typical feature of road races that cyclist organize themselves in groups. This is because cyclists can save quite some energy when they ride in the slipstream of others. The drafting effect gives rise to complex tactical and game-theoretical behavior. For instance, teams usually try to protect their captain as long as possible from the headwind to increase the likelihood of the captain to win the race. Moreover, in breakaway groups, riders typically collaborate well and rotate their positions in the group as long as they are far away from the finish line. However, the closer they get to the finish, the more they try to save their legs for the final sprint. Hence, some of the riders in the breakaway become defectors and the collaboration within the group swiftly falls apart.</p><p>Another interesting phenomenon that can be observed in bicycle races is the so-called “washing machine” effect. It appears when a lot of riders want to get to the front of the peloton in the final phase of a race. Since the space at the front is limited, forward-pressing riders must push others backwards. Usually, riders start to move forwards at the outsides and move backwards through the center of the peloton. Thus, a rider within the peloton can get rotated like clothes in a “washing machine”.</p><h3>Theory</h3><p>So let’s try to model a peloton using an agent-based model. As always, someone already worked on this problem before. For instance, there is a cool article of Erick Ratamero that you can find on <a href="https://erickmartins.github.io/files/moped_new.pdf">GitHub</a>. I took a lot of inspiration from Erick’s model. However, I formulated these concepts in a bit more abstract and non-dimensional manner.</p><h4>Flocking algorithm</h4><p>In nature, various species show flocking behavior, i.e. starlings form big swarms in late summer and autumn before they migrate towards their winter habitats. Since a peloton has some similarities with a flock of birds, I used a modified flocking algorithm to model the movement of the agents. Interestingly, there are just three rules an agent has to follow such that flocking behavior emerges:</p><ul><li><strong>Cohesion</strong>: Attraction towards the mean position of the flock</li><li><strong>Separation</strong>: Repulsion from agents in the close neighborhood</li><li><strong>Alignment</strong>: Adjust the moving direction towards the average moving direction</li></ul><p>For thepeloton model, we can omit the <em>alignment</em> rule because we are going to model the peloton on a straight road and it is the road that prescribes the direction of movement. In a typical flocking model, the neighborhood of an agent for which the separation is calculated comprises all other agents which are located within a certain distance around that agent. To make the peloton model a bit more realistic and to simulate human vision, only neighbors in front of an agent are considered for the separation.</p><p>Moreover, to simplify the model, only velocities of an agent relative to the mean velocity of the peloton are modeled. Hence, the frame of reference is moving with the average speed of the peloton. This will make it easier to generate nice animations later.</p><h4>Energetic component</h4><p>As described above, front runners of a peloton spent more energy than cyclists that can hide in the slipstream. However, after a while they get exhausted and need to drop back into the peloton where they can recover slowly from their efforts.</p><p>Here, this behavior is simulated by introducing an <em>energy</em> variable. Its value increases up to a certain maximum value as long as an agent is moving in the slipstream of other agents but decreases rather quickly if the agent faces the headwind. Then, if the energy of an agent drops below a certain value, the agent starts to move backwards into the peloton.</p><h3>Implementation in Julia</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/984870c4498e72536c9e57360e6432e0/href">https://medium.com/media/984870c4498e72536c9e57360e6432e0/href</a></iframe><p>To setup the model, two structs are used, one for the agent and another one for the model itself. Moreover, I used outer constructors to initialize the model with a standard setup that will generate nice dynamics.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/255dda96cdd8f06a6f1c0bfac5bf1e45/href">https://medium.com/media/255dda96cdd8f06a6f1c0bfac5bf1e45/href</a></iframe><p>The flocking behavior of an agent is computed by looping over all other agents, computing the heading and the distance to the other agents and using these variables to compute the cohesion and the separation. Note, this implementation of the flocking algorithm follows closely the <em>flocking</em> example of the Agents.jl library that you can find <a href="https://juliadynamics.github.io/Agents.jl/stable/examples/flock/">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8eedbdf15a65fad0a2ac582296bf0fa4/href">https://medium.com/media/8eedbdf15a65fad0a2ac582296bf0fa4/href</a></iframe><p>To compute the energy usage of an agent, again we need to loop over all agents and calculate the heading and the distance to the other agents. The closer an agent is to other agents in front of it, the smaller is its energy usage. The energy usage even can become negative which means that the agent can recover to some extent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a4b4db96200622166339a9c902dcd47/href">https://medium.com/media/2a4b4db96200622166339a9c902dcd47/href</a></iframe><p>Finally, the flocking algorithm and the energy component of the model are combined in a step function. In addition, a random movement and a bias term are introduced. The bias term will nudge an agent towards the road center as well as towards its desired rank.</p><p>The desired rank of the agent is updated at the end of the loop (line 34). The update_desired_rank! function (line 1–9) simulates the behavior that exhausted agents can not stay in the front of the peloton, start to move backwards and are replaced by other agents that press forward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2117f53e65311bb5fa55728cdb2547c4/href">https://medium.com/media/2117f53e65311bb5fa55728cdb2547c4/href</a></iframe><p>Now, we are set to plot the dynamics of this model using a loop over a scatter plot where we color the markers depending on the energy of the agent.</p><h3>Results</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*INS0NM6MxR40A09Nszpt4w.gif" /></figure><p>Finally, we can generate a nice animation! To me, this already looks pretty much like a peloton from above.</p><p>I know, there is more to race cycling than just these simple rules that I implemented above and one could go nuts and implement e.g. game-theoretical components, roads that face uphill/downhill or teams that follow own team strategies. However, this is for another blog post I guess 😉.</p><p>I hope you enjoyed this little blog post. You can find the complete code for the peloton model <a href="https://gist.github.com/pjpetersik/8821b0fe2279c05ce10f29b982d030f4">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f011ef1d678" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/using-the-julia-language-to-simulate-a-peloton-in-a-bicycle-race-5f011ef1d678">Simulate a peloton in a bicycle race using the Julia language</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A simple cellular automaton of plant epidemics in Julia]]></title>
            <link>https://blog.devgenius.io/a-simple-cellular-automaton-of-plant-epidemics-in-julia-29ac62d87516?source=rss-254444637052------2</link>
            <guid isPermaLink="false">https://medium.com/p/29ac62d87516</guid>
            <category><![CDATA[julia]]></category>
            <category><![CDATA[wine]]></category>
            <category><![CDATA[complexity]]></category>
            <category><![CDATA[cellular-automata]]></category>
            <category><![CDATA[modeling]]></category>
            <dc:creator><![CDATA[Paul Petersik]]></dc:creator>
            <pubDate>Mon, 28 Mar 2022 10:00:48 GMT</pubDate>
            <atom:updated>2026-05-29T14:01:16.169Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bg_5EGibD6gCceinFd0z0w.jpeg" /></figure><p>I recently started to learn the Julia programming language because I was intrigued by its simple syntax and the prospects of high performance. One of my first little projects was a so-called cellular automaton which I used to model the spread of a plant disease on a lattice.</p><h4>Real-world motivation</h4><p>In viticulture, there is a growing community of winemakers that start to plant grapevine varieties that are to some extent resistant against fungal diseases. Typical varieties such as Merlot or Chardonnay are highly susceptible for fungal diseases i.e., powdery or downy mildew. If winemakers would not apply fungicides against these pathogens, they would lose large parts of their yield each year. For more details on grapevine diseases, check out the articles on the VineForecast blog for <a href="https://www.vineforecast.com/en/powdery-mildew-vine-the-weather-and-fungal-diseases-in-viticulture-2/">powdery</a> and <a href="https://www.vineforecast.com/en/the-weather-and-fungal-diseases-1-downy-mildew-in-viticulture/">downy</a> mildew.</p><p>So, you may now ask the question: “Why not only plant resistant varieties?” The problem is, most consumers do not really like the wine made from resistant varieties (yet).</p><p>Therefore, I wondered if an intercropping of the popular but susceptible varieties with the less popular but resistant varieties would slow down disease dynamics in such a way that fungicide usage for the susceptible varieties could be reduced. The initial hypothesis is that the resistant varieties somewhat act as a wave-breaker for the disease propagation and therefore can protect the susceptible varieties from an approaching disease wave.</p><h3>Cellular automaton</h3><p>To model the epidemics in an intercropping field, I used a cellular automaton. A cellular automaton consists of multiple cells that are arranged on a lattice. Each cell is in a certain state. Here, I used the common SIR notion. The SIR model (<strong>S</strong>usceptible, <strong>I</strong>nfected, <strong>R</strong>emoved) is a typical approach to model diseases. You can find various articles one medium that introduce you to the different variations of this model (<a href="https://medium.com/towards-data-science/infectious-disease-modelling-part-i-understanding-sir-28d60e29fdfc">e.g. https://medium.com/towards-data-science/infectious-disease-modelling-part-i-understanding-sir-28d60e29fdfc</a>).</p><h4>Transition rules</h4><p>The dynamics of a cellular automaton are introduced by transition rules. These transition rules are applied for every update step on each cell of the model. For the sake of simplicity, just two transition rules are considered for our cellular automaton. The first rule defines the transition of a cell from state S to state I and the second rule the transition from state I to R:</p><ol><li>S → I: If a neighbour in the 8-neighbourhood of a cell is infected, the cell becomes infected with some probability <em>p_infection.</em></li><li>I → R: If a cell is already infected, it becomes removed with a probability <em>p_removal.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H1qqWcZStXeJBbFTO8G1rg.jpeg" /><figcaption>Grapevine leaf with a strong damage by downy mildew</figcaption></figure><p>Note, that a cell that entered the infected state will become removed eventually if <em>p_removal &gt; 0. </em>This is a reasonable assumption for grapevine diseases since infected tissues of a grapevine cannot recover from the infection and will die eventually.</p><h4>Modelling intercropping</h4><p>Intercropping of resistant and susceptible varieties can be modelled by assigning different values of<em> p_infection</em> to the cells of the lattice. Grapevines are often grown in rows. Therefore, for a first experiment, I chose a distribution where every other row has a low value for <em>p_infection </em>(resistant variety) and the remaining rows have a high value for <em>p_infection </em>(susceptible variety). Note, also in reality, resistant varieties are not entirely immune against fungal diseases but possess some ability to reduce the infection efficiency of the pathogen.</p><h3>Implentation in Julia</h3><p>The Julia code for this cellular automaton basically consists out of a bunch of for-loops and conditional statements where the three states are encoded using an array of integer values (0 → S, 1 → I, 2 → R):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bd14ba4007159e3a695204bb9112da3/href">https://medium.com/media/2bd14ba4007159e3a695204bb9112da3/href</a></iframe><p>Line 4 to 18 initialize the model with 200 x 200 lattice for a simulation of 500 update steps. To get the dynamics started, a first hotspot of infected cells is initialized in the lower left corner. The value for <em>p_infection </em>is set to 50% for the susceptible varieties and to 2.5% for resistant varieties. With a probability of 20% an infected cell becomes removed.</p><p>Then, line 20 to 44 compute the evolution of the model. Line 28 to 36 implement the S → I transition rule and line 39 to 41 the I → R transition rule.</p><p>Finally, line 46 to 48 generates a GIF for the evolution of the states.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*UxxRLF_2VMaXT_uK8mD4ww.gif" /><figcaption>Evolution of the disease with state S in green, I in light-brown and R in pink</figcaption></figure><p>You can clearly see how the disease spreads faster in the vertical than in the horizontal direction. Therefore, the columns of resistant varieties, as expected, slow down the horizontal propagation of the epidemic. Moreover, there is an alternating pattern between columns for which (nearly) all cells are removed and columns where a good fraction of the cells remain in state S (green) until the end of the simulation. This is reassembling the distribution of resistant and susceptible varieties.</p><p>Next, let’s investigate if another distribution of resistant varieties is more capable of slowing down the horizontal propagation. For this, I alternated four columns of the susceptible variety with four columns of the resistant variety. Hence, in total the same number of resistant varieties are present as in the previous experiment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*QyPIgoX_j6wOMfMADC5eqA.gif" /></figure><p>Interestingly, the horizontal disease spread is significantly slower than in the previous experiment. The disease wave towards the left even stops completely.</p><h3>Conclusion</h3><p>It seems that the distribution of resistant varieties in a field actually influences the overall disease dynamics. Moreover, a distribution where multiple columns of resistant varieties are placed next to each other might be more capable to slow down disease propagation.</p><p>However, the initial infection of a field often does not happen only at one location but more homogeneously throughout a field. In such a more realistic case, the distribution of resistant varieties might have a smaller influence on the overall disease dynamics. But this is for another article ;).</p><h4>Links</h4><ul><li><a href="https://twitter.com/pjpetersik">https://twitter.com/pjpetersik</a></li><li><a href="https://www.instagram.com/vineforecast/">https://www.instagram.com/vineforecast/</a></li><li><a href="https://www.vineforecast.com/en/">https://www.vineforecast.com/en/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29ac62d87516" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/a-simple-cellular-automaton-of-plant-epidemics-in-julia-29ac62d87516">A simple cellular automaton of plant epidemics in Julia</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>